javascript - 巴别塔 7 : Uncaught ReferenceError after transpiling a module

标签 javascript webpack google-chrome-extension babeljs

我的 Chrome 扩展项目使用来自 https://github.com/tfoxy/chrome-promise/blob/master/chrome-promise.js 的 Chrome-Promise 模块为了 promise Chrome API 的回调式函数。

使用 Babel 6 编译项目一直运行良好。最近我搬到了 Babel 7。从那时起,当我允许 chrome-promise.js 文件被 Babel 转译时,这会导致在运行延期。当我按原样保留 chrome-promise.js 时,该程序运行良好,未转译。

这是我的 webpack 配置 js 文件的摘录:

module.exports = {
  mode: 'production',
  entry: {
    cs: ['./build/cs.js'],
    bg: ['./build/bg.js'],
    popup: ['./build/popup.js'],
    chromepromise: ['./build/chromepromise.js']
  },
  output: {
    path: path.join(__dirname, 'build'),
    filename: '[name].js'
  },
  module: {
    rules: [{ 
      test: /\.js$/, 
      exclude: [/node_modules/],
      loader: "babel-loader" 
    }]
  },

还有我的 package.json 之一,带有 Babel 设置:

{
  "name": "Test_CRX",
  "version": "1.0.0",
  "main": "cs.js",
  "scripts": {
    "build": "node build",
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "description": "",
  "dependencies": {
    "@babel/runtime": "^7.1.2",
    "archiver": "^3.0.0",
    "babel-polyfill": "^6.26.0",
    "babel-regenerator-runtime": "^6.5.0",
    "chrome-promise": "^3.0.1",
    "terser": "^3.10.1"
  },
  "devDependencies": {
    "@babel/core": "^7.1.2",
    "@babel/plugin-proposal-decorators": "^7.1.2",
    "@babel/plugin-proposal-function-bind": "^7.0.0",
    "@babel/plugin-transform-modules-umd": "^7.1.0",
    "@babel/plugin-transform-regenerator": "^7.0.0",
    "@babel/plugin-transform-runtime": "^7.1.0",
    "@babel/preset-env": "^7.1.0",
    "@babel/preset-react": "^7.0.0",
    "babel-loader": "^8.0.4",
    "babel-plugin-add-module-exports": "^1.0.0",
    "shelljs": "^0.8.1",
    "terser-webpack-plugin": "^1.1.0",
    "uglifyjs-webpack-plugin": "^2.0.1",
    "webpack": "^4.20.2",
    "webpack-cli": "^3.1.2"
  },
  "babel": {
    "presets": [
      [
        "@babel/env",
        {
          "targets": {
            "chrome": 60
          }
        }
      ],
      "@babel/react"
    ],
    "plugins": [
    ]
  }
}

注意:虽然我用Terser压缩了JS代码,但停用后还是报错。

编辑 查看我在 github.com/Steve06/chrome-promise-babel-7-issue-repo 上制作的最小 repo

最佳答案

@babel/env 预设默认将您的文件转译为 commonjs,这需要转译后的文件由 require导入。为了使其与您的 Chrome 扩展程序兼容,您需要将文件转换为 umd 模块。将其放入您的 package.json:

"presets": [
  [
    "@babel/env",
    {
      "targets": {
        "chrome": 60,
        "modules": "umd"
      }
    }
  ]
],

你还应该知道 source/chrome-promise.js 已经是一个 umd 模块,所以它实际上不需要转译。

关于javascript - 巴别塔 7 : Uncaught ReferenceError after transpiling a module,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52929562/

相关文章:

javascript - WordPress 中的 Ajax 调用不会转到 admin-ajax.php

Javascript变量继承问题

javascript - 重构npm请求方法代码

javascript - Next.js:你可能需要一个合适的加载器来处理这个文件类型,目前没有配置加载器来处理这个文件

javascript - 创建 chrome 扩展以将数据发送到本地主机服务器

visual-studio-2012 - 我可以获得 Chrome 应用程序和扩展程序的智能感知吗?

javascript - TypeScript 中子类原型(prototype)的构造函数是可枚举的

javascript - 在 Webpack 插件中添加依赖

Docker 撰写,webpack 权限被拒绝

google-chrome - 在 Chrome 中禁用 "Fit to Page"