UNPKG

3.66 kBJavaScriptView Raw
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");
9
10/** @typedef {import("webpack-sources").Source} Source */
11/** @typedef {import("./Generator").GenerateContext} GenerateContext */
12
13/**
14 * @param {InitFragment} fragment the init fragment
15 * @param {number} index index
16 * @returns {[InitFragment, number]} tuple with both
17 */
18const extractFragmentIndex = (fragment, index) => [fragment, index];
19
20/**
21 * @param {[InitFragment, number]} a first pair
22 * @param {[InitFragment, number]} b second pair
23 * @returns {number} sort value
24 */
25const 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
33class InitFragment {
34 /**
35 * @param {string|Source} content the source code that will be included as initialization code
36 * @param {number} stage category of initialization code (contribute to order)
37 * @param {number} position position in the category (contribute to order)
38 * @param {string=} key unique key to avoid emitting the same initialization code twice
39 * @param {string|Source=} endContent the source code that will be included at the end of the module
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 * @param {GenerateContext} generateContext context for generate
51 * @returns {string|Source} the source code that will be included as initialization code
52 */
53 getContent(generateContext) {
54 return this.content;
55 }
56
57 /**
58 * @param {GenerateContext} generateContext context for generate
59 * @returns {string|Source=} the source code that will be included at the end of the module
60 */
61 getEndContent(generateContext) {
62 return this.endContent;
63 }
64
65 static addToSource(source, initFragments, generateContext) {
66 if (initFragments.length > 0) {
67 // Sort fragments by position. If 2 fragments have the same position,
68 // use their index.
69 const sortedFragments = initFragments
70 .map(extractFragmentIndex)
71 .sort(sortFragmentWithIndex);
72
73 // Deduplicate fragments. If a fragment has no key, it is always included.
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
110InitFragment.prototype.merge = undefined;
111
112InitFragment.STAGE_CONSTANTS = 10;
113InitFragment.STAGE_ASYNC_BOUNDARY = 20;
114InitFragment.STAGE_HARMONY_EXPORTS = 30;
115InitFragment.STAGE_HARMONY_IMPORTS = 40;
116InitFragment.STAGE_PROVIDES = 50;
117InitFragment.STAGE_ASYNC_DEPENDENCIES = 60;
118InitFragment.STAGE_ASYNC_HARMONY_IMPORTS = 70;
119
120module.exports = InitFragment;