问题描述
我第一次使用 jspm 已经遇到了障碍.
I am using jspm for the first time and already ran into a snag.
我需要弄清楚如何填充"我们公司私有 npm 注册表中的专有脚本.
I need to figure out how to "shim" a proprietary script which lives on our company's private npm registry.
包:widget
- 驻留在私有 npm 注册表中
- 不是CommonJS、UMD/AMD 模块
- 依赖于
lodash
和jquery
,但假设它们存在于全局范围内 - 在全局范围内公开
Widget
- Resides on private npm registry
- Is not a CommonJS, UMD/AMD module
- Depends on
lodash
andjquery
, but assumes they exist on global scope - Exposes
Widget
on the global scope
这是(假设的)代码
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>
当我在本地 Web 服务器中运行此页面时,出现错误:
When I run this page in a local web server, I get an error:
未捕获的引用:_ 未定义
如何为 widget
提供垫片"?
How can I provide a "shim" for widget
?
推荐答案
你最好的办法是,如果你可以更新 Widget 包的 package.json,你就可以告诉 JSPM 它需要一个垫片:
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"] }
}
}
(其中widget"是包内的模块名称.)
(Where "widget" is the module name inside the package.)
如果因为某些原因你不能直接接触那个npm包,那么你可以在jspm安装时覆盖shim信息:
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'] } } }"
(同样,其中 'widget' 是模块名称,作为包本身内部的本地名称.)
(Again, where 'widget' is the module name, as local to inside the package itself.)
这篇关于我如何填充依赖于全局 jQuery & 的非 CommonJS、非 AMD 包洛达什?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!