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 $); install browserify-shim:
npm install browserify-shim --save-devin package.json file, tell browserify use browserify-shim transform:
{ "browserify": { "transform": [ "browserify-shim" ] } }in package.json file, tell browserify-shim map jquery jquery in global scope:
{ "browserify-shim": { "jquery": "global:jquery" } }run browserify
browserify mymodule.js > bundle.js
if examine bundle.js notice require('jquery') replaced (window.jquery).
Comments
Post a Comment