source: imaps-frontend/node_modules/webpack/lib/InitFragment.js

main
Last change on this file was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 4 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 5.3 KB
RevLine 
[79a0317]1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Florent Cailhol @ooflorent
4*/
5
6"use strict";
7
8const { ConcatSource } = require("webpack-sources");
9const makeSerializable = require("./util/makeSerializable");
10
11/** @typedef {import("webpack-sources").Source} Source */
12/** @typedef {import("./Generator").GenerateContext} GenerateContext */
13/** @typedef {import("./serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
14/** @typedef {import("./serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
15
16/**
17 * @template T
18 * @param {InitFragment<T>} fragment the init fragment
19 * @param {number} index index
20 * @returns {[InitFragment<T>, number]} tuple with both
21 */
22const extractFragmentIndex = (fragment, index) => [fragment, index];
23
24/**
25 * @template T
26 * @param {[InitFragment<T>, number]} a first pair
27 * @param {[InitFragment<T>, number]} b second pair
28 * @returns {number} sort value
29 */
30const sortFragmentWithIndex = ([a, i], [b, j]) => {
31 const stageCmp = a.stage - b.stage;
32 if (stageCmp !== 0) return stageCmp;
33 const positionCmp = a.position - b.position;
34 if (positionCmp !== 0) return positionCmp;
35 return i - j;
36};
37
38/**
39 * @template GenerateContext
40 */
41class InitFragment {
42 /**
43 * @param {string | Source | undefined} content the source code that will be included as initialization code
44 * @param {number} stage category of initialization code (contribute to order)
45 * @param {number} position position in the category (contribute to order)
46 * @param {string=} key unique key to avoid emitting the same initialization code twice
47 * @param {string | Source=} endContent the source code that will be included at the end of the module
48 */
49 constructor(content, stage, position, key, endContent) {
50 this.content = content;
51 this.stage = stage;
52 this.position = position;
53 this.key = key;
54 this.endContent = endContent;
55 }
56
57 /**
58 * @param {GenerateContext} context context
59 * @returns {string | Source | undefined} the source code that will be included as initialization code
60 */
61 getContent(context) {
62 return this.content;
63 }
64
65 /**
66 * @param {GenerateContext} context context
67 * @returns {string|Source=} the source code that will be included at the end of the module
68 */
69 getEndContent(context) {
70 return this.endContent;
71 }
72
73 /**
74 * @template Context
75 * @template T
76 * @param {Source} source sources
77 * @param {InitFragment<T>[]} initFragments init fragments
78 * @param {Context} context context
79 * @returns {Source} source
80 */
81 static addToSource(source, initFragments, context) {
82 if (initFragments.length > 0) {
83 // Sort fragments by position. If 2 fragments have the same position,
84 // use their index.
85 const sortedFragments = initFragments
86 .map(extractFragmentIndex)
87 .sort(sortFragmentWithIndex);
88
89 // Deduplicate fragments. If a fragment has no key, it is always included.
90 const keyedFragments = new Map();
91 for (const [fragment] of sortedFragments) {
92 if (
93 typeof (
94 /** @type {InitFragment<T> & { mergeAll?: (fragments: InitFragment<Context>[]) => InitFragment<Context>[] }} */
95 (fragment).mergeAll
96 ) === "function"
97 ) {
98 if (!fragment.key) {
99 throw new Error(
100 `InitFragment with mergeAll function must have a valid key: ${fragment.constructor.name}`
101 );
102 }
103 const oldValue = keyedFragments.get(fragment.key);
104 if (oldValue === undefined) {
105 keyedFragments.set(fragment.key, fragment);
106 } else if (Array.isArray(oldValue)) {
107 oldValue.push(fragment);
108 } else {
109 keyedFragments.set(fragment.key, [oldValue, fragment]);
110 }
111 continue;
112 } else if (typeof fragment.merge === "function") {
113 const oldValue = keyedFragments.get(fragment.key);
114 if (oldValue !== undefined) {
115 keyedFragments.set(fragment.key, fragment.merge(oldValue));
116 continue;
117 }
118 }
119 keyedFragments.set(fragment.key || Symbol("fragment key"), fragment);
120 }
121
122 const concatSource = new ConcatSource();
123 const endContents = [];
124 for (let fragment of keyedFragments.values()) {
125 if (Array.isArray(fragment)) {
126 fragment = fragment[0].mergeAll(fragment);
127 }
128 concatSource.add(fragment.getContent(context));
129 const endContent = fragment.getEndContent(context);
130 if (endContent) {
131 endContents.push(endContent);
132 }
133 }
134
135 concatSource.add(source);
136 for (const content of endContents.reverse()) {
137 concatSource.add(content);
138 }
139 return concatSource;
140 }
141 return source;
142 }
143
144 /**
145 * @param {ObjectSerializerContext} context context
146 */
147 serialize(context) {
148 const { write } = context;
149
150 write(this.content);
151 write(this.stage);
152 write(this.position);
153 write(this.key);
154 write(this.endContent);
155 }
156
157 /**
158 * @param {ObjectDeserializerContext} context context
159 */
160 deserialize(context) {
161 const { read } = context;
162
163 this.content = read();
164 this.stage = read();
165 this.position = read();
166 this.key = read();
167 this.endContent = read();
168 }
169}
170
171makeSerializable(InitFragment, "webpack/lib/InitFragment");
172
173InitFragment.prototype.merge =
174 /** @type {TODO} */
175 (undefined);
176
177InitFragment.STAGE_CONSTANTS = 10;
178InitFragment.STAGE_ASYNC_BOUNDARY = 20;
179InitFragment.STAGE_HARMONY_EXPORTS = 30;
180InitFragment.STAGE_HARMONY_IMPORTS = 40;
181InitFragment.STAGE_PROVIDES = 50;
182InitFragment.STAGE_ASYNC_DEPENDENCIES = 60;
183InitFragment.STAGE_ASYNC_HARMONY_IMPORTS = 70;
184
185module.exports = InitFragment;
Note: See TracBrowser for help on using the repository browser.