Webpack不包括node_modules [英] Webpack not excluding node_modules

查看:2712
本文介绍了Webpack不包括node_modules的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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:


  1. target:'node'添加到 webpack.config.js 。这将排除本机节点模块(路径,fs等)捆绑在一起。

  2. 使用 webpack-node-externals 以排除其他 node_modules

  1. Add target: 'node' to your webpack.config.js. This will exclude native node modules (path, fs, etc.) from being bundled.
  2. 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆