-
Notifications
You must be signed in to change notification settings - Fork 26
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
Browser(ify) friendliness #2
Browser(ify) friendliness #2
Conversation
Hello @zertosh Thanks for the PR! What is the difference between the browser version and the main module? Is it just moving Why does envify need to be a dependency? |
Yup that's the only code difference.
envify needs to be a dependency because browserify will try to locate and apply that transform when someone tries to bundle Without envify, browserify will produce (comments removed for brevity): (function (process){
var __DEV__ = process.env.NODE_ENV !== 'production';
var warning = function() {};
if (__DEV__) {
warning = function(condition, format, args) {
/* code */
};
}
module.exports = warning;
}).call(this,require('_process')) With envify, the value of var __DEV__ = "production" !== 'production';
var warning = function() {};
if (__DEV__) {
warning = function(condition, format, args) {
/* code */
};
}
module.exports = warning; But by moving the code a bit, you can produce: var warning = function() {};
if ("production" !== 'production') {
warning = function(condition, format, args) {
/* code */
};
} And then a tool like uglify can remove the entire block of unreachable code. This is the same thing React does and the same thing I do with invariant. |
Also, you want to have two versions of the file because for nodejs use, your original version is more efficient. Accessing |
I am familiar with envify and the browserify community of packages. What I usually do is globally transform using envify. And uglify usually takes care of dead code. I don't like the idea of tracking two scripts that are identical. Are you finding uglify dead code elimination does not work as intended with this package? |
Applying global transforms can lead to very unexpected results. As for the dead code elimination, no, uglify (as of v2.4.23) is only able to determine that a conditional branch of code is unreachable if the condition's test is a constant. I'm bringing this issue up, not because I'm using |
Fair enough. Merging. I feel this might break expected behavior for some. Will bump as major release. |
Browser(ify) friendliness
Thanks! |
This adds a browserify friendly version of the module. It allows for the package to be used in browserify builds with
detectGlobals: false
, and for the unused parts to be minified away.cc: @BerkeleyTrue