Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
Improve error message when .use() not called for a Plugin
Browse files Browse the repository at this point in the history
Previously if `.toConfig()` was called when a plugin was only partially
configured, it raised with the unhelpful:

`TypeError: Cannot read property '__expression' of undefined`

Now the scenario results in an Error of form:

`Invalid plugin configuration: plugin('foo').use(<Plugin>) was not called to specify the plugin`

Or for minimizer plugins:

`Invalid optimization.minimizer configuration: optimization.minimizer('foo').use(<Plugin>) was not called to specify the plugin`

Fixes scenario 1 in:
#204 (comment)
  • Loading branch information
edmorley committed Jul 20, 2020
1 parent 74b1791 commit d7b641f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ module.exports = Orderable(
const args = this.get('args');
let pluginPath = null;

if (plugin === undefined) {
throw new Error(
`Invalid ${this.type} configuration: ${this.type}('${this.name}').use(<Plugin>) was not called to specify the plugin`,
);
}

// Support using the path to a plugin rather than the plugin itself,
// allowing expensive require()s to be skipped in cases where the plugin
// or webpack configuration won't end up being used.
Expand Down
8 changes: 8 additions & 0 deletions test/Plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,11 @@ test('toConfig with plugin as path', () => {
expect(initialized.__pluginConstructorName).toBe('EnvironmentPlugin');
expect(initialized.__pluginPath).toBe(envPluginPath);
});

test('toConfig without having called use()', () => {
const plugin = new Plugin(null, 'gamma', 'optimization.minimizer');

expect(() => plugin.toConfig()).toThrow(
"Invalid optimization.minimizer configuration: optimization.minimizer('gamma').use(<Plugin>) was not called to specify the plugin",
);
});

0 comments on commit d7b641f

Please sign in to comment.