我无法让Webpack在babel
上的文件上运行node_modules
。特别是,我正在尝试使用自定义webpack加载器转换.svg
文件(来自FontAwesome或其他.svg
源),然后将其传递给Babel以转换为JSX。
在我尝试的许多不同配置中,问题通常表现为:
Unexpected token
Module parse failed: Unexpected token (1:0) You may need an appropriate loader to handle this file type. <svg ...></svg>
这表明自定义加载器和babel都没有被应用,即文件只是在没有任何加载器的情况下被传递。
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
提供exclude
和module.rules
论据,但我尝试的各种排列似乎不起作用。
存储库:https://github.com/brianmhunt/bug-reports/tree/jsx-svg-import
$ git clone [email protected]:brianmhunt/bug-reports.git`
$ git checkout jsx-svg-import
$ make
作为detailed in bug #736 of babel-loader:
...将
.babelrc
转换为具有相同配置的babel.config.js
,babel-loader
[正常工作]。
解释是:
...因为.babelrc不会在不同的包中“泄漏”(由package.json分隔)