javascript - browserify Using JQuery globally and in Module -


i have been given template work client has 28 different jquery plugins client wants use (e.g. ditching them not option).

however want use browserify modularise code, short of trying shim 28 plugin , thir dependancy can't work out how , not have load jquery browserify , globally.

i tried doing this:

window.jquery = require('jquery') window.$ = window.jquery 

and this:

var globals = function(){   window.jquery = require('jquery')   window.$ = window.jquery }  globals(); 

but neither seem work , plugins throw error. how might make work?

this pretty way it, think.

  1. npm install jquery
  2. npm install browserify-shim
  3. put line in package.json:

    browserify-shim" : {   "./node_modules/jquery/dist/jquery.js" : "$" } 

so on server, usual require('jquery') point node_modules spot. when run browserify, set window.$ same code (you use jquery). also, if did want shim plugins, add them this:

    "browserify-shim" : {       "./node_modules/jquery/dist/jquery.js" : "jquery",       "./plugins/bs_modal.js" :  {          "depends": [ "./node_modules/jquery/dist/jquery.js" ]        }     } 

or, cleaner:

    "browser" : {"jquery: : "./node_modules/jquery/dist/jquery.js"},     "browserify-shim" : {       "jquery" : "jquery",       "./plugins/bs_modal.js" :  {          "depends": [ "jquery" ]        }     } 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -