Repository: webpack/webpack
Pull request #6954 (2018-04-04 09:29:51)

Use compilation instead of this in ChunkModuleIdRangePlugin
<!-- 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?**

bugfix

**Did you add tests for your changes?**

yes

**If relevant, link to documentation update:**

N/A

**Summary**

Because of the arrow function `this` was no longer referencing the `compilation` which was a bug. Since there is no test or documentation about this plugin, I don't know to create a good test case for this. Is this plugin still relevant?

**Does this PR introduce a breaking change?**

no

webpack-bot: <!-- identifier: ci-result -->

Thank you for your pull request! The most important CI builds succeeded, we’ll review the pull request soon.

Patch:
diff --git a/lib/WebpackError.js b/lib/WebpackError.js
index 4fd233881..9e44b5c30 100644
--- a/lib/WebpackError.js
+++ b/lib/WebpackError.js
@@ -5,14 +5,6 @@
 "use strict";
 
 module.exports = class WebpackError extends Error {
-	constructor(message) {
-		super(message);
-
-		this.details = undefined;
-
-		Error.captureStackTrace(this, this.constructor);
-	}
-
 	inspect() {
 		return this.stack + (this.details ? `\n${this.details}` : "");
 	}
diff --git a/lib/optimize/ChunkModuleIdRangePlugin.js b/lib/optimize/ChunkModuleIdRangePlugin.js
index 33d319747..f507e426c 100644
--- a/lib/optimize/ChunkModuleIdRangePlugin.js
+++ b/lib/optimize/ChunkModuleIdRangePlugin.js
@@ -3,35 +3,44 @@
 	Author Tobias Koppers @sokra
 */
 "use strict";
+
+const sortByIndex = (a, b) => {
+	return a.index - b.index;
+};
+
+const sortByIndex2 = (a, b) => {
+	return a.index2 - b.index2;
+};
+
 class ChunkModuleIdRangePlugin {
 	constructor(options) {
 		this.options = options;
 	}
+
 	apply(compiler) {
 		const options = this.options;
 		compiler.hooks.compilation.tap("ChunkModuleIdRangePlugin", compilation => {
 			compilation.hooks.moduleIds.tap("ChunkModuleIdRangePlugin", modules => {
-				const chunk = this.chunks.find(chunk => chunk.name === options.name);
-				if (!chunk)
+				const chunk = compilation.chunks.find(
+					chunk => chunk.name === options.name
+				);
+				if (!chunk) {
 					throw new Error(
-						"ChunkModuleIdRangePlugin: Chunk with name '" +
-							options.name +
-							"' was not found"
+						`ChunkModuleIdRangePlugin: Chunk with name '${
+							options.name
+						}"' was not found`
 					);
-				let currentId = options.start;
+				}
+
 				let chunkModules;
 				if (options.order) {
-					chunkModules = chunk.modules.slice();
+					chunkModules = Array.from(chunk.modulesIterable);
 					switch (options.order) {
 						case "index":
-							chunkModules.sort((a, b) => {
-								return a.index - b.index;
-							});
+							chunkModules.sort(sortByIndex);
 							break;
 						case "index2":
-							chunkModules.sort((a, b) => {
-								return a.index2 - b.index2;
-							});
+							chunkModules.sort(sortByIndex2);
 							break;
 						default:
 							throw new Error(
@@ -40,10 +49,11 @@ class ChunkModuleIdRangePlugin {
 					}
 				} else {
 					chunkModules = modules.filter(m => {
-						return m.chunks.includes(chunk);
+						return m.chunksIterable.has(chunk);
 					});
 				}
 
+				let currentId = options.start || 0;
 				for (let i = 0; i < chunkModules.length; i++) {
 					const m = chunkModules[i];
 					if (m.id === null) {


Test patch:
diff --git a/test/RemovedPlugins.unittest.js b/test/RemovedPlugins.unittest.js
index 1f5f2b30b..e1dfcd7e0 100644
--- a/test/RemovedPlugins.unittest.js
+++ b/test/RemovedPlugins.unittest.js
@@ -4,15 +4,14 @@ require("should");
 
 describe("removed plugin errors", () => {
 	it("should error when accessing removed plugins", () => {
-		(() => webpack.optimize.UglifyJsPlugin).should.throw(RemovedPluginError, {
-			message: /webpack\.optimize\.UglifyJsPlugin has been removed, please use config\.optimization\.minimize instead\./
-		});
+		(() => webpack.optimize.UglifyJsPlugin).should.throw(
+			RemovedPluginError,
+			/webpack\.optimize\.UglifyJsPlugin has been removed, please use config\.optimization\.minimize instead\./
+		);
 
 		(() => webpack.optimize.CommonsChunkPlugin).should.throw(
 			RemovedPluginError,
-			{
-				message: /webpack\.optimize\.CommonsChunkPlugin has been removed, please use config\.optimization\.splitChunks instead\./
-			}
+			/webpack\.optimize\.CommonsChunkPlugin has been removed, please use config\.optimization\.splitChunks instead\./
 		);
 	});
 });
diff --git a/test/statsCases/chunk-module-id-range/a.js b/test/statsCases/chunk-module-id-range/a.js
new file mode 100644
index 000000000..e94fef185
--- /dev/null
+++ b/test/statsCases/chunk-module-id-range/a.js
@@ -0,0 +1,1 @@
+export default "a";
diff --git a/test/statsCases/chunk-module-id-range/b.js b/test/statsCases/chunk-module-id-range/b.js
new file mode 100644
index 000000000..eff703ff4
--- /dev/null
+++ b/test/statsCases/chunk-module-id-range/b.js
@@ -0,0 +1,1 @@
+export default "b";
diff --git a/test/statsCases/chunk-module-id-range/c.js b/test/statsCases/chunk-module-id-range/c.js
new file mode 100644
index 000000000..5d50db5bc
--- /dev/null
+++ b/test/statsCases/chunk-module-id-range/c.js
@@ -0,0 +1,1 @@
+export default "c";
diff --git a/test/statsCases/chunk-module-id-range/d.js b/test/statsCases/chunk-module-id-range/d.js
new file mode 100644
index 000000000..987d6d7e4
--- /dev/null
+++ b/test/statsCases/chunk-module-id-range/d.js
@@ -0,0 +1,1 @@
+export default "d";
diff --git a/test/statsCases/chunk-module-id-range/e.js b/test/statsCases/chunk-module-id-range/e.js
new file mode 100644
index 000000000..d97e38b22
--- /dev/null
+++ b/test/statsCases/chunk-module-id-range/e.js
@@ -0,0 +1,1 @@
+export default "e";
diff --git a/test/statsCases/chunk-module-id-range/expected.txt b/test/statsCases/chunk-module-id-range/expected.txt
new file mode 100644
index 000000000..a34fa06a6
--- /dev/null
+++ b/test/statsCases/chunk-module-id-range/expected.txt
@@ -0,0 +1,22 @@
+Hash: 27b68d27e07b42624dae
+Time: Xms
+Built at: Thu Jan 01 1970 00:00:00 GMT
+   Asset      Size  Chunks             Chunk Names
+main2.js  3.89 KiB       0  [emitted]  main2
+main1.js  3.89 KiB       1  [emitted]  main1
+Entrypoint main1 = main1.js
+Entrypoint main2 = main2.js
+chunk    {0} main2.js (main2) 136 bytes [entry] [rendered]
+    > ./main2 main2
+   [0] ./e.js 20 bytes {0} [built]
+   [1] ./f.js 20 bytes {0} [built]
+   [2] ./main2.js 56 bytes {0} [built]
+ [100] ./d.js 20 bytes {0} {1} [built]
+ [101] ./a.js 20 bytes {0} {1} [built]
+chunk    {1} main1.js (main1) 136 bytes [entry] [rendered]
+    > ./main1 main1
+   [3] ./b.js 20 bytes {1} [built]
+   [4] ./main1.js 56 bytes {1} [built]
+ [100] ./d.js 20 bytes {0} {1} [built]
+ [101] ./a.js 20 bytes {0} {1} [built]
+ [102] ./c.js 20 bytes {1} [built]
\ No newline at end of file
diff --git a/test/statsCases/chunk-module-id-range/f.js b/test/statsCases/chunk-module-id-range/f.js
new file mode 100644
index 000000000..657d4dee8
--- /dev/null
+++ b/test/statsCases/chunk-module-id-range/f.js
@@ -0,0 +1,1 @@
+export default "f";
diff --git a/test/statsCases/chunk-module-id-range/main1.js b/test/statsCases/chunk-module-id-range/main1.js
new file mode 100644
index 000000000..e1fd20493
--- /dev/null
+++ b/test/statsCases/chunk-module-id-range/main1.js
@@ -0,0 +1,4 @@
+import "./a";
+import "./b";
+import "./c";
+import "./d";
diff --git a/test/statsCases/chunk-module-id-range/main2.js b/test/statsCases/chunk-module-id-range/main2.js
new file mode 100644
index 000000000..4b63b6243
--- /dev/null
+++ b/test/statsCases/chunk-module-id-range/main2.js
@@ -0,0 +1,4 @@
+import "./a";
+import "./d";
+import "./e";
+import "./f";
diff --git a/test/statsCases/chunk-module-id-range/webpack.config.js b/test/statsCases/chunk-module-id-range/webpack.config.js
new file mode 100644
index 000000000..621d9df44
--- /dev/null
+++ b/test/statsCases/chunk-module-id-range/webpack.config.js
@@ -0,0 +1,28 @@
+const webpack = require("../../../");
+
+module.exports = {
+	mode: "none",
+	entry: {
+		main1: "./main1",
+		main2: "./main2"
+	},
+	plugins: [
+		new webpack.optimize.ChunkModuleIdRangePlugin({
+			name: "main1",
+			start: 100,
+			end: 102
+		}),
+		new webpack.optimize.ChunkModuleIdRangePlugin({
+			name: "main2",
+			order: "index2"
+		})
+	],
+	stats: {
+		chunks: true,
+		chunkModules: true,
+		chunkOrigins: true,
+		entrypoints: true,
+		modules: false,
+		publicPath: true
+	}
+};