繁体   English   中英

"Babel 不会从 'node_modules' 转译导入的模块"

[英]Babel does not transpile imported modules from 'node_modules'

我从node_modules<\/code>转译导入的模块时遇到问题。 由于某种原因,Babel 不会转译从node_modules<\/code>导入的模块,而是转译从src<\/code>导入的模块。

这是一个示例仓库: https<\/a> :\/\/github.com\/NikitaKA\/babeltest

main.js<\/strong>

// result code contains const and let, but it shouldn't. :(

扩展我的意见:

你真的不想转发所有的node_modules - 它需要很长时间,而且那里的大部分代码应该已经是ES5(除非它们实际上是ES6模块,在这种情况下,ES6入口点被宣布为"module"package.json清单中”。

query-string@6.x 不是 ,它在自述文件中这样说:

该模块面向Node.js 6或更高版本以及最新版本的Chrome,Firefox和Safari。 如果要支持旧版浏览器,请使用版本5: npm install query-string@5

在这种情况下的解决方案是再次转换模块,这可以通过修改webpack配置中的exclude属性来完成:

{
  test: /\.js$/,
  exclude: /node_modules\/(?!(es6-module|another-es6-module)\/).*/,
},

es6-module将不再忽略模块es6-moduleanother-es6-module ,并将使用其余的源代码进行转换。

请参阅webpack项目中GitHub问题

webpack@3.12.0babel-core@6.26.3 babel-core@6.26.3 webpack@3.12.0 babel-core@6.26.3babel-core@6.26.3测试

如果你使用 Vue,有一个简单的解决方案。 只需在transpileDependencies<\/code>中列出您的模块:

vue.config.js<\/strong>

module.exports = {
   publicPath: '',
   outputDir: 'www',
   transpileDependencies: [
    'query-string'
  ],
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM