4

I am using jspm for the first time and already ran into a snag.

I need to figure out how to "shim" a proprietary script which lives on our company's private npm registry.

Package: widget

  • Resides on private npm registry
  • Is not a CommonJS, UMD/AMD module
  • Depends on lodash and jquery, but assumes they exist on global scope
  • Exposes Widget on the global scope

Here's the (hypothetical) code

var Widget = {
  render: function(el, symbol) {
    symbol = _.trim(symbol);
    $(el).text(symbol);
  }
};

app.js

var widget = require("Widget");
widget.render(document.getElementById("name"), " Fred ");

index.html

<body>
  <div id="name"></div>

  <script src="jspm_packages/system.js"></script>
  <script src="config.js"></script>
  <script>
    System.import("app");
  </script>
</body>

When I run this page in a local web server, I get an error:

Uncaught Reference: _ is not defined

How can I provide a "shim" for widget?

jessegavin
  • 74,067
  • 28
  • 136
  • 164

1 Answers1

0

Your best bet is that if you can update the package.json for the Widget package you can tell JSPM it needs a shim there:

{
  "shim": {
    "widget": { "deps": ["jquery", "lodash"] }
  }
}

(Where "widget" is the module name inside the package.)

If for some reason you can't directly touch that npm package, then you can override the shim information when you jspm install it:

jspm install widget -o "{ shim: { 'widget': { deps: ['jquery', 'lodash'] } } }"

(Again, where 'widget' is the module name, as local to inside the package itself.)

WorldMaker
  • 129
  • 6
  • This approach works fine for the jquery dependency because the jquery package referenced from jspm registry does a global export. But lodash alias is referencing npm:lodash and this particular package does not export a global. So my workaround is to import lodash from my main module and manually export it on window. I saw a ticket in systemjs for this particular issue: https://github.com/jspm/registry/issues/431 – Max Aug 05 '15 at 07:48