Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to hook into require('some-file.json') ? #58

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

derhuerst
Copy link

@derhuerst derhuerst commented Dec 9, 2019

I've made this PR to demonstrate a use case that, as far as I understand what pirates is intended for, should work: hooking into require() calls to .json files.

node -v # v13.2.0
npm -v # 6.13.2

npm ls --depth 0
# /Users/j/playground/pirates
# ├── @babel/[email protected]
# ├── @babel/[email protected]
# ├── @babel/[email protected]
# ├── [email protected]
# ├── [email protected]
# ├── [email protected]
# ├── [email protected]
# ├── [email protected]
# ├── [email protected]
# ├── [email protected]
# ├── [email protected]
# ├── [email protected]
# ├── [email protected]
# ├── [email protected]
# ├── [email protected]
# ├── [email protected]
# ├── [email protected]
# ├── [email protected]
# ├── [email protected]
# ├── [email protected]
# └── [email protected]
#
# npm ERR! peer dep missing: ajv@^5.0.0, required by [email protected]
# npm ERR! peer dep missing: ajv@^5.0.0, required by [email protected]

ava
#  ✔ GoodNonPirateHooks › non-pirates hooks
#  ✔ basics › basics
#  ✔ basics › matchers
#  ✔ basics › matcher is called only once per file
#  ✔ extensions › exts
#  ✖ extensions › addHook works with .json files
#  ✔ extensions › chain
#
#  1 test failed
#
#  extensions › addHook works with .json files
#
#  /Users/j/playground/pirates/test/extensions.js:36
#
#   35:   assertModule(t, 'json.js', 'json.json from json.js');
#   36:   t.deepEqual(actual, expected);
#   37: });
#
#  Difference:
#
#    [
#      '/Users/j/playground/pirates/test/fixture/json.js',
#  +   '/Users/j/playground/pirates/test/fixture/json.json',
#    ]

package-lock.json.zip

If this is intended to work, I can submit a fix.

@jonknowles
Copy link

I've just been fighting this issue of attempting to intercept a JSON module compilation myself and found this PR during my debugging. If I am reading the node source code right, though, it looks like there is no "compile" hook for JSON - it just loads it from the filesystem, parses it, and is done: https://github.com/nodejs/node/blob/726143e683738b0e60580bdc247e6cfa60a0783a/lib/internal/modules/cjs/loader.js#L1106

If that's the case, I doubt this library will be able to implement the feature that this test is trying to spec out. I'd like to hear from the project maintainers on this though, because they surely know the possibilities here better than I do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants