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