简体   繁体   English

如何使用带有 webpack 的 babel 来兼容 ie11

[英]How to use babel with webpack for ie11 compatibility

It's the first time i use webpack with babel, my goal is to make my small template app compatible with ie11.这是我第一次使用 webpack 和 babel,我的目标是让我的小模板应用程序与 ie11 兼容。

For some reason I ignore, my JS does not work at all in ie11 even though I did set it as a target in my config.出于某种原因,我忽略了,我的 JS 在 ie11 中根本不起作用,即使我确实将它设置为我的配置中的目标。 To test it, I use a ie11 on the inte.net but I don't have access to the stack errors since I'm on MacOS.为了测试它,我在 inte.net 上使用了 ie11,但由于我在 MacOS 上,所以无法访问堆栈错误。

What am I missing here?我在这里错过了什么?

Source code for more info: https://github.com/VelynnXV/Front-End-Workflow更多信息的源代码: https://github.com/VelynnXV/Front-End-Workflow

website: https://nifty-noether-cafbd5.netlify.app/网址: https://nifty-noether-cafbd5.netlify.app/

app.js应用程序.js

import regeneratorRuntime from "regenerator-runtime";

async function app() {

  console.log('App entry point')
  const template = document.getElementById('app')
  await new Promise(r => setTimeout(() => r(), 2500))
  template.innerHTML = `
  <div class="web-container">
      <div id="">
          Async / awat test
      </div>
  </div>
`
  console.log('App finished')
};
app();

webpack.config.json webpack.config.json

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
  mode: 'development',
  entry: ['core-js/stable', './src/app.js'],
  output: {
    path: path.resolve(__dirname, './dist'),
    filename: 'app.js',
  },
  devServer: {
    publicPath: "./src/",
    contentBase: path.join(__dirname, 'dist'),
    compress: true,
    port: 9000,
  },
  plugins: [
    new HtmlWebpackPlugin({ // will generate the html file WITH app.js
      // see plugin here : https://webpack.js.org/plugins/html-webpack-plugin/
      template: './src/index.html',
      filename: './index.html'
    })
  ],
  module: {
    rules: [ // set of rules letting webpack know how to handle .xyz files 
      {
        test: /\.m?js$/, // source: https://webpack.js.org/loaders/babel-loader/
        exclude: /(node_modules|bower_components)/,
        use: {
          loader: 'babel-loader',

        }
      }
    ],
  },
};

babel.config.js babel.config.js

// babel.config.js

module.exports = api => {
    return {
      plugins: [
        "@babel/plugin-proposal-nullish-coalescing-operator",
        "@babel/plugin-proposal-optional-chaining",
        "@babel/plugin-transform-runtime",
      ],
      
      presets: [
        [
          "@babel/preset-env",
          {
            useBuiltIns: "entry",
            corejs:3,
            // caller.target will be the same as the target option from webpack
            targets: api.caller(caller => caller && caller.target === "node")
              ? { node: "current" }
              : { chrome: "58", ie: "11" }
          }
        ]
      ]
    }
  }
  

package.json package.json

{
  "name": "front-end-workflow",
  "version": "1.0.0",
  "description": "",
  "main": "src/app.js",
  "scripts": {
    "dev": "npm run clean && npm run build && webpack serve",
    "build": "webpack",
    "clean": "rimraf ./dist"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@babel/core": "^7.12.17",
    "@babel/plugin-transform-runtime": "^7.12.17",
    "@babel/preset-env": "^7.12.17",
    "babel-loader": "^8.2.2",
    "css-loader": "^5.0.2",
    "html-loader": "^2.1.0",
    "html-webpack-plugin": "^5.2.0",
    "sass": "^1.32.8",
    "sass-loader": "^11.0.1",
    "style-loader": "^2.0.0",
    "webpack": "^5.23.0",
    "webpack-cli": "^4.5.0",
    "webpack-dev-server": "^3.11.2"
  },
  "dependencies": {
    "@babel/runtime": "^7.6.3",
    "core-js": "^3.3.2"
  }
}


You almost have a complete configuration for IE11 support.您几乎拥有了 IE11 支持的完整配置。 The only thing you're missing is a target: "es5" option in your webpack configuration.您唯一缺少的是 webpack 配置中的target: "es5"选项。 Babel correctly transpiled your files. Babel 正确地转译了你的文件。 Webpack also injected all the necessary polyfills. Webpack 还注入了所有必要的 polyfill。 However, you need to tell Webpack when it bundles the code together to use a syntax that your target browser can understand.但是,您需要在将代码捆绑在一起时告诉 Webpack 以使用您的目标浏览器可以理解的语法。 For whatever reason, Webpack set the default to a version of ES that contained arrow functions.无论出于何种原因,Webpack 将默认设置为包含箭头函数的 ES 版本。 The error that IE11 console was showing ( SCRIPT1002:syntax error ) was pointing at the very first occurrence of an arrow function in your bundled app.js file. IE11 控制台显示的错误( SCRIPT1002:syntax error )指向捆绑的app.js文件中第一次出现的箭头 function。

An extra tip: use comments: false in your babel config to strip the code comments out of your bundle.一个额外的提示:在你的 babel 配置中使用comments: false来从你的包中删除代码注释。 This can slightly decrease the size of your bundle.这可以稍微减小你的包的大小。

You can git apply this diff in your repo to take the changes in.您可以git apply此差异以进行更改。

diff --git a/babel.config.js b/babel.config.js
index 8d2442b..273176c 100644
--- a/babel.config.js
+++ b/babel.config.js
@@ -2,6 +2,7 @@
 
 module.exports = api => {
     return {
+      comments: false,
       plugins: [
         "@babel/plugin-transform-runtime",
       ],
diff --git a/webpack.config.js b/webpack.config.js
index 2243a11..08af521 100644
--- a/webpack.config.js
+++ b/webpack.config.js
@@ -21,6 +21,7 @@ module.exports = {
       filename: './index.html'
     })
   ],
+  target: "es5",
   module: {
     rules: [ // set of rules letting webpack know how to handle .xyz files using loader
       // see loaders : https://webpack.js.org/loaders/

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

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