webpack 5.65.0 → 5.66.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.

Files changed (60) hide show
  1. package/lib/CacheFacade.js +2 -9
  2. package/lib/Chunk.js +2 -0
  3. package/lib/Compilation.js +79 -38
  4. package/lib/Dependency.js +10 -0
  5. package/lib/DependencyTemplate.js +9 -0
  6. package/lib/ExternalModule.js +92 -52
  7. package/lib/Generator.js +2 -0
  8. package/lib/Module.js +24 -1
  9. package/lib/ModuleFilenameHelpers.js +5 -1
  10. package/lib/NormalModule.js +3 -1
  11. package/lib/RuntimeGlobals.js +11 -1
  12. package/lib/RuntimePlugin.js +25 -0
  13. package/lib/RuntimeTemplate.js +21 -0
  14. package/lib/Template.js +2 -1
  15. package/lib/Watching.js +1 -1
  16. package/lib/WebpackOptionsApply.js +43 -2
  17. package/lib/asset/AssetGenerator.js +3 -1
  18. package/lib/asset/RawDataUrlModule.js +145 -0
  19. package/lib/config/defaults.js +59 -3
  20. package/lib/css/CssGenerator.js +106 -0
  21. package/lib/css/CssLoadingRuntimeModule.js +393 -0
  22. package/lib/css/CssModulesPlugin.js +444 -0
  23. package/lib/css/CssParser.js +618 -0
  24. package/lib/css/walkCssTokens.js +659 -0
  25. package/lib/dependencies/CssExportDependency.js +85 -0
  26. package/lib/dependencies/CssImportDependency.js +75 -0
  27. package/lib/dependencies/CssLocalIdentifierDependency.js +119 -0
  28. package/lib/dependencies/CssSelfLocalIdentifierDependency.js +101 -0
  29. package/lib/dependencies/CssUrlDependency.js +132 -0
  30. package/lib/dependencies/URLDependency.js +3 -8
  31. package/lib/esm/ModuleChunkLoadingRuntimeModule.js +1 -1
  32. package/lib/hmr/lazyCompilationBackend.js +3 -1
  33. package/lib/javascript/JavascriptGenerator.js +1 -0
  34. package/lib/javascript/StartupHelpers.js +3 -3
  35. package/lib/library/AssignLibraryPlugin.js +26 -3
  36. package/lib/library/EnableLibraryPlugin.js +11 -0
  37. package/lib/node/ReadFileChunkLoadingRuntimeModule.js +1 -1
  38. package/lib/node/RequireChunkLoadingRuntimeModule.js +1 -1
  39. package/lib/optimize/ConcatenatedModule.js +10 -4
  40. package/lib/util/hash/xxhash64.js +2 -2
  41. package/lib/util/internalSerializables.js +11 -0
  42. package/lib/web/JsonpChunkLoadingRuntimeModule.js +2 -2
  43. package/lib/webworker/ImportScriptsChunkLoadingRuntimeModule.js +1 -1
  44. package/package.json +2 -2
  45. package/schemas/WebpackOptions.check.js +1 -1
  46. package/schemas/WebpackOptions.json +55 -1
  47. package/schemas/plugins/container/ContainerPlugin.check.js +1 -1
  48. package/schemas/plugins/container/ContainerPlugin.json +2 -1
  49. package/schemas/plugins/container/ContainerReferencePlugin.check.js +1 -1
  50. package/schemas/plugins/container/ContainerReferencePlugin.json +1 -0
  51. package/schemas/plugins/container/ExternalsType.check.js +1 -1
  52. package/schemas/plugins/container/ModuleFederationPlugin.check.js +1 -1
  53. package/schemas/plugins/container/ModuleFederationPlugin.json +3 -1
  54. package/schemas/plugins/css/CssGeneratorOptions.check.d.ts +7 -0
  55. package/schemas/plugins/css/CssGeneratorOptions.check.js +6 -0
  56. package/schemas/plugins/css/CssGeneratorOptions.json +3 -0
  57. package/schemas/plugins/css/CssParserOptions.check.d.ts +7 -0
  58. package/schemas/plugins/css/CssParserOptions.check.js +6 -0
  59. package/schemas/plugins/css/CssParserOptions.json +3 -0
  60. package/types.d.ts +86 -2
@@ -118,11 +118,47 @@ class WebpackOptionsApply extends OptionsApply {
118
118
  if (options.externalsPresets.webAsync) {
119
119
  //@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
120
120
  const ExternalsPlugin = require("./ExternalsPlugin");
121
- new ExternalsPlugin("import", /^(https?:\/\/|std:)/).apply(compiler);
121
+ new ExternalsPlugin(
122
+ "import",
123
+ options.experiments.css
124
+ ? ({ request, dependencyType }, callback) => {
125
+ if (dependencyType === "url") {
126
+ if (/^(\/\/|https?:\/\/)/.test(request))
127
+ return callback(null, `asset ${request}`);
128
+ } else if (dependencyType === "css-import") {
129
+ if (/^(\/\/|https?:\/\/)/.test(request))
130
+ return callback(null, `css-import ${request}`);
131
+ } else if (/^(\/\/|https?:\/\/|std:)/.test(request)) {
132
+ if (/^\.css(\?|$)/.test(request))
133
+ return callback(null, `css-import ${request}`);
134
+ return callback(null, `import ${request}`);
135
+ }
136
+ callback();
137
+ }
138
+ : /^(\/\/|https?:\/\/|std:)/
139
+ ).apply(compiler);
122
140
  } else if (options.externalsPresets.web) {
123
141
  //@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
124
142
  const ExternalsPlugin = require("./ExternalsPlugin");
125
- new ExternalsPlugin("module", /^(https?:\/\/|std:)/).apply(compiler);
143
+ new ExternalsPlugin(
144
+ "module",
145
+ options.experiments.css
146
+ ? ({ request, dependencyType }, callback) => {
147
+ if (dependencyType === "url") {
148
+ if (/^(\/\/|https?:\/\/)/.test(request))
149
+ return callback(null, `asset ${request}`);
150
+ } else if (dependencyType === "css-import") {
151
+ if (/^(\/\/|https?:\/\/)/.test(request))
152
+ return callback(null, `css-import ${request}`);
153
+ } else if (/^(\/\/|https?:\/\/|std:)/.test(request)) {
154
+ if (/^\.css(\?|$)/.test(request))
155
+ return callback(null, `css-import ${request}`);
156
+ return callback(null, `module ${request}`);
157
+ }
158
+ callback();
159
+ }
160
+ : /^(\/\/|https?:\/\/|std:)/
161
+ ).apply(compiler);
126
162
  }
127
163
 
128
164
  new ChunkPrefetchPreloadPlugin().apply(compiler);
@@ -253,6 +289,11 @@ class WebpackOptionsApply extends OptionsApply {
253
289
  }).apply(compiler);
254
290
  }
255
291
 
292
+ if (options.experiments.css) {
293
+ const CssModulesPlugin = require("./css/CssModulesPlugin");
294
+ new CssModulesPlugin().apply(compiler);
295
+ }
296
+
256
297
  if (options.experiments.lazyCompilation) {
257
298
  const LazyCompilationPlugin = require("./hmr/LazyCompilationPlugin");
258
299
  const lazyOptions =
@@ -49,7 +49,7 @@ const mergeAssetInfo = (a, b) => {
49
49
  case "immutable":
50
50
  case "development":
51
51
  case "hotModuleReplacement":
52
- case "javascriptModule ":
52
+ case "javascriptModule":
53
53
  result[key] = a[key] || b[key];
54
54
  break;
55
55
  case "related":
@@ -191,6 +191,8 @@ class AssetGenerator extends Generator {
191
191
  encoding ? `;${encoding}` : ""
192
192
  },${encodedContent}`;
193
193
  }
194
+ const data = getData();
195
+ data.set("url", encodedSource);
194
196
  return new RawSource(
195
197
  `${RuntimeGlobals.module}.exports = ${JSON.stringify(
196
198
  encodedSource
@@ -0,0 +1,145 @@
1
+ /*
2
+ MIT License http://www.opensource.org/licenses/mit-license.php
3
+ Author Tobias Koppers @sokra
4
+ */
5
+
6
+ "use strict";
7
+
8
+ const { RawSource } = require("webpack-sources");
9
+ const Module = require("../Module");
10
+ const RuntimeGlobals = require("../RuntimeGlobals");
11
+ const makeSerializable = require("../util/makeSerializable");
12
+
13
+ /** @typedef {import("../../declarations/WebpackOptions").WebpackOptionsNormalized} WebpackOptions */
14
+ /** @typedef {import("../Compilation")} Compilation */
15
+ /** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
16
+ /** @typedef {import("../Module").CodeGenerationContext} CodeGenerationContext */
17
+ /** @typedef {import("../Module").CodeGenerationResult} CodeGenerationResult */
18
+ /** @typedef {import("../Module").NeedBuildContext} NeedBuildContext */
19
+ /** @typedef {import("../RequestShortener")} RequestShortener */
20
+ /** @typedef {import("../ResolverFactory").ResolverWithOptions} ResolverWithOptions */
21
+ /** @typedef {import("../WebpackError")} WebpackError */
22
+ /** @typedef {import("../util/Hash")} Hash */
23
+ /** @typedef {import("../util/fs").InputFileSystem} InputFileSystem */
24
+
25
+ const TYPES = new Set(["javascript"]);
26
+
27
+ class RawDataUrlModule extends Module {
28
+ /**
29
+ * @param {string} url raw url
30
+ * @param {string} identifier unique identifier
31
+ * @param {string=} readableIdentifier readable identifier
32
+ */
33
+ constructor(url, identifier, readableIdentifier) {
34
+ super("asset/raw-data-url", null);
35
+ this.url = url;
36
+ this.identifierStr = identifier || this.url;
37
+ this.readableIdentifierStr = readableIdentifier || this.identifierStr;
38
+ }
39
+
40
+ /**
41
+ * @returns {Set<string>} types available (do not mutate)
42
+ */
43
+ getSourceTypes() {
44
+ return TYPES;
45
+ }
46
+
47
+ /**
48
+ * @returns {string} a unique identifier of the module
49
+ */
50
+ identifier() {
51
+ return this.identifierStr;
52
+ }
53
+
54
+ /**
55
+ * @param {string=} type the source type for which the size should be estimated
56
+ * @returns {number} the estimated size of the module (must be non-zero)
57
+ */
58
+ size(type) {
59
+ return Math.max(1, this.url.length);
60
+ }
61
+
62
+ /**
63
+ * @param {RequestShortener} requestShortener the request shortener
64
+ * @returns {string} a user readable identifier of the module
65
+ */
66
+ readableIdentifier(requestShortener) {
67
+ return requestShortener.shorten(this.readableIdentifierStr);
68
+ }
69
+
70
+ /**
71
+ * @param {NeedBuildContext} context context info
72
+ * @param {function(WebpackError=, boolean=): void} callback callback function, returns true, if the module needs a rebuild
73
+ * @returns {void}
74
+ */
75
+ needBuild(context, callback) {
76
+ return callback(null, !this.buildMeta);
77
+ }
78
+
79
+ /**
80
+ * @param {WebpackOptions} options webpack options
81
+ * @param {Compilation} compilation the compilation
82
+ * @param {ResolverWithOptions} resolver the resolver
83
+ * @param {InputFileSystem} fs the file system
84
+ * @param {function(WebpackError=): void} callback callback function
85
+ * @returns {void}
86
+ */
87
+ build(options, compilation, resolver, fs, callback) {
88
+ this.buildMeta = {};
89
+ this.buildInfo = {
90
+ cacheable: true
91
+ };
92
+ callback();
93
+ }
94
+
95
+ /**
96
+ * @param {CodeGenerationContext} context context for code generation
97
+ * @returns {CodeGenerationResult} result
98
+ */
99
+ codeGeneration(context) {
100
+ const sources = new Map();
101
+ sources.set(
102
+ "javascript",
103
+ new RawSource(`module.exports = ${JSON.stringify(this.url)};`)
104
+ );
105
+ const data = new Map();
106
+ data.set("url", this.url);
107
+ const runtimeRequirements = new Set();
108
+ runtimeRequirements.add(RuntimeGlobals.module);
109
+ return { sources, runtimeRequirements, data };
110
+ }
111
+
112
+ /**
113
+ * @param {Hash} hash the hash used to track dependencies
114
+ * @param {UpdateHashContext} context context
115
+ * @returns {void}
116
+ */
117
+ updateHash(hash, context) {
118
+ hash.update(this.url);
119
+ super.updateHash(hash, context);
120
+ }
121
+
122
+ serialize(context) {
123
+ const { write } = context;
124
+
125
+ write(this.url);
126
+ write(this.identifierStr);
127
+ write(this.readableIdentifierStr);
128
+
129
+ super.serialize(context);
130
+ }
131
+
132
+ deserialize(context) {
133
+ const { read } = context;
134
+
135
+ this.url = read();
136
+ this.identifierStr = read();
137
+ this.readableIdentifierStr = read();
138
+
139
+ super.deserialize(context);
140
+ }
141
+ }
142
+
143
+ makeSerializable(RawDataUrlModule, "webpack/lib/asset/RawDataUrlModule");
144
+
145
+ module.exports = RawDataUrlModule;
@@ -184,6 +184,7 @@ const applyWebpackOptionsDefaults = options => {
184
184
  cache,
185
185
  syncWebAssembly: options.experiments.syncWebAssembly,
186
186
  asyncWebAssembly: options.experiments.asyncWebAssembly,
187
+ css: options.experiments.css,
187
188
  futureDefaults
188
189
  });
189
190
 
@@ -239,6 +240,7 @@ const applyWebpackOptionsDefaults = options => {
239
240
  applyOptimizationDefaults(options.optimization, {
240
241
  development,
241
242
  production,
243
+ css: options.experiments.css,
242
244
  records: !!(options.recordsInputPath || options.recordsOutputPath)
243
245
  });
244
246
 
@@ -276,6 +278,7 @@ const applyExperimentsDefaults = (experiments, { production, development }) => {
276
278
  D(experiments, "lazyCompilation", undefined);
277
279
  D(experiments, "buildHttp", undefined);
278
280
  D(experiments, "cacheUnaffected", experiments.futureDefaults);
281
+ D(experiments, "css", experiments.futureDefaults);
279
282
 
280
283
  if (typeof experiments.buildHttp === "object") {
281
284
  D(experiments.buildHttp, "frozen", production);
@@ -458,12 +461,13 @@ const applyJavascriptParserOptionsDefaults = (
458
461
  * @param {boolean} options.cache is caching enabled
459
462
  * @param {boolean} options.syncWebAssembly is syncWebAssembly enabled
460
463
  * @param {boolean} options.asyncWebAssembly is asyncWebAssembly enabled
464
+ * @param {boolean} options.css is css enabled
461
465
  * @param {boolean} options.futureDefaults is future defaults enabled
462
466
  * @returns {void}
463
467
  */
464
468
  const applyModuleDefaults = (
465
469
  module,
466
- { cache, syncWebAssembly, asyncWebAssembly, futureDefaults }
470
+ { cache, syncWebAssembly, asyncWebAssembly, css, futureDefaults }
467
471
  ) => {
468
472
  if (cache) {
469
473
  D(module, "unsafeCache", module => {
@@ -587,6 +591,41 @@ const applyModuleDefaults = (
587
591
  ...wasm
588
592
  });
589
593
  }
594
+ if (css) {
595
+ const cssRule = {
596
+ type: "css",
597
+ resolve: {
598
+ fullySpecified: true,
599
+ preferRelative: true
600
+ }
601
+ };
602
+ const cssModulesRule = {
603
+ type: "css/module",
604
+ resolve: {
605
+ fullySpecified: true
606
+ }
607
+ };
608
+ rules.push({
609
+ test: /\.css$/i,
610
+ oneOf: [
611
+ {
612
+ test: /\.module\.css$/i,
613
+ ...cssModulesRule
614
+ },
615
+ {
616
+ ...cssRule
617
+ }
618
+ ]
619
+ });
620
+ rules.push({
621
+ mimetype: "text/css+module",
622
+ ...cssModulesRule
623
+ });
624
+ rules.push({
625
+ mimetype: "text/css",
626
+ ...cssRule
627
+ });
628
+ }
590
629
  rules.push(
591
630
  {
592
631
  dependency: "url",
@@ -692,6 +731,20 @@ const applyOutputDefaults = (
692
731
  }
693
732
  return output.module ? "[id].mjs" : "[id].js";
694
733
  });
734
+ F(output, "cssFilename", () => {
735
+ const filename = output.filename;
736
+ if (typeof filename !== "function") {
737
+ return filename.replace(/\.[mc]?js(\?|$)/, ".css$1");
738
+ }
739
+ return "[id].css";
740
+ });
741
+ F(output, "cssChunkFilename", () => {
742
+ const chunkFilename = output.chunkFilename;
743
+ if (typeof chunkFilename !== "function") {
744
+ return chunkFilename.replace(/\.[mc]?js(\?|$)/, ".css$1");
745
+ }
746
+ return "[id].css";
747
+ });
695
748
  D(output, "assetModuleFilename", "[hash][ext][query]");
696
749
  D(output, "webassemblyModuleFilename", "[hash].module.wasm");
697
750
  D(output, "compareBeforeEmit", true);
@@ -1030,12 +1083,13 @@ const applyPerformanceDefaults = (performance, { production }) => {
1030
1083
  * @param {Object} options options
1031
1084
  * @param {boolean} options.production is production
1032
1085
  * @param {boolean} options.development is development
1086
+ * @param {boolean} options.css is css enabled
1033
1087
  * @param {boolean} options.records using records
1034
1088
  * @returns {void}
1035
1089
  */
1036
1090
  const applyOptimizationDefaults = (
1037
1091
  optimization,
1038
- { production, development, records }
1092
+ { production, development, css, records }
1039
1093
  ) => {
1040
1094
  D(optimization, "removeAvailableModules", false);
1041
1095
  D(optimization, "removeEmptyChunks", true);
@@ -1086,7 +1140,9 @@ const applyOptimizationDefaults = (
1086
1140
  });
1087
1141
  const { splitChunks } = optimization;
1088
1142
  if (splitChunks) {
1089
- A(splitChunks, "defaultSizeTypes", () => ["javascript", "unknown"]);
1143
+ A(splitChunks, "defaultSizeTypes", () =>
1144
+ css ? ["javascript", "css", "unknown"] : ["javascript", "unknown"]
1145
+ );
1090
1146
  D(splitChunks, "hidePathInfo", production);
1091
1147
  D(splitChunks, "chunks", "async");
1092
1148
  D(splitChunks, "usedExports", optimization.usedExports === true);
@@ -0,0 +1,106 @@
1
+ /*
2
+ MIT License http://www.opensource.org/licenses/mit-license.php
3
+ Author Sergey Melyukov @smelukov
4
+ */
5
+
6
+ "use strict";
7
+
8
+ const { ReplaceSource } = require("webpack-sources");
9
+ const Generator = require("../Generator");
10
+ const InitFragment = require("../InitFragment");
11
+
12
+ /** @typedef {import("webpack-sources").Source} Source */
13
+ /** @typedef {import("../Dependency")} Dependency */
14
+ /** @typedef {import("../Generator").GenerateContext} GenerateContext */
15
+ /** @typedef {import("../Generator").UpdateHashContext} UpdateHashContext */
16
+ /** @typedef {import("../NormalModule")} NormalModule */
17
+ /** @typedef {import("../util/Hash")} Hash */
18
+
19
+ const TYPES = new Set(["css"]);
20
+
21
+ class CssGenerator extends Generator {
22
+ constructor() {
23
+ super();
24
+ }
25
+
26
+ /**
27
+ * @param {NormalModule} module module for which the code should be generated
28
+ * @param {GenerateContext} generateContext context for generate
29
+ * @returns {Source} generated code
30
+ */
31
+ generate(module, generateContext) {
32
+ const originalSource = module.originalSource();
33
+ const source = new ReplaceSource(originalSource);
34
+ const initFragments = [];
35
+ const cssExports = new Map();
36
+
37
+ const templateContext = {
38
+ runtimeTemplate: generateContext.runtimeTemplate,
39
+ dependencyTemplates: generateContext.dependencyTemplates,
40
+ moduleGraph: generateContext.moduleGraph,
41
+ chunkGraph: generateContext.chunkGraph,
42
+ module,
43
+ runtime: generateContext.runtime,
44
+ runtimeRequirements: generateContext.runtimeRequirements,
45
+ concatenationScope: generateContext.concatenationScope,
46
+ codeGenerationResults: generateContext.codeGenerationResults,
47
+ initFragments,
48
+ cssExports
49
+ };
50
+
51
+ const handleDependency = dependency => {
52
+ const constructor = /** @type {new (...args: any[]) => Dependency} */ (
53
+ dependency.constructor
54
+ );
55
+ const template = generateContext.dependencyTemplates.get(constructor);
56
+ if (!template) {
57
+ throw new Error(
58
+ "No template for dependency: " + dependency.constructor.name
59
+ );
60
+ }
61
+
62
+ template.apply(dependency, source, templateContext);
63
+ };
64
+ module.dependencies.forEach(handleDependency);
65
+ if (module.presentationalDependencies !== undefined)
66
+ module.presentationalDependencies.forEach(handleDependency);
67
+
68
+ if (cssExports.size > 0) {
69
+ const data = generateContext.getData();
70
+ data.set("css-exports", cssExports);
71
+ }
72
+
73
+ return InitFragment.addToSource(source, initFragments, generateContext);
74
+ }
75
+
76
+ /**
77
+ * @param {NormalModule} module fresh module
78
+ * @returns {Set<string>} available types (do not mutate)
79
+ */
80
+ getTypes(module) {
81
+ return TYPES;
82
+ }
83
+
84
+ /**
85
+ * @param {NormalModule} module the module
86
+ * @param {string=} type source type
87
+ * @returns {number} estimate size of the module
88
+ */
89
+ getSize(module, type) {
90
+ const originalSource = module.originalSource();
91
+
92
+ if (!originalSource) {
93
+ return 0;
94
+ }
95
+
96
+ return originalSource.size();
97
+ }
98
+
99
+ /**
100
+ * @param {Hash} hash hash that will be modified
101
+ * @param {UpdateHashContext} updateHashContext context for updating hash
102
+ */
103
+ updateHash(hash, { module }) {}
104
+ }
105
+
106
+ module.exports = CssGenerator;