Someone else mentioned an external option, which should be the actual solution. Now consider these tips. You can know most, if not all, of them already, but some of them may help. This will not turn a 600k file into 1k, but it can be significant nonetheless.
Start by checking the advanced options https://github.com/substack/node-browserify#usage and, more specifically, the -no-bundle-external options. If an external parameter works with require ('b') but still includes external libraries, this is your best bet;
Also consider options for global variables. When I started working with the browser, I had one function compiled into a huge library, as it contained all the stubs for its own NodeJS modules. The above options were key in solving this problem. This was in the Grunt task, but if I remember correctly, it was not with the grunt-browsify task, it can still be relevant.
Install the build environment before launching the browser: application, if you have not already done so. There are libraries that depend on these variables for their production compilation (I think React is one of them).
grunt.loadNpmTasks('grunt-env'); // initConfig task env: { dist: { NODE_ENV : 'production', }, dev: { NODE_ENV: 'development', } } grunt.registerTask("build_dist", ['env:dist', 'browserify:app']
Try running Uglify with these parameters defined;
// initConfig task options options: { compress:{ dead_code : true, // discard unreachable code drop_debugger : true, // discard "debugger" statements global_defs : { // global definitions "DEBUG": false, // matters for some libraries }, } }
You can optimize a lot more, but this should help you get started.
B is included separately and is considered global, it works fine for me, performing a dirty replacement bit at my build stage, which requires swapping ("B") with global.
If you upgrade to global.B after compilation, your package will have all the necessary ('b') dependencies. This hack is (possibly) better:
This is unacceptable, but I don’t know how to reduce it, since I don’t know what the hell it is (or more importantly, I can exclude that it still works).
Go to the source code of the generated library, you should be able to identify the different modules and their path to the files.