Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forward bundle through watch error events #3909

Merged
merged 1 commit into from Dec 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion cli/run/watch-cli.ts
Expand Up @@ -129,14 +129,17 @@ export async function watch(command: any) {
if (event.result && event.result.getTimings) {
printTimings(event.result.getTimings());
}
event.result.close().catch(error => handleError(error, true));
break;

case 'END':
if (!silent && isTTY) {
stderr(`\n[${dateTime()}] waiting for changes...`);
}
}

if ('result' in event && event.result) {
event.result.close().catch(error => handleError(error, true));
}
});
}

Expand Down
8 changes: 6 additions & 2 deletions docs/02-javascript-api.md
Expand Up @@ -196,11 +196,15 @@ watcher.on('event', event => {
// END — finished building all bundles
// ERROR — encountered an error while bundling
// * event.error contains the error that was thrown
// * event.result is null for build errors and contains the
// bundle object for output generation errors. As with
// "BUNDLE_END", you should call "event.result.close()" if
// present once you are done.
});

// This will make sure that bundles are properly closed after each run
watcher.on('event', ({ code, result }) => {
if (code === 'BUNDLE_END') {
watcher.on('event', ({ result }) => {
if (result) {
result.close();
}
});
Expand Down
2 changes: 1 addition & 1 deletion src/rollup/types.d.ts
Expand Up @@ -857,7 +857,7 @@ export type RollupWatcherEvent =
result: RollupBuild;
}
| { code: 'END' }
| { code: 'ERROR'; error: RollupError };
| { code: 'ERROR'; error: RollupError; result: RollupBuild | null };

export interface RollupWatcher
extends TypedEventEmitter<{
Expand Down
52 changes: 23 additions & 29 deletions src/watch/watch.ts
Expand Up @@ -100,27 +100,18 @@ export class Watcher {

private async run() {
this.running = true;

this.emitter.emit('event', {
code: 'START'
});

try {
for (const task of this.tasks) {
await task.run();
}
this.running = false;
this.emitter.emit('event', {
code: 'END'
});
} catch (error) {
this.running = false;
this.emitter.emit('event', {
code: 'ERROR',
error
});
for (const task of this.tasks) {
await task.run();
}

this.running = false;
this.emitter.emit('event', {
code: 'END'
});
if (this.rerun) {
this.rerun = false;
this.invalidate();
Expand Down Expand Up @@ -182,7 +173,7 @@ export class Task {
this.watcher.invalidate({ id, event: details.event });
}

async run() {
async run(): Promise<void> {
if (!this.invalidated) return;
this.invalidated = false;

Expand All @@ -198,14 +189,15 @@ export class Task {
input: this.options.input,
output: this.outputFiles
});
let result: RollupBuild | null = null;

try {
const result = await rollupInternal(options, this.watcher.emitter);
result = await rollupInternal(options, this.watcher.emitter);
if (this.closed) {
return;
}
this.updateWatchedFiles(result);
this.skipWrite || (await Promise.all(this.outputs.map(output => result.write(output))));
this.skipWrite || (await Promise.all(this.outputs.map(output => result!.write(output))));
this.watcher.emitter.emit('event', {
code: 'BUNDLE_END',
duration: Date.now() - start,
Expand All @@ -214,19 +206,21 @@ export class Task {
result
});
} catch (error) {
if (this.closed) {
return;
}

if (Array.isArray(error.watchFiles)) {
for (const id of error.watchFiles) {
this.watchFile(id);
if (!this.closed) {
if (Array.isArray(error.watchFiles)) {
for (const id of error.watchFiles) {
this.watchFile(id);
}
}
if (error.id) {
this.cache.modules = this.cache.modules.filter(module => module.id !== error.id);
}
}
if (error.id) {
this.cache.modules = this.cache.modules.filter(module => module.id !== error.id);
}
throw error;
this.watcher.emitter.emit('event', {
code: 'ERROR',
error,
result
});
}
}

Expand Down
19 changes: 19 additions & 0 deletions test/cli/samples/watch/close-on-generate-error/_config.js
@@ -0,0 +1,19 @@
const { assertIncludes } = require('../../../../utils.js');

module.exports = {
description: 'closes the bundle on generate errors',
command: 'rollup -cw',
abortOnStderr(data) {
if (data.includes('Bundle closed')) {
return true;
}
},
stderr(stderr) {
assertIncludes(
stderr,
'[!] Error: You must specify "output.file" or "output.dir" for the build.'
);
assertIncludes(stderr, 'Bundle closed');
return false;
}
};
1 change: 1 addition & 0 deletions test/cli/samples/watch/close-on-generate-error/main.js
@@ -0,0 +1 @@
export default 42;
14 changes: 14 additions & 0 deletions test/cli/samples/watch/close-on-generate-error/rollup.config.js
@@ -0,0 +1,14 @@
export default {
input: 'main.js',
plugins: [
{
name: 'faulty-close',
closeBundle() {
console.error('Bundle closed')
}
}
],
output: {
format: "es"
}
};