Skip to content

Commit c87abd8

Browse files
committed
Add inline SerializeBuilder
reasons?
1 parent ca59626 commit c87abd8

4 files changed

Lines changed: 103 additions & 9 deletions

File tree

packages/host/app/initializers/experimental-rehydrate.ts

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,113 @@
11
import type Application from '@ember/application';
22

3-
import { serializeBuilder } from '@glimmer/node';
43
// @ts-expect-error - glimmer internals not typed for direct import
54
import { clientBuilder, rehydrationBuilder } from '@glimmer/runtime';
5+
// @ts-expect-error - glimmer internals not typed for direct import
6+
import { ConcreteBounds, NewElementBuilder } from '@glimmer/runtime';
67

78
declare const FastBoot: unknown;
89

10+
// Inlined from @glimmer/node to avoid pulling in a second copy of
11+
// @glimmer/runtime (and its transitive @glimmer/global-context) which
12+
// would cause webpack to bundle two uninitialised copies and break at
13+
// runtime with "scheduleDestroyed is not a function".
14+
const NEEDS_EXTRA_CLOSE = new WeakMap();
15+
16+
class SerializeBuilder extends (NewElementBuilder as any) {
17+
serializeBlockDepth = 0;
18+
19+
__openBlock() {
20+
let { tagName } = this.element;
21+
if (tagName !== 'TITLE' && tagName !== 'SCRIPT' && tagName !== 'STYLE') {
22+
let depth = this.serializeBlockDepth++;
23+
this.__appendComment(`%+b:${depth}%`);
24+
}
25+
super.__openBlock();
26+
}
27+
28+
__closeBlock() {
29+
let { tagName } = this.element;
30+
super.__closeBlock();
31+
if (tagName !== 'TITLE' && tagName !== 'SCRIPT' && tagName !== 'STYLE') {
32+
let depth = --this.serializeBlockDepth;
33+
this.__appendComment(`%-b:${depth}%`);
34+
}
35+
}
36+
37+
__appendHTML(html: string) {
38+
let { tagName } = this.element;
39+
if (tagName === 'TITLE' || tagName === 'SCRIPT' || tagName === 'STYLE') {
40+
return super.__appendHTML(html);
41+
}
42+
let first = this.__appendComment('%glmr%');
43+
if (tagName === 'TABLE') {
44+
let openIndex = html.indexOf('<');
45+
if (openIndex > -1 && html.slice(openIndex + 1, openIndex + 3) === 'tr') {
46+
html = `<tbody>${html}</tbody>`;
47+
}
48+
}
49+
if (html === '') {
50+
this.__appendComment('% %');
51+
} else {
52+
super.__appendHTML(html);
53+
}
54+
let last = this.__appendComment('%glmr%');
55+
return new (ConcreteBounds as any)(this.element, first, last);
56+
}
57+
58+
__appendText(string: string) {
59+
let { tagName } = this.element;
60+
let current = ((): any => {
61+
let { element, nextSibling } = this as any;
62+
return nextSibling === null ? element.lastChild : nextSibling.previousSibling;
63+
})();
64+
if (tagName === 'TITLE' || tagName === 'SCRIPT' || tagName === 'STYLE') {
65+
return super.__appendText(string);
66+
}
67+
if (string === '') {
68+
return this.__appendComment('% %');
69+
}
70+
if (current && current.nodeType === 3) {
71+
this.__appendComment('%|%');
72+
}
73+
return super.__appendText(string);
74+
}
75+
76+
closeElement() {
77+
if (NEEDS_EXTRA_CLOSE.has(this.element)) {
78+
NEEDS_EXTRA_CLOSE.delete(this.element);
79+
super.closeElement();
80+
}
81+
return super.closeElement();
82+
}
83+
84+
openElement(tag: string) {
85+
if (
86+
tag === 'tr' &&
87+
this.element.tagName !== 'TBODY' &&
88+
this.element.tagName !== 'THEAD' &&
89+
this.element.tagName !== 'TFOOT'
90+
) {
91+
this.openElement('tbody');
92+
NEEDS_EXTRA_CLOSE.set(this.constructing, true);
93+
this.flushElement(null);
94+
}
95+
return super.openElement(tag);
96+
}
97+
98+
pushRemoteElement(element: any, cursorId: string, insertBefore: any = null) {
99+
let { dom } = this as any;
100+
let script = dom.createElement('script');
101+
script.setAttribute('glmr', cursorId);
102+
dom.insertBefore(element, script, insertBefore);
103+
return super.pushRemoteElement(element, cursorId, insertBefore);
104+
}
105+
}
106+
107+
function serializeBuilder(env: any, cursor: any) {
108+
return SerializeBuilder.forInitialRender(env, cursor);
109+
}
110+
9111
export function initialize(application: Application): void {
10112
// Don't override in FastBoot (server-side) — let Ember's default serialize mode work
11113
if (typeof FastBoot !== 'undefined') {

packages/host/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555
"@embroider/webpack": "^4.0.4",
5656
"@floating-ui/dom": "catalog:",
5757
"@glimmer/component": "catalog:",
58-
"@glimmer/node": "0.92.4",
5958
"@glimmer/tracking": "^1.1.2",
6059
"@glint/core": "1.3.0",
6160
"@glint/environment-ember-loose": "catalog:",

packages/realm-server/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
"@cardstack/local-types": "workspace:*",
1313
"@cardstack/postgres": "workspace:*",
1414
"@cardstack/runtime-common": "workspace:*",
15-
"@glimmer/node": "0.92.4",
1615
"@koa/cors": "catalog:",
1716
"@koa/router": "catalog:",
1817
"@octokit/rest": "catalog:",

pnpm-lock.yaml

Lines changed: 0 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)