Chrome 扩展中的 UMD 模块

米格威尔

UMD 模块定义大致如下:

(function (root, factory) {
    if (typeof define === 'function' && define.amd) {
        // AMD. Register as an anonymous module.
        define(['exports', 'b'], factory);
    } else if (typeof exports === 'object' && typeof exports.nodeName !== 'string') {
        // CommonJS
        factory(exports, require('b'));
    } else {
        // Browser globals
        factory((root.commonJsStrict = {}), root.b);
    }
}(this, function (exports, b) {
    //use b in some fashion.

    // attach properties to the exports object to define
    // the exported module properties.
    exports.action = function () {};
}));

问题是 Chrome 扩展不支持这些导出模块的任何方法:

  • define 不存在
  • exports 不存在
  • this 不是必须的 window

出于这个原因,UMD 模块似乎在 Chrome 扩展环境中失败。是否有任何解决方法可以让 UMD 模块正确导出到windowChrome 扩展程序中对象?

米格威尔

正如@wOxxOm 正确指出的那样,Chrome 扩展环境与浏览器相同,并且this确实绑定到window,因此 UMD 模块可以并且应该与扩展一起使用。

事实证明,实际问题是 babel 生成了一个由this替换的包undefined,这是本期概述和解决的问题:如何阻止 babel 从 transpiling 'this' 到 'undefined' (and inserting "use strict")

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章