How can I exclude code path when bundling with webpack/browserify?

后端 未结 4 711
再見小時候
再見小時候 2021-02-12 09:27

I have a library that can be used with both node.js and the browser. I am using CommonJS then publishing for the web version using webpack. My code looks like this:



        
4条回答
  •  南笙
    南笙 (楼主)
    2021-02-12 10:14

    In order to exclude node_modules and native node libraries from being bundled, you need to:

    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 all 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 
        ...
    };
    

提交回复
热议问题