From 8884f70a24f6c95c78064a9803cb06b3d6987e97 Mon Sep 17 00:00:00 2001 From: Alice Gaudon Date: Tue, 11 May 2021 13:53:49 +0200 Subject: [PATCH] AssetPreCompiler: watch all view directories instead of the first one --- src/frontend/AssetPreCompiler.ts | 44 ++++++++++++-------------------- 1 file changed, 17 insertions(+), 27 deletions(-) diff --git a/src/frontend/AssetPreCompiler.ts b/src/frontend/AssetPreCompiler.ts index a08dd3f..fc09716 100644 --- a/src/frontend/AssetPreCompiler.ts +++ b/src/frontend/AssetPreCompiler.ts @@ -60,11 +60,6 @@ export default abstract class AssetPreCompiler { return this.assetPaths; } - public getPrimaryAssetPath(): string { - if (this.assetPaths.length === 0) throw new Error('No asset path was found.'); - return this.assetPaths[0]; - } - public abstract preCompile(canonicalName: string, alsoCompileDependents: boolean): Promise; public onPreCompile(afterPreCompileHandler: (watch: boolean) => Promise): void { @@ -101,39 +96,34 @@ export default abstract class AssetPreCompiler { } public async watch(): Promise { - this.watcher = chokidar.watch(this.getPrimaryAssetPath(), {persistent: true}); + const watchedPaths = this.assetPaths.map(p => `${p}/**/*.${this.getExtension()}`); + this.watcher = chokidar.watch(watchedPaths, {persistent: true}); this.watcher.on('ready', () => { if (!this.watcher) return; - logger.info(`Watching ${this.extension} assets for changes`); + logger.info(`Watching ${this.extension} assets for changes in ${watchedPaths}`); this.watcher.on('add', (file) => { - if (file.endsWith('.' + this.extension)) { - this.onNewFile(file) - .then(() => this.preCompile(this.toCanonicalName(file), true)) - .then(() => { - return this.afterPreCompile(true); - }) - .catch(err => logger.error(err)); - } + this.onNewFile(file) + .then(() => this.preCompile(this.toCanonicalName(file), true)) + .then(() => { + return this.afterPreCompile(true); + }) + .catch(err => logger.error(err)); }); this.watcher.on('change', (file) => { - if (file.endsWith('.' + this.extension)) { - (this.onFileChange ? this.onFileChange(file) : Promise.resolve()) - .then(() => this.preCompile(this.toCanonicalName(file), true)) - .then(() => { - return this.afterPreCompile(true); - }) - .catch(err => logger.error(err)); - } + (this.onFileChange ? this.onFileChange(file) : Promise.resolve()) + .then(() => this.preCompile(this.toCanonicalName(file), true)) + .then(() => { + return this.afterPreCompile(true); + }) + .catch(err => logger.error(err)); }); this.watcher.on('unlink', (file) => { - if (file.endsWith('.' + this.extension)) { - this.onFileRemove(file) - .catch(err => logger.error(err)); - } + this.onFileRemove(file) + .catch(err => logger.error(err)); }); }); }