Babel没有通过Webpack加载器从`node_modules`导入运行

问题描述 投票:0回答:1

我无法让Webpack在babel上的文件上运行node_modules。特别是,我正在尝试使用自定义webpack加载器转换.svg文件(来自FontAwesome或其他.svg源),然后将其传递给Babel以转换为JSX。

在我尝试的许多不同配置中,问题通常表现为:

1. Unexpected token

Module parse failed: Unexpected token (1:0)
You may need an appropriate loader to handle this file type.
<svg ...></svg>

这表明自定义加载器和babel都没有被应用,即文件只是在没有任何加载器的情况下被传递。

2. Syntax Error

SyntaxError: /Users/bmh/Repos/bug-reports/node_modules/icon/ad.svg: 
  Unexpected token (2:20)
1 |
2 |     const element = <div>hey</div>;

这表明自定义加载程序正在工作(因为为了测试的目的,它被硬编码为<div>hey</div>),但后续的babel-loader没有被调用。

.svg不在node_modules中时,问题不会发生,这意味着自定义加载程序和babel-loader都按预期工作。 webpack或babel决定不将babel变换应用于node_modules中的文件,并且不清楚如何解决此问题。

我发现似乎有重量的一个建议是为webpack include提供excludemodule.rules论据,但我尝试的各种排列似乎不起作用。

Reproduce

存储库:https://github.com/brianmhunt/bug-reports/tree/jsx-svg-import

$ git clone [email protected]:brianmhunt/bug-reports.git`
$ git checkout jsx-svg-import
$ make
webpack babeljs babel-loader
1个回答
0
投票

作为detailed in bug #736 of babel-loader

...将.babelrc转换为具有相同配置的babel.config.jsbabel-loader [正常工作]。

解释是:

...因为.babelrc不会在不同的包中“泄漏”(由package.json分隔)

© www.soinside.com 2019 - 2024. All rights reserved.