From bcb71bbf2945c59e2afb02697639e2432c6ca68d Mon Sep 17 00:00:00 2001 From: Emilio Martinez Date: Thu, 12 Jul 2018 00:52:43 -0700 Subject: [PATCH] feat(webpack): fill Compiler- and Compilation-related fs types (#27117) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Please fill in this template. - [X] Use a meaningful title for the pull request. Include the name of the package modified. - [X] Test the change in your own code. (Compile and run.) - [x] Add or edit tests to reflect the change. (Run with `npm test`.) - [X] Follow the advice from the [readme](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/README.md#make-a-pull-request). - [X] Avoid [common mistakes](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/README.md#common-mistakes). - [x] Run `npm run lint package-name` (or `tsc` if no `tslint.json` is present). Select one of these and delete the others: If changing an existing definition: - [X] Provide a URL to documentation or source code which provides context for the suggested changes: (See bottom of description) - [X] Increase the version number in the header if appropriate. - [X] If you are making substantial changes, consider adding a `tslint.json` containing `{ "extends": "dtslint/dt.json" }`. --- Changes in this PR are related to a few Webpack Compiler and Compilation properties. Adds definitions for following file timestamp properties: * `Compiler.fileTimestamps`. See https://github.com/webpack/webpack/blob/v4.15.1/lib/Compiler.js#L138. * `Compiler.contextTimestamps`. See https://github.com/webpack/webpack/blob/v4.15.1/lib/Compiler.js#L140. * `Compilation.fileTimestamps`. See https://github.com/webpack/webpack/blob/v4.15.1/lib/Compilation.js#L467. * `Compilation.contextTimestamps`. See https://github.com/webpack/webpack/blob/v4.15.1/lib/Compilation.js#L469. Adds definitions for the following file system properties: * `Compiler.inputFileSystem` — Roughly a subset of `fs` read features. See https://github.com/webpack/enhanced-resolve/blob/v4.1.0/lib/NodeJsInputFileSystem.js and https://github.com/webpack/memory-fs/blob/v0.4.1/lib/MemoryFileSystem.js. * `Compiler.outputFileSystem` — Roughly a subset of `fs` output features. See https://github.com/webpack/webpack/blob/v4.15.1/lib/node/NodeOutputFileSystem.js#L9:7. Both file system properties above, while initialized as `null`, have their value set almost immediately right after by Webpack, making them generally not `null` by the time the Compiler can be accessed. See https://github.com/webpack/webpack/blob/master/lib/webpack.js#L34. --- types/webpack/index.d.ts | 27 ++++++++++++++++++++++++++- types/webpack/webpack-tests.ts | 23 +++++++++++++++++++---- 2 files changed, 45 insertions(+), 5 deletions(-) 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 + ); }); } ]