Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a regular commonjs module to an external via a webpack plugin?

Tags:

webpack

I'm trying to use a webpack plugin to convert a regular dependency into an external one.

I'm not sure I'm doing it right. I tried doing the following, but I'm getting an error:

import * as ExternalModule from 'webpack/lib/ExternalModule';

export class Externalizer {
    apply(compiler) {
        compiler.plugin('compilation', (compilation, params) => {
            params.normalModuleFactory.plugin('before-resolve', async (request, callback) => {
                return callback(null, new ExternalModule(request.request, 'commonjs'));
            });
        });
    }
}

When trying to use this as a plugin, The error I'm getting is: TypeError: Cannot read property 'request' of undefined from handleExternals.

like image 421
Madd0g Avatar asked May 28 '18 10:05

Madd0g


1 Answers

FWIW, it seems like ExternalModule is not meant to be used like this, I solved my problem by implementing a custom externals function in webpack.config.js

like image 173
Madd0g Avatar answered Sep 28 '22 07:09

Madd0g