Ste*_*e06 5 javascript google-chrome-extension webpack babeljs chrome-promise
我的Chrome扩展程序项目使用https://github.com/tfoxy/chrome-promise/blob/master/chrome-promise.js中的Chrome-Promise模块,以宣传Chrome API的回调式功能.
使用Babel 6实现项目的传输一直很好.最近我搬到了Babel 7.从那时起,当我允许通过Babel转换chrome-promise.js文件时,这会导致运行扩展时出现" 未捕获的ReferenceError:ChromePromise未定义 "错误.当我离开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"
}]
},
Run Code Online (Sandbox Code Playgroud)
和我的一个带有Babel设置的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": [
]
}
}
Run Code Online (Sandbox Code Playgroud)
注意:虽然我使用Terser来缩小JS代码,但是当我停用它时仍会出现错误.
编辑 查看我在github.com/Steve06/chrome-promise-babel-7-issue-repo上制作的最小回购
@babel/env默认情况下,预设将文件转换为commonjs,这要求转换后的文件包含在require或中import。为了使其与您的 Chrome 扩展兼容,您需要将文件转译为umd模块。把这个放在你的package.json:
"presets": [
[
"@babel/env",
{
"targets": {
"chrome": 60,
"modules": "umd"
}
}
]
],
Run Code Online (Sandbox Code Playgroud)
您还应该知道它source/chrome-promise.js已经是一个umd模块,因此实际上不需要转译。
| 归档时间: |
|
| 查看次数: |
468 次 |
| 最近记录: |