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 .tap() used on an undefined plugin (#271)
Browse files Browse the repository at this point in the history
Previously if `.tap()` was called for a plugin that didn't exist, the
`.tap()` call would succeed, but the `.toConfig()` would fail later with:

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

This scenario can typically occur when trying to customise a plugin
added by a previous preset (where the preset only sometimes adds the
plugin, such as in development), and forgetting to add a conditional
using `.has()` before calling `.tap()`.

Whilst #270 changes that exception to a slightly more useful error
message, it is still only shown at the time of the `.toConfig()`, where
it's much harder to debug the cause.

Now the `.tap()` call itself will show the error:

`Error: Cannot call .tap() on a plugin that has not yet been defined. Call plugin('foo').use(<Plugin>) first`

Whilst this error check does technically break the (unlikely) scenario
of calling `.tap()` in a preset before the plugin is defined by a later
preset, that ordering was already broken, since the `.tap()`'s args
would have been overwritten by the subsequent preset.

Fixes #125.
  • Loading branch information
edmorley committed Jul 20, 2020
1 parent 7ad85cb commit 38ef3c4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ module.exports = Orderable(
}

tap(f) {
if (!this.has('plugin')) {
throw new Error(
`Cannot call .tap() on a plugin that has not yet been defined. Call ${this.type}('${this.name}').use(<Plugin>) first.`,
);
}
this.set('args', f(this.get('args') || []));
return this;
}
Expand Down
8 changes: 8 additions & 0 deletions test/Plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,11 @@ test('toConfig without having called use()', () => {
"Invalid optimization.minimizer configuration: optimization.minimizer('gamma').use(<Plugin>) was not called to specify the plugin",
);
});

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

expect(() => plugin.tap(() => [])).toThrow(
"Cannot call .tap() on a plugin that has not yet been defined. Call optimization.minimizer('gamma').use(<Plugin>) first.",
);
});

0 comments on commit 38ef3c4

Please sign in to comment.