简体   繁体   English

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

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

I got a problem with transpiling imported modules from node_modules<\/code> .我从node_modules<\/code>转译导入的模块时遇到问题。 Babel for some reason doesn't transpile imported module from node_modules<\/code> , but transpile modules imported from src<\/code> .由于某种原因,Babel 不会转译从node_modules<\/code>导入的模块,而是转译从src<\/code>导入的模块。

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

main.js<\/strong> main.js<\/strong>

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

To expand upon my comments: 扩展我的意见:

You really don't want to transpile all of node_modules – it'll take a long time, and most of the code there should already be ES5 (unless they're actually ES6 modules, in which case the ES6 entry point is announced as "module" in the package.json manifest). 你真的不想转发所有的node_modules - 它需要很长时间,而且那里的大部分代码应该已经是ES5(除非它们实际上是ES6模块,在这种情况下,ES6入口点被宣布为"module"package.json清单中”。

query-string@6.x isn't , and it says so in its README : query-string@6.x 不是 ,它在自述文件中这样说:

This module targets Node.js 6 or later and the latest version of Chrome, Firefox, and Safari. 该模块面向Node.js 6或更高版本以及最新版本的Chrome,Firefox和Safari。 If you want support for older browsers, use version 5: npm install query-string@5 . 如果要支持旧版浏览器,请使用版本5: npm install query-string@5

The solution in this case is to transpile the module again, this can be done by modifying the exclude property in your webpack config: 在这种情况下的解决方案是再次转换模块,这可以通过修改webpack配置中的exclude属性来完成:

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

Modules es6-module and another-es6-module will no longer be ignored by webpack and will be transpiled with the rest of your source code. es6-module将不再忽略模块es6-moduleanother-es6-module ,并将使用其余的源代码进行转换。

See the GitHub issue on the webpack project . 请参阅webpack项目中GitHub问题

Tested with webpack@3.12.0 , babel-core@6.26.3 and babel-core@6.26.3 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测试

If you're using Vue, there is a simple solution.如果你使用 Vue,有一个简单的解决方案。 Just list your module in transpileDependencies<\/code> :只需在transpileDependencies<\/code>中列出您的模块:

vue.config.js<\/strong> 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