How do I shim a non CommonJS, non AMD package whic

2019-06-26 08:39发布

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?

标签: jspm systemjs
1条回答
淡お忘
2楼-- · 2019-06-26 08:52

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.)

查看更多
登录 后发表回答