3

I'm facing a problem with exposing global jquery to several npm loaded non-commonjs libs.

I have a following jquery expose config in package.json:

"browserify-shim": {
"jquery": "global:jQuery",
...

And I'm trying to apply this to this datepicker from eonasdan that has a common-js dependency resolving:

'use strict';
if (typeof define === 'function' && define.amd) {
    // AMD is used - Register as an anonymous module.
    define(['jquery', 'moment'], factory);
} else if (typeof exports === 'object') {
    factory(require('jquery'), require('moment'));
} else {

But the compiled file doesn't replace require('jquery') with a global variable construct as it happens in all the rest of files including other libs, like this compiled bootstrap-slider for example:

if(typeof define === "function" && define.amd) {
    define(["jquery"], factory);
}
else if(typeof module === "object" && module.exports) {
    var jQuery;
    try {
        jQuery = (typeof window !== "undefined" ? window['jQuery'] : typeof global !== "undefined" ? global['jQuery'] : null);
    }
    catch (err) {
        jQuery = null;
    }
    module.exports = factory(jQuery);
}

What could possibly be a reason of this lib being such exceptional?

Does the depth of require chain matter for browserify-shim? Cause the slider is being required in the main bundling file, while datepicker is a dependency of dependency of a bundle (3rd level)

Brock
  • 1,635
  • 2
  • 18
  • 27
  • I ended up looking [here](https://github.com/Eonasdan/bootstrap-datetimepicker/blob/master/src/js/bootstrap-datetimepicker.js) and reading [this](http://stackoverflow.com/questions/24835954/configure-a-generic-jquery-plugin-with-browserify-shim), and I'm wondering whether browserify-shim applies it's config in all the dependencies too; have you declared jquery as a dependency for the libraries you use? – Kenney Sep 16 '15 at 18:53
  • Yes I've been trying to shim this lib declaring jQ as a dependency, but as you can see, the lib is actually CommonJS compatible and does resolve well un-shimmed (with exception for require('jquery') replacement). For now I've solved the problem by dropping jquery expose approach, just made all the application sections to rely onto single bundle that holds 100% of scripts loaded (well, except for analytics and alike) – Brock Sep 17 '15 at 17:04
  • Identical problem with Magnific Popup. – chrylis -cautiouslyoptimistic- Feb 12 '16 at 18:15

0 Answers0