Use the package.json "browser" field to toggle Node.js-specific functionality #145
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Following from the discussion in
webpack/webpack#8826, this is how it'd work
with the existing "browser" field rather than any new special comment or flag.
Feel free to close this PR if you don't like it!
By using the "browser" field, we can tell bundlers to use the
infer-label.browser.ts
file instead ofinfer-label.ts
. This obviatesthe need for the customRequire hack and lazy loading
fs
. In this caseit has the side benefit of omitting the Node-specific
inferLabel
function from the bundle entirely, whereas previously it would still be
included, just never run.
An alternate solution is to use:
Then the
fs
module will return an empty object when bundled;inferLabel
would need to doisNode && fs && fs.readFileSync
to checkif files can be read.
AFAIK, the only bundler this doesn't work in at the moment is Meteor, I
don't know if that's a blocker for you.