1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const { ConcatSource } = require("webpack-sources");
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | const extractFragmentIndex = (fragment, index) => [fragment, index];
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | const sortFragmentWithIndex = ([a, i], [b, j]) => {
|
26 | const stageCmp = a.stage - b.stage;
|
27 | if (stageCmp !== 0) return stageCmp;
|
28 | const positionCmp = a.position - b.position;
|
29 | if (positionCmp !== 0) return positionCmp;
|
30 | return i - j;
|
31 | };
|
32 |
|
33 | class InitFragment {
|
34 | |
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | constructor(content, stage, position, key, endContent) {
|
42 | this.content = content;
|
43 | this.stage = stage;
|
44 | this.position = position;
|
45 | this.key = key;
|
46 | this.endContent = endContent;
|
47 | }
|
48 |
|
49 | |
50 |
|
51 |
|
52 |
|
53 | getContent(generateContext) {
|
54 | return this.content;
|
55 | }
|
56 |
|
57 | |
58 |
|
59 |
|
60 |
|
61 | getEndContent(generateContext) {
|
62 | return this.endContent;
|
63 | }
|
64 |
|
65 | static addToSource(source, initFragments, generateContext) {
|
66 | if (initFragments.length > 0) {
|
67 |
|
68 |
|
69 | const sortedFragments = initFragments
|
70 | .map(extractFragmentIndex)
|
71 | .sort(sortFragmentWithIndex);
|
72 |
|
73 |
|
74 | const keyedFragments = new Map();
|
75 | for (const [fragment] of sortedFragments) {
|
76 | if (typeof fragment.merge === "function") {
|
77 | const oldValue = keyedFragments.get(fragment.key);
|
78 | if (oldValue !== undefined) {
|
79 | keyedFragments.set(
|
80 | fragment.key || Symbol(),
|
81 | fragment.merge(oldValue)
|
82 | );
|
83 | continue;
|
84 | }
|
85 | }
|
86 | keyedFragments.set(fragment.key || Symbol(), fragment);
|
87 | }
|
88 |
|
89 | const concatSource = new ConcatSource();
|
90 | const endContents = [];
|
91 | for (const fragment of keyedFragments.values()) {
|
92 | concatSource.add(fragment.getContent(generateContext));
|
93 | const endContent = fragment.getEndContent(generateContext);
|
94 | if (endContent) {
|
95 | endContents.push(endContent);
|
96 | }
|
97 | }
|
98 |
|
99 | concatSource.add(source);
|
100 | for (const content of endContents.reverse()) {
|
101 | concatSource.add(content);
|
102 | }
|
103 | return concatSource;
|
104 | } else {
|
105 | return source;
|
106 | }
|
107 | }
|
108 | }
|
109 |
|
110 | InitFragment.prototype.merge = undefined;
|
111 |
|
112 | InitFragment.STAGE_CONSTANTS = 10;
|
113 | InitFragment.STAGE_ASYNC_BOUNDARY = 20;
|
114 | InitFragment.STAGE_HARMONY_EXPORTS = 30;
|
115 | InitFragment.STAGE_HARMONY_IMPORTS = 40;
|
116 | InitFragment.STAGE_PROVIDES = 50;
|
117 | InitFragment.STAGE_ASYNC_DEPENDENCIES = 60;
|
118 | InitFragment.STAGE_ASYNC_HARMONY_IMPORTS = 70;
|
119 |
|
120 | module.exports = InitFragment;
|