2017-02-08 74 views
0

我正在嘗試使用RequireJS的Node庫,但無法使其與AMD兼容。 我使用這個庫: https://github.com/wdavidw/node-pad將CommonJS第三方庫包裝爲AMD模塊

它輸出一個函數。問題是,如何將它作爲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

shim選項不換行非AMD模塊爲AMD define。它確實意味着用於不支持任何模塊系統的腳本。與RequireJS加載CommonJS的模塊,需要做的最少的工作是用define電話這種形式把它包起來:

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

有很多方法可以做到這一點。您可以在任何構建中實施構建步驟,以用於添加包裝。或者你可以使用已經像RequireJS的優化器(r.js)那樣的工具。

相關問題