首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用babel与webpack兼容ie11

如何使用babel与webpack兼容ie11
EN

Stack Overflow用户
提问于 2021-02-17 19:20:49
回答 1查看 2.9K关注 0票数 2

这是我第一次在babel上使用webpack,我的目标是使我的小模板应用程序与ie11兼容。

由于一些我忽略的原因,我的JS在ie11中根本不工作,即使我在配置中将它设置为目标。为了测试它,我在互联网上使用了一个ie11,但是我没有访问堆栈错误的权限,因为我在MacOS上。

我在这里错过了什么?

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

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

app.js

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
// 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

代码语言:javascript
复制
{
  "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"
  }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-02-21 10:48:44

您几乎有了IE11支持的完整配置。您所缺少的唯一东西是webpack配置中的target: "es5"选项。巴贝尔正确地传送了你的文件。Webpack还注射了所有必要的填充物。但是,当Webpack将代码捆绑在一起使用目标浏览器能够理解的语法时,您需要告诉它。出于任何原因,Webpack将默认设置为包含箭头函数的ES版本。IE11控制台显示的错误(SCRIPT1002:syntax error)指向绑定的app.js文件中首次出现箭头函数。

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

您可以在回购中git apply此差异以接受更改。

代码语言:javascript
复制
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/
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66248657

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档