如何解决使用babel和webpack构建捆绑包时生成的重复帮助程序功能
我正在尝试使用webpack和babel构建尽可能小的捆绑包。
我知道,为了跳过extends
,classCallCheck
等的重复,我应该使用 @ 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"
};
};
webpack:
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;
@ babel / plugin-transform-runtime 的version 参数应添加到babel配置文件中:
["@babel/plugin-transform-runtime",{"version": "7.11.5"}]
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。