source: frontend/node_modules/webpack/lib/javascript/JavascriptGenerator.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 11 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 9.1 KB
Line 
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const util = require("util");
9const { RawSource, ReplaceSource } = require("webpack-sources");
10const Generator = require("../Generator");
11const InitFragment = require("../InitFragment");
12const { JAVASCRIPT_TYPES } = require("../ModuleSourceTypeConstants");
13const HarmonyCompatibilityDependency = require("../dependencies/HarmonyCompatibilityDependency");
14
15/** @typedef {import("webpack-sources").Source} Source */
16/** @typedef {import("../Compilation").DependencyConstructor} DependencyConstructor */
17/** @typedef {import("../DependenciesBlock")} DependenciesBlock */
18/** @typedef {import("../Dependency")} Dependency */
19/** @typedef {import("../DependencyTemplate")} DependencyTemplate */
20/** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
21/** @typedef {import("../Generator").GenerateContext} GenerateContext */
22/** @typedef {import("../Module")} Module */
23/** @typedef {import("../Module").ConcatenationBailoutReasonContext} ConcatenationBailoutReasonContext */
24/** @typedef {import("../Module").SourceType} SourceType */
25/** @typedef {import("../Module").SourceTypes} SourceTypes */
26/** @typedef {import("../NormalModule")} NormalModule */
27
28const DEFAULT_SOURCE = {
29 source() {
30 return new RawSource("throw new Error('No source available');");
31 },
32 /**
33 * Returns the estimated size for the requested source type.
34 * @returns {number} size of the DEFAULT_SOURCE.source()
35 */
36 size() {
37 return 39;
38 }
39};
40
41// TODO: clean up this file
42// replace with newer constructs
43
44const deprecatedGetInitFragments = util.deprecate(
45 /**
46 * Handles the callback logic for this hook.
47 * @param {DependencyTemplate} template template
48 * @param {Dependency} dependency dependency
49 * @param {DependencyTemplateContext} templateContext template context
50 * @returns {InitFragment<GenerateContext>[]} init fragments
51 */
52 (template, dependency, templateContext) =>
53 /** @type {DependencyTemplate & { getInitFragments: (dependency: Dependency, dependencyTemplateContext: DependencyTemplateContext) => InitFragment<GenerateContext>[] }} */
54 (template).getInitFragments(dependency, templateContext),
55 "DependencyTemplate.getInitFragment is deprecated (use apply(dep, source, { initFragments }) instead)",
56 "DEP_WEBPACK_JAVASCRIPT_GENERATOR_GET_INIT_FRAGMENTS"
57);
58
59class JavascriptGenerator extends Generator {
60 /**
61 * Returns the source types available for this module.
62 * @param {NormalModule} module fresh module
63 * @returns {SourceTypes} available types (do not mutate)
64 */
65 getTypes(module) {
66 return JAVASCRIPT_TYPES;
67 }
68
69 /**
70 * Returns the estimated size for the requested source type.
71 * @param {NormalModule} module the module
72 * @param {SourceType=} type source type
73 * @returns {number} estimate size of the module
74 */
75 getSize(module, type) {
76 const originalSource = module.originalSource();
77 if (!originalSource) {
78 return DEFAULT_SOURCE.size();
79 }
80 return originalSource.size();
81 }
82
83 /**
84 * Returns the reason this module cannot be concatenated, when one exists.
85 * @param {NormalModule} module module for which the bailout reason should be determined
86 * @param {ConcatenationBailoutReasonContext} context context
87 * @returns {string | undefined} reason why this module can't be concatenated, undefined when it can be concatenated
88 */
89 getConcatenationBailoutReason(module, context) {
90 // Only harmony modules are valid for optimization
91 if (
92 !module.buildMeta ||
93 module.buildMeta.exportsType !== "namespace" ||
94 module.presentationalDependencies === undefined ||
95 !module.presentationalDependencies.some(
96 (d) => d instanceof HarmonyCompatibilityDependency
97 )
98 ) {
99 return "Module is not an ECMAScript module";
100 }
101
102 // Some expressions are not compatible with module concatenation
103 // because they may produce unexpected results. The plugin bails out
104 // if some were detected upfront.
105 if (module.buildInfo && module.buildInfo.moduleConcatenationBailout) {
106 return `Module uses ${module.buildInfo.moduleConcatenationBailout}`;
107 }
108 }
109
110 /**
111 * Processes the provided module.
112 * @param {Module} module the current module
113 * @param {Dependency} dependency the dependency to generate
114 * @param {InitFragment<GenerateContext>[]} initFragments mutable list of init fragments
115 * @param {ReplaceSource} source the current replace source which can be modified
116 * @param {GenerateContext} generateContext the render context
117 * @returns {void}
118 */
119 sourceDependency(module, dependency, initFragments, source, generateContext) {
120 const constructor =
121 /** @type {DependencyConstructor} */
122 (dependency.constructor);
123 const template = generateContext.dependencyTemplates.get(constructor);
124 if (!template) {
125 throw new Error(
126 `No template for dependency: ${dependency.constructor.name}`
127 );
128 }
129
130 /** @type {InitFragment<GenerateContext>[] | undefined} */
131 let chunkInitFragments;
132
133 /** @type {DependencyTemplateContext} */
134 const templateContext = {
135 runtimeTemplate: generateContext.runtimeTemplate,
136 dependencyTemplates: generateContext.dependencyTemplates,
137 moduleGraph: generateContext.moduleGraph,
138 chunkGraph: generateContext.chunkGraph,
139 module,
140 runtime: generateContext.runtime,
141 runtimeRequirements: generateContext.runtimeRequirements,
142 concatenationScope: generateContext.concatenationScope,
143 codeGenerationResults:
144 /** @type {NonNullable<GenerateContext["codeGenerationResults"]>} */
145 (generateContext.codeGenerationResults),
146 initFragments,
147 get chunkInitFragments() {
148 if (!chunkInitFragments) {
149 const data =
150 /** @type {NonNullable<GenerateContext["getData"]>} */
151 (generateContext.getData)();
152 chunkInitFragments = data.get("chunkInitFragments");
153 if (!chunkInitFragments) {
154 chunkInitFragments = [];
155 data.set("chunkInitFragments", chunkInitFragments);
156 }
157 }
158
159 return chunkInitFragments;
160 }
161 };
162
163 template.apply(dependency, source, templateContext);
164
165 // TODO remove in webpack 6
166 if ("getInitFragments" in template) {
167 const fragments = deprecatedGetInitFragments(
168 template,
169 dependency,
170 templateContext
171 );
172
173 if (fragments) {
174 for (const fragment of fragments) {
175 initFragments.push(fragment);
176 }
177 }
178 }
179 }
180
181 /**
182 * Processes the provided module.
183 * @param {Module} module the module to generate
184 * @param {DependenciesBlock} block the dependencies block which will be processed
185 * @param {InitFragment<GenerateContext>[]} initFragments mutable list of init fragments
186 * @param {ReplaceSource} source the current replace source which can be modified
187 * @param {GenerateContext} generateContext the generateContext
188 * @returns {void}
189 */
190 sourceBlock(module, block, initFragments, source, generateContext) {
191 for (const dependency of block.dependencies) {
192 this.sourceDependency(
193 module,
194 dependency,
195 initFragments,
196 source,
197 generateContext
198 );
199 }
200
201 for (const childBlock of block.blocks) {
202 this.sourceBlock(
203 module,
204 childBlock,
205 initFragments,
206 source,
207 generateContext
208 );
209 }
210 }
211
212 /**
213 * Processes the provided module.
214 * @param {Module} module the module to generate
215 * @param {InitFragment<GenerateContext>[]} initFragments mutable list of init fragments
216 * @param {ReplaceSource} source the current replace source which can be modified
217 * @param {GenerateContext} generateContext the generateContext
218 * @returns {void}
219 */
220 sourceModule(module, initFragments, source, generateContext) {
221 for (const dependency of module.dependencies) {
222 this.sourceDependency(
223 module,
224 dependency,
225 initFragments,
226 source,
227 generateContext
228 );
229 }
230
231 if (module.presentationalDependencies !== undefined) {
232 for (const dependency of module.presentationalDependencies) {
233 this.sourceDependency(
234 module,
235 dependency,
236 initFragments,
237 source,
238 generateContext
239 );
240 }
241 }
242
243 for (const childBlock of module.blocks) {
244 this.sourceBlock(
245 module,
246 childBlock,
247 initFragments,
248 source,
249 generateContext
250 );
251 }
252 }
253
254 /**
255 * Generates generated code for this runtime module.
256 * @param {NormalModule} module module for which the code should be generated
257 * @param {GenerateContext} generateContext context for generate
258 * @returns {Source | null} generated code
259 */
260 generate(module, generateContext) {
261 const originalSource = module.originalSource();
262 if (!originalSource) {
263 return DEFAULT_SOURCE.source();
264 }
265
266 const source = new ReplaceSource(originalSource);
267 /** @type {InitFragment<GenerateContext>[]} */
268 const initFragments = [];
269
270 this.sourceModule(module, initFragments, source, generateContext);
271
272 return InitFragment.addToSource(source, initFragments, generateContext);
273 }
274
275 /**
276 * Generates fallback output for the provided error condition.
277 * @param {Error} error the error
278 * @param {NormalModule} module module for which the code should be generated
279 * @param {GenerateContext} generateContext context for generate
280 * @returns {Source | null} generated code
281 */
282 generateError(error, module, generateContext) {
283 return new RawSource(`throw new Error(${JSON.stringify(error.message)});`);
284 }
285}
286
287module.exports = JavascriptGenerator;
Note: See TracBrowser for help on using the repository browser.