liady / webpack-node-externals

Easily exclude node modules in Webpack
MIT License
1.3k stars 62 forks source link

allowlist doesn't seem to work #86

Open wujekbogdan opened 4 years ago

wujekbogdan commented 4 years ago

That's my current solution that works as exected. All node_modules are excluded from build except for some-library. When I compile the code then I can see the source of some-library being transpiled with Babel.

const path = require('path');
const pkg = require('./package.json');

const depsToBundle = ['some-library'];

const dependencies = Object.keys(pkg.dependencies).reduce((acc, name) => {
  if (depsToBundle.includes(name)) {
    return acc;
  }

  return {
    ...acc,
    [name]: pkg.dependencies[name],
  };
}, {});

module.exports = {
  mode: 'production',
  target: 'node',
  entry: [path.join(__dirname, '/src/index.js')],
  output: {
    path: path.join(__dirname, '/dist'),
    filename: 'index.js',
    libraryTarget: 'commonjs',
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        use: 'babel-loader',
        exclude: new RegExp(`node_modules/(?!(${depsToBundle.join('|')})/).*`),
      },
    ],
  },
  externals: [...Object.keys(dependencies)],
  devtool: 'source-map',
};

I'd like to achieve the same with webpack-node-externals, so refactored my config to be:

const path = require('path');
const externals = require('webpack-node-externals');

const depsToBundle = ['some-library'];

module.exports = {
  mode: 'production',
  target: 'node',
  entry: [path.join(__dirname, '/src/index.js')],
  output: {
    path: path.join(__dirname, '/dist'),
    filename: 'index.js',
    libraryTarget: 'commonjs',
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        use: 'babel-loader',
        exclude: new RegExp(`node_modules/(?!(${depsToBundle.join('|')})/).*`),
      },
    ],
  },
  externals: [
    externals({
      allowlist: depsToBundle,
    }),
  ],
  devtool: 'source-map',
};

When I compile the code I can no longer see that lib being bundled.


I'm not sure is it a bug or am I missing something.

liady commented 3 years ago

@wujekbogdan which webpack-node-externals version do you have?

wujekbogdan commented 3 years ago

I'm sorry but it was some work-in-progress code and I never committed it to my repo so I can't tell, but it was the most recent version of the lib because it was newly installed when reporting this bug.

liady commented 3 years ago

@wujekbogdan is it working now?