From 4d6bbd57e397009277258f0240811226f584ff39 Mon Sep 17 00:00:00 2001 From: Dan Bucholtz Date: Wed, 16 Nov 2016 13:42:16 -0600 Subject: [PATCH] fix(webpack): invalidate cache by use of timestamps --- src/webpack.ts | 25 +++++-------------------- src/webpack/watch-memory-system.ts | 24 +++++++++--------------- 2 files changed, 14 insertions(+), 35 deletions(-) diff --git a/src/webpack.ts b/src/webpack.ts index 4b0ef8dc..1f8091ff 100644 --- a/src/webpack.ts +++ b/src/webpack.ts @@ -3,7 +3,7 @@ import { BuildContext, BuildState, File, TaskInfo } from './util/interfaces'; import { BuildError, IgnorableError } from './util/errors'; import { changeExtension, readFileAsync, setContext } from './util/helpers'; import { emit, EventType } from './util/events'; -import { extname, join } from 'path'; +import { join } from 'path'; import { fillConfigDefaults, generateContext, getUserConfigFile, replacePathVars } from './util/config'; import { Logger } from './logger/logger'; import * as webpackApi from 'webpack'; @@ -47,26 +47,11 @@ export function webpack(context: BuildContext, configFile: string) { export function webpackUpdate(event: string, path: string, context: BuildContext, configFile: string) { const logger = new Logger('webpack update'); - const extension = extname(path); - const webpackConfig = getWebpackConfig(context, configFile); - return Promise.resolve().then(() => { - if (extension === '.ts') { - Logger.debug('webpackUpdate: Typescript File Changed'); - return typescriptFileChanged(path, context.fileCache); - } else { - Logger.debug('webpackUpdate: Non-Typescript File Changed'); - return otherFileChanged(path).then((file: File) => { - return [file]; - }); - } - }) - .then((files: File[]) => { - Logger.debug('webpackUpdate: Starting Incremental Build'); - const promisetoReturn = runWebpackIncrementalBuild(false, context, webpackConfig); - emit(EventType.WebpackFilesChanged, [path]); - return promisetoReturn; - }).then((stats: any) => { + Logger.debug('webpackUpdate: Starting Incremental Build'); + const promisetoReturn = runWebpackIncrementalBuild(false, context, webpackConfig); + emit(EventType.WebpackFilesChanged, [path]); + return promisetoReturn.then((stats: any) => { // the webpack incremental build finished, so reset the list of pending promises pendingPromises = []; Logger.debug('webpackUpdate: Incremental Build Done, processing Data'); diff --git a/src/webpack/watch-memory-system.ts b/src/webpack/watch-memory-system.ts index ad469f65..ca0e0391 100644 --- a/src/webpack/watch-memory-system.ts +++ b/src/webpack/watch-memory-system.ts @@ -1,11 +1,13 @@ import { FileCache } from '../util/file-cache'; import { on, EventType } from '../util/events'; +import { Logger } from '../logger/logger'; export class WatchMemorySystem { private changes: Set; private isAggregating: boolean; private isListening: boolean; + private lastWatchEventTimestamp: number = Date.now(); private filePathsBeingWatched: string[]; private dirPaths: string[]; @@ -48,10 +50,13 @@ export class WatchMemorySystem { startListening() { this.isListening = true; - on(EventType.WebpackFilesChanged, ((filePaths: string[]) => { + on(EventType.WebpackFilesChanged, () => { this.changes = new Set(); + const filePaths = this.fileCache.getAll().filter(file => file.timestamp >= this.lastWatchEventTimestamp).map(file => file.path); + Logger.debug('filePaths: ', filePaths); + this.lastWatchEventTimestamp = Date.now(); this.processChanges(filePaths); - })); + }); } processChanges(filePaths: string[]) { @@ -59,20 +64,9 @@ export class WatchMemorySystem { for ( const path of filePaths) { this.changes.add(path); } + // don't bother waiting around, just call doneAggregating right away. + // keep it as a function in case we need to wait via setTimeout a bit in the future this.doneAggregating(this.changes); - /*if (this.isAggregating) { - // we're currently aggregating file changes into a collection - this.changes.add(filePath); - } else { - // we're starting a new listen, so start a new list of files, and start aggregating the files - this.changes = new Set(); - this.changes.add(filePath); - this.isAggregating = true; - setTimeout(() => { - // we're done aggregating files - this.doneAggregating(this.changes); - }, AGGREGATING_DURATION_IN_MILLIS); - }*/ } doneAggregating(changes: Set) {