From f9377906eac49f234e4dad99d0d63a4211c1e450 Mon Sep 17 00:00:00 2001 From: jamesgeorge007 Date: Sun, 23 Jun 2019 13:24:14 +0530 Subject: [PATCH 1/6] ease of access for contents of emitted assets --- lib/Compiler.js | 4 + .../__snapshots__/StatsTestCases.test.js.snap | 94 +++++++++++++++++++ 2 files changed, 98 insertions(+) diff --git a/lib/Compiler.js b/lib/Compiler.js index 97ae85c3437..4913daaaf9f 100644 --- a/lib/Compiler.js +++ b/lib/Compiler.js @@ -57,6 +57,8 @@ class Compiler extends Tapable { emit: new AsyncSeriesHook(["compilation"]), /** @type {AsyncSeriesHook} */ afterEmit: new AsyncSeriesHook(["compilation"]), + /** @type {AsyncSeriesHook} */ + assetEmitted: new AsyncSeriesHook(["compilation"]), /** @type {SyncHook} */ thisCompilation: new SyncHook(["compilation", "params"]), @@ -430,6 +432,7 @@ class Compiler extends Tapable { : targetFileGeneration + 1; cacheEntry.writtenTo.set(targetPath, newGeneration); this._assetEmittingWrittenFiles.set(targetPath, newGeneration); + this.hooks.assetEmitted.callAsync(file, content, callback); callback(); }); } else { @@ -445,6 +448,7 @@ class Compiler extends Tapable { source.existsAt = targetPath; source.emitted = true; + this.hooks.assetEmitted.callAsync(file, content, callback); this.outputFileSystem.writeFile(targetPath, content, callback); } }; diff --git a/test/__snapshots__/StatsTestCases.test.js.snap b/test/__snapshots__/StatsTestCases.test.js.snap index 3e3fb1f582a..b5b0a25318c 100644 --- a/test/__snapshots__/StatsTestCases.test.js.snap +++ b/test/__snapshots__/StatsTestCases.test.js.snap @@ -1608,6 +1608,18 @@ chunk {7} main.js (main) 523 bytes >{0}< >{1}< >{2}< >{5}< [entry] [rendered] [5] ./index.js 523 bytes {7} [built]" `; +exports[`StatsTestCases should print correct stats for optimize-chunks 2`] = ` +"Time: Xms +Built at: Thu Jan 01 1970 00:00:00 GMT + Asset Size Chunks Chunk Names +main.js 3.75 KiB 0 main +Entrypoint main = main.js +[0] ./index.js 25 bytes {0} [built] + +ERROR in ./index.js +Module not found: Error: Can't resolve 'does-not-exist' in 'Xdir/module-trace-disabled-in-error'" +`; + exports[`StatsTestCases should print correct stats for parse-error 1`] = ` " Asset Size Chunks Chunk Names main.js 4.11 KiB 0 main @@ -1629,6 +1641,19 @@ You may need an appropriate loader to handle this file type, currently no loader @ ./index.js" `; +exports[`StatsTestCases should print correct stats for parse-error 2`] = ` +"Time: Xms +Built at: Thu Jan 01 1970 00:00:00 GMT + Asset Size Chunks Chunk Names +main.js 3.75 KiB 0 main +Entrypoint main = main.js +[0] ./index.js 25 bytes {0} [built] + +ERROR in ./index.js +Module not found: Error: Can't resolve 'does-not-exist' in 'Xdir/module-trace-enabled-in-error' + @ ./index.js 1:0-25" +`; + exports[`StatsTestCases should print correct stats for performance-different-mode-and-target 1`] = ` "Hash: fdd848419e60e2d013b0259ddac6a2fc2be16d5d2ff383e3c2c0c58d07840c13a912a795f6e4940364a02963586e6b6811420c13a912a795f6e494032ff383e3c2c0c58d0784 Child @@ -1734,6 +1759,28 @@ Child For more info visit https://webpack.js.org/guides/code-splitting/" `; +exports[`StatsTestCases should print correct stats for performance-different-mode-and-target 2`] = ` +"Hash: 51d6721efa0198f0e5a4 +Time: Xms +Built at: Thu Jan 01 1970 00:00:00 GMT + Asset Size Chunks Chunk Names +bundle.js 3.57 KiB 0 main + child.js 3.57 KiB +Entrypoint main = bundle.js +[0] ./index.js 0 bytes {0} [built] + +WARNING in configuration +The 'mode' option has not been set, webpack will fallback to 'production' for this value. Set 'mode' option to 'development' or 'production' to enable defaults for each environment. +You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/configuration/mode/ +Child child: + Asset Size Chunks Chunk Names + child.js 3.57 KiB 0 child + Entrypoint child = child.js + [0] ./index.js 0 bytes {0} [built] + + ERROR in forced error" +`; + exports[`StatsTestCases should print correct stats for performance-disabled 1`] = ` "Time: Xms Built at: Thu Jan 01 1970 00:00:00 GMT @@ -1751,6 +1798,28 @@ Entrypoint main = main.js [5] ./e.js 22 bytes {1} [built]" `; +exports[`StatsTestCases should print correct stats for performance-disabled 2`] = ` +"Hash: 51d6721efa0198f0e5a4 +Time: Xms +Built at: Thu Jan 01 1970 00:00:00 GMT + Asset Size Chunks Chunk Names +bundle.js 3.57 KiB 0 main + child.js 3.57 KiB +Entrypoint main = bundle.js +[0] ./index.js 0 bytes {0} [built] + +WARNING in configuration +The 'mode' option has not been set, webpack will fallback to 'production' for this value. Set 'mode' option to 'development' or 'production' to enable defaults for each environment. +You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/configuration/mode/ +Child child: + Asset Size Chunks Chunk Names + child.js 3.57 KiB 0 child + Entrypoint child = child.js + [0] ./index.js 0 bytes {0} [built] + + ERROR in forced error" +`; + exports[`StatsTestCases should print correct stats for performance-error 1`] = ` "Time: Xms Built at: Thu Jan 01 1970 00:00:00 GMT @@ -1910,6 +1979,31 @@ chunk {5} preloaded2.js (preloaded2) 0 bytes <{2}> [rendered] chunk {6} preloaded3.js (preloaded3) 0 bytes <{2}> [rendered]" `; +exports[`StatsTestCases should print correct stats for preload 2`] = ` +"Time: Xms +Built at: Thu Jan 01 1970 00:00:00 GMT + Asset Size Chunks Chunk Names +main.js 3.75 KiB 0 main +Entrypoint main = main.js +[0] ./index.js 25 bytes {0} [built] + +ERROR in ./index.js +Module not found: Error: Can't resolve 'does-not-exist' in 'Xdir/module-trace-disabled-in-error'" +`; + +exports[`StatsTestCases should print correct stats for preload 3`] = ` +"Time: Xms +Built at: Thu Jan 01 1970 00:00:00 GMT + Asset Size Chunks Chunk Names +main.js 3.75 KiB 0 main +Entrypoint main = main.js +[0] ./index.js 25 bytes {0} [built] + +ERROR in ./index.js +Module not found: Error: Can't resolve 'does-not-exist' in 'Xdir/module-trace-enabled-in-error' + @ ./index.js 1:0-25" +`; + exports[`StatsTestCases should print correct stats for preset-detailed 1`] = ` "Hash: 2b293d5b94136dfe6d9c Time: Xms From de3e609a587e7628fbac57afb501bae15fe2dddb Mon Sep 17 00:00:00 2001 From: James George Date: Fri, 5 Jul 2019 13:55:08 +0530 Subject: [PATCH 2/6] refactor --- lib/Compiler.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/Compiler.js b/lib/Compiler.js index 4913daaaf9f..47a2360da8c 100644 --- a/lib/Compiler.js +++ b/lib/Compiler.js @@ -449,7 +449,10 @@ class Compiler extends Tapable { source.existsAt = targetPath; source.emitted = true; this.hooks.assetEmitted.callAsync(file, content, callback); - this.outputFileSystem.writeFile(targetPath, content, callback); + this.outputFileSystem.writeFile(targetPath, content, err => { + if(err) return callback(err); + this.hooks.assetEmitted.callAsync(file, content, callback); + }); } }; From a302cfa8d0ef2cf7ea2e8358554532ab6dae18e3 Mon Sep 17 00:00:00 2001 From: James George Date: Fri, 5 Jul 2019 13:55:21 +0530 Subject: [PATCH 3/6] Minor fix --- lib/Compiler.js | 1 - test/__snapshots__/StatsTestCases.test.js.snap | 1 - 2 files changed, 2 deletions(-) diff --git a/lib/Compiler.js b/lib/Compiler.js index 47a2360da8c..472dc9b6070 100644 --- a/lib/Compiler.js +++ b/lib/Compiler.js @@ -433,7 +433,6 @@ class Compiler extends Tapable { cacheEntry.writtenTo.set(targetPath, newGeneration); this._assetEmittingWrittenFiles.set(targetPath, newGeneration); this.hooks.assetEmitted.callAsync(file, content, callback); - callback(); }); } else { if (source.existsAt === targetPath) { diff --git a/test/__snapshots__/StatsTestCases.test.js.snap b/test/__snapshots__/StatsTestCases.test.js.snap index b5b0a25318c..12a6809d444 100644 --- a/test/__snapshots__/StatsTestCases.test.js.snap +++ b/test/__snapshots__/StatsTestCases.test.js.snap @@ -1608,7 +1608,6 @@ chunk {7} main.js (main) 523 bytes >{0}< >{1}< >{2}< >{5}< [entry] [rendered] [5] ./index.js 523 bytes {7} [built]" `; -exports[`StatsTestCases should print correct stats for optimize-chunks 2`] = ` "Time: Xms Built at: Thu Jan 01 1970 00:00:00 GMT Asset Size Chunks Chunk Names From 164f48f1fd104cc021c302411916bf0e653b90a1 Mon Sep 17 00:00:00 2001 From: jamesgeorge007 Date: Fri, 5 Jul 2019 14:11:40 +0530 Subject: [PATCH 4/6] fix: code lint --- lib/Compiler.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/Compiler.js b/lib/Compiler.js index 472dc9b6070..554a98a7ca7 100644 --- a/lib/Compiler.js +++ b/lib/Compiler.js @@ -448,9 +448,9 @@ class Compiler extends Tapable { source.existsAt = targetPath; source.emitted = true; this.hooks.assetEmitted.callAsync(file, content, callback); - this.outputFileSystem.writeFile(targetPath, content, err => { - if(err) return callback(err); - this.hooks.assetEmitted.callAsync(file, content, callback); + this.outputFileSystem.writeFile(targetPath, content, err => { + if (err) return callback(err); + this.hooks.assetEmitted.callAsync(file, content, callback); }); } }; From 94bcce0b245e75efb117dc05cbd743442469497f Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Thu, 1 Aug 2019 12:13:54 +0200 Subject: [PATCH 5/6] fix code and snapshots --- lib/Compiler.js | 5 +- .../__snapshots__/StatsTestCases.test.js.snap | 93 ------------------- 2 files changed, 2 insertions(+), 96 deletions(-) diff --git a/lib/Compiler.js b/lib/Compiler.js index 554a98a7ca7..ee9ed0b14a7 100644 --- a/lib/Compiler.js +++ b/lib/Compiler.js @@ -55,10 +55,10 @@ class Compiler extends Tapable { run: new AsyncSeriesHook(["compiler"]), /** @type {AsyncSeriesHook} */ emit: new AsyncSeriesHook(["compilation"]), + /** @type {AsyncSeriesHook} */ + assetEmitted: new AsyncSeriesHook(["file", "content"]), /** @type {AsyncSeriesHook} */ afterEmit: new AsyncSeriesHook(["compilation"]), - /** @type {AsyncSeriesHook} */ - assetEmitted: new AsyncSeriesHook(["compilation"]), /** @type {SyncHook} */ thisCompilation: new SyncHook(["compilation", "params"]), @@ -447,7 +447,6 @@ class Compiler extends Tapable { source.existsAt = targetPath; source.emitted = true; - this.hooks.assetEmitted.callAsync(file, content, callback); this.outputFileSystem.writeFile(targetPath, content, err => { if (err) return callback(err); this.hooks.assetEmitted.callAsync(file, content, callback); diff --git a/test/__snapshots__/StatsTestCases.test.js.snap b/test/__snapshots__/StatsTestCases.test.js.snap index 12a6809d444..3e3fb1f582a 100644 --- a/test/__snapshots__/StatsTestCases.test.js.snap +++ b/test/__snapshots__/StatsTestCases.test.js.snap @@ -1608,17 +1608,6 @@ chunk {7} main.js (main) 523 bytes >{0}< >{1}< >{2}< >{5}< [entry] [rendered] [5] ./index.js 523 bytes {7} [built]" `; -"Time: Xms -Built at: Thu Jan 01 1970 00:00:00 GMT - Asset Size Chunks Chunk Names -main.js 3.75 KiB 0 main -Entrypoint main = main.js -[0] ./index.js 25 bytes {0} [built] - -ERROR in ./index.js -Module not found: Error: Can't resolve 'does-not-exist' in 'Xdir/module-trace-disabled-in-error'" -`; - exports[`StatsTestCases should print correct stats for parse-error 1`] = ` " Asset Size Chunks Chunk Names main.js 4.11 KiB 0 main @@ -1640,19 +1629,6 @@ You may need an appropriate loader to handle this file type, currently no loader @ ./index.js" `; -exports[`StatsTestCases should print correct stats for parse-error 2`] = ` -"Time: Xms -Built at: Thu Jan 01 1970 00:00:00 GMT - Asset Size Chunks Chunk Names -main.js 3.75 KiB 0 main -Entrypoint main = main.js -[0] ./index.js 25 bytes {0} [built] - -ERROR in ./index.js -Module not found: Error: Can't resolve 'does-not-exist' in 'Xdir/module-trace-enabled-in-error' - @ ./index.js 1:0-25" -`; - exports[`StatsTestCases should print correct stats for performance-different-mode-and-target 1`] = ` "Hash: fdd848419e60e2d013b0259ddac6a2fc2be16d5d2ff383e3c2c0c58d07840c13a912a795f6e4940364a02963586e6b6811420c13a912a795f6e494032ff383e3c2c0c58d0784 Child @@ -1758,28 +1734,6 @@ Child For more info visit https://webpack.js.org/guides/code-splitting/" `; -exports[`StatsTestCases should print correct stats for performance-different-mode-and-target 2`] = ` -"Hash: 51d6721efa0198f0e5a4 -Time: Xms -Built at: Thu Jan 01 1970 00:00:00 GMT - Asset Size Chunks Chunk Names -bundle.js 3.57 KiB 0 main - child.js 3.57 KiB -Entrypoint main = bundle.js -[0] ./index.js 0 bytes {0} [built] - -WARNING in configuration -The 'mode' option has not been set, webpack will fallback to 'production' for this value. Set 'mode' option to 'development' or 'production' to enable defaults for each environment. -You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/configuration/mode/ -Child child: - Asset Size Chunks Chunk Names - child.js 3.57 KiB 0 child - Entrypoint child = child.js - [0] ./index.js 0 bytes {0} [built] - - ERROR in forced error" -`; - exports[`StatsTestCases should print correct stats for performance-disabled 1`] = ` "Time: Xms Built at: Thu Jan 01 1970 00:00:00 GMT @@ -1797,28 +1751,6 @@ Entrypoint main = main.js [5] ./e.js 22 bytes {1} [built]" `; -exports[`StatsTestCases should print correct stats for performance-disabled 2`] = ` -"Hash: 51d6721efa0198f0e5a4 -Time: Xms -Built at: Thu Jan 01 1970 00:00:00 GMT - Asset Size Chunks Chunk Names -bundle.js 3.57 KiB 0 main - child.js 3.57 KiB -Entrypoint main = bundle.js -[0] ./index.js 0 bytes {0} [built] - -WARNING in configuration -The 'mode' option has not been set, webpack will fallback to 'production' for this value. Set 'mode' option to 'development' or 'production' to enable defaults for each environment. -You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/configuration/mode/ -Child child: - Asset Size Chunks Chunk Names - child.js 3.57 KiB 0 child - Entrypoint child = child.js - [0] ./index.js 0 bytes {0} [built] - - ERROR in forced error" -`; - exports[`StatsTestCases should print correct stats for performance-error 1`] = ` "Time: Xms Built at: Thu Jan 01 1970 00:00:00 GMT @@ -1978,31 +1910,6 @@ chunk {5} preloaded2.js (preloaded2) 0 bytes <{2}> [rendered] chunk {6} preloaded3.js (preloaded3) 0 bytes <{2}> [rendered]" `; -exports[`StatsTestCases should print correct stats for preload 2`] = ` -"Time: Xms -Built at: Thu Jan 01 1970 00:00:00 GMT - Asset Size Chunks Chunk Names -main.js 3.75 KiB 0 main -Entrypoint main = main.js -[0] ./index.js 25 bytes {0} [built] - -ERROR in ./index.js -Module not found: Error: Can't resolve 'does-not-exist' in 'Xdir/module-trace-disabled-in-error'" -`; - -exports[`StatsTestCases should print correct stats for preload 3`] = ` -"Time: Xms -Built at: Thu Jan 01 1970 00:00:00 GMT - Asset Size Chunks Chunk Names -main.js 3.75 KiB 0 main -Entrypoint main = main.js -[0] ./index.js 25 bytes {0} [built] - -ERROR in ./index.js -Module not found: Error: Can't resolve 'does-not-exist' in 'Xdir/module-trace-enabled-in-error' - @ ./index.js 1:0-25" -`; - exports[`StatsTestCases should print correct stats for preset-detailed 1`] = ` "Hash: 2b293d5b94136dfe6d9c Time: Xms From 53a7a342c5c3d8e95ac709264b4f14eb7c14b2a2 Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Thu, 1 Aug 2019 12:20:41 +0200 Subject: [PATCH 6/6] add test cases --- .../asset-emitted/futureEmitAssets/index.js | 3 +++ .../asset-emitted/futureEmitAssets/module.js | 0 .../futureEmitAssets/webpack.config.js | 21 +++++++++++++++++++ .../configCases/asset-emitted/normal/index.js | 3 +++ .../asset-emitted/normal/module.js | 0 .../asset-emitted/normal/webpack.config.js | 18 ++++++++++++++++ 6 files changed, 45 insertions(+) create mode 100644 test/configCases/asset-emitted/futureEmitAssets/index.js create mode 100644 test/configCases/asset-emitted/futureEmitAssets/module.js create mode 100644 test/configCases/asset-emitted/futureEmitAssets/webpack.config.js create mode 100644 test/configCases/asset-emitted/normal/index.js create mode 100644 test/configCases/asset-emitted/normal/module.js create mode 100644 test/configCases/asset-emitted/normal/webpack.config.js diff --git a/test/configCases/asset-emitted/futureEmitAssets/index.js b/test/configCases/asset-emitted/futureEmitAssets/index.js new file mode 100644 index 00000000000..c5d0cd4199f --- /dev/null +++ b/test/configCases/asset-emitted/futureEmitAssets/index.js @@ -0,0 +1,3 @@ +import("./module"); + +it("should run", () => {}); diff --git a/test/configCases/asset-emitted/futureEmitAssets/module.js b/test/configCases/asset-emitted/futureEmitAssets/module.js new file mode 100644 index 00000000000..e69de29bb2d diff --git a/test/configCases/asset-emitted/futureEmitAssets/webpack.config.js b/test/configCases/asset-emitted/futureEmitAssets/webpack.config.js new file mode 100644 index 00000000000..23a53ecdc67 --- /dev/null +++ b/test/configCases/asset-emitted/futureEmitAssets/webpack.config.js @@ -0,0 +1,21 @@ +module.exports = { + output: { + futureEmitAssets: true + }, + plugins: [ + compiler => { + const files = {}; + compiler.hooks.assetEmitted.tap("Test", (file, buffer) => { + files[file] = Buffer.isBuffer(buffer); + }); + compiler.hooks.afterEmit.tap("Test", () => { + expect(files).toMatchInlineSnapshot(` +Object { + "1.bundle0.js": true, + "bundle0.js": true, +} +`); + }); + } + ] +}; diff --git a/test/configCases/asset-emitted/normal/index.js b/test/configCases/asset-emitted/normal/index.js new file mode 100644 index 00000000000..c5d0cd4199f --- /dev/null +++ b/test/configCases/asset-emitted/normal/index.js @@ -0,0 +1,3 @@ +import("./module"); + +it("should run", () => {}); diff --git a/test/configCases/asset-emitted/normal/module.js b/test/configCases/asset-emitted/normal/module.js new file mode 100644 index 00000000000..e69de29bb2d diff --git a/test/configCases/asset-emitted/normal/webpack.config.js b/test/configCases/asset-emitted/normal/webpack.config.js new file mode 100644 index 00000000000..cd7640029b2 --- /dev/null +++ b/test/configCases/asset-emitted/normal/webpack.config.js @@ -0,0 +1,18 @@ +module.exports = { + plugins: [ + compiler => { + const files = {}; + compiler.hooks.assetEmitted.tap("Test", (file, buffer) => { + files[file] = Buffer.isBuffer(buffer); + }); + compiler.hooks.afterEmit.tap("Test", () => { + expect(files).toMatchInlineSnapshot(` +Object { + "1.bundle0.js": true, + "bundle0.js": true, +} +`); + }); + } + ] +};