[英]Generate ES6 (or ES2022) code ONLY with Webpack & Babel
I have inherited a legacy project which used an older version of Babel and Webpack.我继承了一个遗留项目,它使用了旧版本的 Babel 和 Webpack。
It does not use newer javascript constructs such as classes, async/await.它不使用更新的 javascript 构造,例如类、async/await。
I would like to refactor its code to employ these modern constructs.我想重构它的代码以使用这些现代结构。 I've been partially successful.我取得了部分成功。
I first upgraded the babel packages using the following tool:我首先使用以下工具升级了 babel 包:
npx babel-upgrade --write
I then altered my babel.config.js
(attached below) to target Chrome 97 as it makes Babel accept "modern" code with async/await.然后我更改了我的babel.config.js
(附在下面)以针对 Chrome 97,因为它使 Babel 接受带有 async/await 的“现代”代码。
However, the null coalescing operator (??) still requires a plugin.但是,null 合并运算符(??)仍然需要插件。 I thought Babel would detect this and add a plugin as required?我以为 Babel 会检测到这一点并根据需要添加一个插件?
Is there any way for Babel to automatically include any plugins I need, when I run npm build dev
?当我运行npm build dev
时,Babel 有什么办法可以自动包含我需要的任何插件吗? (package.json defining script is below) (package.json定义脚本如下)
Or (as a last resort) is there a way for Webpack not to invoke babel at all, and bundle source and modules without transpiling them?或者(作为最后的手段)有没有办法让 Webpack 根本不调用 babel,并且在不转换它们的情况下捆绑源和模块?
Can you also tell me what NPM packages I can remove, as I think I'm including too many Babel-related packages (something I inherited).你能告诉我我可以删除哪些 NPM 包吗,因为我认为我包含了太多与 Babel 相关的包(我继承的东西)。
This is my babel.config.js:这是我的 babel.config.js:
return {
"presets": [
[
"@babel/preset-env",
Object.assign({}, process.env.NODE_ENV === "test"
? {
"loose": true,
"targets": {
"node": 8,
"browsers": [">0.25%","not IE 11"]
}
}
: {
"useBuiltIns": "entry",
"corejs": "3.8",
targets: {
chrome: 97 // January 2022 - supports async/await
},
"modules": false
}
)
]
],
"plugins": [
["@babel/plugin-transform-react-jsx", {
"pragma": "h"
}],
"@babel/plugin-proposal-class-properties",
["module:fast-async", { "spec": true }]
]
}
};
This is my packages.json.这是我的 packages.json。 I suspect there are babel-related packages I don't need.我怀疑有一些我不需要的与 babel 相关的包。
NPM run dev is used to build & bundle the script with webpack. NPM run dev 用于构建脚本并将其与 webpack 捆绑在一起。
{
"name": "scts-expenses",
"version": "0.1.0",
"description": "",
"scripts": {
"build": "webpack --config tools/webpack/config/build",
"ci": "webpack --config tools/webpack/config/integration",
"dev": "webpack --config tools/webpack/config/dev",
"lint": "node_modules/.bin/eslint src -c .eslintrc --ext js",
"start": "npm run dev",
"test": "node_modules/.bin/jest",
"watch-dev": "webpack --config tools/webpack/config/integration --watch"
},
"author": "SCTS",
"license": "MIT",
"devDependencies": {
"@babel/cli": "^7.12.10",
"@babel/core": "^7.12.10",
"@babel/node": "^7.12.10",
"@babel/plugin-proposal-class-properties": "^7.18.6",
"@babel/plugin-proposal-object-rest-spread": "^7.12.1",
"@babel/plugin-syntax-dynamic-import": "^7.8.3",
"@babel/plugin-transform-object-assign": "^7.12.1",
"@babel/plugin-transform-react-jsx": "^7.12.12",
"@babel/preset-env": "^7.12.11",
"autoprefixer": "^9.8.6",
"babel-core": "7.0.0-bridge.0",
"babel-jest": "^23.4.2",
"babel-loader": "^8.2.2",
"babel-plugin-syntax-dynamic-import": "^6.18.0",
"bluebird": "^3.7.2",
"browser-sync": "^2.26.13",
"browser-sync-webpack-plugin": "^2.3.0",
"clean-webpack-plugin": "^3.0.0",
"copy-webpack-plugin": "^5.1.2",
"cors": "^2.8.5",
"cross-env": "^5.2.0",
"css-loader": "^3.6.0",
"eslint": "^6.8.0",
"eslint-plugin-babel": "^5.3.1",
"eslint-plugin-jest": "^23.20.0",
"eval": "^0.1.4",
"fast-async": "^6.3.8",
"filemanager-webpack-plugin": "^2.0.5",
"imagemin-webpack-plugin": "^2.4.2",
"jest": "^24.9.0",
"jest-axe": "^3.5.0",
"mini-css-extract-plugin": "^0.8.2",
"node-sass": "^4.14.1",
"postcss": "^7.0.35",
"postcss-css-variables": "^0.13.0",
"postcss-custom-properties": "^9.2.0",
"postcss-loader": "^3.0.0",
"preact": "^10.5.7",
"preact-render-to-json": "^3.6.6",
"preact-render-to-string": "^5.1.12",
"sass-loader": "^8.0.2",
"style-loader": "^1.3.0",
"webpack": "^4.44.2",
"webpack-cli": "^3.3.12",
"webpack-dev-server": "^3.11.0",
"webpack-hot-middleware": "^2.25.0",
"webpack-merge": "^4.2.2"
},
"dependencies": {
"@microsoft/applicationinsights-web": "^2.5.10",
"abortcontroller-polyfill": "^1.7.1",
"core-js": "^3.24.1",
"custom-event-polyfill": "^1.0.7",
"element-closest-polyfill": "^1.0.2",
"es6-object-assign": "^1.1.0",
"form-request-submit-polyfill": "^2.0.0",
"picturefill": "^3.0.3",
"promise-polyfill": "^8.2.0",
"regenerator-runtime": "^0.13.9",
"unfetch": "^4.2.0",
"url-search-params-polyfill": "^8.1.0"
}
}
Finally, this is my Webpack 4 config.最后,这是我的 Webpack 4 配置。 I couldn't upgrade to the latest Webpack package, I got error messages when I tried.我无法升级到最新的 Webpack package,尝试时出现错误消息。 I don't think the settings are 100% compatible.我不认为这些设置是 100% 兼容的。
const base = require('../base');
const path = require('path');
const merge = require('webpack-merge');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const FileManagerPlugin = require('filemanager-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const ImageminPlugin = require('imagemin-webpack-plugin').default;
// const BrowserSyncPlugin = require('browser-sync-webpack-plugin');
const paths = require('../../../../paths.config');
module.exports = [
merge(base.main, {
output: {
filename: 'static-entry.js',
path: path.join(process.cwd(), paths.integrationOutput),
libraryTarget: `umd`
},
mode: 'development',
plugins: [
new FileManagerPlugin({
onEnd: {
delete: [path.join(process.cwd(), paths.integrationOutput, 'static-entry.js')]
}
}),
new MiniCssExtractPlugin({
filename: path.join(paths.dest.css, 'index.css'),
chunkFilename: '[id].css',
ignoreOrder: false,
}),
new CopyWebpackPlugin([{
from: path.join(process.cwd(), paths.src.assets),
to: path.join(process.cwd(), paths.integrationOutput, paths.dest.assets)
}]),
new CopyWebpackPlugin([{
from: path.join(process.cwd(), paths.src.img),
to: path.join(process.cwd(), paths.integrationOutput, paths.dest.img)
}]),
new ImageminPlugin({ test: /\.(jpe?g|png|gif|svg)$/i })
],
}),
merge(base.javascript, {
output: {
filename: '[name].js',
publicPath: paths.webpackPublicPath,
path: path.join(process.cwd(), paths.integrationOutput, paths.dest.js)
},
mode: 'development',
devtool: 'source-map',
plugins: [
new CleanWebpackPlugin()
]
})
];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.