Webpack 中 babel-loader 的“排除”选项

Posted

技术标签:

【中文标题】Webpack 中 babel-loader 的“排除”选项【英文标题】:"exclude" options of babel-loader in Webpack 【发布时间】:2017-01-14 18:38:55 【问题描述】: 模块.exports = 条目:'./index.js', 输出: 文件名:'index.min.js', 小路: '。/' , 模块: 装载机:[ 测试:/\.js$/, 装载机:'巴别塔', 排除:/node_modules/, 询问: 预设:['es2015'], 插件:['转换运行时'] , 缓存目录:真 ] 常数 pi = 3.14159265; 让 s = "你好"; for (let codePoint of s) 控制台.log(代码点);

当我删除“exclude”这一行时,编译后的文件会变大两倍,并抛出错误:“$extend is not a function”。

我想知道为什么 exclude 选项会影响编译过程。我只编译 ./index.js

【问题讨论】:

【参考方案1】:

这是导致此行为的 transform-runtime 插件 - 如果您不使用它,则大小是相同的。

为了找到您问题的根源,我创建了两种配置,一种排除了node_modules,另一种与您的配置相同。另外,我让 webpack 生成源映射(webpack 的 -d 参数 / devtool: "source-map" 配置)并查看包含的文件 not 存在于排除版本中(简单差异)。这些是

webpack:///../~/babel-runtime/core-js/object/create.js
webpack:///../~/babel-runtime/core-js/object/define-properties.js
webpack:///../~/babel-runtime/core-js/object/define-property.js
webpack:///../~/babel-runtime/core-js/object/get-own-property-descriptor.js
webpack:///../~/babel-runtime/core-js/object/get-own-property-names.js
webpack:///../~/babel-runtime/core-js/object/get-own-property-symbols.js
webpack:///../~/babel-runtime/core-js/object/get-prototype-of.js
webpack:///../~/babel-runtime/core-js/object/is-extensible.js
webpack:///../~/babel-runtime/core-js/object/keys.js
webpack:///../~/babel-runtime/core-js/object/prevent-extensions.js
webpack:///../~/babel-runtime/core-js/symbol.js
webpack:///../~/babel-runtime/core-js/symbol/iterator.js
webpack:///../~/babel-runtime/helpers/typeof.js
webpack:///../~/core-js/library/fn/object/create.js
webpack:///../~/core-js/library/fn/object/define-properties.js
webpack:///../~/core-js/library/fn/object/define-property.js
webpack:///../~/core-js/library/fn/object/get-own-property-descriptor.js
webpack:///../~/core-js/library/fn/object/get-own-property-names.js
webpack:///../~/core-js/library/fn/object/get-own-property-symbols.js
webpack:///../~/core-js/library/fn/object/get-prototype-of.js
webpack:///../~/core-js/library/fn/object/is-extensible.js
webpack:///../~/core-js/library/fn/object/keys.js
webpack:///../~/core-js/library/fn/object/prevent-extensions.js
webpack:///../~/core-js/library/fn/symbol/index.js
webpack:///../~/core-js/library/fn/symbol/iterator.js
webpack:///../~/core-js/library/modules/_enum-keys.js
webpack:///../~/core-js/library/modules/_is-array.js
webpack:///../~/core-js/library/modules/_keyof.js
webpack:///../~/core-js/library/modules/_meta.js
webpack:///../~/core-js/library/modules/_object-gopd.js
webpack:///../~/core-js/library/modules/_object-gopn-ext.js
webpack:///../~/core-js/library/modules/_object-gopn.js
webpack:///../~/core-js/library/modules/_object-gops.js
webpack:///../~/core-js/library/modules/_object-pie.js
webpack:///../~/core-js/library/modules/_object-sap.js
webpack:///../~/core-js/library/modules/_wks-define.js
webpack:///../~/core-js/library/modules/_wks-ext.js
webpack:///../~/core-js/library/modules/es6.object.create.js
webpack:///../~/core-js/library/modules/es6.object.define-properties.js
webpack:///../~/core-js/library/modules/es6.object.define-property.js
webpack:///../~/core-js/library/modules/es6.object.get-own-property-descriptor.js
webpack:///../~/core-js/library/modules/es6.object.get-own-property-names.js
webpack:///../~/core-js/library/modules/es6.object.get-prototype-of.js
webpack:///../~/core-js/library/modules/es6.object.is-extensible.js
webpack:///../~/core-js/library/modules/es6.object.keys.js
webpack:///../~/core-js/library/modules/es6.object.prevent-extensions.js
webpack:///../~/core-js/library/modules/es6.symbol.js
webpack:///../~/core-js/library/modules/es7.symbol.async-iterator.js
webpack:///../~/core-js/library/modules/es7.symbol.observable.js

transform-runtime 插件会处理这些文件,因此当您不排除 node_modules 时,它们会包含在您的构建中。

【讨论】:

以上是关于Webpack 中 babel-loader 的“排除”选项的主要内容,如果未能解决你的问题,请参考以下文章

ts-loader vs babel-loader 在 Typescript 中使用 webpack

webpack打包react项目babel-loader转换es6语法中‘...’错误

webpack进阶【11】: babel-loader 配置高版本的 js,使之兼容

配置 webpack babel-loader 与在 package.json 中配置有啥区别?

深入webpack学习 devServer babel-loader asset

Webpack 热模块替换不适用于 babel-loader 和预设 es2015