If I have a library that is being pulled down from cdn and wouldn't like it to be part of the final js file but be able to require it using browserify, how would I solve it?
Here is how I currently solve it using alias
and a shim file.
browserify: { options: { debug: true, transform: [ 'reactify' ], alias: [ 'client/shims/jquery.js:jquery' ] }, app: { src: 'client/app.js', dest: 'public/app.js' } }
here is the shim file client/shims/jquery.js
which I alias to jquery
so I can use require('jquery')
instead of the full path.
module.exports = $;
Is there a shortcut in grunt-browserify to support this scenario? I would like to know if it is possible to define it in Gruntfile.js without creating the shim file.
Adding external: [ 'jquery' ]
seems to totally ignore it and doesn't work.
Browserify solves the problems of having too many JS files referenced in your HTML, inability to use Node modules in the browser, and inability to reference your own modules in your own code. Watchify streamlines the process of bundling your files and will make a change every time you change a JS file in your project.
An additional advantage with Browserify is its use of transforms. Transforms work by injecting streams between resolved modules and content that is returned to transpile / convert code. Using transforms, you can support things like ES6 and JSX without having to precompile your code.
We can also include browserify itself as a dependency, however it isn't a dependency for the project to run – any user to our app can find Superman without needing to run Browserify. It is one of our devDependencies – modules required for developers to make updates to this app.
With browserify-shim you can add this in your package.json file:
"browserify": { "transform": [ "browserify-shim" ] }, "browserify-shim": { "jquery": "global:$" }
Then jquery will be available in your modules via require('jquery')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With