source: imaps-frontend/node_modules/webpack/lib/javascript/JavascriptGenerator.js@ 79a0317

main
Last change on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

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