Webpack 排除文件夹 [英] Webpack Exclude folder
问题描述
我正在通过 Webpack 推送一个相当大的应用程序,它从两个库文件夹中提取:第三方和 node_modules.我已经为 js 文件设置了规则:
I'm shoving a rather large app through Webpack, which pulls from two library folders: thirdparty and node_modules. I've setup my rule for js files as such:
{
test: /\.js$/,
loader: 'babel-loader',
include: [
/src\/js\/client/
],
exclude: [
/node_modules/,
/thirdparty/
]
}
我希望 webpack 不会解析这两个目录中的任何一个下的任何文件夹,但是我收到了库的错误.例如:
My hope was that webpack wouldn't parse any folders under either of those two directories, but I'm getting errors for libraries that are. For instance:
WARNING in ./~/durandal/js/system.js
51:8-15 Critical dependency: require function is used in a way in which dependencies cannot be statically extracted
WARNING in ./~/durandal/js/system.js
52:8-15 Critical dependency: require function is used in a way in which dependencies cannot be statically extracted
ERROR in ./~/durandal/js/system.js
Cannot statically analyse 'require(..., ...)' in line 278
@ ./src/js/client/lib/durandal/overrides/views.js 2:0-37
@ ./src/js/client/index.js
我是否误解了包含/排除的功能,或者这是 Webpack 的错误?
Am I misunderstanding the functionality of include/exclude, or is this a bug with Webpack?
推荐答案
规则并不决定 webpack 解析哪些文件,而是仅应用于包含并满足条件的文件.即使你根本没有规则,webpack 仍然会解析你在某处导入的相同文件.
The rules do not decide which files are being parsed by webpack, but instead they are only applied to the files which are included and also satisfy the condition. Even if you had no rule at all, webpack would still parse the same files since you imported them somewhere.
您可以排除模块与 externals
选项.这些外部变量必须以某种方式在运行时出现(例如加载到 标签中).
You can exclude modules from being bundled with the externals
option. These externals will have to be present at runtime in some way (for example loaded in a <script>
tag).
如果你只是想让 webpack 不解析文件,但仍然包含它们,你可以配置 module.noParse
.但正如文档所说,它们不应包含任何导入.
If you just want webpack not to parse the files, but still include them, you can configure module.noParse
. But as the documentation says, they should not contain any imports.
这篇关于Webpack 排除文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!