【问题标题】:How do you use a requirejs friendy JavaScript file without using requirejs? I.o.w. how to demodularize?如何在不使用 requirejs 的情况下使用 requirejs 友好的 JavaScript 文件? I.o.w.如何解模化?
【发布时间】:2013-08-13 14:48:14
【问题描述】:

假设我有一个 JS 库,整齐地包裹在 define('myModule', function(myModule) { return myModule.someObject; });

如何在不使用 requirejs 或任何其他模块处理框架的情况下将 myModule.someObject 绑定到全局范围(请不要问为什么,我知道模块化编程比将内容放在全局范围内有很多好处)?

问题是:在开发过程中,我想使用 requirejs。我们构建的库应该能够被使用 requirejs(AMD、CommonJS 等)的人包含,但也应该以 window.SomeObject 的形式提供给那些不想使用 require 的人,只是为了能够使用我们的SomeObject。开发阶段结束后,所有代码将被压缩并混淆为单个 JS 文件。

我想我只是在用错误的搜索词进行谷歌搜索,因为我只能找到一个问题的答案,即如何包含未包含在 requirejs 友好 define 函数中的代码。

对此的任何想法将不胜感激。谢谢!

--- 编辑 ---

我的文件(在一切开始之前)看起来像:

(function(define, global) {
  define([a,b,c],function(theA, theB, theC) {
    return theA + theB + theC; // or something, it doesn't matter
  });
})(define, this);

我在想这样的事情:

(function(define, global) {
  // same as above
})(typeof define === 'function' 
      ? define 
      : function(factory /* need more args? */) { /* solution here */ }, this);

但我不确定如何正确实现它......

【问题讨论】:

标签: requirejs amd commonjs


【解决方案1】:

我猜你需要包装你的模块,这样就可以在没有 requirejs 的情况下访问它们:

if ( typeof define === "function" && define.amd ) {
    define( "mymodule", [], function () { 
      // do your logic
      return mystuff; 
    } );
} else {
   // do your logic
   window.mystuff = mystuff;
}

jQuery 为例。

【讨论】:

  • 复制 jQuery 不是这里的标准。使用匿名模块总是更可取的。添加了详细的答案。
【解决方案2】:

如果你能提供帮助,我会避免给你的模块一个 id,这会降低它的可移植性。 jQuery 非常烦人,它迫使您设置 jquery 路径选项,但出于兼容性原因,他们这样做了。如果可以的话,总是更喜欢匿名模块。

来自 jQuery 源代码

// Register as a named AMD module, since jQuery can be concatenated with other
// files that may use define, but not via a proper concatenation script that
// understands anonymous AMD modules. A named AMD is safest and most robust
// way to register. Lowercase jquery is used because AMD module names are
// derived from file names, and jQuery is normally delivered in a lowercase
// file name. Do this after creating the global so that if an AMD module wants
// to call noConflict to hide this version of jQuery, it will work.

James Burke 也加入了little more detail here

我会改用umdjs repository 中的一个更常见的例子:

(function (root, factory) {
    if (typeof define === 'function' && define.amd) {
        // AMD. Register as an anonymous module.
        define(['b'], factory);
    } else {
        // Browser globals
        root.amdWeb = factory(root.b);
    }
}(this, function (b) {
    //use b in some fashion.

    // Just return a value to define the module export.
    // This example returns an object, but the module
    // can return a function as the exported value.
    return {};
}));

另一个同样支持 CommonJS 的例子,查看the reqwest library

!function (name, context, definition) {
  if (typeof module != 'undefined' && module.exports) module.exports = definition()
  else if (typeof define == 'function' && define.amd) define(definition)
  else context[name] = definition()
}('reqwest', this, function () {

    return {};

});

【讨论】:

  • 谢谢西蒙!我想有很多阅读和测试要做。 :-)
  • 我会坚持使用 umdjs 中的示例,因为它来自 RequireJS 的作者(与 almond 使用的包装相同) ).
【解决方案3】:

How can I provide a library to others that does not depend on RequireJS?

这允许您发布不随所有 RequireJS 发布的代码,并允许您导出在没有 AMD 加载程序的普通网页上工作的任何类型的 API。

您需要制作一个使用wrapalmond 的构建配置文件。

这一切都感觉很脏,但我已经按照您所描述的内容(通过遵循 almond 自述文件)让它工作。

【讨论】:

  • 太棒了:)没问题。
猜你喜欢
  • 2015-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多