thlorenz / browserify-shim

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

Shimmed modules can't use libraries being pulled in from a CDN as dependencies #156

Closed chadwatson closed 9 years ago

chadwatson commented 9 years ago

I'm getting jQuery from the Google CDN.

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

Then using a shim file, and linking to it in my package.json. package.json

"browserify": {
    "transform": [
        "browserify-shim",
        "browserify-ejs"
    ]
},
"browserify-shim": "./_src/js/config/shim.js"

shim.js

module.exports = {
    'jquery': { 'exports': 'global:$' },
    '../lib/jquery.throttleDebounce.js': { 'depends': { 'jquery' : null } }
}

Using my custom Gulp script I receive the following error:

[13:37:04] gulp-notify: [Compile Error] Error: Cannot find module 'jquery' from '/Users/chadwatson/Projects/gulp-starter/_src/js/lib'

I can successfully use require('jquery') in all of my modules.

I can't seem to find any documentation on how to shim global modules from an external script and then use them as a dependency for a shimmed module in browserify-shim.

bendrucker commented 9 years ago

We need code here — please post the contests of jquery.throttleDebounce.js

LoicMahieu commented 9 years ago

I think your problem is that browserify-shim does not transform whole sources, only local to your project. By default, browserify does not "transform" every modules required. For enable global transforming, use global option. I found solution into issue: https://github.com/thlorenz/browserify-shim/issues/143

So when you are adding browserify-shim:

var b = browserify(opts);
b.transform(require('browserify-shim'), { global: true });
bendrucker commented 9 years ago

Ah yes, didn't catch the external script mention. If this isn't the case please say so and I can reopen.

chadwatson commented 9 years ago

Thanks for the replies!