Javascript Babel 7:传输模块后未捕获的引用错误
我的Chrome扩展项目使用来自的Chrome Promise模块来实现Chrome API的回调风格函数 用巴别塔6传输项目一直都很好。最近我搬到了巴别塔7号。此后,当我允许Babel传输chrome-promise.js文件时,这会导致在运行扩展时出现“未捕获引用错误:ChromePromise未定义”错误。当我将chrome-promise.js保持原样,不进行翻译时,该程序运行良好 以下是我的网页配置js文件的摘录:Javascript Babel 7:传输模块后未捕获的引用错误,javascript,webpack,google-chrome-extension,babeljs,chrome-promise,Javascript,Webpack,Google Chrome Extension,Babeljs,Chrome Promise,我的Chrome扩展项目使用来自的Chrome Promise模块来实现Chrome API的回调风格函数 用巴别塔6传输项目一直都很好。最近我搬到了巴别塔7号。此后,当我允许Babel传输chrome-promise.js文件时,这会导致在运行扩展时出现“未捕获引用错误:ChromePromise未定义”错误。当我将chrome-promise.js保持原样,不进行翻译时,该程序运行良好 以下是我的网页配置js文件的摘录: module.exports = { mode: 'product
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"
}]
},
还有一个带有Babel设置的my package.json:
{
"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上所做的最小回购
@babel/env
预设默认情况下将文件传输到commonjs
,这要求传输的文件包含在require
或import
中。要使其与您的Chrome扩展兼容,您需要将文件作为umd
模块进行传输。将其放入您的包中。json
:
"presets": [
[
"@babel/env",
{
"targets": {
"chrome": 60,
"modules": "umd"
}
}
]
],
您还应该知道,
source/chrome promise.js
已经是一个umd
模块,因此它实际上不需要传输。您如何在项目中包含chrome promise
?通过import
或script
?通过扩展名的manifest.json的background->scrips键,如:“background”:{“scripts”:[“chrome promise.js”,“bg.js”]}文件名正确吗?是的,我知道我在原始帖子中没有破折号就发布了chrome promise,但是manifest.json中的实际文件名和对它的引用始终是一致的。我现在甚至做了一个最低限度的回购来说明这个问题,因为我也在Github上向巴别塔制造商提出了这个问题。检查:您最初的解决方案是错误的,因为“模块”选项需要在“目标”括号之外。Webpack在这方面抛出了一个错误。我编辑了你的帖子。然而,最终的结果是不变的,我仍然得到了未捕获的引用错误。无论它是否已经是一个UMD模块,问题是如何让Babel 7在不返回错误代码的情况下处理它。我已经更新了回购协议,以反映您建议的更改