1

我正在尝试使用 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,但没有成功。任何帮助将不胜感激。

4

1 回答 1

1

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

TL;博士;

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

 ["@babel/plugin-transform-runtime", {"version": "7.11.5"}]
于 2020-09-18T08:13:57.847 回答