首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Chrome扩展中的UMD模块

Chrome扩展中的UMD模块
EN

Stack Overflow用户
提问于 2020-04-23 03:08:20
回答 1查看 210关注 0票数 0

UMD模块的定义大致如下:

代码语言:javascript
复制
(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不exist
  • exports不exist
  • this不绑定到window

由于这个原因,看起来UMD模块在Chrome扩展环境中失败了。有什么办法可以让UMD模块在Chrome扩展中正确地导出到window对象中吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 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")

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61378682

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档