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

Making injecting node builtins optional #588

Closed
ghostwords opened this issue Nov 16, 2014 · 8 comments
Closed

Making injecting node builtins optional #588

ghostwords opened this issue Nov 16, 2014 · 8 comments

Comments

@ghostwords
Copy link

Could injecting dummy objects from https://github.com/webpack/node-libs-browser become optional? I think this is both on-demand (only objects that get used get injected) and optional in browserify (https://github.com/substack/node-browserify/blob/master/lib/builtins.js).

@ghostwords
Copy link
Author

Ah, I should explain my situation a bit more.

My code uses checks like process.env.NODE_ENV != 'production' together with envify-loader and UglifyJs's dead code elimination to remove debugging statements from production code. This works fine, but the code gets an unwanted process module, even though no process calls survive envify-loader.

@jhnns
Copy link
Member

jhnns commented Nov 17, 2014

What does the envify-loader do?

@ghostwords
Copy link
Author

It loads envify, which preprocesses your code to replace instances of process.env.NODE_ENV with its value. Then, UglifyJs strips out blocks surrounded by things like 'production' != 'production'.

Looks like I can work around this in my code by using webpack.DefinePlugin to inject constants that get their values from process.env.NODE_ENV in webpack's config.

Still, I feel like this behavior breaks expectations. If dead-code elimination results in unused modules, those modules should no longer get injected.

@ghostwords
Copy link
Author

Take a look at https://www.npmjs.org/package/react for an example of third-party code using process.env.NODE_ENV to guard dev-only blocks.

Note: by default, React will be in development mode. The development version includes extra warnings about common mistakes, whereas the production version includes extra performance optimizations and strips all error messages.

To use React in production mode, set the environment variable NODE_ENV to production. A minifier that performs dead-code elimination such as UglifyJS is recommended to completely remove the extra code present in development mode.

@jhnns
Copy link
Member

jhnns commented Nov 17, 2014

Yes, the webpack.DefinePlugin is the way to go here.

I'm pretty sure that modules which are required in a dead-code block are not included into the bundle. But maybe this only happens when minification is active @sokra?

@sokra
Copy link
Member

sokra commented Nov 18, 2014

Could injecting dummy objects from https://github.com/webpack/node-libs-browser become optional?

You can disable it with tne node option.

I think this is both on-demand (only objects that get used get injected)

So it is.

This works fine, but the code gets an unwanted process module, even though no process calls survive envify-loader.

That should not be the case.

If dead-code elimination results in unused modules, those modules should no longer get injected.

This should be the case too.

@ghostwords
Copy link
Author

You guys are right, I tried using process.env.NODE_ENV directly in the entry module in combination with envify-loader again, and it worked with no unwanted node-libs-browser process module. I must've had a typo before somewhere. Sorry for the false alarm!

You can disable injecting dummy node objects with the node option.

Ah, good to know, thanks!

@ghostwords
Copy link
Author

Also, just learned to use transform-loader instead of envify-loader: mjohnston/envify-loader#1

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

No branches or pull requests

3 participants