-
Notifications
You must be signed in to change notification settings - Fork 142
/
Copy pathhtml-entrypoint.ts
206 lines (185 loc) · 7.84 KB
/
html-entrypoint.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import { getOrCreate } from '@embroider/shared-internals';
import { readFileSync } from 'fs-extra';
import { join } from 'path';
import { JSDOM } from 'jsdom';
import partition from 'lodash/partition';
import zip from 'lodash/zip';
import Placeholder from './html-placeholder';
import { Variant } from './packager';
export class HTMLEntrypoint {
private dom: JSDOM;
private placeholders: Map<string, Placeholder[]> = new Map();
modules: string[] = [];
scripts: string[] = [];
styles: string[] = [];
constructor(
private pathToVanillaApp: string,
private rootURL: string,
private publicAssetURL: string,
public filename: string
) {
this.dom = new JSDOM(readFileSync(join(this.pathToVanillaApp, this.filename), 'utf8'));
for (let tag of this.handledStyles()) {
let styleTag = tag as HTMLLinkElement;
let href = styleTag.href;
if (!isAbsoluteURL(href)) {
let url = this.relativeToApp(href);
this.styles.push(url);
let placeholder = new Placeholder(styleTag);
let list = getOrCreate(this.placeholders, url, () => []);
list.push(placeholder);
}
}
for (let scriptTag of this.handledScripts()) {
// scriptTag.src include rootURL. Convert it to be relative to the app.
let src = this.relativeToApp(scriptTag.src);
if (scriptTag.type === 'module') {
this.modules.push(src);
} else {
this.scripts.push(src);
}
let placeholder = new Placeholder(scriptTag);
let list = getOrCreate(this.placeholders, src, () => []);
list.push(placeholder);
}
}
private relativeToApp(rootRelativeURL: string) {
return rootRelativeURL.replace(this.rootURL, '');
}
private handledScripts() {
let scriptTags = [...this.dom.window.document.querySelectorAll('script')] as HTMLScriptElement[];
let [ignoredScriptTags, handledScriptTags] = partition(scriptTags, scriptTag => {
return !scriptTag.src || scriptTag.hasAttribute('data-embroider-ignore') || isAbsoluteURL(scriptTag.src);
});
for (let scriptTag of ignoredScriptTags) {
scriptTag.removeAttribute('data-embroider-ignore');
}
return handledScriptTags;
}
private handledStyles() {
let styleTags = [...this.dom.window.document.querySelectorAll('link[rel="stylesheet"]')] as HTMLLinkElement[];
let [ignoredStyleTags, handledStyleTags] = partition(styleTags, styleTag => {
return !styleTag.href || styleTag.hasAttribute('data-embroider-ignore') || isAbsoluteURL(styleTag.href);
});
for (let styleTag of ignoredStyleTags) {
styleTag.removeAttribute('data-embroider-ignore');
}
return handledStyleTags;
}
// bundles maps from input asset to a per-variant map of output assets
render(stats: BundleSummary): string {
let insertedLazy = new Set<string>();
let fastbootVariant = stats.variants.findIndex(v => Boolean(v.runtime === 'fastboot'));
let supportsFastboot = stats.variants.some(v => v.runtime === 'fastboot' || v.runtime === 'all');
for (let [src, placeholders] of this.placeholders) {
let match = stats.entrypoints.get(src);
if (match) {
let firstVariant = stats.variants.findIndex((_, index) => Boolean(match!.get(index)));
let matchingBundles = match.get(firstVariant)!;
let matchingFastbootBundles = fastbootVariant >= 0 ? match.get(fastbootVariant) || [] : [];
for (let placeholder of placeholders) {
if (supportsFastboot && placeholder.isScript()) {
// if there is any fastboot involved, we will emit the lazy bundles
// right before our first script.
let lazyMatch = stats.lazyBundles.get(src);
if (lazyMatch && !insertedLazy.has(src)) {
insertLazyJavascript(lazyMatch, placeholder, this.rootURL);
insertLazyStyles(lazyMatch, placeholder, this.publicAssetURL);
insertedLazy.add(src);
}
}
for (let [base, fastboot] of zip(matchingBundles, matchingFastbootBundles)) {
if (supportsFastboot) {
// the fastboot version gets prefixed with the rootURL, which
// points to our local build directory, because that's where
// fastboot always loads scripts from. If there's no
// fastboot-specific variant, fastboot loads the base variant, but
// still from rootURL rather than publicAssetURL.
fastboot = this.rootURL + (fastboot ?? base);
}
if (base) {
// the browser version gets prefixed with the publicAssetURL
// because that's where browsers will load it from
base = this.publicAssetURL + base;
}
if (!base) {
// this bundle only exists in the fastboot variant
let element = placeholder.start.ownerDocument.createElement('fastboot-script');
element.setAttribute('src', fastboot!);
placeholder.insert(element);
placeholder.insertNewline();
} else if (!fastboot) {
// no fastboot variant
placeholder.insertURL(base);
} else if (fastboot === base) {
// fastboot variant happens to be exactly the same bundle as base
// (and publicAssetURL===rootURL), so a plain script tag covers
// both
placeholder.insertURL(base);
} else {
// we have both and they differ
let element = placeholder.insertURL(base);
if (element) {
element.setAttribute('data-fastboot-src', fastboot);
}
}
}
}
} else {
// no match means keep the original HTML content for this placeholder.
// (If we really wanted it empty instead, there would be matchingBundles
// and it would be an empty list.)
for (let placeholder of placeholders) {
placeholder.reset();
}
}
}
return this.dom.serialize();
}
}
export interface BundleSummary {
// entrypoints.get(inputAsset).get(variantIndex) === outputAssets
//
// these are the output assets that are needed eagerly to boot the given input
// asset
entrypoints: Map<string, Map<number, string[]>>;
// lazyBundles.get(inputAsset) === lazyOutputAssets
//
// these are the output assets that might be loaded lazyily at runtime by the
// given input asset.
//
// These are tracked specifically for the fastboot variant, because that's
// where we need to be responsble for them.
lazyBundles: Map<string, string[]>;
variants: Variant[];
}
function isAbsoluteURL(url: string) {
return /^(?:[a-z]+:)?\/\//i.test(url);
}
// we (somewhat arbitrarily) decide to put the lazy javascript bundles before
// the very first <script> that we have rewritten
function insertLazyJavascript(lazyBundles: string[], placeholder: Placeholder, rootURL: string) {
for (let bundle of lazyBundles) {
if (bundle.endsWith('.js')) {
let element = placeholder.start.ownerDocument.createElement('fastboot-script');
// we're using rootURL instead of publicAssetURL here because
// <fastboot-script> is executed by fastboot, which always loads them from
// the local build directory. NOT from the publicAssetURL that browsers
// will use, which could be a CDN.
element.setAttribute('src', rootURL + bundle);
placeholder.insert(element);
placeholder.insertNewline();
}
}
}
function insertLazyStyles(lazyBundles: string[], placeholder: Placeholder, publicAssetURL: string) {
for (let bundle of lazyBundles) {
if (bundle.endsWith('.css')) {
let element = placeholder.start.ownerDocument.createElement('link');
element.setAttribute('href', publicAssetURL + bundle);
element.setAttribute('rel', 'stylesheet');
placeholder.insert(element);
placeholder.insertNewline();
}
}
}