TJR
TJR

Reputation: 6577

Webpack 3 Correct way of excluding a directory from bundle

I am looking for the correct way to exclude a specific folder from the webpack bundle.

My config looks like this:

const path = require('path');

const config = {
  entry: [
    './client',
  ],
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js',
  },
  devtool: 'source-map',
  resolve: {
    extensions: ['.js', '.jsx'],
  },
  module: {
    rules: [
      {
        test: /./,
        exclude: /src/,
      },
    ],
  },
  plugins: [],
};
module.exports = config;

For my understanding this config should exclude the /src/ directory but instead webpack is keeping adding files to the bundle from this directory, because the test will match any and the exclude will affect any in src directory. Or am I missing something ?

Upvotes: 1

Views: 773

Answers (1)

TJR
TJR

Reputation: 6577

This is how I solved it (if anyone else having the same issue)

With this code I exclude all files from bundle which are located in /src/server/*

  externals: [
    function excludeDir(context, request, callback) {
      if (/.src\/server./.test(path.resolve(context, request))) {
        return callback(null, 'commonjs');
      }
      return callback();
    },
  ],

Upvotes: 1

Related Questions