Skip to content

Commit

Permalink
Abort the build for moduleParsed errors
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Oct 23, 2021
1 parent af3b0c8 commit 36e693b
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 8 deletions.
4 changes: 3 additions & 1 deletion docs/05-plugin-development.md
Expand Up @@ -687,7 +687,9 @@ Returns `null` if the module id cannot be found.
Get ids of the files which has been watched previously. Include both files added by plugins with `this.addWatchFile` and files added implicitly by rollup during the build.
#### `this.load({id: string, moduleSideEffects?: boolean | 'no-treeshake' | null, syntheticNamedExports?: boolean | string | null, meta?: {[plugin: string]: any} | null}) => Promise<ModuleInfo>`
#### `this.load`
**Type:** `({id: string, moduleSideEffects?: boolean | 'no-treeshake' | null, syntheticNamedExports?: boolean | string | null, meta?: {[plugin: string]: any} | null}) => Promise<ModuleInfo>`
Loads and parses the module corresponding to the given id, attaching additional meta information to the module if provided. This will trigger the same [`load`](guide/en/#load), [`transform`](guide/en/#transform) and [`moduleParsed`](guide/en/#moduleparsed) hooks that would be triggered if the module were imported by another module.
Expand Down
11 changes: 7 additions & 4 deletions src/ModuleLoader.ts
Expand Up @@ -366,16 +366,19 @@ export class ModuleLoader {
.then(([resolveStaticDependencyPromises, resolveDynamicImportPromises]) =>
Promise.all<unknown>([...resolveStaticDependencyPromises, ...resolveDynamicImportPromises])
)
.then(() => this.pluginDriver.hookParallel('moduleParsed', [module.info]))
.catch(() => {
/* rejections thrown here are also handled within PluginDriver - they are safe to ignore */
});
.then(() => this.pluginDriver.hookParallel('moduleParsed', [module.info]));
loadAndResolveDependenciesPromise.catch(() => {
/* avoid unhandled promise rejections */
});

if (isPreload) {
this.moduleLoadingState.set(module, { loadAndResolveDependenciesPromise, loadPromise });
await loadAndResolveDependenciesPromise;
} else {
this.moduleLoadingState.set(module, { loadAndResolveDependenciesPromise, loadPromise: null });
await this.fetchModuleDependencies(module, ...(await loadPromise));
// To handle errors when resolving dependencies or in moduleParsed
await loadAndResolveDependenciesPromise;
}
return module;
}
Expand Down
22 changes: 22 additions & 0 deletions test/function/samples/plugin-error-module-parsed/_config.js
@@ -0,0 +1,22 @@
const path = require('path');

module.exports = {
description: 'errors in moduleParsed abort the build',
options: {
plugins: [
{
name: 'testPlugin',
moduleParsed() {
throw new Error('broken');
}
}
]
},
error: {
code: 'PLUGIN_ERROR',
hook: 'moduleParsed',
message: 'broken',
plugin: 'testPlugin',
watchFiles: [path.join(__dirname, 'main.js')]
}
};
13 changes: 13 additions & 0 deletions test/function/samples/plugin-error-module-parsed/main.js
@@ -0,0 +1,13 @@
let effect = false;

var b = {
get a() {
effect = true;
}
};

function X() {}
X.prototype = b;
new X().a;

assert.ok(effect);
3 changes: 0 additions & 3 deletions test/function/samples/preload-module/_config.js
Expand Up @@ -9,9 +9,6 @@ const transformedModules = [];
const parsedModules = [];

module.exports = {
solo: true,
// TODO Lukas test
// * moduleParsed errors abort the build (or other hook errors)
description: 'allows pre-loading modules via this.load',
options: {
plugins: [
Expand Down

0 comments on commit 36e693b

Please sign in to comment.