webpack-node-externals icon indicating copy to clipboard operation
webpack-node-externals copied to clipboard

allowlist doesn't seem to work

Open wujekbogdan opened this issue 5 years ago • 3 comments

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.

wujekbogdan avatar Oct 05 '20 16:10 wujekbogdan

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

liady avatar Nov 17 '20 21:11 liady

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.

wujekbogdan avatar Nov 18 '20 10:11 wujekbogdan

@wujekbogdan is it working now?

liady avatar Apr 21 '21 10:04 liady