@tylix/compiler 0.5.1 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/ast/nodes.js +91 -10
- package/src/ast/substituteCapabilityRefs.js +135 -58
- package/src/codegen/emitStatementLines.js +42 -48
- package/src/codegen/generateExpression.js +68 -24
- package/src/codegen/generateMethod.js +5 -3
- package/src/codegen/generatePage.js +74 -44
- package/src/codegen/generateStatement.js +56 -12
- package/src/codegen/generateTemplate.js +124 -53
- package/src/codegen/generateTemplateExpression.js +40 -50
- package/src/codegen/jsGlobals.js +151 -0
- package/src/compileComponent.js +2 -2
- package/src/imports/extractImports.js +112 -26
- package/src/imports/resolveImports.js +186 -0
- package/src/index.js +3 -2
- package/src/lexer/Lexer.js +308 -213
- package/src/lexer/tokenTypes.js +32 -7
- package/src/parser/Parser.js +680 -169
- package/src/parser/languageOptions.js +16 -0
- package/src/parser/parseExpressionString.js +18 -7
- package/src/parser/parsePageFile.js +139 -58
- package/src/parser/parseTemplate.js +202 -26
- package/src/renderPageDocument.js +297 -51
- package/src/runtime/capability.js +32 -17
- package/src/runtime/loadingIndicator.js +91 -29
- package/src/runtime/reactive.js +128 -58
- package/src/typecheck/buildVirtualTs.js +13 -12
- package/src/typecheck/typedTemplateExpr.js +115 -36
- package/src/typecheckPage.js +28 -4
- package/src/imports/resolvePackageComponents.js +0 -41
|
@@ -7,6 +7,7 @@ import { parseComponentFile } from './parser/parseComponentFile.js'
|
|
|
7
7
|
import { parseTemplate } from './parser/parseTemplate.js'
|
|
8
8
|
import { Lexer } from './lexer/Lexer.js'
|
|
9
9
|
import { Parser } from './parser/Parser.js'
|
|
10
|
+
import { normalizeLanguageOptions } from './parser/languageOptions.js'
|
|
10
11
|
import { generatePage } from './codegen/generatePage.js'
|
|
11
12
|
import { generateTemplate } from './codegen/generateTemplate.js'
|
|
12
13
|
import { generateCapability } from './codegen/generateCapability.js'
|
|
@@ -33,14 +34,21 @@ const USE_CHANNEL_SOURCE = fs
|
|
|
33
34
|
.readFileSync(path.join(__dirname, 'runtime', 'useChannel.js'), 'utf-8')
|
|
34
35
|
.replace(/export\s+/g, '')
|
|
35
36
|
|
|
36
|
-
|
|
37
|
-
const NOT_FOUND_SOURCE = fs
|
|
37
|
+
const NOT_FOUND_SOURCE = fs
|
|
38
38
|
.readFileSync(path.join(__dirname, 'runtime', 'notFound.js'), 'utf-8')
|
|
39
39
|
.replace(/export\s+/g, '')
|
|
40
40
|
|
|
41
|
-
|
|
41
|
+
function escapeHtml(text) {
|
|
42
|
+
return String(text)
|
|
43
|
+
.replace(/&/g, '&')
|
|
44
|
+
.replace(/</g, '<')
|
|
45
|
+
.replace(/>/g, '>')
|
|
46
|
+
.replace(/"/g, '"')
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const capabilityCache = new Map() // `${dir}|not=${mode}` -> { signature, code }
|
|
42
50
|
|
|
43
|
-
async function loadCapabilityDefinitions(capabilitiesDir) {
|
|
51
|
+
async function loadCapabilityDefinitions(capabilitiesDir, language) {
|
|
44
52
|
const exists = await fs.promises
|
|
45
53
|
.access(capabilitiesDir)
|
|
46
54
|
.then(() => true)
|
|
@@ -60,7 +68,10 @@ async function loadCapabilityDefinitions(capabilitiesDir) {
|
|
|
60
68
|
.sort()
|
|
61
69
|
.join('|')
|
|
62
70
|
|
|
63
|
-
|
|
71
|
+
// The same directory compiles differently under different language
|
|
72
|
+
// options, so the options are part of the cache key.
|
|
73
|
+
const cacheKey = `${capabilitiesDir}|not=${language.not}`
|
|
74
|
+
const cached = capabilityCache.get(cacheKey)
|
|
64
75
|
if (cached && cached.signature === signature) {
|
|
65
76
|
return cached.code
|
|
66
77
|
}
|
|
@@ -75,6 +86,7 @@ async function loadCapabilityDefinitions(capabilitiesDir) {
|
|
|
75
86
|
try {
|
|
76
87
|
const capabilityNode = new Parser(
|
|
77
88
|
new Lexer(source).tokenize(),
|
|
89
|
+
language,
|
|
78
90
|
).parseCapability()
|
|
79
91
|
return generateCapability(capabilityNode)
|
|
80
92
|
} catch (err) {
|
|
@@ -85,38 +97,148 @@ async function loadCapabilityDefinitions(capabilitiesDir) {
|
|
|
85
97
|
)
|
|
86
98
|
).join('\n\n')
|
|
87
99
|
|
|
88
|
-
capabilityCache.set(
|
|
100
|
+
capabilityCache.set(cacheKey, { signature, code: definitions })
|
|
89
101
|
return definitions
|
|
90
102
|
}
|
|
91
103
|
|
|
92
|
-
|
|
104
|
+
const headCache = new Map() // headFile -> { mtimeMs, html }
|
|
105
|
+
|
|
106
|
+
// Optional project file (app/head.html) whose contents are dropped into
|
|
107
|
+
// <head> on every page: extra <script>, <meta> or <link> tags. A missing
|
|
108
|
+
// file means no extra tags. Cached by modified time so production does not
|
|
109
|
+
// re-read it on every request, while dev edits show up on the next reload.
|
|
110
|
+
async function loadHeadTags(headFile) {
|
|
111
|
+
let stat
|
|
112
|
+
try {
|
|
113
|
+
stat = await fs.promises.stat(headFile)
|
|
114
|
+
} catch {
|
|
115
|
+
return ''
|
|
116
|
+
}
|
|
117
|
+
if (!stat.isFile()) return ''
|
|
118
|
+
|
|
119
|
+
const cached = headCache.get(headFile)
|
|
120
|
+
if (cached && cached.mtimeMs === stat.mtimeMs) return cached.html
|
|
121
|
+
|
|
122
|
+
const html = await fs.promises.readFile(headFile, 'utf-8')
|
|
123
|
+
headCache.set(headFile, { mtimeMs: stat.mtimeMs, html })
|
|
124
|
+
return html
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// The HTML document around every page. This is what renders when the
|
|
128
|
+
// project has no app/index.html. A project file replaces it entirely and
|
|
129
|
+
// uses the same three placeholders.
|
|
130
|
+
const DEFAULT_SHELL = `<!DOCTYPE html>
|
|
131
|
+
<html>
|
|
132
|
+
<head>
|
|
133
|
+
<meta charset="UTF-8">
|
|
134
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
135
|
+
<title>%tylix.title%</title>
|
|
136
|
+
<link rel="icon" type="image/png" href="/favicon.png">
|
|
137
|
+
%tylix.head%
|
|
138
|
+
</head>
|
|
139
|
+
<body>
|
|
140
|
+
%tylix.body%
|
|
141
|
+
</body>
|
|
142
|
+
</html>`
|
|
143
|
+
|
|
144
|
+
const REQUIRED_PLACEHOLDERS = ['%tylix.head%', '%tylix.body%']
|
|
145
|
+
|
|
146
|
+
const shellCache = new Map() // shellFile -> { mtimeMs, html }
|
|
147
|
+
|
|
148
|
+
// Optional project file (app/index.html): the full HTML document, with
|
|
149
|
+
// its own DOCTYPE, <head> and <body>. It must contain %tylix.head% and
|
|
150
|
+
// %tylix.body%; %tylix.title% is optional. Cached by modified time like
|
|
151
|
+
// head.html. A missing file means the built-in shell above.
|
|
152
|
+
async function loadShell(shellFile) {
|
|
153
|
+
let stat
|
|
154
|
+
try {
|
|
155
|
+
stat = await fs.promises.stat(shellFile)
|
|
156
|
+
} catch {
|
|
157
|
+
return DEFAULT_SHELL
|
|
158
|
+
}
|
|
159
|
+
if (!stat.isFile()) return DEFAULT_SHELL
|
|
160
|
+
|
|
161
|
+
const cached = shellCache.get(shellFile)
|
|
162
|
+
if (cached && cached.mtimeMs === stat.mtimeMs) return cached.html
|
|
163
|
+
|
|
164
|
+
const html = await fs.promises.readFile(shellFile, 'utf-8')
|
|
165
|
+
const missing = REQUIRED_PLACEHOLDERS.filter((p) => !html.includes(p))
|
|
166
|
+
if (missing.length > 0) {
|
|
167
|
+
throw new Error(
|
|
168
|
+
`app/index.html is missing ${missing.join(' and ')}. ` +
|
|
169
|
+
'Put %tylix.head% inside <head> and %tylix.body% inside <body>.',
|
|
170
|
+
)
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
shellCache.set(shellFile, { mtimeMs: stat.mtimeMs, html })
|
|
174
|
+
return html
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const loadingCache = new Map() // loadingFile -> { mtimeMs, source }
|
|
178
|
+
|
|
179
|
+
// Optional project file (app/loading.tyx): the loading indicator, written
|
|
180
|
+
// like a layout (<script> and <template>). A missing file returns null and
|
|
181
|
+
// the built-in top bar is used instead. Cached by modified time like
|
|
182
|
+
// head.html and index.html.
|
|
183
|
+
async function loadLoadingPage(loadingFile) {
|
|
184
|
+
let stat
|
|
185
|
+
try {
|
|
186
|
+
stat = await fs.promises.stat(loadingFile)
|
|
187
|
+
} catch {
|
|
188
|
+
return null
|
|
189
|
+
}
|
|
190
|
+
if (!stat.isFile()) return null
|
|
191
|
+
|
|
192
|
+
const cached = loadingCache.get(loadingFile)
|
|
193
|
+
if (cached && cached.mtimeMs === stat.mtimeMs) return cached.source
|
|
194
|
+
|
|
195
|
+
const source = await fs.promises.readFile(loadingFile, 'utf-8')
|
|
196
|
+
loadingCache.set(loadingFile, { mtimeMs: stat.mtimeMs, source })
|
|
197
|
+
return source
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const EMPTY_PAGE = { props: [], state: [], computed: [], actions: [], uses: [] }
|
|
201
|
+
|
|
202
|
+
// Turns a registry key (a tag name such as "AdminCard" or "my-card")
|
|
203
|
+
// into a class name that is always a valid, unique identifier. The
|
|
204
|
+
// index guarantees uniqueness even if two sanitized names coincide,
|
|
205
|
+
// and the prefix keeps a component called Map or Error from shadowing
|
|
206
|
+
// the global of the same name inside the shared inline script.
|
|
207
|
+
function childClassName(name, index) {
|
|
208
|
+
return `__C${index}_${String(name).replace(/[^A-Za-z0-9_$]/g, '_')}`
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// `fallbackClassName` ALWAYS decides the class name -- for native
|
|
212
|
+
// `component Card` files too -- so two files that both declare Card
|
|
213
|
+
// can live in one page.
|
|
214
|
+
function compileComponentSource(source, fallbackClassName, language) {
|
|
93
215
|
const isNativeComponent = /^\s*component\s+\w+/.test(source)
|
|
94
216
|
|
|
95
217
|
if (isNativeComponent) {
|
|
96
|
-
const {
|
|
218
|
+
const { script, template } = parseComponentFile(source)
|
|
97
219
|
const pageNode = substituteCapabilityRefs(
|
|
98
220
|
script.trim().length > 0
|
|
99
|
-
? new Parser(new Lexer(script).tokenize()).parse()
|
|
100
|
-
:
|
|
221
|
+
? new Parser(new Lexer(script).tokenize(), language).parse()
|
|
222
|
+
: EMPTY_PAGE,
|
|
101
223
|
)
|
|
102
224
|
|
|
103
|
-
const classSource = generatePage(pageNode,
|
|
104
|
-
const templateNodes = parseTemplate(template)
|
|
225
|
+
const classSource = generatePage(pageNode, fallbackClassName)
|
|
226
|
+
const templateNodes = parseTemplate(template, language)
|
|
105
227
|
const { code, rootVar } = generateTemplate(templateNodes)
|
|
106
228
|
|
|
107
|
-
return { classSource, code, rootVar, className:
|
|
229
|
+
return { classSource, code, rootVar, className: fallbackClassName }
|
|
108
230
|
}
|
|
109
231
|
|
|
110
232
|
const { script, template } = parseComponent(source)
|
|
111
233
|
|
|
112
234
|
const pageNode = substituteCapabilityRefs(
|
|
113
235
|
script.trim().length > 0
|
|
114
|
-
? new Parser(new Lexer(script).tokenize()).parse()
|
|
115
|
-
:
|
|
236
|
+
? new Parser(new Lexer(script).tokenize(), language).parse()
|
|
237
|
+
: EMPTY_PAGE,
|
|
116
238
|
)
|
|
117
239
|
|
|
118
240
|
const classSource = generatePage(pageNode, fallbackClassName)
|
|
119
|
-
const templateNodes = parseTemplate(template)
|
|
241
|
+
const templateNodes = parseTemplate(template, language)
|
|
120
242
|
const { code, rootVar } = generateTemplate(templateNodes)
|
|
121
243
|
|
|
122
244
|
return { classSource, code, rootVar, className: fallbackClassName }
|
|
@@ -136,17 +258,17 @@ function applyLineOffset(err, offset) {
|
|
|
136
258
|
return err
|
|
137
259
|
}
|
|
138
260
|
|
|
139
|
-
function parseScriptWithLineOffset(script, offset) {
|
|
261
|
+
function parseScriptWithLineOffset(script, offset, language) {
|
|
140
262
|
try {
|
|
141
|
-
return new Parser(new Lexer(script).tokenize()).parse()
|
|
263
|
+
return new Parser(new Lexer(script).tokenize(), language).parse()
|
|
142
264
|
} catch (err) {
|
|
143
265
|
throw applyLineOffset(err, offset)
|
|
144
266
|
}
|
|
145
267
|
}
|
|
146
268
|
|
|
147
|
-
function parseTemplateWithLineOffset(template, offset) {
|
|
269
|
+
function parseTemplateWithLineOffset(template, offset, language) {
|
|
148
270
|
try {
|
|
149
|
-
return parseTemplate(template)
|
|
271
|
+
return parseTemplate(template, language)
|
|
150
272
|
} catch (err) {
|
|
151
273
|
throw applyLineOffset(err, offset)
|
|
152
274
|
}
|
|
@@ -165,8 +287,31 @@ export async function renderPageDocument(
|
|
|
165
287
|
// working exactly as before.
|
|
166
288
|
stylesheets = ['/tailwind.css'],
|
|
167
289
|
capabilitiesDir = path.join(process.cwd(), 'app', 'capabilities'),
|
|
290
|
+
// Optional file of extra <head> tags (scripts, meta, links).
|
|
291
|
+
headFile = path.join(process.cwd(), 'app', 'head.html'),
|
|
292
|
+
// Optional full HTML document that wraps every page.
|
|
293
|
+
shellFile = path.join(process.cwd(), 'app', 'index.html'),
|
|
294
|
+
// Optional loading indicator page, mounted before the page renders.
|
|
295
|
+
loadingFile = path.join(process.cwd(), 'app', 'loading.tyx'),
|
|
296
|
+
// Compiler language switches, e.g. { not: 'loose' }. Omitted means
|
|
297
|
+
// legacy behaviour, so existing callers compile unchanged.
|
|
298
|
+
language: languageOption = {},
|
|
299
|
+
// false turns off the built-in top loading bar. A project loading page
|
|
300
|
+
// (app/loading.tyx) also replaces it.
|
|
301
|
+
loadingBar = true,
|
|
168
302
|
} = {},
|
|
169
303
|
) {
|
|
304
|
+
// createApp already has a variable called `language` that holds
|
|
305
|
+
// 'typescript' | 'javascript'. Passing that here would silently fall
|
|
306
|
+
// back to the defaults, so refuse it loudly instead.
|
|
307
|
+
if (languageOption !== null && typeof languageOption !== 'object') {
|
|
308
|
+
throw new Error(
|
|
309
|
+
`renderPageDocument: the "language" option must be an object like { not: 'loose' }, got ${typeof languageOption} "${languageOption}". ` +
|
|
310
|
+
'The project language ("typescript"/"javascript") is a different setting.',
|
|
311
|
+
)
|
|
312
|
+
}
|
|
313
|
+
const language = normalizeLanguageOptions(languageOption ?? {})
|
|
314
|
+
|
|
170
315
|
const {
|
|
171
316
|
pageName,
|
|
172
317
|
script,
|
|
@@ -178,35 +323,103 @@ export async function renderPageDocument(
|
|
|
178
323
|
|
|
179
324
|
const pageNode = substituteCapabilityRefs(
|
|
180
325
|
script.trim().length > 0
|
|
181
|
-
? parseScriptWithLineOffset(script, scriptStartLine)
|
|
182
|
-
:
|
|
326
|
+
? parseScriptWithLineOffset(script, scriptStartLine, language)
|
|
327
|
+
: EMPTY_PAGE,
|
|
183
328
|
)
|
|
184
329
|
|
|
185
330
|
const classSource = generatePage(pageNode, pageName)
|
|
186
331
|
|
|
187
|
-
const templateNodes = parseTemplateWithLineOffset(
|
|
332
|
+
const templateNodes = parseTemplateWithLineOffset(
|
|
333
|
+
template,
|
|
334
|
+
templateStartLine,
|
|
335
|
+
language,
|
|
336
|
+
)
|
|
188
337
|
const { code, rootVar } = generateTemplate(templateNodes)
|
|
189
338
|
|
|
190
|
-
const capabilityDefinitions = await loadCapabilityDefinitions(
|
|
339
|
+
const capabilityDefinitions = await loadCapabilityDefinitions(
|
|
340
|
+
capabilitiesDir,
|
|
341
|
+
language,
|
|
342
|
+
)
|
|
343
|
+
const headTags = await loadHeadTags(headFile)
|
|
344
|
+
const shell = await loadShell(shellFile)
|
|
345
|
+
|
|
346
|
+
// The loading page is compiled like a layout and mounted before the
|
|
347
|
+
// page's own data requests start, so the very first request shows it.
|
|
348
|
+
// It receives the shared loading state (see runtime/loadingIndicator.js)
|
|
349
|
+
// as its "loading" prop. A failure while mounting it is logged and never
|
|
350
|
+
// blocks the page.
|
|
351
|
+
const loadingSource = await loadLoadingPage(loadingFile)
|
|
352
|
+
let loadingClassSource = ''
|
|
353
|
+
let loadingMountCode = ''
|
|
354
|
+
if (loadingSource !== null) {
|
|
355
|
+
let loadingCompiled
|
|
356
|
+
try {
|
|
357
|
+
loadingCompiled = compileComponentSource(
|
|
358
|
+
loadingSource,
|
|
359
|
+
'__Loading',
|
|
360
|
+
language,
|
|
361
|
+
)
|
|
362
|
+
} catch (err) {
|
|
363
|
+
err.message = `In app/loading.tyx: ${err.message}`
|
|
364
|
+
throw err
|
|
365
|
+
}
|
|
366
|
+
loadingClassSource = loadingCompiled.classSource
|
|
367
|
+
loadingMountCode = `
|
|
368
|
+
try {
|
|
369
|
+
const loadingInstance = new ${loadingCompiled.className}({ loading: __tylixLoading });
|
|
370
|
+
(function (document, instance) {
|
|
371
|
+
${loadingCompiled.code}
|
|
372
|
+
document.body.appendChild(${loadingCompiled.rootVar});
|
|
373
|
+
})(document, loadingInstance);
|
|
374
|
+
if (typeof loadingInstance.__onMount === "function") {
|
|
375
|
+
Promise.resolve(loadingInstance.__onMount()).catch((err) => {
|
|
376
|
+
console.error("loading page onMount failed:", err);
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
} catch (err) {
|
|
380
|
+
console.error("loading page failed:", err);
|
|
381
|
+
}`
|
|
382
|
+
}
|
|
191
383
|
|
|
384
|
+
// Each child is compiled exactly once, under a unique class name
|
|
385
|
+
// derived from its registry key (the tag name the page uses), not
|
|
386
|
+
// from whatever `component X` line the file declares.
|
|
192
387
|
const childNames = Object.keys(childComponents)
|
|
193
|
-
const
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
388
|
+
const compiledChildren = childNames.map((name, i) => {
|
|
389
|
+
try {
|
|
390
|
+
return compileComponentSource(
|
|
391
|
+
childComponents[name],
|
|
392
|
+
childClassName(name, i),
|
|
393
|
+
language,
|
|
394
|
+
)
|
|
395
|
+
} catch (err) {
|
|
396
|
+
err.message = `In component <${name}>: ${err.message}`
|
|
397
|
+
throw err
|
|
398
|
+
}
|
|
399
|
+
})
|
|
400
|
+
const childClassSources = compiledChildren
|
|
401
|
+
.map((c) => c.classSource)
|
|
197
402
|
.join('\n\n')
|
|
198
403
|
|
|
199
|
-
const childRegistrations =
|
|
200
|
-
.map((
|
|
201
|
-
const
|
|
404
|
+
const childRegistrations = compiledChildren
|
|
405
|
+
.map((compiled, i) => {
|
|
406
|
+
const name = childNames[i]
|
|
407
|
+
const label = JSON.stringify(name)
|
|
202
408
|
const { code: childCode, rootVar: childRootVar, className } = compiled
|
|
203
|
-
return ` components[${
|
|
409
|
+
return ` components[${label}] = {
|
|
204
410
|
mount(document, props = {}) {
|
|
205
411
|
const instance = new ${className}(props);
|
|
206
412
|
const node = (function (document, instance) {
|
|
207
413
|
${childCode}
|
|
208
414
|
return ${childRootVar};
|
|
209
415
|
})(document, instance);
|
|
416
|
+
// needs/prefetch first, then onMount, same order as pages.
|
|
417
|
+
// Runs a microtask later, after the caller has attached the node.
|
|
418
|
+
Promise.resolve()
|
|
419
|
+
.then(() => (typeof instance.__ready === "function" ? instance.__ready() : null))
|
|
420
|
+
.catch((err) => console.error("component ready failed (" + ${label} + "):", err))
|
|
421
|
+
.then(() => (typeof instance.__onMount === "function" ? instance.__onMount() : null))
|
|
422
|
+
.catch((err) => console.error("component onMount failed (" + ${label} + "):", err));
|
|
210
423
|
return { node, instance };
|
|
211
424
|
},
|
|
212
425
|
};`
|
|
@@ -217,10 +430,23 @@ ${childCode}
|
|
|
217
430
|
let layoutMountCode = `document.getElementById("app").appendChild(${rootVar});`
|
|
218
431
|
|
|
219
432
|
if (layout !== null) {
|
|
220
|
-
|
|
433
|
+
let layoutCompiled
|
|
434
|
+
try {
|
|
435
|
+
layoutCompiled = compileComponentSource(layout, '__Layout', language)
|
|
436
|
+
} catch (err) {
|
|
437
|
+
err.message = `In _layout.tyx: ${err.message}`
|
|
438
|
+
throw err
|
|
439
|
+
}
|
|
221
440
|
layoutClassSource = layoutCompiled.classSource
|
|
222
441
|
layoutMountCode = `
|
|
223
442
|
const layoutInstance = new ${layoutCompiled.className}();
|
|
443
|
+
if (typeof layoutInstance.__ready === "function") {
|
|
444
|
+
try {
|
|
445
|
+
await layoutInstance.__ready();
|
|
446
|
+
} catch (err) {
|
|
447
|
+
console.error("layout ready failed:", err);
|
|
448
|
+
}
|
|
449
|
+
}
|
|
224
450
|
(function (document, instance, pageRoot) {
|
|
225
451
|
${layoutCompiled.code}
|
|
226
452
|
const slot = ${layoutCompiled.rootVar}.querySelector("[data-tylix-slot]");
|
|
@@ -229,7 +455,14 @@ ${layoutCompiled.code}
|
|
|
229
455
|
}
|
|
230
456
|
slot.appendChild(pageRoot);
|
|
231
457
|
document.getElementById("app").appendChild(${layoutCompiled.rootVar});
|
|
232
|
-
})(document, layoutInstance, ${rootVar})
|
|
458
|
+
})(document, layoutInstance, ${rootVar});
|
|
459
|
+
if (typeof layoutInstance.__onMount === "function") {
|
|
460
|
+
try {
|
|
461
|
+
await layoutInstance.__onMount();
|
|
462
|
+
} catch (err) {
|
|
463
|
+
console.error("layout onMount failed:", err);
|
|
464
|
+
}
|
|
465
|
+
}`
|
|
233
466
|
}
|
|
234
467
|
|
|
235
468
|
const channelsPortScript =
|
|
@@ -240,6 +473,8 @@ ${layoutCompiled.code}
|
|
|
240
473
|
// If __ready() rejects (a failed needs/prefetch call), log it and keep
|
|
241
474
|
// going so the page still renders, instead of showing a blank screen.
|
|
242
475
|
const inlineScript = `
|
|
476
|
+
${loadingClassSource || !loadingBar ? 'window.__TYLIX_CUSTOM_LOADING__ = true;' : ''}
|
|
477
|
+
|
|
243
478
|
${RUNTIME_SOURCE}
|
|
244
479
|
|
|
245
480
|
${CAPABILITY_SOURCE}
|
|
@@ -262,11 +497,14 @@ ${childClassSources}
|
|
|
262
497
|
|
|
263
498
|
${layoutClassSource}
|
|
264
499
|
|
|
500
|
+
${loadingClassSource}
|
|
501
|
+
|
|
265
502
|
document.addEventListener("DOMContentLoaded", () => {
|
|
266
503
|
const instance = new ${pageName}(${JSON.stringify(props)});
|
|
267
504
|
const components = {};
|
|
268
505
|
${childRegistrations}
|
|
269
506
|
(async function (document, instance, components) {
|
|
507
|
+
${loadingMountCode}
|
|
270
508
|
if (typeof instance.__ready === "function") {
|
|
271
509
|
try {
|
|
272
510
|
await instance.__ready();
|
|
@@ -305,6 +543,7 @@ ${layoutMountCode}
|
|
|
305
543
|
{ name: 'page class', code: classSource },
|
|
306
544
|
{ name: 'child components', code: childClassSources },
|
|
307
545
|
{ name: 'layout', code: layoutClassSource },
|
|
546
|
+
{ name: 'loading page', code: loadingClassSource },
|
|
308
547
|
]
|
|
309
548
|
|
|
310
549
|
for (const segment of scriptSegments) {
|
|
@@ -330,12 +569,16 @@ ${layoutMountCode}
|
|
|
330
569
|
throw genErr
|
|
331
570
|
}
|
|
332
571
|
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
572
|
+
// A "</script" anywhere in the generated code (a <pre> sample, a
|
|
573
|
+
// route prop, a string) would end the script element early, and
|
|
574
|
+
// "<!--" can start an HTML comment inside it. Both replacements mean
|
|
575
|
+
// the same thing to the JS engine. Done AFTER the validation above,
|
|
576
|
+
// which must see the unescaped source.
|
|
577
|
+
const safeScript = inlineScript
|
|
578
|
+
.replace(/<\/(script)/gi, '<\\/$1')
|
|
579
|
+
.replace(/<!--/g, '<\\!--')
|
|
580
|
+
|
|
581
|
+
const themeScript = `<script>
|
|
339
582
|
(function () {
|
|
340
583
|
var stored = localStorage.getItem("theme");
|
|
341
584
|
var prefersDark = window.matchMedia("(prefers-color-scheme: dark)").matches;
|
|
@@ -343,15 +586,18 @@ ${layoutMountCode}
|
|
|
343
586
|
document.documentElement.classList.add("dark");
|
|
344
587
|
}
|
|
345
588
|
})();
|
|
346
|
-
</script
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
589
|
+
</script>`
|
|
590
|
+
|
|
591
|
+
const values = {
|
|
592
|
+
title: escapeHtml(pageNode.title ?? pageName),
|
|
593
|
+
head: [channelsPortScript, themeScript, styleLinks, headTags, styleTag]
|
|
594
|
+
.filter((part) => part && part.length > 0)
|
|
595
|
+
.join('\n '),
|
|
596
|
+
body: `<div id="app"></div>\n <script>${safeScript}</script>`,
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
// One pass with a function replacer, so a "$&" or a placeholder-looking
|
|
600
|
+
// string inside the generated script is never treated as a pattern or
|
|
601
|
+
// substituted a second time.
|
|
602
|
+
return shell.replace(/%tylix\.(title|head|body)%/g, (_, key) => values[key])
|
|
357
603
|
}
|
|
@@ -3,9 +3,7 @@
|
|
|
3
3
|
// usesAccessors, which call resolveCapability(name) as a getter).
|
|
4
4
|
// One instance per capability name, shared across every page in the
|
|
5
5
|
// app -- resolveCapability memoizes on first resolution rather than
|
|
6
|
-
// re-running the def
|
|
7
|
-
// createResource's .load() gives resources, just app-scoped instead
|
|
8
|
-
// of per-page.
|
|
6
|
+
// re-running the def.
|
|
9
7
|
//
|
|
10
8
|
// A capability def has the shape:
|
|
11
9
|
// {
|
|
@@ -21,7 +19,12 @@
|
|
|
21
19
|
const registry = new Map()
|
|
22
20
|
|
|
23
21
|
export function defineCapability(name, def) {
|
|
24
|
-
registry.set(name, {
|
|
22
|
+
registry.set(name, {
|
|
23
|
+
def,
|
|
24
|
+
instance: null,
|
|
25
|
+
initStarted: false,
|
|
26
|
+
initPromise: null,
|
|
27
|
+
})
|
|
25
28
|
}
|
|
26
29
|
|
|
27
30
|
export function bindActions(actions, instance) {
|
|
@@ -41,21 +44,33 @@ export function resolveCapability(name) {
|
|
|
41
44
|
}
|
|
42
45
|
|
|
43
46
|
if (!entry.instance) {
|
|
44
|
-
|
|
45
|
-
Object.assign(
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
47
|
+
const instance = reactive({ ...entry.def.state })
|
|
48
|
+
Object.assign(instance, bindActions(entry.def.actions, instance))
|
|
49
|
+
// Exposed so pages can `await Auth.ready()` before making a
|
|
50
|
+
// redirect decision. Defined once, here, so resolving does not
|
|
51
|
+
// keep writing to reactive state.
|
|
52
|
+
instance.ready = () => entry.initPromise ?? Promise.resolve()
|
|
53
|
+
entry.instance = instance
|
|
49
54
|
}
|
|
50
55
|
|
|
51
|
-
|
|
52
|
-
|
|
56
|
+
// init() runs exactly once, whether it is async or not.
|
|
57
|
+
if (!entry.initStarted) {
|
|
58
|
+
entry.initStarted = true
|
|
59
|
+
if (entry.def.init) {
|
|
60
|
+
try {
|
|
61
|
+
entry.initPromise = Promise.resolve(
|
|
62
|
+
entry.def.init.call(entry.instance),
|
|
63
|
+
)
|
|
64
|
+
} catch (err) {
|
|
65
|
+
entry.initPromise = Promise.reject(err)
|
|
66
|
+
}
|
|
67
|
+
// Log here so a failed init is never an unhandled rejection;
|
|
68
|
+
// ready() still rejects for anyone awaiting it.
|
|
69
|
+
entry.initPromise.catch((err) => {
|
|
70
|
+
console.error(`Capability "${name}" init failed:`, err)
|
|
71
|
+
})
|
|
72
|
+
}
|
|
53
73
|
}
|
|
54
74
|
|
|
55
|
-
// Exposed so pages can `await Auth.ready()` before making a
|
|
56
|
-
// redirect decision, instead of racing the reactive `loading` flag
|
|
57
|
-
// with their own onMount timing.
|
|
58
|
-
entry.instance.ready = () => entry.initPromise ?? Promise.resolve()
|
|
59
|
-
|
|
60
75
|
return entry.instance
|
|
61
|
-
}
|
|
76
|
+
}
|