javascript - How do I use Browserify with external dependencies? -


i trying introduce browserify site, don't want rewrite js , don't want duplicate instances of jquery , other libraries bundled browserify build.

if build module listing jquery external dependency, how point @ global jquery instance? goal eliminate mylibs global (example below), don't want use in module.

this i'm trying (psudo-code). in site's repo - not module's. module installed bower:

var mylibs.jquery = $.noconflict(); // global used lots of existing code module.exports = {     jquery: mylibs.jquery // can imported module require('jquery') }; 

something i'm trying achieve. possible?

you can achieve using browserify-shim. assuming you've got module named mymodule.js depends on jquery in global scope following contents:

var $ = require('jquery');  console.log(typeof $); 
  1. install browserify-shim:

    npm install browserify-shim --save-dev 
  2. in package.json file, tell browserify use browserify-shim transform:

    {     "browserify": {         "transform": [ "browserify-shim" ]     } } 
  3. in package.json file, tell browserify-shim map jquery jquery in global scope:

    {     "browserify-shim": {         "jquery": "global:jquery"     } } 
  4. run browserify

    browserify mymodule.js > bundle.js 

if examine bundle.js notice require('jquery') replaced (window.jquery).


Comments

Popular posts from this blog

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -

python 3.x - Mapping specific letters onto a list of words -