/
bootstrap-lazy.ts
174 lines (158 loc) · 6.25 KB
/
bootstrap-lazy.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
import * as d from '../declarations';
import { appDidLoad } from './update-component';
import { BUILD } from '@app-data';
import { CMP_FLAGS } from '@utils';
import { connectedCallback } from './connected-callback';
import { convertScopedToShadow, registerStyle } from './styles';
import { createTime, installDevTools } from './profile';
import { disconnectedCallback } from './disconnected-callback';
import { doc, getHostRef, plt, registerHost, win, supportsShadow } from '@platform';
import { hmrStart } from './hmr-component';
import { HYDRATED_CSS, HYDRATED_STYLE_ID, PLATFORM_FLAGS, PROXY_FLAGS } from './runtime-constants';
import { patchCloneNode, patchSlotAppendChild, patchChildSlotNodes } from './dom-extras';
import { proxyComponent } from './proxy-component';
export const bootstrapLazy = (lazyBundles: d.LazyBundlesRuntimeData, options: d.CustomElementsDefineOptions = {}) => {
if (BUILD.profile && performance.mark) {
performance.mark('st:app:start');
}
installDevTools();
const endBootstrap = createTime('bootstrapLazy');
const cmpTags: string[] = [];
const exclude = options.exclude || [];
const customElements = win.customElements;
const head = doc.head;
const metaCharset = /*@__PURE__*/ head.querySelector('meta[charset]');
const visibilityStyle = /*@__PURE__*/ doc.createElement('style');
const deferredConnectedCallbacks: { connectedCallback: () => void }[] = [];
const styles = /*@__PURE__*/ doc.querySelectorAll(`[${HYDRATED_STYLE_ID}]`);
let appLoadFallback: any;
let isBootstrapping = true;
let i = 0;
Object.assign(plt, options);
plt.$resourcesUrl$ = new URL(options.resourcesUrl || './', doc.baseURI).href;
if (BUILD.asyncQueue) {
if (options.syncQueue) {
plt.$flags$ |= PLATFORM_FLAGS.queueSync;
}
}
if (BUILD.hydrateClientSide) {
// If the app is already hydrated there is not point to disable the
// async queue. This will improve the first input delay
plt.$flags$ |= PLATFORM_FLAGS.appLoaded;
}
if (BUILD.hydrateClientSide && BUILD.shadowDom) {
for (; i < styles.length; i++) {
registerStyle(styles[i].getAttribute(HYDRATED_STYLE_ID), convertScopedToShadow(styles[i].innerHTML), true);
}
}
lazyBundles.map(lazyBundle =>
lazyBundle[1].map(compactMeta => {
const cmpMeta: d.ComponentRuntimeMeta = {
$flags$: compactMeta[0],
$tagName$: compactMeta[1],
$members$: compactMeta[2],
$listeners$: compactMeta[3],
};
if (BUILD.member) {
cmpMeta.$members$ = compactMeta[2];
}
if (BUILD.hostListener) {
cmpMeta.$listeners$ = compactMeta[3];
}
if (BUILD.reflect) {
cmpMeta.$attrsToReflect$ = [];
}
if (BUILD.watchCallback) {
cmpMeta.$watchers$ = {};
}
if (BUILD.shadowDom && !supportsShadow && cmpMeta.$flags$ & CMP_FLAGS.shadowDomEncapsulation) {
cmpMeta.$flags$ |= CMP_FLAGS.needsShadowDomShim;
}
const tagName = BUILD.transformTagName && options.transformTagName ? options.transformTagName(cmpMeta.$tagName$) : cmpMeta.$tagName$;
const HostElement = class extends HTMLElement {
['s-p']: Promise<void>[];
['s-rc']: (() => void)[];
// StencilLazyHost
constructor(self: HTMLElement) {
// @ts-ignore
super(self);
self = this;
registerHost(self, cmpMeta);
if (BUILD.shadowDom && cmpMeta.$flags$ & CMP_FLAGS.shadowDomEncapsulation) {
// this component is using shadow dom
// and this browser supports shadow dom
// add the read-only property "shadowRoot" to the host element
// adding the shadow root build conditionals to minimize runtime
if (supportsShadow) {
if (BUILD.shadowDelegatesFocus) {
self.attachShadow({
mode: 'open',
delegatesFocus: !!(cmpMeta.$flags$ & CMP_FLAGS.shadowDelegatesFocus),
});
} else {
self.attachShadow({ mode: 'open' });
}
} else if (!BUILD.hydrateServerSide && !('shadowRoot' in self)) {
(self as any).shadowRoot = self;
}
}
if (BUILD.slotChildNodesFix) {
patchChildSlotNodes(self, cmpMeta);
}
}
connectedCallback() {
if (appLoadFallback) {
clearTimeout(appLoadFallback);
appLoadFallback = null;
}
if (isBootstrapping) {
// connectedCallback will be processed once all components have been registered
deferredConnectedCallbacks.push(this);
} else {
plt.jmp(() => connectedCallback(this));
}
}
disconnectedCallback() {
plt.jmp(() => disconnectedCallback(this));
}
componentOnReady() {
return getHostRef(this).$onReadyPromise$;
}
};
if (BUILD.cloneNodeFix) {
patchCloneNode(HostElement.prototype);
}
if (BUILD.appendChildSlotFix) {
patchSlotAppendChild(HostElement.prototype);
}
if (BUILD.hotModuleReplacement) {
(HostElement as any).prototype['s-hmr'] = function (hmrVersionId: string) {
hmrStart(this, cmpMeta, hmrVersionId);
};
}
cmpMeta.$lazyBundleId$ = lazyBundle[0];
if (!exclude.includes(tagName) && !customElements.get(tagName)) {
cmpTags.push(tagName);
customElements.define(tagName, proxyComponent(HostElement as any, cmpMeta, PROXY_FLAGS.isElementConstructor) as any);
}
}),
);
if (BUILD.hydratedClass || BUILD.hydratedAttribute) {
visibilityStyle.innerHTML = cmpTags + HYDRATED_CSS;
visibilityStyle.setAttribute('data-styles', '');
head.insertBefore(visibilityStyle, metaCharset ? metaCharset.nextSibling : head.firstChild);
}
// Process deferred connectedCallbacks now all components have been registered
isBootstrapping = false;
if (deferredConnectedCallbacks.length) {
deferredConnectedCallbacks.map(host => host.connectedCallback());
} else {
if (BUILD.profile) {
plt.jmp(() => (appLoadFallback = setTimeout(appDidLoad, 30, 'timeout')));
} else {
plt.jmp(() => (appLoadFallback = setTimeout(appDidLoad, 30)));
}
}
// Fallback appLoad event
endBootstrap();
};