将CommonJS第三方库包装为AMD模块

时间:2017-02-08 21:29:58

标签: javascript node.js requirejs amd commonjs

我正在尝试使用带有RequireJS的Node库,但我无法使其与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();
    });
});

1 个答案:

答案 0 :(得分:1)

shim选项不会将非AMD模块封装到AMD define中。它实际上意味着用于不支持任何模块系统的脚本。要使用RequireJS加载CommonJS模块,需要完成的最小工作是使用此表单的define调用来包装它:

define(function (require, exports, module) {
  // script contents here...
});

有很多方法可以做到这一点。您可以在用于添加包装的任何构建中实现构建步骤。或者你可以使用像RequireJS'优化器(r.js)那样的工具。