使用 babel 和 webpack 构建 bundle 时生成的重复辅助函数

Duplicate helper functions generated when building bundle with babel and webpack

我正在尝试使用 webpack 和 babel 构建尽可能小的包。

我知道为了跳过 extendsclassCallCheck 等的重复。我应该使用 @babel/plugin-transform-runtime 并且这个有效不出所料。

不幸的是,仍然有一些函数在模块的开头定义了很多次,正在使用它们。其中一些是:

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _createForOfIteratorHelper(o, allowArrayLike) { var it; if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = o[Symbol.iterator](); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }

等等...

我的配置文件是:

babel.config.js

module.exports = function(api) {
  api.cache(false);
  const presets = [
    ["@babel/preset-typescript"],
    [
      "@babel/preset-env",
      {
        "targets": {
          "edge": "14",
          "chrome": "61",
          "safari": "11.1",
          "ie": "11",
          "ios": "8"
        }
      }
    ]
  ];
  const plugins = [
    ["@babel/plugin-proposal-class-properties", { "loose": true }],
    ["@babel/plugin-transform-runtime"]]
  return {
    presets,
    plugins,
    sourceType: "unambiguous"
  };
};

网络包:

      config.entry = [
        "core-js/stable/array/includes",
        "core-js/stable/array/find",
        "core-js/stable/array/from",
        "core-js/stable/promise",
        "core-js/stable/map",
        "core-js/stable/set",
        "core-js/stable/symbol/async-iterator",
        "regenerator-runtime/runtime",
        "intersection-observer",
        bundleEntry];

我玩了很多配置babel,但都没有成功。任何帮助将不胜感激。

原来是github最近的问题:
https://github.com/preconstruct/preconstruct/issues/297 https://github.com/preconstruct/preconstruct/issues/318

TL;DR;

version @babel/plugin-transform-runtime的参数应该在babel配置文件中添加:

 ["@babel/plugin-transform-runtime", {"version": "7.11.5"}]