如何在IE11中正确使用babel/corejs 3/webpack?

ktca8awb  于 2022-12-08  发布在  Babel
关注(0)|答案(2)|浏览(162)

使用我当前的配置(见下文),我收到此错误:

[object Error]{description: "Argument ob...", message: "Argument ob...", name: "TypeError", number: -2147418113, stack: "TypeError: ...", Symbol()_7.bs7gi3oa3wi: undefined}

我试着根据Symbol()_ ... : undefined}进行挖掘,但我找不到任何明确的指示。
这是我的.babel.config.js

module.exports = function (api) {
    api.cache(true);
    const presets = [
      [
        '@babel/preset-env',
        {
         // modules: false,
          corejs:"3.6.4",
          useBuiltIns: 'usage',
          targets: {
            browsers: [
              "edge >= 16",
              "safari >= 9",
              "firefox >= 57",
              "ie >= 11",
              "ios >= 9",
              "chrome >= 49"
            ]
          }
        }
      ]
    ];
    const plugins= [
        ["@babel/plugin-proposal-decorators", { decoratorsBeforeExport: true }],
        ["@babel/plugin-proposal-class-properties", { "loose": true }]
    ];
    return {
      presets,
      plugins
    }
  }

这是我的webpackconfig.js

module.exports = {
  module: {
    rules: [
      {
        test: /\.js$/,
       // exclude: /node_modules/,
       exclude : [
        /\bcore-js\b/,
        /\bwebpack\/buildin\b/
      ],
        use: {
          loader: 'babel-loader',
          options:{
            sourceType: "unambiguous"
          }
        },
      },
    ],
  },
  devtool:"cheap-source-map",
  resolve: {
    extensions: ['*', '.js'],
  },
  output: {
    path: path.resolve(__dirname, './dist'),
    filename: 'shim.js',
  }
};

我也尝试了很多替代品,这是我目前的一个,与entry:"usage"和不排除node_modules
这是从我的package.json

"devDependencies": {
    "@babel/core": "^7.9.0",
    "@babel/plugin-proposal-class-properties": "^7.8.3",
    "@babel/plugin-proposal-decorators": "^7.8.3",
    "@babel/preset-env": "^7.9.5",
    "babel-loader": "^8.1.0",
    "eslint": "^6.8.0",
    "eslint-config-google": "^0.14.0",
    "webpack": "^4.42.1",
    "webpack-cli": "^3.3.11",
    "webpack-dev-server": "^3.10.3",
    "dotenv-webpack": "^1.7.0"
  },
  "dependencies": {
    "core-js": "^3.6.4",
    "ismobilejs": "^1.0.3",
    "localforage": "1.7.3",
    "postmate": "^1.5.2",
    "uuid": "^7.0.3"
  }

错误似乎来自Postmate库的第一次调用,即new Postmate({...})(我之前有一个console.log)。在此调用之前,我有一个localforage,并且承诺成功完成。

qnyhuwrf

qnyhuwrf1#

使用useBuiltIns: "usage"

通常,您必须在代码输入文件中导入要使用的模块(* i. g. Postmate*);无多边形填充;所使用的每个多边形填充都将由@babel/preset-env相应地处理。此外,@babel/preset-env中的corejs版本必须是单个数字(* 即32 *)。

babel.config.js的内容:

module.exports = function (api) {
  api.cache(true);
  const presets = [
    [
      '@babel/preset-env',
      {
        corejs : {
          version : "3",
          proposals : true
        },
        useBuiltIns: 'usage',
        targets: {
          browsers: [
            "edge >= 16",
            "safari >= 9",
            "firefox >= 57",
            "ie >= 11",
            "ios >= 9",
            "chrome >= 49"
          ]
        }
      }
    ]
  ];
  const plugins= [
      ["@babel/plugin-proposal-decorators", { decoratorsBeforeExport: true }],
      ["@babel/plugin-proposal-class-properties", { "loose": true }]
  ];
  return {
    presets,
    plugins
  }
}

webpackconfig.js的内容:

const path = require('path');

module.exports = {
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: [
          /\bcore-js\b/,
          /\bwebpack\/buildin\b/
        ],
        use: {
          loader: 'babel-loader',
          options: {
            babelrc: false,
            configFile: path.resolve(__dirname, 'babel.config.js'),
            compact: false,
            cacheDirectory: true,
            sourceMaps: false,
          },
        },
      },
    ],
  },
  devtool: "cheap-source-map",
  output: {
    path: path.resolve(__dirname, './dist'),
    filename: 'shim.js',
  }
}

条目JS文件src/index.js的内容:

import Postmate from 'postmate';

// Postmate and rest of the code
...

它将生成:

dist/shim.js      177K
dist/shim.js.map  140K

您可以在IE 11中使用useBuiltIns: "usage"测试联机分布式示例:https://zikro.gr/dbg/so/61044894/usage/。(* 子iFrame具有一个按钮,可将父窗口背景颜色更改为随机颜色 *)
你可以在这个Github仓库/分支中找到包含整个项目和用法示例分支的仓库:https://github.com/clytras/ie11-postmate/tree/usage

使用useBuiltIns: "entry"

根据此问题讨论"using Symbol causes exception in IE11",您必须:
1.在.js文件的规则中排除@babel-runtimecore-js
1.在babel.config.js文件中预设corejs: "3"useBuiltIns: 'entry'@babel/preset-env
1.在您的输入源JS文件中必须有core-js/stablepostmate导入。

babel.config.js的内容:

module.exports = function (api) {
  api.cache(true);
  const presets = [
    [
      '@babel/preset-env',
      {
        corejs:"3",
        useBuiltIns: 'entry',
        targets: {
          browsers: [
            "edge >= 16",
            "safari >= 9",
            "firefox >= 57",
            "ie >= 11",
            "ios >= 9",
            "chrome >= 49"
          ]
        }
      }
    ]
  ];
  const plugins= [
      ["@babel/plugin-proposal-decorators", { decoratorsBeforeExport: true }],
      ["@babel/plugin-proposal-class-properties", { "loose": true }]
  ];
  return {
    presets,
    plugins
  }
}

webpackconfig.js的内容:

const path = require('path');

module.exports = {
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /@babel(?:\/|\\{1,2})runtime|core-js/,
        use: {
          loader: 'babel-loader',
          options: {
            babelrc: false,
            configFile: path.resolve(__dirname, 'babel.config.js'),
            compact: false,
            cacheDirectory: true,
            sourceMaps: false,
          },
        },
      },
    ],
  },
  devtool:"cheap-source-map",
  output: {
    path: path.resolve(__dirname, './dist'),
    filename: 'shim.js',
  }
}

条目JS文件src/index.js的内容:

import 'core-js/stable';
window.Postmate = require('postmate/build/postmate.min.js');

// Postmate and rest of the code
...

它将生成:

dist/shim.js      641K
dist/shim.js.map  459K

您可以在IE 11中进行测试,网址为:https://zikro.gr/dbg/so/61044894/

ymdaylpp

ymdaylpp2#

您可能遗漏了一些导入,我建议您查看react-app-polyfills为支持IE11而导入的内容-错误消息与Symbol有关。core-js>=3不再导入IE11需要的core-js/stable的所有内容。在撰写本文时,以下内容可能足够了:
第一个
希望这对你有帮助

相关问题