如何将babel与webpack一起使用以实现ie11兼容性

林恩

这是我第一次将Webpack与babel结合使用,我的目标是使我的小型模板应用程序与ie11兼容。

由于某些原因,我忽略了,即使我确实在配置中将其设置为目标,我的JS也无法在ie11中正常工作。为了测试它,我在互联网上使用了ie11,但是由于我使用的是MacOS,所以我无法访问堆栈错误。

我在这里想念什么?

更多信息的源代码:https : //github.com/VelynnXV/Front-End-Workflow

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

app.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

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

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

{
  "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"
  }
}


阿提拉·维切雷克(AttilaVečerek)

您几乎已经具有IE11支持的完整配置。您唯一缺少的是target: "es5"Webpack配置中的一个选项。Babel正确地转译了您的文件。Webpack还注入了所有必要的polyfill。但是,您需要告诉Webpack何时将代码捆绑在一起,以使用目标浏览器可以理解的语法。无论出于何种原因,Webpack都会将默认设置设置为包含箭头功能的ES版本。IE11控制台显示的错误(SCRIPT1002:syntax error)指向捆绑app.js文件中最先出现的箭头功能

一个额外的提示:comments: false在babel配置中使用,以将代码注释从捆绑中删除。这会稍微减小捆绑包的大小。

您可以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/

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Laravel Mix:配置Babel以实现IE11兼容性(转换和polyfill)

获取@ babel / present-env的默认实现以与IE11一起使用

与Babel一起使用IE11

GWT兼容性IE11

如何将萝卜与 Pycharm 一起使用以实现行为步骤自动完成

如何将ByteBuddy @Pipe批注与@FieldValue一起使用以实现委托模式?

将Pylzma与流媒体和7Zip兼容性一起使用

如何将Webpack与babel-loader和flow一起使用

如何正确识别IE11 WebGL兼容性(clearStencil,着色器)

如何将Webpack与Typescript一起使用?

如何将Webpack与Express一起使用?

如何将Webpack babel-loader和es6与worker-loader一起使用?

在IE11浏览器中使用具有IE9兼容性的MutationObserver

如何将AWS凭证与Jenkins一起使用以部署到Elastic Beanstalk?

如何将div与子表一起使用以隐藏数据

如何将Selenium与多个Url一起使用以写入csv

如何将QPropertyAnimation与QPainter一起使用以绘制圆弧

如何将URLSessionStreamTask与URLSession一起使用以进行分块编码传输

如何将getFilter()与ListAdapter一起使用以过滤ListView?

如何将useState与React-redux-firebase一起使用以重新渲染

如何将Autofac与DynamicProxy一起使用以确定目标代理类的名称空间

在Windows 8.1中,如何将IE设置为旧版本(兼容性视图)?

使用Angular 5的IE 11兼容性错误

将babel-loader与webpack一起使用时如何指定cacheDirectory选项?

C ++ 11 constexpr兼容性实现

具有 angular.js 和 IE11 兼容性的 Web 组件

对浏览器的兼容性降低到ie11

如何将 std::bind 与 std::function 一起使用以便将方法作为回调传递?

如何将webpack-dev-server与多个webpack配置一起使用?