将 CommonJS 3rd 方库包装为 AMD 模块
Wrap CommonJS 3rd party library as AMD module
我正在尝试将 Node 库与 RequireJS 一起使用,但我无法使其与 AMD 兼容。
我使用这个库:
https://github.com/wdavidw/node-pad
导出函数。问题是,如何将其包装为 AMD 模块?我尝试用 shim options
导出它,但没有成功。我没有看到错误,但它解析了一个 undefined
实例。
有什么建议吗?
到目前为止我尝试了什么:
let config: RequireConfig = {
paths: {
"text": "../node_modules/requirejs-text/text",
"plugins": "../node_modules/durandal/js/plugins",
"durandal": "../node_modules/durandal/js",
"transitions": "../node_modules/durandal/js/transitions",
"knockout": "../node_modules/knockout/build/output/knockout-latest",
"jquery": "../node_modules/jquery/dist/jquery",
"bootstrap": "../node_modules/bootstrap/dist/js/bootstrap",
"pad": "../node_modules/pad/lib/index",
"i18n-iso-countries": "../node_modules/i18n-iso-countries/index",
"json": "../node_modules/requirejs-plugins/src/json",
"alpha2codes": "../node_modules/i18n-iso-countries/alpha2codes",
"codes": "../node_modules/i18n-iso-countries/codes.json",
"languages": "../node_modules/i18n-iso-countries/languages",
"ar": "../node_modules/i18n-iso-countries/langs/ar.json",
"cs": "../node_modules/i18n-iso-countries/langs/cs.json",
"de": "../node_modules/i18n-iso-countries/langs/de.json",
"en": "../node_modules/i18n-iso-countries/langs/en.json",
"es": "../node_modules/i18n-iso-countries/langs/es.json",
"et": "../node_modules/i18n-iso-countries/langs/et.json",
"fi": "../node_modules/i18n-iso-countries/langs/fi.json",
"fr": "../node_modules/i18n-iso-countries/langs/fr.json",
"hu": "../node_modules/i18n-iso-countries/langs/hu.json",
"it": "../node_modules/i18n-iso-countries/langs/it.json",
"nb": "../node_modules/i18n-iso-countries/langs/nb.json",
"nl": "../node_modules/i18n-iso-countries/langs/nl.json",
"nn": "../node_modules/i18n-iso-countries/langs/nn.json",
"pl": "../node_modules/i18n-iso-countries/langs/pl.json",
"pt": "../node_modules/i18n-iso-countries/langs/pt.json",
"ru": "../node_modules/i18n-iso-countries/langs/ru.json",
"sv": "../node_modules/i18n-iso-countries/langs/sv.json",
"tr": "../node_modules/i18n-iso-countries/langs/tr.json",
"zh": "../node_modules/i18n-iso-countries/langs/zh.json"
},
shim: {
"bootstrap": {
deps: ["jquery"]
},
"i18n-iso-countries": {
deps: ["pad", "alpha2codes", "languages"]
},
"pad": {
exports: "pad"
}
},
urlArgs: `=${new Date().getTime()}`
};
requirejs.config(config);
require(['jquery', 'bootstrap'], () => {
require(["./bootstrapper"], (b) => {
var instance = new b();
instance.init();
});
});
shim
选项不会将 non-AMD 模块包装到 AMD define
中。它实际上是用于不支持任何模块系统的脚本。要使用 RequireJS 加载 CommonJS 模块,需要完成的最少工作是用这种形式的 define
调用包装它:
define(function (require, exports, module) {
// script contents here...
});
有很多方法可以做到。您也可以在用于添加包装的任何构建中实施构建步骤。或者你可以使用一个工具,它已经像 RequireJS 的优化器(r.js
)。
我正在尝试将 Node 库与 RequireJS 一起使用,但我无法使其与 AMD 兼容。 我使用这个库: https://github.com/wdavidw/node-pad
导出函数。问题是,如何将其包装为 AMD 模块?我尝试用 shim options
导出它,但没有成功。我没有看到错误,但它解析了一个 undefined
实例。
有什么建议吗?
到目前为止我尝试了什么:
let config: RequireConfig = {
paths: {
"text": "../node_modules/requirejs-text/text",
"plugins": "../node_modules/durandal/js/plugins",
"durandal": "../node_modules/durandal/js",
"transitions": "../node_modules/durandal/js/transitions",
"knockout": "../node_modules/knockout/build/output/knockout-latest",
"jquery": "../node_modules/jquery/dist/jquery",
"bootstrap": "../node_modules/bootstrap/dist/js/bootstrap",
"pad": "../node_modules/pad/lib/index",
"i18n-iso-countries": "../node_modules/i18n-iso-countries/index",
"json": "../node_modules/requirejs-plugins/src/json",
"alpha2codes": "../node_modules/i18n-iso-countries/alpha2codes",
"codes": "../node_modules/i18n-iso-countries/codes.json",
"languages": "../node_modules/i18n-iso-countries/languages",
"ar": "../node_modules/i18n-iso-countries/langs/ar.json",
"cs": "../node_modules/i18n-iso-countries/langs/cs.json",
"de": "../node_modules/i18n-iso-countries/langs/de.json",
"en": "../node_modules/i18n-iso-countries/langs/en.json",
"es": "../node_modules/i18n-iso-countries/langs/es.json",
"et": "../node_modules/i18n-iso-countries/langs/et.json",
"fi": "../node_modules/i18n-iso-countries/langs/fi.json",
"fr": "../node_modules/i18n-iso-countries/langs/fr.json",
"hu": "../node_modules/i18n-iso-countries/langs/hu.json",
"it": "../node_modules/i18n-iso-countries/langs/it.json",
"nb": "../node_modules/i18n-iso-countries/langs/nb.json",
"nl": "../node_modules/i18n-iso-countries/langs/nl.json",
"nn": "../node_modules/i18n-iso-countries/langs/nn.json",
"pl": "../node_modules/i18n-iso-countries/langs/pl.json",
"pt": "../node_modules/i18n-iso-countries/langs/pt.json",
"ru": "../node_modules/i18n-iso-countries/langs/ru.json",
"sv": "../node_modules/i18n-iso-countries/langs/sv.json",
"tr": "../node_modules/i18n-iso-countries/langs/tr.json",
"zh": "../node_modules/i18n-iso-countries/langs/zh.json"
},
shim: {
"bootstrap": {
deps: ["jquery"]
},
"i18n-iso-countries": {
deps: ["pad", "alpha2codes", "languages"]
},
"pad": {
exports: "pad"
}
},
urlArgs: `=${new Date().getTime()}`
};
requirejs.config(config);
require(['jquery', 'bootstrap'], () => {
require(["./bootstrapper"], (b) => {
var instance = new b();
instance.init();
});
});
shim
选项不会将 non-AMD 模块包装到 AMD define
中。它实际上是用于不支持任何模块系统的脚本。要使用 RequireJS 加载 CommonJS 模块,需要完成的最少工作是用这种形式的 define
调用包装它:
define(function (require, exports, module) {
// script contents here...
});
有很多方法可以做到。您也可以在用于添加包装的任何构建中实施构建步骤。或者你可以使用一个工具,它已经像 RequireJS 的优化器(r.js
)。