如何为IE11正确使用babel / corejs3 / webpack?

时间:2020-04-05 15:21:41

标签: javascript webpack internet-explorer-11 babeljs core-js

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

   [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且承诺成功完成。

2 个答案:

答案 0 :(得分:6)

使用useBuiltIns: "usage"


您通常必须在代码输入文件中导入要使用的模块(例如Postmate );没有polyfills; @babel/preset-env将相应地处理所使用的每个polyfill。另外,corejs@babel/preset-env的版本必须是一个数字(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

您可以在{strong> IE 11 (此处为https://zikro.gr/dbg/so/61044894/usage/)中使用useBuiltIns: "usage"测试在线分布式示例。 (子iFrame具有将父窗口背景颜色更改为随机颜色的按钮

您可以在此Github存储库/分支中找到包含整个项目和使用示例分支的存储库:https://github.com/clytras/ie11-postmate/tree/usage

使用useBuiltIns: "entry"


根据此问题讨论"using Symbol causes exception in IE11",您必须:

  1. @babel-runtime个文件的规则中排除core-js.js
  2. corejs: "3"文件中已将useBuiltIns: 'entry'@babel/preset-env预置到babel.config.js
  3. 条目源JS文件中必须有core-js/stablepostmate导入。

bavel.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/

答案 1 :(得分:0)

您可能缺少一些导入,我建议您查看IE11支持下的react-app-polyfills导入是什么-错误消息与Symbol相关。 core-js>=3不再使用core-js/stable导入IE11所需的所有内容。在撰写本文时,这可能已足够:

// If  you need `fetch` or `Object.assign`
npm install whatwg-fetch object-assign
// Make sure we're in a Browser-like environment before importing polyfills
// This prevents `fetch()` from being imported in a Node test environment
if (typeof window !== 'undefined') {
  // fetch() polyfill for making API calls.
  require('whatwg-fetch');
}

// Object.assign() is commonly used with React.
// It will use the native implementation if it's present and isn't buggy.
Object.assign = require('object-assign');

/// This may rid you of your error message

// Support for...of (a commonly used syntax feature that requires Symbols)
require('core-js/features/symbol');
// Support iterable spread (...Set, ...Map)
require('core-js/features/array/from');

希望这会有所帮助