webpack - 使用 babel regenerator-runtime 的异步/等待在 ie11 中不起作用

标签 webpack babeljs internet-explorer-11 babel-polyfill babel-preset-env

我有一个模板应用程序,我想与 ie11 一起使用,因为我正在使用 webpack + babel。 由于某种原因,我忽略了,即使我在配置中将其设置为目标,我的 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-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"
  }
}


最佳答案

使用 webpack 5,您可能必须为 es5 设置 target 才能使其在 IE11 上运行,如下所示:

// webpack.config.js

module.exports = {
  // ...
  target: ["web", 'es5'],
}

关于webpack - 使用 babel regenerator-runtime 的异步/等待在 ie11 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66297291/

相关文章:

json - 当 Angulars $http.post 与大型/复杂 json 数据集一起使用时,Internet Explorer 11 崩溃

javascript - 找不到通过 Webpack 文件加载器加载的某些图像

reactjs - 类型错误 : Cannot read property 'theme' of undefined at runMicrotasks

javascript - 扁平化 babel 输出

javascript - 解析错误: Line 1: Illegal import declaration

css - last-of-type 不适用于 IE11 和 Edge 中的自定义元素

javascript - 滚动事件在 IE11 中过于不稳定,Chrome 建议提高我的 React 应用程序中的 RAIL 性能

javascript - 使用 webpack-dev-server 时 webpack 构建中缺少 bundle.js

javascript - eslint-plugin-import 为 webpack 别名错误

javascript - 脚本5022 : Exception thrown and not caught