Webpack无法转换从npm包导入的模块

时间:2018-01-26 11:10:03

标签: webpack babeljs

我用.babelrc

创建了一个npm包
{
  "presets": ["stage-3"]
}

此部分位于webpack.config.js

module: {
  rules: [
    {
      test: /\.jsx?/,
      include: APP_DIR,
      loader: 'babel-loader'
    }
  ]
]

源代码包含以下内容:{ ...oldObject },因此新手ES6功能和编译正常。

然而,在另一个项目中,我实际上需要那个包,我在package.json

"dependencies": {
  "r2d2": "git+ssh://git@xxxxxxxx:~/r2d2"
}

.babelrc包括:

{
  "presets": [
    "env",
    "stage-3"
  ]
}

webpack.config.js部分相同。 如果我尝试从这里编译,webpack会抛出该错误:

ERROR in ./node_modules/r2d2/src/js/main.js
Module parse failed: Unexpected token (80:12)
You may need an appropriate loader to handle this file type.
|           .keys(Actions)
|           .reduce((result, key) => ({
|             ...result,
|             [key]: Actions[key](id)
|           }), {})
@ ./src/js/main.js 5:23-51

为什么会这样,我该怎么做才能防止这种情况发生?

1 个答案:

答案 0 :(得分:1)

select a.airport_code, 
       a.airport_location, 
       b.airport_code, 
       b.airport_location
from   airport_table a join airport_table b
where  a.airport_code <> b.airport_code

具体说是只编译include: APP_DIR, 内的内容。

APP_DIR

不在该文件夹中,因此无法编译。

最简单的解决方法是将您的加载程序配置扩展为

./node_modules/r2d2/src/js/main.js