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不existexports不existthis不绑定到window由于这个原因,看起来UMD模块在Chrome扩展环境中失败了。有什么办法可以让UMD模块在Chrome扩展中正确地导出到window对象中吗?
发布于 2020-04-23 15:24:27
正如@wOxxOm正确指出的那样,Chrome扩展环境与浏览器相同,this确实绑定到window,因此UMD模块可以而且应该与扩展一起工作。
事实证明,实际的问题是babel生成了一个用this代替undefined的包,这就是在这个问题中概述和解决的问题:How to stop babel from transpiling 'this' to 'undefined' (and inserting "use strict")。
https://stackoverflow.com/questions/61378682
复制相似问题