Using browserify shim with browserify insertGlobalVars option

661 views Asked by At

I'm using browerify with browserify-shim to us the version of underscore and jQuery defined by the environment it's running in. I found myself requiring the same modules over and over again, so went to look for a way to automate that. The option opts.insertGlobalVars does it, but can I make it work with browserify-shim?

package.json

"browserify-shim": {
   "jquery": "global:jQuery",
   "underscore": "global:_"
}

gruntfile.js

browserify: {
  options: {
    browserifyOptions: {
      transform: ['browserify-shim'],
        insertGlobalVars: {
           _: function(file, dir) {
             return 'require("underscore")';
           }
        }
     }
  }

The _ get recognised as variable and an attempt is made to define it. Unfortunately this results in the file not being found. Since I don't include the file myself I can't provide a path or than in the example above.

Error: Cannot find module 'jquery' from 'path/to/file'

Edit: using require('underscore') directly inside my module does work.

0

There are 0 answers