1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const { ConcatSource } = require("webpack-sources");
|
9 | const makeSerializable = require("./util/makeSerializable");
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | const extractFragmentIndex = (fragment, index) => [fragment, index];
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | const sortFragmentWithIndex = ([a, i], [b, j]) => {
|
27 | const stageCmp = a.stage - b.stage;
|
28 | if (stageCmp !== 0) return stageCmp;
|
29 | const positionCmp = a.position - b.position;
|
30 | if (positionCmp !== 0) return positionCmp;
|
31 | return i - j;
|
32 | };
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | class InitFragment {
|
38 | |
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | constructor(content, stage, position, key, endContent) {
|
46 | this.content = content;
|
47 | this.stage = stage;
|
48 | this.position = position;
|
49 | this.key = key;
|
50 | this.endContent = endContent;
|
51 | }
|
52 |
|
53 | |
54 |
|
55 |
|
56 |
|
57 | getContent(context) {
|
58 | return this.content;
|
59 | }
|
60 |
|
61 | |
62 |
|
63 |
|
64 |
|
65 | getEndContent(context) {
|
66 | return this.endContent;
|
67 | }
|
68 |
|
69 | static addToSource(source, initFragments, context) {
|
70 | if (initFragments.length > 0) {
|
71 |
|
72 |
|
73 | const sortedFragments = initFragments
|
74 | .map(extractFragmentIndex)
|
75 | .sort(sortFragmentWithIndex);
|
76 |
|
77 |
|
78 | const keyedFragments = new Map();
|
79 | for (const [fragment] of sortedFragments) {
|
80 | if (typeof fragment.mergeAll === "function") {
|
81 | if (!fragment.key) {
|
82 | throw new Error(
|
83 | `InitFragment with mergeAll function must have a valid key: ${fragment.constructor.name}`
|
84 | );
|
85 | }
|
86 | const oldValue = keyedFragments.get(fragment.key);
|
87 | if (oldValue === undefined) {
|
88 | keyedFragments.set(fragment.key, fragment);
|
89 | } else if (Array.isArray(oldValue)) {
|
90 | oldValue.push(fragment);
|
91 | } else {
|
92 | keyedFragments.set(fragment.key, [oldValue, fragment]);
|
93 | }
|
94 | continue;
|
95 | } else if (typeof fragment.merge === "function") {
|
96 | const oldValue = keyedFragments.get(fragment.key);
|
97 | if (oldValue !== undefined) {
|
98 | keyedFragments.set(fragment.key, fragment.merge(oldValue));
|
99 | continue;
|
100 | }
|
101 | }
|
102 | keyedFragments.set(fragment.key || Symbol(), fragment);
|
103 | }
|
104 |
|
105 | const concatSource = new ConcatSource();
|
106 | const endContents = [];
|
107 | for (let fragment of keyedFragments.values()) {
|
108 | if (Array.isArray(fragment)) {
|
109 | fragment = fragment[0].mergeAll(fragment);
|
110 | }
|
111 | concatSource.add(fragment.getContent(context));
|
112 | const endContent = fragment.getEndContent(context);
|
113 | if (endContent) {
|
114 | endContents.push(endContent);
|
115 | }
|
116 | }
|
117 |
|
118 | concatSource.add(source);
|
119 | for (const content of endContents.reverse()) {
|
120 | concatSource.add(content);
|
121 | }
|
122 | return concatSource;
|
123 | } else {
|
124 | return source;
|
125 | }
|
126 | }
|
127 |
|
128 | serialize(context) {
|
129 | const { write } = context;
|
130 |
|
131 | write(this.content);
|
132 | write(this.stage);
|
133 | write(this.position);
|
134 | write(this.key);
|
135 | write(this.endContent);
|
136 | }
|
137 |
|
138 | deserialize(context) {
|
139 | const { read } = context;
|
140 |
|
141 | this.content = read();
|
142 | this.stage = read();
|
143 | this.position = read();
|
144 | this.key = read();
|
145 | this.endContent = read();
|
146 | }
|
147 | }
|
148 |
|
149 | makeSerializable(InitFragment, "webpack/lib/InitFragment");
|
150 |
|
151 | InitFragment.prototype.merge = undefined;
|
152 |
|
153 | InitFragment.STAGE_CONSTANTS = 10;
|
154 | InitFragment.STAGE_ASYNC_BOUNDARY = 20;
|
155 | InitFragment.STAGE_HARMONY_EXPORTS = 30;
|
156 | InitFragment.STAGE_HARMONY_IMPORTS = 40;
|
157 | InitFragment.STAGE_PROVIDES = 50;
|
158 | InitFragment.STAGE_ASYNC_DEPENDENCIES = 60;
|
159 | InitFragment.STAGE_ASYNC_HARMONY_IMPORTS = 70;
|
160 |
|
161 | module.exports = InitFragment;
|