【问题标题】:How to create IE11 Bundles with Webpack 5 and Babel 7如何使用 Webpack 5 和 Babel 7 创建 IE11 Bundle
【发布时间】:2020-11-27 10:08:22
【问题描述】:

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

【问题讨论】:

  • 我花了几天的时间试图让它发挥作用后创建了这个自我回答的问题。希望它也能帮助其他人避免痛苦。

标签: javascript webpack babeljs internet-explorer-11 babel-loader


【解决方案1】:

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

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

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),
  )
})

【讨论】:

  • 感谢您发布此问题的解决方案。我建议你尝试标记你自己对这个问题的答案。它肯定会在未来帮助其他社区成员解决类似的问题。感谢您的理解。
  • 缺少配置 "arrowFunction: false" webpack.js.org/configuration/output/#outputenvironment 以使其与 IE11 和 webpack 5 一起使用
  • browserlist 已经隐式处理了,对吗?
  • @AnthumChris 不是 webpack 的箭头。使用您的确切代码示例打开输出文件,您会发现没有"arrowFunction: false"的箭头
  • 我在生成的包中没有看到箭头函数。请下载上面的 Gist 并运行以下命令:npm install && npm run build && grep '=>' dist/main.js || echo '✅ No arrow functions exist.'
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-17
  • 1970-01-01
  • 2017-12-26
  • 1970-01-01
  • 2023-01-28
  • 2018-01-30
  • 2020-10-14
相关资源
最近更新 更多