1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const { ConcatSource, PrefixSource } = require("webpack-sources");
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | const START_LOWERCASE_ALPHABET_CODE = "a".charCodeAt(0);
|
27 | const START_UPPERCASE_ALPHABET_CODE = "A".charCodeAt(0);
|
28 | const DELTA_A_TO_Z = "z".charCodeAt(0) - START_LOWERCASE_ALPHABET_CODE + 1;
|
29 | const NUMBER_OF_IDENTIFIER_START_CHARS = DELTA_A_TO_Z * 2 + 2;
|
30 | const NUMBER_OF_IDENTIFIER_CONTINUATION_CHARS =
|
31 | NUMBER_OF_IDENTIFIER_START_CHARS + 10;
|
32 | const FUNCTION_CONTENT_REGEX = /^function\s?\(\)\s?\{\r?\n?|\r?\n?\}$/g;
|
33 | const INDENT_MULTILINE_REGEX = /^\t/gm;
|
34 | const LINE_SEPARATOR_REGEX = /\r?\n/g;
|
35 | const IDENTIFIER_NAME_REPLACE_REGEX = /^([^a-zA-Z$_])/;
|
36 | const IDENTIFIER_ALPHA_NUMERIC_NAME_REPLACE_REGEX = /[^a-zA-Z0-9$]+/g;
|
37 | const COMMENT_END_REGEX = /\*\//g;
|
38 | const PATH_NAME_NORMALIZE_REPLACE_REGEX = /[^a-zA-Z0-9_!§$()=\-^°]+/g;
|
39 | const MATCH_PADDED_HYPHENS_REPLACE_REGEX = /^-|-$/g;
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 | class Template {
|
88 | |
89 |
|
90 |
|
91 |
|
92 |
|
93 | static getFunctionContent(fn) {
|
94 | return fn
|
95 | .toString()
|
96 | .replace(FUNCTION_CONTENT_REGEX, "")
|
97 | .replace(INDENT_MULTILINE_REGEX, "")
|
98 | .replace(LINE_SEPARATOR_REGEX, "\n");
|
99 | }
|
100 |
|
101 | |
102 |
|
103 |
|
104 |
|
105 | static toIdentifier(str) {
|
106 | if (typeof str !== "string") return "";
|
107 | return str
|
108 | .replace(IDENTIFIER_NAME_REPLACE_REGEX, "_$1")
|
109 | .replace(IDENTIFIER_ALPHA_NUMERIC_NAME_REPLACE_REGEX, "_");
|
110 | }
|
111 | |
112 |
|
113 |
|
114 |
|
115 |
|
116 | static toComment(str) {
|
117 | if (!str) return "";
|
118 | return `/*! ${str.replace(COMMENT_END_REGEX, "* /")} */`;
|
119 | }
|
120 |
|
121 | |
122 |
|
123 |
|
124 |
|
125 |
|
126 | static toNormalComment(str) {
|
127 | if (!str) return "";
|
128 | return `/* ${str.replace(COMMENT_END_REGEX, "* /")} */`;
|
129 | }
|
130 |
|
131 | |
132 |
|
133 |
|
134 |
|
135 | static toPath(str) {
|
136 | if (typeof str !== "string") return "";
|
137 | return str
|
138 | .replace(PATH_NAME_NORMALIZE_REPLACE_REGEX, "-")
|
139 | .replace(MATCH_PADDED_HYPHENS_REPLACE_REGEX, "");
|
140 | }
|
141 |
|
142 |
|
143 | |
144 |
|
145 |
|
146 |
|
147 | static numberToIdentifier(n) {
|
148 | if (n >= NUMBER_OF_IDENTIFIER_START_CHARS) {
|
149 |
|
150 | return (
|
151 | Template.numberToIdentifier(n % NUMBER_OF_IDENTIFIER_START_CHARS) +
|
152 | Template.numberToIdentifierContinuation(
|
153 | Math.floor(n / NUMBER_OF_IDENTIFIER_START_CHARS)
|
154 | )
|
155 | );
|
156 | }
|
157 |
|
158 |
|
159 | if (n < DELTA_A_TO_Z) {
|
160 | return String.fromCharCode(START_LOWERCASE_ALPHABET_CODE + n);
|
161 | }
|
162 | n -= DELTA_A_TO_Z;
|
163 |
|
164 |
|
165 | if (n < DELTA_A_TO_Z) {
|
166 | return String.fromCharCode(START_UPPERCASE_ALPHABET_CODE + n);
|
167 | }
|
168 |
|
169 | if (n === DELTA_A_TO_Z) return "_";
|
170 | return "$";
|
171 | }
|
172 |
|
173 | |
174 |
|
175 |
|
176 |
|
177 | static numberToIdentifierContinuation(n) {
|
178 | if (n >= NUMBER_OF_IDENTIFIER_CONTINUATION_CHARS) {
|
179 |
|
180 | return (
|
181 | Template.numberToIdentifierContinuation(
|
182 | n % NUMBER_OF_IDENTIFIER_CONTINUATION_CHARS
|
183 | ) +
|
184 | Template.numberToIdentifierContinuation(
|
185 | Math.floor(n / NUMBER_OF_IDENTIFIER_CONTINUATION_CHARS)
|
186 | )
|
187 | );
|
188 | }
|
189 |
|
190 |
|
191 | if (n < DELTA_A_TO_Z) {
|
192 | return String.fromCharCode(START_LOWERCASE_ALPHABET_CODE + n);
|
193 | }
|
194 | n -= DELTA_A_TO_Z;
|
195 |
|
196 |
|
197 | if (n < DELTA_A_TO_Z) {
|
198 | return String.fromCharCode(START_UPPERCASE_ALPHABET_CODE + n);
|
199 | }
|
200 | n -= DELTA_A_TO_Z;
|
201 |
|
202 |
|
203 | if (n < 10) {
|
204 | return `${n}`;
|
205 | }
|
206 |
|
207 | if (n === 10) return "_";
|
208 | return "$";
|
209 | }
|
210 |
|
211 | |
212 |
|
213 |
|
214 |
|
215 |
|
216 | static indent(s) {
|
217 | if (Array.isArray(s)) {
|
218 | return s.map(Template.indent).join("\n");
|
219 | } else {
|
220 | const str = s.trimRight();
|
221 | if (!str) return "";
|
222 | const ind = str[0] === "\n" ? "" : "\t";
|
223 | return ind + str.replace(/\n([^\n])/g, "\n\t$1");
|
224 | }
|
225 | }
|
226 |
|
227 | |
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 | static prefix(s, prefix) {
|
234 | const str = Template.asString(s).trim();
|
235 | if (!str) return "";
|
236 | const ind = str[0] === "\n" ? "" : prefix;
|
237 | return ind + str.replace(/\n([^\n])/g, "\n" + prefix + "$1");
|
238 | }
|
239 |
|
240 | |
241 |
|
242 |
|
243 |
|
244 |
|
245 | static asString(str) {
|
246 | if (Array.isArray(str)) {
|
247 | return str.join("\n");
|
248 | }
|
249 | return str;
|
250 | }
|
251 |
|
252 | |
253 |
|
254 |
|
255 |
|
256 |
|
257 | |
258 |
|
259 |
|
260 |
|
261 |
|
262 | static getModulesArrayBounds(modules) {
|
263 | let maxId = -Infinity;
|
264 | let minId = Infinity;
|
265 | for (const module of modules) {
|
266 | const moduleId = module.id;
|
267 | if (typeof moduleId !== "number") return false;
|
268 | if (maxId < moduleId) maxId = moduleId;
|
269 | if (minId > moduleId) minId = moduleId;
|
270 | }
|
271 | if (minId < 16 + ("" + minId).length) {
|
272 |
|
273 | minId = 0;
|
274 | }
|
275 |
|
276 | let objectOverhead = -1;
|
277 | for (const module of modules) {
|
278 |
|
279 | objectOverhead += `${module.id}`.length + 2;
|
280 | }
|
281 |
|
282 | const arrayOverhead = minId === 0 ? maxId : 16 + `${minId}`.length + maxId;
|
283 | return arrayOverhead < objectOverhead ? [minId, maxId] : false;
|
284 | }
|
285 |
|
286 | |
287 |
|
288 |
|
289 |
|
290 |
|
291 |
|
292 |
|
293 | static renderChunkModules(renderContext, modules, renderModule, prefix = "") {
|
294 | const { chunkGraph } = renderContext;
|
295 | var source = new ConcatSource();
|
296 | if (modules.length === 0) {
|
297 | return null;
|
298 | }
|
299 |
|
300 | const allModules = modules.map(module => {
|
301 | return {
|
302 | id: chunkGraph.getModuleId(module),
|
303 | source: renderModule(module) || "false"
|
304 | };
|
305 | });
|
306 | const bounds = Template.getModulesArrayBounds(allModules);
|
307 | if (bounds) {
|
308 |
|
309 | const minId = bounds[0];
|
310 | const maxId = bounds[1];
|
311 | if (minId !== 0) {
|
312 | source.add(`Array(${minId}).concat(`);
|
313 | }
|
314 | source.add("[\n");
|
315 |
|
316 | const modules = new Map();
|
317 | for (const module of allModules) {
|
318 | modules.set(module.id, module);
|
319 | }
|
320 | for (let idx = minId; idx <= maxId; idx++) {
|
321 | const module = modules.get(idx);
|
322 | if (idx !== minId) {
|
323 | source.add(",\n");
|
324 | }
|
325 | source.add(`/* ${idx} */`);
|
326 | if (module) {
|
327 | source.add("\n");
|
328 | source.add(module.source);
|
329 | }
|
330 | }
|
331 | source.add("\n" + prefix + "]");
|
332 | if (minId !== 0) {
|
333 | source.add(")");
|
334 | }
|
335 | } else {
|
336 |
|
337 | source.add("{\n");
|
338 | for (let i = 0; i < allModules.length; i++) {
|
339 | const module = allModules[i];
|
340 | if (i !== 0) {
|
341 | source.add(",\n");
|
342 | }
|
343 | source.add(`\n/***/ ${JSON.stringify(module.id)}:\n`);
|
344 | source.add(module.source);
|
345 | }
|
346 | source.add(`\n\n${prefix}}`);
|
347 | }
|
348 | return source;
|
349 | }
|
350 |
|
351 | |
352 |
|
353 |
|
354 |
|
355 |
|
356 | static renderRuntimeModules(runtimeModules, renderContext) {
|
357 | const source = new ConcatSource();
|
358 | for (const module of runtimeModules) {
|
359 | const codeGenerationResults = renderContext.codeGenerationResults;
|
360 | let runtimeSource;
|
361 | if (codeGenerationResults) {
|
362 | runtimeSource = codeGenerationResults.getSource(
|
363 | module,
|
364 | renderContext.chunk.runtime,
|
365 | "runtime"
|
366 | );
|
367 | } else {
|
368 | const codeGenResult = module.codeGeneration({
|
369 | chunkGraph: renderContext.chunkGraph,
|
370 | dependencyTemplates: renderContext.dependencyTemplates,
|
371 | moduleGraph: renderContext.moduleGraph,
|
372 | runtimeTemplate: renderContext.runtimeTemplate,
|
373 | runtime: renderContext.chunk.runtime
|
374 | });
|
375 | if (!codeGenResult) continue;
|
376 | runtimeSource = codeGenResult.sources.get("runtime");
|
377 | }
|
378 | if (runtimeSource) {
|
379 | source.add(Template.toNormalComment(module.identifier()) + "\n");
|
380 | if (!module.shouldIsolate()) {
|
381 | source.add(runtimeSource);
|
382 | } else if (renderContext.runtimeTemplate.supportsArrowFunction()) {
|
383 | source.add("(() => {\n");
|
384 | if (renderContext.useStrict) source.add('\t"use strict";\n');
|
385 | source.add(new PrefixSource("\t", runtimeSource));
|
386 | source.add("\n})();\n\n");
|
387 | } else {
|
388 | source.add("!function() {\n");
|
389 | if (renderContext.useStrict) source.add('\t"use strict";\n');
|
390 | source.add(new PrefixSource("\t", runtimeSource));
|
391 | source.add("\n}();\n\n");
|
392 | }
|
393 | }
|
394 | }
|
395 | return source;
|
396 | }
|
397 |
|
398 | |
399 |
|
400 |
|
401 |
|
402 |
|
403 | static renderChunkRuntimeModules(runtimeModules, renderContext) {
|
404 | return new PrefixSource(
|
405 | "/******/ ",
|
406 | new ConcatSource(
|
407 | "function(__webpack_require__) { // webpackRuntimeModules\n",
|
408 | '"use strict";\n\n',
|
409 | this.renderRuntimeModules(runtimeModules, renderContext),
|
410 | "}\n"
|
411 | )
|
412 | );
|
413 | }
|
414 | }
|
415 |
|
416 | module.exports = Template;
|
417 | module.exports.NUMBER_OF_IDENTIFIER_START_CHARS =
|
418 | NUMBER_OF_IDENTIFIER_START_CHARS;
|
419 | module.exports.NUMBER_OF_IDENTIFIER_CONTINUATION_CHARS =
|
420 | NUMBER_OF_IDENTIFIER_CONTINUATION_CHARS;
|