gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 04:34:44 25 4
gpt4 key购买 nike

我的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com