Skip to content

Commit

Permalink
add ability to configure build delay
Browse files Browse the repository at this point in the history
  • Loading branch information
mattdesl committed Apr 17, 2020
1 parent c0c206e commit 1bf3808
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
1 change: 1 addition & 0 deletions src/rollup/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,7 @@ export interface RollupWatcher
event: (event: RollupWatcherEvent) => void;
restart: () => void;
}> {
delay?: number;
close(): void;
}

Expand Down
31 changes: 16 additions & 15 deletions src/watch/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import {
RollupBuild,
RollupCache,
RollupWatcher,
WatcherOptions,
WatcherOptions
} from '../rollup/types';
import { mergeOptions } from '../utils/mergeOptions';
import { ensureArray, GenericConfigObject } from '../utils/parseOptions';
import { FileWatcher } from './fileWatcher';

const DELAY = 200;
const DEFAULT_DELAY = 200;

export class Watcher {
emitter: RollupWatcher;
Expand All @@ -27,7 +27,7 @@ export class Watcher {
constructor(configs: GenericConfigObject[] | GenericConfigObject, emitter: RollupWatcher) {
this.emitter = emitter;
emitter.close = this.close.bind(this);
this.tasks = ensureArray(configs).map((config) => new Task(this, config));
this.tasks = ensureArray(configs).map(config => new Task(this, config));
this.running = true;
process.nextTick(() => this.run());
}
Expand Down Expand Up @@ -55,6 +55,7 @@ export class Watcher {

if (this.buildTimeout) clearTimeout(this.buildTimeout);

const delay: number = this.emitter.delay != null ? this.emitter.delay : DEFAULT_DELAY;
this.buildTimeout = setTimeout(() => {
this.buildTimeout = null;
for (const id of this.invalidatedIds) {
Expand All @@ -63,14 +64,14 @@ export class Watcher {
this.invalidatedIds.clear();
this.emit('restart');
this.run();
}, DELAY);
}, delay);
}

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

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

try {
Expand All @@ -79,13 +80,13 @@ export class Watcher {
}
this.running = false;
this.emit('event', {
code: 'END',
code: 'END'
});
} catch (error) {
this.running = false;
this.emit('event', {
code: 'ERROR',
error,
error
});
}

Expand Down Expand Up @@ -119,7 +120,7 @@ export class Task {
this.skipWrite = config.watch && !!(config.watch as GenericConfigObject).skipWrite;
this.options = mergeOptions(config);
this.outputs = this.options.output;
this.outputFiles = this.outputs.map((output) => {
this.outputFiles = this.outputs.map(output => {
if (output.file || output.dir) return path.resolve(output.file || output.dir!);
return undefined as any;
});
Expand All @@ -129,7 +130,7 @@ export class Task {
this.fileWatcher = new FileWatcher(this, {
...watchOptions.chokidar,
disableGlobbing: true,
ignoreInitial: true,
ignoreInitial: true
});
}

Expand All @@ -156,15 +157,15 @@ export class Task {

const options = {
...this.options,
cache: this.cache,
cache: this.cache
};

const start = Date.now();

this.watcher.emit('event', {
code: 'BUNDLE_START',
input: this.options.input,
output: this.outputFiles,
output: this.outputFiles
});

try {
Expand All @@ -173,13 +174,13 @@ export class Task {
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.emit('event', {
code: 'BUNDLE_END',
duration: Date.now() - start,
input: this.options.input,
output: this.outputFiles,
result,
result
});
} catch (error) {
if (this.closed) {
Expand All @@ -192,7 +193,7 @@ export class Task {
}
}
if (error.id) {
this.cache.modules = this.cache.modules.filter((module) => module.id !== error.id);
this.cache.modules = this.cache.modules.filter(module => module.id !== error.id);
}
throw error;
}
Expand Down Expand Up @@ -222,7 +223,7 @@ export class Task {
if (!this.filter(id)) return;
this.watched.add(id);

if (this.outputFiles.some((file) => file === id)) {
if (this.outputFiles.some(file => file === id)) {
throw new Error('Cannot import the generated bundle');
}

Expand Down

0 comments on commit 1bf3808

Please sign in to comment.