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

Use the package.json "browser" field to toggle Node.js-specific functionality #145

Merged
merged 3 commits into from May 12, 2019
Merged

Use the package.json "browser" field to toggle Node.js-specific functionality #145

merged 3 commits into from May 12, 2019

Conversation

goto-bus-stop
Copy link
Contributor

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 of infer-label.ts. This obviates
the need for the customRequire hack and lazy loading fs. In this case
it 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:

"browser": {
	"fs": false
}

Then the fs module will return an empty object when bundled;
inferLabel would need to do isNode && fs && fs.readFileSync to check
if 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.

goto-bus-stop and others added 3 commits May 10, 2019 12:32
By using the "browser" field, we can tell bundlers to use the
`infer-label.browser.ts` file instead of `infer-label.ts`. This obviates
the need for the customRequire hack and lazy loading `fs`.
@sindresorhus sindresorhus changed the title Use "browser" field to toggle Node-specific functionality Use the package.json "browser" field to toggle Node.js-specific functionality May 12, 2019
@sindresorhus sindresorhus merged commit 8fbce4a into sindresorhus:master May 12, 2019
@sindresorhus
Copy link
Owner

Alright. This seems like the best solution for now... Thanks :)

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