thlorenz / browserify-shim

📩 Makes CommonJS incompatible files browserifyable.
MIT License
933 stars 87 forks source link

injecting a global #177

Closed vvo closed 9 years ago

vvo commented 9 years ago

Hi, I was wondering if given this file:

file.js

(function($) {
  // use $
})(window.jQuery)

I was able to inject window.jQuery = require('jquery'); by using browserify-shim?

I cannot make it work, the only thing I want is to require('./vendor/file.js') with window.jQuery being a commonJS reference to jQuery (since we can now require('jquery')

Thanks!

bendrucker commented 9 years ago

What config are you using?

vvo commented 9 years ago

@bendrucker I was more asking on a feature side if that was feasible.

bendrucker commented 9 years ago

Well it should work

vvo commented 9 years ago

So what would be the configuration to use to inject window.jQuery as require('jquery') in a file tree containing a package.json and a file2.js being required in index.js with require('./file2.js') that look like:

(function($) {
  // use $
})(window.jQuery)

?

bendrucker commented 9 years ago

If I'm understanding right, this should be in the pkg:

{
  "browserify-shim": {
    "./file2.js": {
      "depends": "jquery:jQuery"
    }
  }
}
vvo commented 9 years ago

Thanks, what would be the configuration if ./file.2 is moduleName?

Like

app.js

var module = require('moduleName');

moduleName

(function($) {
  // use $
})(window.jQuery)

Basically I am trying to force to use my node_modules jQuery to typeahead (0.10.5): https://github.com/twitter/typeahead.js/blob/v0.10.5/dist/typeahead.bundle.js

thx @bendrucker

bendrucker commented 9 years ago

So that story gets a bit more complicated. Browserify transforms don't run on code in node_modules unless you make it a global transform.

browserify -e app.js -g browserify-shim
{
  "module-name": {
    "depends": "jquery:jQuery"
  }
}
bendrucker commented 9 years ago

Assuming this is resolved, please update if it isn't

vvo commented 9 years ago

Thanks for the answer, did not know about global transforms. Sorry for late reply