如何使RequireJS兼容的庫可以包含在其他JS中?

[英]How to make RequireJS-compatible library to be includable to other JS?


I develop an application and some modules for it. This modules I include with RequireJS, but want to include also in another applications which has not RequireJS support.

我為它開發了一個應用程序和一些模塊。這個模塊我包含RequireJS,但也希望包含在另一個沒有RequireJS支持的應用程序中。

How need I to modify my library file to let it to be included in other JS file (by copying content)?

我需要如何修改我的庫文件以使其包含在其他JS文件中(通過復制內容)?

So I want library to have both ways of use:

所以我希望庫有兩種使用方式:

requirejs(["helper/util"], function(util) {
  // library loaded
  window.MyUtilModule = util;
  window.MyUtilModule.someFunc();
});

and

/* >> No RequreJS library load here << */
/* >> Code copied from 'helper/util.js' goes here << */
window.MyUtilModule.someFunc(); // already loaded by simple including code into another file

1 个解决方案

#1


0  

Read about UMD. Typical UMD header looks like:

了解UMD。典型的UMD標題如下:

(function (factory) {
  if (typeof exports === 'object') {
    module.exports = factory(require('backbone'), require('underscore'));
  } else if (typeof define === 'function' && define.amd) {
    define(['backbone', 'underscore'], factory);
  }
}(function (Backbone, _) {
  // your code
}));

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2016/09/02/a8287ef7fb77085ba3f7444718484aa7.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com