Repository: webpack/webpack
Pull request #6905 (2018-03-29 13:29:58)

SizeFormatHelpers: defend against invalid sizes
<!-- Thanks for submitting a pull request! Please provide enough information so that others can review your pull request. -->

**What kind of change does this PR introduce?**

Avoids having `NaN undefined` in the chunk size.

before:
```
Hash: a767d25c0ab9f6137502
Version: webpack 4.3.0
Time: 351ms
Built at: 3/29/2018 2:57:47 PM
                           Asset           Size  Chunks             Chunk Names
                       bundle.js       6.33 KiB    main  [emitted]  main
                     0.bundle.js      462 bytes       0  [emitted]
c0a8be7a5c1867a7212e.module.wasm  NaN undefined       0  [emitted]
                      index.html      182 bytes          [emitted]
Entrypoint main = bundle.js
[./src/index.js] 90 bytes {main} [built]
[./src/test.wasm] 71 bytes {0} [built]
Child html-webpack-plugin for "index.html":
     1 asset
    Entrypoint undefined = index.html
    [../../buildin/module.js] (webpack)/buildin/module.js 497 bytes {0} [built]
        + 2 hidden modules
```
after:
```
Hash: 6d167786e870e3758cab
Version: webpack 4.3.0
Time: 335ms
Built at: 2018-3-29 15:14:06
                           Asset          Size  Chunks             Chunk Names
                       bundle.js      8.58 KiB    main  [emitted]  main
                     0.bundle.js     492 bytes       0  [emitted]
c0a8be7a5c1867a7212e.module.wasm  unknown size       0  [emitted]
                      index.html     182 bytes          [emitted]
Entrypoint main = bundle.js
[./src/index.js] 90 bytes {main} [built]
[./src/test.wasm] 71 bytes {0} [built]
Child html-webpack-plugin for "index.html":
     1 asset
    Entrypoint undefined = index.html
    [../../buildin/global.js] (webpack)/buildin/global.js 489 bytes {0} [built]
    [../../buildin/module.js] (webpack)/buildin/module.js 497 bytes {0} [built]
        + 2 hidden modules
```

and https://github.com/webpack/webpack-sources/pull/38 fixes the wasm unknown size.

<!-- E.g. a bugfix, feature, refactoring, build related change, etc… -->

**Did you add tests for your changes?**
yes :+1: 

<!-- Note that we won't merge your changes if you don't add tests -->

**If relevant, link to documentation update:**

<!-- Link PR from webpack/webpack.js.org here, or N/A -->

**Summary**

<!-- Explain the **motivation** for making this change. What existing problem does the pull request solve? -->
<!-- Try to link to an open issue for more information. -->

**Does this PR introduce a breaking change?**
no :+1: 

<!-- If this PR introduces a breaking change, please describe the impact and a migration path for existing applications. -->

**Other information**


Patch:
diff --git a/lib/SizeFormatHelpers.js b/lib/SizeFormatHelpers.js
index 85bbd2c02..c4677f60b 100644
--- a/lib/SizeFormatHelpers.js
+++ b/lib/SizeFormatHelpers.js
@@ -7,6 +7,10 @@
 const SizeFormatHelpers = exports;
 
 SizeFormatHelpers.formatSize = size => {
+	if (typeof size !== "number" || Number.isNaN(size) === true) {
+		return "unknown size";
+	}
+
 	if (size <= 0) {
 		return "0 bytes";
 	}


Test patch:
diff --git a/test/SizeFormatHelpers.unittest.js b/test/SizeFormatHelpers.unittest.js
index 7d3f8c842..d641ee342 100644
--- a/test/SizeFormatHelpers.unittest.js
+++ b/test/SizeFormatHelpers.unittest.js
@@ -43,5 +43,10 @@ describe("SizeFormatHelpers", () => {
 				"1.2 GiB"
 			);
 		});
+
+		it("should handle undefined/NaN", () => {
+			should(SizeFormatHelpers.formatSize(undefined)).be.eql("unknown size");
+			should(SizeFormatHelpers.formatSize(NaN)).be.eql("unknown size");
+		});
 	});
 });