webpack 5.59.0 → 5.71.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 (209) hide show
  1. package/README.md +1 -7
  2. package/hot/lazy-compilation-node.js +3 -1
  3. package/hot/poll.js +1 -1
  4. package/hot/signal.js +1 -1
  5. package/lib/APIPlugin.js +33 -0
  6. package/lib/BannerPlugin.js +10 -4
  7. package/lib/Cache.js +1 -1
  8. package/lib/CacheFacade.js +4 -11
  9. package/lib/Chunk.js +6 -3
  10. package/lib/ChunkGraph.js +1 -2
  11. package/lib/ChunkGroup.js +1 -1
  12. package/lib/CleanPlugin.js +81 -20
  13. package/lib/Compilation.js +179 -91
  14. package/lib/Compiler.js +86 -17
  15. package/lib/ConstPlugin.js +2 -2
  16. package/lib/ContextModule.js +142 -51
  17. package/lib/ContextModuleFactory.js +65 -25
  18. package/lib/DelegatedModule.js +1 -1
  19. package/lib/DelegatedModuleFactoryPlugin.js +1 -1
  20. package/lib/Dependency.js +17 -0
  21. package/lib/DependencyTemplate.js +9 -0
  22. package/lib/DependencyTemplates.js +1 -1
  23. package/lib/DllModule.js +1 -1
  24. package/lib/EntryOptionPlugin.js +2 -0
  25. package/lib/ErrorHelpers.js +2 -2
  26. package/lib/EvalDevToolModulePlugin.js +16 -1
  27. package/lib/EvalSourceMapDevToolPlugin.js +18 -1
  28. package/lib/ExportsInfo.js +4 -4
  29. package/lib/ExternalModule.js +94 -54
  30. package/lib/ExternalModuleFactoryPlugin.js +5 -5
  31. package/lib/FileSystemInfo.js +89 -44
  32. package/lib/Generator.js +3 -0
  33. package/lib/HookWebpackError.js +1 -1
  34. package/lib/HotModuleReplacementPlugin.js +3 -1
  35. package/lib/LoaderOptionsPlugin.js +1 -1
  36. package/lib/Module.js +27 -4
  37. package/lib/ModuleFilenameHelpers.js +8 -4
  38. package/lib/ModuleHashingError.js +29 -0
  39. package/lib/MultiCompiler.js +1 -1
  40. package/lib/MultiWatching.js +1 -1
  41. package/lib/NodeStuffPlugin.js +10 -0
  42. package/lib/NormalModule.js +41 -26
  43. package/lib/NormalModuleFactory.js +42 -37
  44. package/lib/ProgressPlugin.js +4 -5
  45. package/lib/RawModule.js +1 -1
  46. package/lib/RuntimeGlobals.js +29 -1
  47. package/lib/RuntimeModule.js +1 -1
  48. package/lib/RuntimePlugin.js +77 -1
  49. package/lib/RuntimeTemplate.js +114 -2
  50. package/lib/Template.js +2 -1
  51. package/lib/TemplatedPathPlugin.js +48 -23
  52. package/lib/WatchIgnorePlugin.js +19 -7
  53. package/lib/Watching.js +33 -19
  54. package/lib/WebpackOptionsApply.js +57 -11
  55. package/lib/asset/AssetGenerator.js +193 -63
  56. package/lib/asset/AssetModulesPlugin.js +3 -0
  57. package/lib/asset/RawDataUrlModule.js +148 -0
  58. package/lib/async-modules/AwaitDependenciesInitFragment.js +4 -4
  59. package/lib/buildChunkGraph.js +36 -6
  60. package/lib/cache/PackFileCacheStrategy.js +7 -4
  61. package/lib/cache/ResolverCachePlugin.js +90 -29
  62. package/lib/cli.js +44 -3
  63. package/lib/config/browserslistTargetHandler.js +41 -6
  64. package/lib/config/defaults.js +115 -19
  65. package/lib/config/normalization.js +9 -0
  66. package/lib/config/target.js +10 -0
  67. package/lib/container/ContainerEntryModule.js +8 -5
  68. package/lib/container/FallbackModule.js +4 -4
  69. package/lib/container/RemoteModule.js +4 -2
  70. package/lib/css/CssExportsGenerator.js +139 -0
  71. package/lib/css/CssGenerator.js +109 -0
  72. package/lib/css/CssLoadingRuntimeModule.js +440 -0
  73. package/lib/css/CssModulesPlugin.js +462 -0
  74. package/lib/css/CssParser.js +618 -0
  75. package/lib/css/walkCssTokens.js +659 -0
  76. package/lib/debug/ProfilingPlugin.js +24 -21
  77. package/lib/dependencies/AMDRequireDependency.js +6 -6
  78. package/lib/dependencies/CommonJsExportsParserPlugin.js +1 -2
  79. package/lib/dependencies/CommonJsFullRequireDependency.js +5 -1
  80. package/lib/dependencies/CommonJsImportsParserPlugin.js +5 -3
  81. package/lib/dependencies/CommonJsRequireContextDependency.js +5 -1
  82. package/lib/dependencies/ContextDependency.js +1 -0
  83. package/lib/dependencies/ContextDependencyHelpers.js +3 -3
  84. package/lib/dependencies/ContextDependencyTemplateAsRequireCall.js +4 -1
  85. package/lib/dependencies/ContextElementDependency.js +41 -3
  86. package/lib/dependencies/CreateScriptUrlDependency.js +12 -0
  87. package/lib/dependencies/CssExportDependency.js +85 -0
  88. package/lib/dependencies/CssImportDependency.js +75 -0
  89. package/lib/dependencies/CssLocalIdentifierDependency.js +119 -0
  90. package/lib/dependencies/CssSelfLocalIdentifierDependency.js +101 -0
  91. package/lib/dependencies/CssUrlDependency.js +132 -0
  92. package/lib/dependencies/ExportsInfoDependency.js +6 -0
  93. package/lib/dependencies/HarmonyAcceptImportDependency.js +5 -3
  94. package/lib/dependencies/HarmonyCompatibilityDependency.js +5 -5
  95. package/lib/dependencies/HarmonyEvaluatedImportSpecifierDependency.js +95 -0
  96. package/lib/dependencies/HarmonyExportDependencyParserPlugin.js +12 -3
  97. package/lib/dependencies/HarmonyExportImportedSpecifierDependency.js +25 -17
  98. package/lib/dependencies/HarmonyExportInitFragment.js +4 -1
  99. package/lib/dependencies/HarmonyImportDependency.js +23 -0
  100. package/lib/dependencies/HarmonyImportDependencyParserPlugin.js +142 -45
  101. package/lib/dependencies/HarmonyImportSpecifierDependency.js +46 -22
  102. package/lib/dependencies/HarmonyModulesPlugin.js +10 -0
  103. package/lib/dependencies/ImportContextDependency.js +0 -2
  104. package/lib/dependencies/ImportMetaContextDependency.js +35 -0
  105. package/lib/dependencies/ImportMetaContextDependencyParserPlugin.js +252 -0
  106. package/lib/dependencies/ImportMetaContextPlugin.js +59 -0
  107. package/lib/dependencies/ImportMetaPlugin.js +22 -3
  108. package/lib/dependencies/LoaderPlugin.js +4 -2
  109. package/lib/dependencies/RequireContextDependency.js +0 -16
  110. package/lib/dependencies/RequireEnsureDependency.js +2 -2
  111. package/lib/dependencies/URLDependency.js +3 -8
  112. package/lib/dependencies/URLPlugin.js +1 -1
  113. package/lib/esm/ModuleChunkFormatPlugin.js +74 -49
  114. package/lib/esm/ModuleChunkLoadingPlugin.js +3 -1
  115. package/lib/esm/ModuleChunkLoadingRuntimeModule.js +25 -9
  116. package/lib/hmr/HotModuleReplacement.runtime.js +29 -14
  117. package/lib/hmr/JavascriptHotModuleReplacement.runtime.js +4 -3
  118. package/lib/hmr/LazyCompilationPlugin.js +54 -26
  119. package/lib/hmr/lazyCompilationBackend.js +51 -12
  120. package/lib/ids/DeterministicModuleIdsPlugin.js +55 -35
  121. package/lib/ids/HashedModuleIdsPlugin.js +11 -14
  122. package/lib/ids/IdHelpers.js +25 -11
  123. package/lib/ids/NamedModuleIdsPlugin.js +6 -9
  124. package/lib/ids/NaturalModuleIdsPlugin.js +10 -13
  125. package/lib/ids/OccurrenceModuleIdsPlugin.js +13 -10
  126. package/lib/ids/SyncModuleIdsPlugin.js +140 -0
  127. package/lib/index.js +13 -0
  128. package/lib/javascript/ArrayPushCallbackChunkFormatPlugin.js +2 -2
  129. package/lib/javascript/BasicEvaluatedExpression.js +5 -2
  130. package/lib/javascript/ChunkHelpers.js +33 -0
  131. package/lib/javascript/JavascriptGenerator.js +1 -0
  132. package/lib/javascript/JavascriptModulesPlugin.js +27 -2
  133. package/lib/javascript/JavascriptParser.js +82 -48
  134. package/lib/javascript/StartupHelpers.js +7 -30
  135. package/lib/library/AssignLibraryPlugin.js +39 -15
  136. package/lib/library/EnableLibraryPlugin.js +11 -0
  137. package/lib/library/UmdLibraryPlugin.js +5 -3
  138. package/lib/node/NodeTargetPlugin.js +3 -0
  139. package/lib/node/NodeWatchFileSystem.js +85 -31
  140. package/lib/node/ReadFileChunkLoadingRuntimeModule.js +23 -8
  141. package/lib/node/RequireChunkLoadingRuntimeModule.js +24 -9
  142. package/lib/optimize/ConcatenatedModule.js +21 -9
  143. package/lib/optimize/ModuleConcatenationPlugin.js +5 -2
  144. package/lib/optimize/SplitChunksPlugin.js +8 -1
  145. package/lib/runtime/AsyncModuleRuntimeModule.js +27 -17
  146. package/lib/runtime/BaseUriRuntimeModule.js +31 -0
  147. package/lib/runtime/CreateScriptRuntimeModule.js +36 -0
  148. package/lib/runtime/CreateScriptUrlRuntimeModule.js +9 -34
  149. package/lib/runtime/GetTrustedTypesPolicyRuntimeModule.js +76 -0
  150. package/lib/schemes/HttpUriPlugin.js +77 -14
  151. package/lib/serialization/FileMiddleware.js +44 -9
  152. package/lib/sharing/ConsumeSharedModule.js +8 -2
  153. package/lib/sharing/ConsumeSharedRuntimeModule.js +26 -5
  154. package/lib/sharing/ProvideSharedModule.js +4 -2
  155. package/lib/sharing/ShareRuntimeModule.js +1 -1
  156. package/lib/sharing/utils.js +1 -1
  157. package/lib/stats/DefaultStatsFactoryPlugin.js +113 -68
  158. package/lib/stats/DefaultStatsPrinterPlugin.js +89 -24
  159. package/lib/util/ArrayHelpers.js +30 -0
  160. package/lib/util/AsyncQueue.js +1 -1
  161. package/lib/util/compileBooleanMatcher.js +1 -1
  162. package/lib/util/create-schema-validation.js +9 -2
  163. package/lib/util/createHash.js +12 -0
  164. package/lib/util/deprecation.js +10 -2
  165. package/lib/util/deterministicGrouping.js +1 -1
  166. package/lib/util/extractUrlAndGlobal.js +3 -0
  167. package/lib/util/fs.js +11 -0
  168. package/lib/util/hash/BatchedHash.js +7 -4
  169. package/lib/util/hash/md4.js +20 -0
  170. package/lib/util/hash/wasm-hash.js +163 -0
  171. package/lib/util/hash/xxhash64.js +5 -139
  172. package/lib/util/identifier.js +65 -44
  173. package/lib/util/internalSerializables.js +15 -0
  174. package/lib/util/nonNumericOnlyHash.js +22 -0
  175. package/lib/util/semver.js +17 -10
  176. package/lib/wasm-async/AsyncWebAssemblyJavascriptGenerator.js +9 -3
  177. package/lib/web/JsonpChunkLoadingRuntimeModule.js +20 -9
  178. package/lib/webpack.js +10 -3
  179. package/lib/webworker/ImportScriptsChunkLoadingPlugin.js +3 -11
  180. package/lib/webworker/ImportScriptsChunkLoadingRuntimeModule.js +33 -22
  181. package/module.d.ts +215 -0
  182. package/package.json +23 -28
  183. package/schemas/WebpackOptions.check.js +1 -1
  184. package/schemas/WebpackOptions.json +254 -29
  185. package/schemas/plugins/DllReferencePlugin.check.js +1 -1
  186. package/schemas/plugins/HashedModuleIdsPlugin.check.js +1 -1
  187. package/schemas/plugins/ProgressPlugin.check.js +1 -1
  188. package/schemas/plugins/asset/AssetGeneratorOptions.check.js +1 -1
  189. package/schemas/plugins/asset/AssetParserOptions.check.js +1 -1
  190. package/schemas/plugins/asset/AssetResourceGeneratorOptions.check.js +1 -1
  191. package/schemas/plugins/container/ContainerPlugin.check.js +1 -1
  192. package/schemas/plugins/container/ContainerPlugin.json +2 -1
  193. package/schemas/plugins/container/ContainerReferencePlugin.check.js +1 -1
  194. package/schemas/plugins/container/ContainerReferencePlugin.json +1 -0
  195. package/schemas/plugins/container/ExternalsType.check.js +1 -1
  196. package/schemas/plugins/container/ModuleFederationPlugin.check.js +1 -1
  197. package/schemas/plugins/container/ModuleFederationPlugin.json +3 -1
  198. package/schemas/plugins/css/CssGeneratorOptions.check.d.ts +7 -0
  199. package/schemas/plugins/css/CssGeneratorOptions.check.js +6 -0
  200. package/schemas/plugins/css/CssGeneratorOptions.json +3 -0
  201. package/schemas/plugins/css/CssParserOptions.check.d.ts +7 -0
  202. package/schemas/plugins/css/CssParserOptions.check.js +6 -0
  203. package/schemas/plugins/css/CssParserOptions.json +3 -0
  204. package/schemas/plugins/optimize/AggressiveSplittingPlugin.check.js +1 -1
  205. package/schemas/plugins/optimize/LimitChunkCountPlugin.check.js +1 -1
  206. package/schemas/plugins/optimize/MinChunkSizePlugin.check.js +1 -1
  207. package/schemas/plugins/schemes/HttpUriPlugin.check.js +1 -1
  208. package/schemas/plugins/schemes/HttpUriPlugin.json +4 -0
  209. package/types.d.ts +628 -179
@@ -10,6 +10,7 @@ const AsyncDependenciesBlock = require("../AsyncDependenciesBlock");
10
10
  const Module = require("../Module");
11
11
  const RuntimeGlobals = require("../RuntimeGlobals");
12
12
  const Template = require("../Template");
13
+ const StaticExportsDependency = require("../dependencies/StaticExportsDependency");
13
14
  const makeSerializable = require("../util/makeSerializable");
14
15
  const ContainerExposedDependency = require("./ContainerExposedDependency");
15
16
 
@@ -78,12 +79,14 @@ class ContainerEntryModule extends Module {
78
79
  * @returns {string | null} an identifier for library inclusion
79
80
  */
80
81
  libIdent(options) {
81
- return `webpack/container/entry/${this._name}`;
82
+ return `${this.layer ? `(${this.layer})/` : ""}webpack/container/entry/${
83
+ this._name
84
+ }`;
82
85
  }
83
86
 
84
87
  /**
85
88
  * @param {NeedBuildContext} context context info
86
- * @param {function(WebpackError=, boolean=): void} callback callback function, returns true, if the module needs a rebuild
89
+ * @param {function((WebpackError | null)=, boolean=): void} callback callback function, returns true, if the module needs a rebuild
87
90
  * @returns {void}
88
91
  */
89
92
  needBuild(context, callback) {
@@ -104,6 +107,7 @@ class ContainerEntryModule extends Module {
104
107
  strict: true,
105
108
  topLevelDeclarations: new Set(["moduleMap", "get", "init"])
106
109
  };
110
+ this.buildMeta.exportsType = "namespace";
107
111
 
108
112
  this.clearDependenciesAndBlocks();
109
113
 
@@ -127,6 +131,7 @@ class ContainerEntryModule extends Module {
127
131
  }
128
132
  this.addBlock(block);
129
133
  }
134
+ this.addDependency(new StaticExportsDependency(["get", "init"], false));
130
135
 
131
136
  callback();
132
137
  }
@@ -217,10 +222,8 @@ class ContainerEntryModule extends Module {
217
222
  ])};`,
218
223
  `var init = ${runtimeTemplate.basicFunction("shareScope, initScope", [
219
224
  `if (!${RuntimeGlobals.shareScopeMap}) return;`,
220
- `var oldScope = ${RuntimeGlobals.shareScopeMap}[${JSON.stringify(
221
- this._shareScope
222
- )}];`,
223
225
  `var name = ${JSON.stringify(this._shareScope)}`,
226
+ `var oldScope = ${RuntimeGlobals.shareScopeMap}[name];`,
224
227
  `if(oldScope && oldScope !== shareScope) throw new Error("Container initialization failed as it has already been initialized with a different share scope");`,
225
228
  `${RuntimeGlobals.shareScopeMap}[name] = shareScope;`,
226
229
  `return ${RuntimeGlobals.initializeSharing}(name, initScope);`
@@ -60,9 +60,9 @@ class FallbackModule extends Module {
60
60
  * @returns {string | null} an identifier for library inclusion
61
61
  */
62
62
  libIdent(options) {
63
- return `webpack/container/fallback/${this.requests[0]}/and ${
64
- this.requests.length - 1
65
- } more`;
63
+ return `${this.layer ? `(${this.layer})/` : ""}webpack/container/fallback/${
64
+ this.requests[0]
65
+ }/and ${this.requests.length - 1} more`;
66
66
  }
67
67
 
68
68
  /**
@@ -76,7 +76,7 @@ class FallbackModule extends Module {
76
76
 
77
77
  /**
78
78
  * @param {NeedBuildContext} context context info
79
- * @param {function(WebpackError=, boolean=): void} callback callback function, returns true, if the module needs a rebuild
79
+ * @param {function((WebpackError | null)=, boolean=): void} callback callback function, returns true, if the module needs a rebuild
80
80
  * @returns {void}
81
81
  */
82
82
  needBuild(context, callback) {
@@ -67,12 +67,14 @@ class RemoteModule extends Module {
67
67
  * @returns {string | null} an identifier for library inclusion
68
68
  */
69
69
  libIdent(options) {
70
- return `webpack/container/remote/${this.request}`;
70
+ return `${this.layer ? `(${this.layer})/` : ""}webpack/container/remote/${
71
+ this.request
72
+ }`;
71
73
  }
72
74
 
73
75
  /**
74
76
  * @param {NeedBuildContext} context context info
75
- * @param {function(WebpackError=, boolean=): void} callback callback function, returns true, if the module needs a rebuild
77
+ * @param {function((WebpackError | null)=, boolean=): void} callback callback function, returns true, if the module needs a rebuild
76
78
  * @returns {void}
77
79
  */
78
80
  needBuild(context, callback) {
@@ -0,0 +1,139 @@
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, RawSource, ConcatSource } = require("webpack-sources");
9
+ const { UsageState } = require("../ExportsInfo");
10
+ const Generator = require("../Generator");
11
+ const RuntimeGlobals = require("../RuntimeGlobals");
12
+ const Template = require("../Template");
13
+
14
+ /** @typedef {import("webpack-sources").Source} Source */
15
+ /** @typedef {import("../Dependency")} Dependency */
16
+ /** @typedef {import("../Generator").GenerateContext} GenerateContext */
17
+ /** @typedef {import("../Generator").UpdateHashContext} UpdateHashContext */
18
+ /** @typedef {import("../Module").ConcatenationBailoutReasonContext} ConcatenationBailoutReasonContext */
19
+ /** @typedef {import("../NormalModule")} NormalModule */
20
+ /** @typedef {import("../util/Hash")} Hash */
21
+
22
+ const TYPES = new Set(["javascript"]);
23
+
24
+ class CssExportsGenerator extends Generator {
25
+ constructor() {
26
+ super();
27
+ }
28
+
29
+ // TODO add getConcatenationBailoutReason to allow concatenation
30
+ // but how to make it have a module id
31
+
32
+ /**
33
+ * @param {NormalModule} module module for which the code should be generated
34
+ * @param {GenerateContext} generateContext context for generate
35
+ * @returns {Source} generated code
36
+ */
37
+ generate(module, generateContext) {
38
+ const source = new ReplaceSource(new RawSource(""));
39
+ const initFragments = [];
40
+ const cssExports = new Map();
41
+
42
+ generateContext.runtimeRequirements.add(RuntimeGlobals.module);
43
+
44
+ const runtimeRequirements = new Set();
45
+
46
+ const templateContext = {
47
+ runtimeTemplate: generateContext.runtimeTemplate,
48
+ dependencyTemplates: generateContext.dependencyTemplates,
49
+ moduleGraph: generateContext.moduleGraph,
50
+ chunkGraph: generateContext.chunkGraph,
51
+ module,
52
+ runtime: generateContext.runtime,
53
+ runtimeRequirements: runtimeRequirements,
54
+ concatenationScope: generateContext.concatenationScope,
55
+ codeGenerationResults: generateContext.codeGenerationResults,
56
+ initFragments,
57
+ cssExports
58
+ };
59
+
60
+ const handleDependency = dependency => {
61
+ const constructor = /** @type {new (...args: any[]) => Dependency} */ (
62
+ dependency.constructor
63
+ );
64
+ const template = generateContext.dependencyTemplates.get(constructor);
65
+ if (!template) {
66
+ throw new Error(
67
+ "No template for dependency: " + dependency.constructor.name
68
+ );
69
+ }
70
+
71
+ template.apply(dependency, source, templateContext);
72
+ };
73
+ module.dependencies.forEach(handleDependency);
74
+
75
+ if (generateContext.concatenationScope) {
76
+ const source = new ConcatSource();
77
+ const usedIdentifiers = new Set();
78
+ for (const [k, v] of cssExports) {
79
+ let identifier = Template.toIdentifier(k);
80
+ let i = 0;
81
+ while (usedIdentifiers.has(identifier)) {
82
+ identifier = Template.toIdentifier(k + i);
83
+ }
84
+ usedIdentifiers.add(identifier);
85
+ generateContext.concatenationScope.registerExport(k, identifier);
86
+ source.add(
87
+ `${
88
+ generateContext.runtimeTemplate.supportsConst ? "const" : "var"
89
+ } ${identifier} = ${JSON.stringify(v)};\n`
90
+ );
91
+ }
92
+ return source;
93
+ } else {
94
+ const otherUsed =
95
+ generateContext.moduleGraph
96
+ .getExportsInfo(module)
97
+ .otherExportsInfo.getUsed(generateContext.runtime) !==
98
+ UsageState.Unused;
99
+ if (otherUsed) {
100
+ generateContext.runtimeRequirements.add(
101
+ RuntimeGlobals.makeNamespaceObject
102
+ );
103
+ }
104
+ return new RawSource(
105
+ `${otherUsed ? `${RuntimeGlobals.makeNamespaceObject}(` : ""}${
106
+ module.moduleArgument
107
+ }.exports = {\n${Array.from(
108
+ cssExports,
109
+ ([k, v]) => `\t${JSON.stringify(k)}: ${JSON.stringify(v)}`
110
+ ).join(",\n")}\n}${otherUsed ? ")" : ""};`
111
+ );
112
+ }
113
+ }
114
+
115
+ /**
116
+ * @param {NormalModule} module fresh module
117
+ * @returns {Set<string>} available types (do not mutate)
118
+ */
119
+ getTypes(module) {
120
+ return TYPES;
121
+ }
122
+
123
+ /**
124
+ * @param {NormalModule} module the module
125
+ * @param {string=} type source type
126
+ * @returns {number} estimate size of the module
127
+ */
128
+ getSize(module, type) {
129
+ return 42;
130
+ }
131
+
132
+ /**
133
+ * @param {Hash} hash hash that will be modified
134
+ * @param {UpdateHashContext} updateHashContext context for updating hash
135
+ */
136
+ updateHash(hash, { module }) {}
137
+ }
138
+
139
+ module.exports = CssExportsGenerator;
@@ -0,0 +1,109 @@
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
+ const RuntimeGlobals = require("../RuntimeGlobals");
12
+
13
+ /** @typedef {import("webpack-sources").Source} Source */
14
+ /** @typedef {import("../Dependency")} Dependency */
15
+ /** @typedef {import("../Generator").GenerateContext} GenerateContext */
16
+ /** @typedef {import("../Generator").UpdateHashContext} UpdateHashContext */
17
+ /** @typedef {import("../NormalModule")} NormalModule */
18
+ /** @typedef {import("../util/Hash")} Hash */
19
+
20
+ const TYPES = new Set(["css"]);
21
+
22
+ class CssGenerator extends Generator {
23
+ constructor() {
24
+ super();
25
+ }
26
+
27
+ /**
28
+ * @param {NormalModule} module module for which the code should be generated
29
+ * @param {GenerateContext} generateContext context for generate
30
+ * @returns {Source} generated code
31
+ */
32
+ generate(module, generateContext) {
33
+ const originalSource = module.originalSource();
34
+ const source = new ReplaceSource(originalSource);
35
+ const initFragments = [];
36
+ const cssExports = new Map();
37
+
38
+ generateContext.runtimeRequirements.add(RuntimeGlobals.hasCssModules);
39
+
40
+ const templateContext = {
41
+ runtimeTemplate: generateContext.runtimeTemplate,
42
+ dependencyTemplates: generateContext.dependencyTemplates,
43
+ moduleGraph: generateContext.moduleGraph,
44
+ chunkGraph: generateContext.chunkGraph,
45
+ module,
46
+ runtime: generateContext.runtime,
47
+ runtimeRequirements: generateContext.runtimeRequirements,
48
+ concatenationScope: generateContext.concatenationScope,
49
+ codeGenerationResults: generateContext.codeGenerationResults,
50
+ initFragments,
51
+ cssExports
52
+ };
53
+
54
+ const handleDependency = dependency => {
55
+ const constructor = /** @type {new (...args: any[]) => Dependency} */ (
56
+ dependency.constructor
57
+ );
58
+ const template = generateContext.dependencyTemplates.get(constructor);
59
+ if (!template) {
60
+ throw new Error(
61
+ "No template for dependency: " + dependency.constructor.name
62
+ );
63
+ }
64
+
65
+ template.apply(dependency, source, templateContext);
66
+ };
67
+ module.dependencies.forEach(handleDependency);
68
+ if (module.presentationalDependencies !== undefined)
69
+ module.presentationalDependencies.forEach(handleDependency);
70
+
71
+ if (cssExports.size > 0) {
72
+ const data = generateContext.getData();
73
+ data.set("css-exports", cssExports);
74
+ }
75
+
76
+ return InitFragment.addToSource(source, initFragments, generateContext);
77
+ }
78
+
79
+ /**
80
+ * @param {NormalModule} module fresh module
81
+ * @returns {Set<string>} available types (do not mutate)
82
+ */
83
+ getTypes(module) {
84
+ return TYPES;
85
+ }
86
+
87
+ /**
88
+ * @param {NormalModule} module the module
89
+ * @param {string=} type source type
90
+ * @returns {number} estimate size of the module
91
+ */
92
+ getSize(module, type) {
93
+ const originalSource = module.originalSource();
94
+
95
+ if (!originalSource) {
96
+ return 0;
97
+ }
98
+
99
+ return originalSource.size();
100
+ }
101
+
102
+ /**
103
+ * @param {Hash} hash hash that will be modified
104
+ * @param {UpdateHashContext} updateHashContext context for updating hash
105
+ */
106
+ updateHash(hash, { module }) {}
107
+ }
108
+
109
+ module.exports = CssGenerator;