Search code examples
javascriptjqueryjspmsystemjses6-module-loader

Use jspm to load script that depends on global jQuery


Yes I've read How do I shim a non CommonJS, non AMD package which depends on global jQuery & lodash?.

I'm trying to load X.js, through jspm, which is not a 'package' but an old js file I have no control over that needs a global jQuery object and needs to run like it is in a script tag.

I'm using System.import('app/X'); to load it.

I tried various shim / globals tricks to make it load but I can't quite figure it out.

How would one write the config.js to be able to import that X file so that it sees a global jQuery object? Do I have to make X a 'package' and install it to be able to shim it better?

Thanks.


Solution

  • If you installed jquery through jspm, all you need is to set the meta 'deps' property like this:

    System.config({
      meta: {
        'app/X': {
           deps: ['jquery']
        }
      }
    });
    
    System.import('app/X');
    

    Be sure to get the X path correctly and check how jspm sets up System.config 'paths' and 'map', by default trailing .js is added automatically (with paths *.js wildcard) so you must not add it.

    Maybe try to look at these links from the documentation as well https://github.com/systemjs/systemjs/blob/master/docs/module-formats.md#globals https://github.com/systemjs/systemjs/blob/master/docs/config-api.md#meta