webpack 5.43.0 → 5.44.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of webpack might be problematic. Click here for more details.

@@ -12,7 +12,7 @@ const { resolveByProperty, cachedSetProperty } = require("./util/cleverMerge");
12
12
  /** @typedef {import("../declarations/WebpackOptions").Externals} Externals */
13
13
  /** @typedef {import("./NormalModuleFactory")} NormalModuleFactory */
14
14
 
15
- const UNSPECIFIED_EXTERNAL_TYPE_REGEXP = /^[a-z0-9]+ /;
15
+ const UNSPECIFIED_EXTERNAL_TYPE_REGEXP = /^[a-z0-9-]+ /;
16
16
  const EMPTY_RESOLVE_OPTIONS = {};
17
17
 
18
18
  // TODO webpack 6 remove this
package/lib/Template.js CHANGED
@@ -379,6 +379,7 @@ class Template {
379
379
  source.add(Template.toNormalComment(module.identifier()) + "\n");
380
380
  if (!module.shouldIsolate()) {
381
381
  source.add(runtimeSource);
382
+ source.add("\n\n");
382
383
  } else if (renderContext.runtimeTemplate.supportsArrowFunction()) {
383
384
  source.add("(() => {\n");
384
385
  if (renderContext.useStrict) source.add('\t"use strict";\n');
@@ -0,0 +1,29 @@
1
+ /*
2
+ MIT License http://www.opensource.org/licenses/mit-license.php
3
+ */
4
+
5
+ "use strict";
6
+
7
+ const RuntimeModule = require("../RuntimeModule");
8
+
9
+ class ExportWebpackRequireRuntimeModule extends RuntimeModule {
10
+ constructor() {
11
+ super("export webpack runtime", RuntimeModule.STAGE_ATTACH);
12
+ }
13
+
14
+ /**
15
+ * @returns {boolean} true, if the runtime module should get it's own scope
16
+ */
17
+ shouldIsolate() {
18
+ return false;
19
+ }
20
+
21
+ /**
22
+ * @returns {string} runtime code
23
+ */
24
+ generate() {
25
+ return "export default __webpack_require__;";
26
+ }
27
+ }
28
+
29
+ module.exports = ExportWebpackRequireRuntimeModule;
@@ -5,13 +5,18 @@
5
5
 
6
6
  "use strict";
7
7
 
8
- const { ConcatSource } = require("webpack-sources");
8
+ const { ConcatSource, RawSource } = require("webpack-sources");
9
9
  const { RuntimeGlobals } = require("..");
10
10
  const HotUpdateChunk = require("../HotUpdateChunk");
11
11
  const Template = require("../Template");
12
12
  const {
13
- getCompilationHooks
13
+ getCompilationHooks,
14
+ getChunkFilenameTemplate
14
15
  } = require("../javascript/JavascriptModulesPlugin");
16
+ const {
17
+ generateEntryStartup,
18
+ updateHashForEntryStartup
19
+ } = require("../javascript/StartupHelpers");
15
20
 
16
21
  /** @typedef {import("../Compiler")} Compiler */
17
22
 
@@ -30,8 +35,9 @@ class ModuleChunkFormatPlugin {
30
35
  (chunk, set) => {
31
36
  if (chunk.hasRuntime()) return;
32
37
  if (compilation.chunkGraph.getNumberOfEntryModules(chunk) > 0) {
33
- set.add(RuntimeGlobals.onChunksLoaded);
34
38
  set.add(RuntimeGlobals.require);
39
+ set.add(RuntimeGlobals.startupEntrypoint);
40
+ set.add(RuntimeGlobals.externalInstallChunk);
35
41
  }
36
42
  }
37
43
  );
@@ -39,7 +45,7 @@ class ModuleChunkFormatPlugin {
39
45
  hooks.renderChunk.tap(
40
46
  "ModuleChunkFormatPlugin",
41
47
  (modules, renderContext) => {
42
- const { chunk, chunkGraph } = renderContext;
48
+ const { chunk, chunkGraph, runtimeTemplate } = renderContext;
43
49
  const hotUpdateChunk =
44
50
  chunk instanceof HotUpdateChunk ? chunk : null;
45
51
  const source = new ConcatSource();
@@ -68,9 +74,84 @@ class ModuleChunkFormatPlugin {
68
74
  chunkGraph.getChunkEntryModulesWithChunkGroupIterable(chunk)
69
75
  );
70
76
  if (entries.length > 0) {
71
- throw new Error(
72
- "Entry modules in chunk is not implemented for module chunk format yet"
77
+ const runtimeChunk = entries[0][1].getRuntimeChunk();
78
+ const currentOutputName = compilation
79
+ .getPath(
80
+ getChunkFilenameTemplate(chunk, compilation.outputOptions),
81
+ {
82
+ chunk,
83
+ contentHashType: "javascript"
84
+ }
85
+ )
86
+ .split("/");
87
+ const runtimeOutputName = compilation
88
+ .getPath(
89
+ getChunkFilenameTemplate(
90
+ runtimeChunk,
91
+ compilation.outputOptions
92
+ ),
93
+ {
94
+ chunk: runtimeChunk,
95
+ contentHashType: "javascript"
96
+ }
97
+ )
98
+ .split("/");
99
+
100
+ // remove filename, we only need the directory
101
+ const outputFilename = currentOutputName.pop();
102
+
103
+ // remove common parts
104
+ while (
105
+ currentOutputName.length > 0 &&
106
+ runtimeOutputName.length > 0 &&
107
+ currentOutputName[0] === runtimeOutputName[0]
108
+ ) {
109
+ currentOutputName.shift();
110
+ runtimeOutputName.shift();
111
+ }
112
+
113
+ // create final path
114
+ const runtimePath =
115
+ (currentOutputName.length > 0
116
+ ? "../".repeat(currentOutputName.length)
117
+ : "./") + runtimeOutputName.join("/");
118
+
119
+ const entrySource = new ConcatSource();
120
+ entrySource.add(source);
121
+ entrySource.add(";\n\n// load runtime\n");
122
+ entrySource.add(
123
+ `import __webpack_require__ from ${JSON.stringify(
124
+ runtimePath
125
+ )};\n`
126
+ );
127
+ entrySource.add(
128
+ `import * as __webpack_self_exports__ from ${JSON.stringify(
129
+ "./" + outputFilename
130
+ )};\n`
131
+ );
132
+ entrySource.add(
133
+ `${RuntimeGlobals.externalInstallChunk}(__webpack_self_exports__);\n`
134
+ );
135
+ const startupSource = new RawSource(
136
+ generateEntryStartup(
137
+ chunkGraph,
138
+ runtimeTemplate,
139
+ entries,
140
+ chunk,
141
+ false
142
+ )
143
+ );
144
+ entrySource.add(
145
+ hooks.renderStartup.call(
146
+ startupSource,
147
+ entries[entries.length - 1][0],
148
+ {
149
+ ...renderContext,
150
+ inlined: false
151
+ }
152
+ )
73
153
  );
154
+ return entrySource;
74
155
  }
75
156
  }
76
157
  return source;
@@ -82,11 +163,10 @@ class ModuleChunkFormatPlugin {
82
163
  if (chunk.hasRuntime()) return;
83
164
  hash.update("ModuleChunkFormatPlugin");
84
165
  hash.update("1");
85
- // TODO
86
- // const entries = Array.from(
87
- // chunkGraph.getChunkEntryModulesWithChunkGroupIterable(chunk)
88
- // );
89
- // updateHashForEntryStartup(hash, chunkGraph, entries, chunk);
166
+ const entries = Array.from(
167
+ chunkGraph.getChunkEntryModulesWithChunkGroupIterable(chunk)
168
+ );
169
+ updateHashForEntryStartup(hash, chunkGraph, entries, chunk);
90
170
  }
91
171
  );
92
172
  }
@@ -6,6 +6,7 @@
6
6
  "use strict";
7
7
 
8
8
  const RuntimeGlobals = require("../RuntimeGlobals");
9
+ const ExportWebpackRequireRuntimeModule = require("./ExportWebpackRequireRuntimeModule");
9
10
  const ModuleChunkLoadingRuntimeModule = require("./ModuleChunkLoadingRuntimeModule");
10
11
 
11
12
  /** @typedef {import("../Compiler")} Compiler */
@@ -45,9 +46,21 @@ class ModuleChunkLoadingPlugin {
45
46
  compilation.hooks.runtimeRequirementInTree
46
47
  .for(RuntimeGlobals.baseURI)
47
48
  .tap("ModuleChunkLoadingPlugin", handler);
49
+ compilation.hooks.runtimeRequirementInTree
50
+ .for(RuntimeGlobals.externalInstallChunk)
51
+ .tap("ModuleChunkLoadingPlugin", handler);
48
52
  compilation.hooks.runtimeRequirementInTree
49
53
  .for(RuntimeGlobals.onChunksLoaded)
50
54
  .tap("ModuleChunkLoadingPlugin", handler);
55
+ compilation.hooks.runtimeRequirementInTree
56
+ .for(RuntimeGlobals.externalInstallChunk)
57
+ .tap("ModuleChunkLoadingPlugin", (chunk, set) => {
58
+ if (!isEnabledForChunk(chunk)) return;
59
+ compilation.addRuntimeModule(
60
+ chunk,
61
+ new ExportWebpackRequireRuntimeModule()
62
+ );
63
+ });
51
64
 
52
65
  compilation.hooks.runtimeRequirementInTree
53
66
  .for(RuntimeGlobals.ensureChunkHandlers)
@@ -67,6 +67,9 @@ class ModuleChunkLoadingRuntimeModule extends RuntimeModule {
67
67
  } = compilation;
68
68
  const fn = RuntimeGlobals.ensureChunkHandlers;
69
69
  const withBaseURI = this._runtimeRequirements.has(RuntimeGlobals.baseURI);
70
+ const withExternalInstallChunk = this._runtimeRequirements.has(
71
+ RuntimeGlobals.externalInstallChunk
72
+ );
70
73
  const withLoading = this._runtimeRequirements.has(
71
74
  RuntimeGlobals.ensureChunkHandlers
72
75
  );
@@ -110,6 +113,38 @@ class ModuleChunkLoadingRuntimeModule extends RuntimeModule {
110
113
  ),
111
114
  "};",
112
115
  "",
116
+ withLoading || withExternalInstallChunk
117
+ ? `var installChunk = ${runtimeTemplate.basicFunction("data", [
118
+ runtimeTemplate.destructureObject(
119
+ ["ids", "modules", "runtime"],
120
+ "data"
121
+ ),
122
+ '// add "modules" to the modules object,',
123
+ '// then flag all "ids" as loaded and fire callback',
124
+ "var moduleId, chunkId, i = 0;",
125
+ "for(moduleId in modules) {",
126
+ Template.indent([
127
+ `if(${RuntimeGlobals.hasOwnProperty}(modules, moduleId)) {`,
128
+ Template.indent(
129
+ `${RuntimeGlobals.moduleFactories}[moduleId] = modules[moduleId];`
130
+ ),
131
+ "}"
132
+ ]),
133
+ "}",
134
+ "if(runtime) runtime(__webpack_require__);",
135
+ "for(;i < ids.length; i++) {",
136
+ Template.indent([
137
+ "chunkId = ids[i];",
138
+ `if(${RuntimeGlobals.hasOwnProperty}(installedChunks, chunkId) && installedChunks[chunkId]) {`,
139
+ Template.indent("installedChunks[chunkId][0]();"),
140
+ "}",
141
+ "installedChunks[ids[i]] = 0;"
142
+ ]),
143
+ "}",
144
+ withOnChunkLoad ? `${RuntimeGlobals.onChunksLoaded}();` : ""
145
+ ])}`
146
+ : "// no install chunk",
147
+ "",
113
148
  withLoading
114
149
  ? Template.asString([
115
150
  `${fn}.j = ${runtimeTemplate.basicFunction(
@@ -137,45 +172,13 @@ class ModuleChunkLoadingRuntimeModule extends RuntimeModule {
137
172
  rootOutputDir
138
173
  )} + ${
139
174
  RuntimeGlobals.getChunkScriptFilename
140
- }(chunkId)).then(${runtimeTemplate.basicFunction(
141
- "data",
175
+ }(chunkId)).then(installChunk, ${runtimeTemplate.basicFunction(
176
+ "e",
142
177
  [
143
- runtimeTemplate.destructureObject(
144
- ["ids", "modules", "runtime"],
145
- "data"
146
- ),
147
- '// add "modules" to the modules object,',
148
- '// then flag all "ids" as loaded and fire callback',
149
- "var moduleId, chunkId, i = 0;",
150
- "for(moduleId in modules) {",
151
- Template.indent([
152
- `if(${RuntimeGlobals.hasOwnProperty}(modules, moduleId)) {`,
153
- Template.indent(
154
- `${RuntimeGlobals.moduleFactories}[moduleId] = modules[moduleId];`
155
- ),
156
- "}"
157
- ]),
158
- "}",
159
- "if(runtime) runtime(__webpack_require__);",
160
- "for(;i < ids.length; i++) {",
161
- Template.indent([
162
- "chunkId = ids[i];",
163
- `if(${RuntimeGlobals.hasOwnProperty}(installedChunks, chunkId) && installedChunks[chunkId]) {`,
164
- Template.indent(
165
- "installedChunks[chunkId][0]();"
166
- ),
167
- "}",
168
- "installedChunks[ids[i]] = 0;"
169
- ]),
170
- "}",
171
- withOnChunkLoad
172
- ? `${RuntimeGlobals.onChunksLoaded}();`
173
- : ""
178
+ "if(installedChunks[chunkId] !== 0) installedChunks[chunkId] = undefined;",
179
+ "throw e;"
174
180
  ]
175
- )}, ${runtimeTemplate.basicFunction("e", [
176
- "if(installedChunks[chunkId] !== 0) installedChunks[chunkId] = undefined;",
177
- "throw e;"
178
- ])});`,
181
+ )});`,
179
182
  `var promise = Promise.race([promise, new Promise(${runtimeTemplate.expressionFunction(
180
183
  `installedChunkData = installedChunks[chunkId] = [resolve]`,
181
184
  "resolve"
@@ -193,6 +196,12 @@ class ModuleChunkLoadingRuntimeModule extends RuntimeModule {
193
196
  ])
194
197
  : "// no chunk on demand loading",
195
198
  "",
199
+ withExternalInstallChunk
200
+ ? Template.asString([
201
+ `${RuntimeGlobals.externalInstallChunk} = installChunk;`
202
+ ])
203
+ : "// no external install chunk",
204
+ "",
196
205
  withOnChunkLoad
197
206
  ? `${
198
207
  RuntimeGlobals.onChunksLoaded
@@ -20,7 +20,7 @@ module.exports = (compiler, client, callback) => {
20
20
  const activeModules = new Map();
21
21
  const prefix = "/lazy-compilation-using-";
22
22
 
23
- const server = http.createServer((req, res) => {
23
+ const requestListener = (req, res) => {
24
24
  const keys = req.url.slice(prefix.length).split("@");
25
25
  req.socket.on("close", () => {
26
26
  setTimeout(() => {
@@ -51,7 +51,8 @@ module.exports = (compiler, client, callback) => {
51
51
  }
52
52
  }
53
53
  if (moduleActivated && compiler.watching) compiler.watching.invalidate();
54
- });
54
+ };
55
+ const server = http.createServer(requestListener);
55
56
  let isClosing = false;
56
57
  /** @type {Set<import("net").Socket>} */
57
58
  const sockets = new Set();
@@ -78,6 +79,8 @@ module.exports = (compiler, client, callback) => {
78
79
  callback(null, {
79
80
  dispose(callback) {
80
81
  isClosing = true;
82
+ // Removing the listener is a workaround for a memory leak in node.js
83
+ server.off("request", requestListener);
81
84
  server.close(err => {
82
85
  callback(err);
83
86
  });
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "webpack",
3
- "version": "5.43.0",
3
+ "version": "5.44.0",
4
4
  "author": "Tobias Koppers @sokra",
5
5
  "description": "Packs CommonJs/AMD modules for the browser. Allows to split your codebase into multiple bundles, which can be loaded on demand. Support loaders to preprocess files, i.e. json, jsx, es7, css, less, ... and your custom stuff.",
6
6
  "license": "MIT",
7
7
  "dependencies": {
8
8
  "@types/eslint-scope": "^3.7.0",
9
- "@types/estree": "^0.0.49",
9
+ "@types/estree": "^0.0.50",
10
10
  "@webassemblyjs/ast": "1.11.1",
11
11
  "@webassemblyjs/wasm-edit": "1.11.1",
12
12
  "@webassemblyjs/wasm-parser": "1.11.1",
@@ -131,10 +131,10 @@
131
131
  ],
132
132
  "scripts": {
133
133
  "setup": "node ./setup/setup.js",
134
- "test": "node --max-old-space-size=4096 --experimental-vm-modules --trace-deprecation node_modules/jest-cli/bin/jest",
134
+ "test": "node --expose-gc --max-old-space-size=4096 --experimental-vm-modules --trace-deprecation node_modules/jest-cli/bin/jest --logHeapUsage",
135
135
  "test:update-snapshots": "yarn jest -u",
136
- "test:integration": "node --max-old-space-size=4096 --experimental-vm-modules --trace-deprecation node_modules/jest-cli/bin/jest --testMatch \"<rootDir>/test/*.test.js\"",
137
- "test:basic": "node --max-old-space-size=4096 --experimental-vm-modules --trace-deprecation node_modules/jest-cli/bin/jest --testMatch \"<rootDir>/te{st/TestCasesNormal,st/StatsTestCases,st/ConfigTestCases}.test.js\"",
136
+ "test:integration": "node --expose-gc --max-old-space-size=4096 --experimental-vm-modules --trace-deprecation node_modules/jest-cli/bin/jest --logHeapUsage --testMatch \"<rootDir>/test/*.test.js\"",
137
+ "test:basic": "node --expose-gc --max-old-space-size=4096 --experimental-vm-modules --trace-deprecation node_modules/jest-cli/bin/jest --logHeapUsage --testMatch \"<rootDir>/te{st/TestCasesNormal,st/StatsTestCases,st/ConfigTestCases}.test.js\"",
138
138
  "test:unit": "node --max-old-space-size=4096 --experimental-vm-modules --trace-deprecation node_modules/jest-cli/bin/jest --testMatch \"<rootDir>/test/*.unittest.js\"",
139
139
  "travis:integration": "yarn cover:integration --ci $JEST",
140
140
  "travis:basic": "yarn cover:basic --ci $JEST",
@@ -165,9 +165,9 @@
165
165
  "benchmark": "node --max-old-space-size=4096 --experimental-vm-modules --trace-deprecation node_modules/jest-cli/bin/jest --testMatch \"<rootDir>/test/*.benchmark.js\" --runInBand",
166
166
  "cover": "yarn cover:all && yarn cover:report",
167
167
  "cover:clean": "rimraf .nyc_output coverage",
168
- "cover:all": "node --max-old-space-size=4096 --experimental-vm-modules node_modules/jest-cli/bin/jest --coverage",
169
- "cover:basic": "node --max-old-space-size=4096 --experimental-vm-modules node_modules/jest-cli/bin/jest --testMatch \"<rootDir>/te{st/TestCasesNormal,st/StatsTestCases,st/ConfigTestCases}.test.js\" --coverage",
170
- "cover:integration": "node --max-old-space-size=4096 --experimental-vm-modules node_modules/jest-cli/bin/jest --testMatch \"<rootDir>/test/*.test.js\" --coverage",
168
+ "cover:all": "node --expose-gc --max-old-space-size=4096 --experimental-vm-modules node_modules/jest-cli/bin/jest --logHeapUsage --coverage",
169
+ "cover:basic": "node --expose-gc --max-old-space-size=4096 --experimental-vm-modules node_modules/jest-cli/bin/jest --logHeapUsage --testMatch \"<rootDir>/te{st/TestCasesNormal,st/StatsTestCases,st/ConfigTestCases}.test.js\" --coverage",
170
+ "cover:integration": "node --expose-gc --max-old-space-size=4096 --experimental-vm-modules node_modules/jest-cli/bin/jest --logHeapUsage --testMatch \"<rootDir>/test/*.test.js\" --coverage",
171
171
  "cover:unit": "node --max-old-space-size=4096 --experimental-vm-modules node_modules/jest-cli/bin/jest --testMatch \"<rootDir>/test/*.unittest.js\" --coverage",
172
172
  "cover:types": "node node_modules/tooling/type-coverage",
173
173
  "cover:merge": "nyc merge .nyc_output coverage/coverage-nyc.json && rimraf .nyc_output",