diff --git a/types/webpack/index.d.ts b/types/webpack/index.d.ts index da6d84cc1e..d9db284ae8 100644 --- a/types/webpack/index.d.ts +++ b/types/webpack/index.d.ts @@ -947,6 +947,8 @@ declare namespace webpack { childrenCounters: any; usedChunkIds: any; usedModuleIds: any; + fileTimestamps: Map; + contextTimestamps: Map; getStats(): Stats; addModule(module: CompilationModule, cacheGroup: any): any; // tslint:disable-next-line:ban-types @@ -1017,6 +1019,26 @@ declare namespace webpack { invalidate(): void; } + interface InputFileSystem { + purge(): void; + readFile(path: string, callback: (err: Error, contents: Buffer) => void): void; + readFileSync(path: string): Buffer; + readlink(path: string, callback: (err: Error, linkString: string) => void): void; + readlinkSync(path: string): string; + stat(path: string, callback: (err: Error, stats: any) => void): void; + statSync(path: string): any; + } + + interface OutputFileSystem { + join(...paths: string[]): string; + mkdir(path: string, callback: (err: Error) => void): void; + mkdirp(path: string, callback: (err: Error) => void): void; + purge(): void; + rmdir(path: string, callback: (err: Error) => void): void; + unlink(path: string, callback: (err: Error) => void): void; + writeFile(path: string, data: any, callback: (err: Error) => void): void; + } + class Compiler extends Tapable implements ICompiler { constructor(); @@ -1025,7 +1047,10 @@ declare namespace webpack { name: string; options: Configuration; - outputFileSystem: any; + inputFileSystem: InputFileSystem; + outputFileSystem: OutputFileSystem; + fileTimestamps: Map; + contextTimestamps: Map; run(handler: Compiler.Handler): void; watch(watchOptions: Compiler.WatchOptions, handler: Compiler.Handler): Compiler.Watching; } diff --git a/types/webpack/webpack-tests.ts b/types/webpack/webpack-tests.ts index 5a41905b8d..1e1a95fe97 100644 --- a/types/webpack/webpack-tests.ts +++ b/types/webpack/webpack-tests.ts @@ -158,11 +158,26 @@ declare const require: any; declare const path: any; configuration = { plugins: [ - function(this: webpack.Compiler) { - this.plugin("done", stats => { - require("fs").writeFileSync( + function apply(this: webpack.Compiler) { + const prevTimestamps = new Map(); + const startTime = Date.now(); + + this.hooks.emit.tap("SomePlugin", (compilation: webpack.compilation.Compilation) => { + for (const filepath in compilation.fileTimestamps.keys()) { + const prevTimestamp = prevTimestamps.get(filepath) || startTime; + const newTimestamp = compilation.fileTimestamps.get(filepath) || Infinity; + if (prevTimestamp < newTimestamp) { + this.inputFileSystem.readFileSync(filepath).toString('utf-8'); + } + } + }); + + this.hooks.afterEmit.tapAsync("afterEmit", (stats, callback) => { + this.outputFileSystem.writeFile( path.join(__dirname, "...", "stats.json"), - JSON.stringify(stats.toJson())); + JSON.stringify(stats.getStats().toJson()), + callback + ); }); } ]