简体   繁体   English

如何使用 Webpack 5 和 Babel 7 创建 IE11 捆绑包

[英]How to create IE11 Bundles with Webpack 5 and Babel 7

How can we compile modern JavaScript into backwards-compatible JavaScript bundles that can be used with Internet Explorer 11 (ie11)?我们如何将现代 JavaScript 编译成向后兼容的 JavaScript 包,可以与 Internet Explorer 11 (ie11) 一起使用? Specifically, how can we do this with the latest versions of Webpack 5 and Babel 7?具体来说,我们如何使用最新版本的 Webpack 5 和 Babel 7 来做到这一点?

Here's the most minimal configuration I could create, with a test file that's included to test with IE 11. Download on GitHub .这是我可以创建的最小配置,其中包含一个测试文件,用于使用 IE 11 进行测试。 在 GitHub 上下载

package.json package.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 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问候.js

export default '2020 has been one hell of a year!'

index.js 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),
  )
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM