gpt4 book ai didi

javascript - 如何使用 Webpack 5 和 Babel 7 创建 IE11 Bundle

转载 作者:行者123 更新时间:2023-12-04 12:18:01 33 4
gpt4 key购买 nike

我们如何将现代 JavaScript 编译成可与 Internet Explorer 11 (ie11) 一起使用的向后兼容的 JavaScript 包?具体来说,我们如何使用最新版本的 Webpack 5 和 Babel 7 做到这一点?

最佳答案

这是我可以创建的最小配置,其中包含一个测试文件,用于使用 IE 11 进行测试。Download on GitHub .
包.json

{
"browserslist": [
"ie 11"
],
"scripts": {
"dev": "webpack -w",
"build": "webpack"
},
"devDependencies": {
"@babel/core": "^7.12.9",
"@babel/preset-env": "^7.12.7",
"babel-loader": "^8.2.2",
"core-js": "^3.8.0",
"webpack": "^5.8.0",
"webpack-cli": "^4.2.0"
}
}
webpack.config.js
module.exports = {
entry: './index.js',
module: {
rules: [{
test: /\.m?js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: [
['@babel/preset-env', {
useBuiltIns: 'usage',
corejs: 3
}]
]
}
}
}]
}
}
Greeting.js
export default '2020 has been one hell of a year!'
index.js
/* 
* Test that uses modern JavaScript and will be compiled to work in IE 11
*/
import greeting from './Greeting'

window.addEventListener('load', async () => {
const o = {
greeting: await Promise.resolve(greeting)
}

console.log(
o,
Object.entries(o),
Object.keys(o),
Object.values(o),
)
})

关于javascript - 如何使用 Webpack 5 和 Babel 7 创建 IE11 Bundle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65035451/

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