簡體   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