Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

require cdn libraries in browserify without bundling it in the final js file

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.

like image 317
prabir Avatar asked Dec 22 '13 22:12

prabir


People also ask

When should I use Browserify?

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.

What are some of the benefits of Browserify?

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.

Is Browserify a dev dependency?

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.


1 Answers

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')

like image 155
silkAdmin Avatar answered Oct 04 '22 12:10

silkAdmin