1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const util = require("util");
|
9 | const { RawSource, ReplaceSource } = require("webpack-sources");
|
10 | const Generator = require("../Generator");
|
11 | const InitFragment = require("../InitFragment");
|
12 | const HarmonyCompatibilityDependency = require("../dependencies/HarmonyCompatibilityDependency");
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | const deprecatedGetInitFragments = util.deprecate(
|
28 | (template, dependency, templateContext) =>
|
29 | template.getInitFragments(dependency, templateContext),
|
30 | "DependencyTemplate.getInitFragment is deprecated (use apply(dep, source, { initFragments }) instead)",
|
31 | "DEP_WEBPACK_JAVASCRIPT_GENERATOR_GET_INIT_FRAGMENTS"
|
32 | );
|
33 |
|
34 | const TYPES = new Set(["javascript"]);
|
35 |
|
36 | class JavascriptGenerator extends Generator {
|
37 | |
38 |
|
39 |
|
40 |
|
41 | getTypes(module) {
|
42 | return TYPES;
|
43 | }
|
44 |
|
45 | |
46 |
|
47 |
|
48 |
|
49 |
|
50 | getSize(module, type) {
|
51 | const originalSource = module.originalSource();
|
52 | if (!originalSource) {
|
53 | return 39;
|
54 | }
|
55 | return originalSource.size();
|
56 | }
|
57 |
|
58 | |
59 |
|
60 |
|
61 |
|
62 |
|
63 | getConcatenationBailoutReason(module, context) {
|
64 |
|
65 | if (
|
66 | !module.buildMeta ||
|
67 | module.buildMeta.exportsType !== "namespace" ||
|
68 | module.presentationalDependencies === undefined ||
|
69 | !module.presentationalDependencies.some(
|
70 | d => d instanceof HarmonyCompatibilityDependency
|
71 | )
|
72 | ) {
|
73 | return "Module is not an ECMAScript module";
|
74 | }
|
75 |
|
76 |
|
77 |
|
78 |
|
79 | if (module.buildInfo && module.buildInfo.moduleConcatenationBailout) {
|
80 | return `Module uses ${module.buildInfo.moduleConcatenationBailout}`;
|
81 | }
|
82 | }
|
83 |
|
84 | |
85 |
|
86 |
|
87 |
|
88 |
|
89 | generate(module, generateContext) {
|
90 | const originalSource = module.originalSource();
|
91 | if (!originalSource) {
|
92 | return new RawSource("throw new Error('No source available');");
|
93 | }
|
94 |
|
95 | const source = new ReplaceSource(originalSource);
|
96 | const initFragments = [];
|
97 |
|
98 | this.sourceModule(module, initFragments, source, generateContext);
|
99 |
|
100 | return InitFragment.addToSource(source, initFragments, generateContext);
|
101 | }
|
102 |
|
103 | |
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 | sourceModule(module, initFragments, source, generateContext) {
|
111 | for (const dependency of module.dependencies) {
|
112 | this.sourceDependency(
|
113 | module,
|
114 | dependency,
|
115 | initFragments,
|
116 | source,
|
117 | generateContext
|
118 | );
|
119 | }
|
120 |
|
121 | if (module.presentationalDependencies !== undefined) {
|
122 | for (const dependency of module.presentationalDependencies) {
|
123 | this.sourceDependency(
|
124 | module,
|
125 | dependency,
|
126 | initFragments,
|
127 | source,
|
128 | generateContext
|
129 | );
|
130 | }
|
131 | }
|
132 |
|
133 | for (const childBlock of module.blocks) {
|
134 | this.sourceBlock(
|
135 | module,
|
136 | childBlock,
|
137 | initFragments,
|
138 | source,
|
139 | generateContext
|
140 | );
|
141 | }
|
142 | }
|
143 |
|
144 | |
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 | sourceBlock(module, block, initFragments, source, generateContext) {
|
153 | for (const dependency of block.dependencies) {
|
154 | this.sourceDependency(
|
155 | module,
|
156 | dependency,
|
157 | initFragments,
|
158 | source,
|
159 | generateContext
|
160 | );
|
161 | }
|
162 |
|
163 | for (const childBlock of block.blocks) {
|
164 | this.sourceBlock(
|
165 | module,
|
166 | childBlock,
|
167 | initFragments,
|
168 | source,
|
169 | generateContext
|
170 | );
|
171 | }
|
172 | }
|
173 |
|
174 | |
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 | sourceDependency(module, dependency, initFragments, source, generateContext) {
|
183 | const constructor = /** @type {new (...args: any[]) => Dependency} */ (dependency.constructor);
|
184 | const template = generateContext.dependencyTemplates.get(constructor);
|
185 | if (!template) {
|
186 | throw new Error(
|
187 | "No template for dependency: " + dependency.constructor.name
|
188 | );
|
189 | }
|
190 |
|
191 | const templateContext = {
|
192 | runtimeTemplate: generateContext.runtimeTemplate,
|
193 | dependencyTemplates: generateContext.dependencyTemplates,
|
194 | moduleGraph: generateContext.moduleGraph,
|
195 | chunkGraph: generateContext.chunkGraph,
|
196 | module,
|
197 | runtime: generateContext.runtime,
|
198 | runtimeRequirements: generateContext.runtimeRequirements,
|
199 | concatenationScope: generateContext.concatenationScope,
|
200 | initFragments
|
201 | };
|
202 |
|
203 | template.apply(dependency, source, templateContext);
|
204 |
|
205 |
|
206 | if ("getInitFragments" in template) {
|
207 | const fragments = deprecatedGetInitFragments(
|
208 | template,
|
209 | dependency,
|
210 | templateContext
|
211 | );
|
212 |
|
213 | if (fragments) {
|
214 | for (const fragment of fragments) {
|
215 | initFragments.push(fragment);
|
216 | }
|
217 | }
|
218 | }
|
219 | }
|
220 | }
|
221 |
|
222 | module.exports = JavascriptGenerator;
|