问题描述
我正在使用webpack来构建我正在构建的Node框架(尽管我应该使用gulp,但不可否认)。当我包含EJS模块时,webpack将其包含在已编译的源代码中,即使我明确告诉它要排除node_modules目录。
I'm using webpack for a Node framework that I'm building (though I should probably use gulp, admittedly). When I include the EJS module, webpack includes it in the compiled source, even though I explicitly tell it to exclude the node_modules dir.
module.exports = {
context: __dirname,
target: 'node',
// ...
output: {
libraryTarget: 'commonjs'
// ...
},
module: {
loaders: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader?{ "stage": 0, "optional": ["runtime"] }'
}
]
}
};
正如您所看到的,我对JS文件进行了测试,并告诉它排除node_modules;为什么忽略我的排除?
As you can see, I have a test for JS files, and I tell it to exclude node_modules; why is it ignoring my exclude?
推荐答案
从您的配置文件中,您似乎只是排除用
babel-loader
解析,但不是捆绑。
From your config file, it seems like you're only excluding
node_modules
from being parsed with babel-loader
, but not from being bundled.
为了从捆绑中排除
node_modules
和本机节点库,您需要:
In order to exclude
node_modules
and native node libraries from bundling, you need to:
- 将
target:'node'
添加到webpack.config.js
。这将(路径,fs等)捆绑在一起。 - 使用以排除其他
node_modules
。
Add
target: 'node'
to yourwebpack.config.js
. This will exclude native node modules (path, fs, etc.) from being bundled.Use webpack-node-externals in order to exclude other
node_modules
.
所以你的结果配置文件应如下所示:
So your result config file should look like:
var nodeExternals = require('webpack-node-externals');
...
module.exports = {
...
target: 'node', // in order to ignore built-in modules like path, fs, etc.
externals: [nodeExternals()], // in order to ignore all modules in node_modules folder
...
};
这篇关于Webpack不包括node_modules的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!