@tylix/compiler 0.5.2 → 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 +270 -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'
|
|
@@ -37,9 +38,17 @@ const NOT_FOUND_SOURCE = fs
|
|
|
37
38
|
.readFileSync(path.join(__dirname, 'runtime', 'notFound.js'), 'utf-8')
|
|
38
39
|
.replace(/export\s+/g, '')
|
|
39
40
|
|
|
40
|
-
|
|
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 }
|
|
41
50
|
|
|
42
|
-
async function loadCapabilityDefinitions(capabilitiesDir) {
|
|
51
|
+
async function loadCapabilityDefinitions(capabilitiesDir, language) {
|
|
43
52
|
const exists = await fs.promises
|
|
44
53
|
.access(capabilitiesDir)
|
|
45
54
|
.then(() => true)
|
|
@@ -59,7 +68,10 @@ async function loadCapabilityDefinitions(capabilitiesDir) {
|
|
|
59
68
|
.sort()
|
|
60
69
|
.join('|')
|
|
61
70
|
|
|
62
|
-
|
|
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)
|
|
63
75
|
if (cached && cached.signature === signature) {
|
|
64
76
|
return cached.code
|
|
65
77
|
}
|
|
@@ -74,6 +86,7 @@ async function loadCapabilityDefinitions(capabilitiesDir) {
|
|
|
74
86
|
try {
|
|
75
87
|
const capabilityNode = new Parser(
|
|
76
88
|
new Lexer(source).tokenize(),
|
|
89
|
+
language,
|
|
77
90
|
).parseCapability()
|
|
78
91
|
return generateCapability(capabilityNode)
|
|
79
92
|
} catch (err) {
|
|
@@ -84,7 +97,7 @@ async function loadCapabilityDefinitions(capabilitiesDir) {
|
|
|
84
97
|
)
|
|
85
98
|
).join('\n\n')
|
|
86
99
|
|
|
87
|
-
capabilityCache.set(
|
|
100
|
+
capabilityCache.set(cacheKey, { signature, code: definitions })
|
|
88
101
|
return definitions
|
|
89
102
|
}
|
|
90
103
|
|
|
@@ -111,34 +124,121 @@ async function loadHeadTags(headFile) {
|
|
|
111
124
|
return html
|
|
112
125
|
}
|
|
113
126
|
|
|
114
|
-
|
|
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) {
|
|
115
215
|
const isNativeComponent = /^\s*component\s+\w+/.test(source)
|
|
116
216
|
|
|
117
217
|
if (isNativeComponent) {
|
|
118
|
-
const {
|
|
218
|
+
const { script, template } = parseComponentFile(source)
|
|
119
219
|
const pageNode = substituteCapabilityRefs(
|
|
120
220
|
script.trim().length > 0
|
|
121
|
-
? new Parser(new Lexer(script).tokenize()).parse()
|
|
122
|
-
:
|
|
221
|
+
? new Parser(new Lexer(script).tokenize(), language).parse()
|
|
222
|
+
: EMPTY_PAGE,
|
|
123
223
|
)
|
|
124
224
|
|
|
125
|
-
const classSource = generatePage(pageNode,
|
|
126
|
-
const templateNodes = parseTemplate(template)
|
|
225
|
+
const classSource = generatePage(pageNode, fallbackClassName)
|
|
226
|
+
const templateNodes = parseTemplate(template, language)
|
|
127
227
|
const { code, rootVar } = generateTemplate(templateNodes)
|
|
128
228
|
|
|
129
|
-
return { classSource, code, rootVar, className:
|
|
229
|
+
return { classSource, code, rootVar, className: fallbackClassName }
|
|
130
230
|
}
|
|
131
231
|
|
|
132
232
|
const { script, template } = parseComponent(source)
|
|
133
233
|
|
|
134
234
|
const pageNode = substituteCapabilityRefs(
|
|
135
235
|
script.trim().length > 0
|
|
136
|
-
? new Parser(new Lexer(script).tokenize()).parse()
|
|
137
|
-
:
|
|
236
|
+
? new Parser(new Lexer(script).tokenize(), language).parse()
|
|
237
|
+
: EMPTY_PAGE,
|
|
138
238
|
)
|
|
139
239
|
|
|
140
240
|
const classSource = generatePage(pageNode, fallbackClassName)
|
|
141
|
-
const templateNodes = parseTemplate(template)
|
|
241
|
+
const templateNodes = parseTemplate(template, language)
|
|
142
242
|
const { code, rootVar } = generateTemplate(templateNodes)
|
|
143
243
|
|
|
144
244
|
return { classSource, code, rootVar, className: fallbackClassName }
|
|
@@ -158,17 +258,17 @@ function applyLineOffset(err, offset) {
|
|
|
158
258
|
return err
|
|
159
259
|
}
|
|
160
260
|
|
|
161
|
-
function parseScriptWithLineOffset(script, offset) {
|
|
261
|
+
function parseScriptWithLineOffset(script, offset, language) {
|
|
162
262
|
try {
|
|
163
|
-
return new Parser(new Lexer(script).tokenize()).parse()
|
|
263
|
+
return new Parser(new Lexer(script).tokenize(), language).parse()
|
|
164
264
|
} catch (err) {
|
|
165
265
|
throw applyLineOffset(err, offset)
|
|
166
266
|
}
|
|
167
267
|
}
|
|
168
268
|
|
|
169
|
-
function parseTemplateWithLineOffset(template, offset) {
|
|
269
|
+
function parseTemplateWithLineOffset(template, offset, language) {
|
|
170
270
|
try {
|
|
171
|
-
return parseTemplate(template)
|
|
271
|
+
return parseTemplate(template, language)
|
|
172
272
|
} catch (err) {
|
|
173
273
|
throw applyLineOffset(err, offset)
|
|
174
274
|
}
|
|
@@ -189,8 +289,29 @@ export async function renderPageDocument(
|
|
|
189
289
|
capabilitiesDir = path.join(process.cwd(), 'app', 'capabilities'),
|
|
190
290
|
// Optional file of extra <head> tags (scripts, meta, links).
|
|
191
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,
|
|
192
302
|
} = {},
|
|
193
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
|
+
|
|
194
315
|
const {
|
|
195
316
|
pageName,
|
|
196
317
|
script,
|
|
@@ -202,36 +323,103 @@ export async function renderPageDocument(
|
|
|
202
323
|
|
|
203
324
|
const pageNode = substituteCapabilityRefs(
|
|
204
325
|
script.trim().length > 0
|
|
205
|
-
? parseScriptWithLineOffset(script, scriptStartLine)
|
|
206
|
-
:
|
|
326
|
+
? parseScriptWithLineOffset(script, scriptStartLine, language)
|
|
327
|
+
: EMPTY_PAGE,
|
|
207
328
|
)
|
|
208
329
|
|
|
209
330
|
const classSource = generatePage(pageNode, pageName)
|
|
210
331
|
|
|
211
|
-
const templateNodes = parseTemplateWithLineOffset(
|
|
332
|
+
const templateNodes = parseTemplateWithLineOffset(
|
|
333
|
+
template,
|
|
334
|
+
templateStartLine,
|
|
335
|
+
language,
|
|
336
|
+
)
|
|
212
337
|
const { code, rootVar } = generateTemplate(templateNodes)
|
|
213
338
|
|
|
214
|
-
const capabilityDefinitions = await loadCapabilityDefinitions(
|
|
339
|
+
const capabilityDefinitions = await loadCapabilityDefinitions(
|
|
340
|
+
capabilitiesDir,
|
|
341
|
+
language,
|
|
342
|
+
)
|
|
215
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
|
+
}
|
|
216
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.
|
|
217
387
|
const childNames = Object.keys(childComponents)
|
|
218
|
-
const
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
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)
|
|
222
402
|
.join('\n\n')
|
|
223
403
|
|
|
224
|
-
const childRegistrations =
|
|
225
|
-
.map((
|
|
226
|
-
const
|
|
404
|
+
const childRegistrations = compiledChildren
|
|
405
|
+
.map((compiled, i) => {
|
|
406
|
+
const name = childNames[i]
|
|
407
|
+
const label = JSON.stringify(name)
|
|
227
408
|
const { code: childCode, rootVar: childRootVar, className } = compiled
|
|
228
|
-
return ` components[${
|
|
409
|
+
return ` components[${label}] = {
|
|
229
410
|
mount(document, props = {}) {
|
|
230
411
|
const instance = new ${className}(props);
|
|
231
412
|
const node = (function (document, instance) {
|
|
232
413
|
${childCode}
|
|
233
414
|
return ${childRootVar};
|
|
234
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));
|
|
235
423
|
return { node, instance };
|
|
236
424
|
},
|
|
237
425
|
};`
|
|
@@ -242,10 +430,23 @@ ${childCode}
|
|
|
242
430
|
let layoutMountCode = `document.getElementById("app").appendChild(${rootVar});`
|
|
243
431
|
|
|
244
432
|
if (layout !== null) {
|
|
245
|
-
|
|
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
|
+
}
|
|
246
440
|
layoutClassSource = layoutCompiled.classSource
|
|
247
441
|
layoutMountCode = `
|
|
248
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
|
+
}
|
|
249
450
|
(function (document, instance, pageRoot) {
|
|
250
451
|
${layoutCompiled.code}
|
|
251
452
|
const slot = ${layoutCompiled.rootVar}.querySelector("[data-tylix-slot]");
|
|
@@ -254,7 +455,14 @@ ${layoutCompiled.code}
|
|
|
254
455
|
}
|
|
255
456
|
slot.appendChild(pageRoot);
|
|
256
457
|
document.getElementById("app").appendChild(${layoutCompiled.rootVar});
|
|
257
|
-
})(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
|
+
}`
|
|
258
466
|
}
|
|
259
467
|
|
|
260
468
|
const channelsPortScript =
|
|
@@ -265,6 +473,8 @@ ${layoutCompiled.code}
|
|
|
265
473
|
// If __ready() rejects (a failed needs/prefetch call), log it and keep
|
|
266
474
|
// going so the page still renders, instead of showing a blank screen.
|
|
267
475
|
const inlineScript = `
|
|
476
|
+
${loadingClassSource || !loadingBar ? 'window.__TYLIX_CUSTOM_LOADING__ = true;' : ''}
|
|
477
|
+
|
|
268
478
|
${RUNTIME_SOURCE}
|
|
269
479
|
|
|
270
480
|
${CAPABILITY_SOURCE}
|
|
@@ -287,11 +497,14 @@ ${childClassSources}
|
|
|
287
497
|
|
|
288
498
|
${layoutClassSource}
|
|
289
499
|
|
|
500
|
+
${loadingClassSource}
|
|
501
|
+
|
|
290
502
|
document.addEventListener("DOMContentLoaded", () => {
|
|
291
503
|
const instance = new ${pageName}(${JSON.stringify(props)});
|
|
292
504
|
const components = {};
|
|
293
505
|
${childRegistrations}
|
|
294
506
|
(async function (document, instance, components) {
|
|
507
|
+
${loadingMountCode}
|
|
295
508
|
if (typeof instance.__ready === "function") {
|
|
296
509
|
try {
|
|
297
510
|
await instance.__ready();
|
|
@@ -330,6 +543,7 @@ ${layoutMountCode}
|
|
|
330
543
|
{ name: 'page class', code: classSource },
|
|
331
544
|
{ name: 'child components', code: childClassSources },
|
|
332
545
|
{ name: 'layout', code: layoutClassSource },
|
|
546
|
+
{ name: 'loading page', code: loadingClassSource },
|
|
333
547
|
]
|
|
334
548
|
|
|
335
549
|
for (const segment of scriptSegments) {
|
|
@@ -355,13 +569,16 @@ ${layoutMountCode}
|
|
|
355
569
|
throw genErr
|
|
356
570
|
}
|
|
357
571
|
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
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>
|
|
365
582
|
(function () {
|
|
366
583
|
var stored = localStorage.getItem("theme");
|
|
367
584
|
var prefersDark = window.matchMedia("(prefers-color-scheme: dark)").matches;
|
|
@@ -369,16 +586,18 @@ ${layoutMountCode}
|
|
|
369
586
|
document.documentElement.classList.add("dark");
|
|
370
587
|
}
|
|
371
588
|
})();
|
|
372
|
-
</script
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
<
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
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])
|
|
384
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
|
+
}
|
|
@@ -1,35 +1,97 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
1
|
+
// Shared state for the loading indicator. The loading page (app/loading.tyx)
|
|
2
|
+
// receives this object as its "loading" prop. When a project has no loading
|
|
3
|
+
// page, a plain top bar is drawn instead, unless the project turned it off
|
|
4
|
+
// with loadingBar: false in tylix.config.
|
|
5
|
+
//
|
|
6
|
+
// active number of requests in flight
|
|
7
|
+
// progress bar width, 0 to 100
|
|
8
|
+
// visible false when nothing is loading
|
|
9
|
+
//
|
|
10
|
+
// The logic below uses plain variables and only WRITES to the reactive
|
|
11
|
+
// object. Reading reactive state here would subscribe whichever template
|
|
12
|
+
// effect happens to be running when a request starts, and creating an
|
|
13
|
+
// effect here would make it a child of that effect, which is torn down
|
|
14
|
+
// the next time the parent re-runs. The built-in bar is therefore painted
|
|
15
|
+
// directly.
|
|
16
|
+
const __tylixLoading = reactive({
|
|
17
|
+
active: 0,
|
|
18
|
+
progress: 0,
|
|
19
|
+
visible: false,
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
let __tylixActive = 0
|
|
23
|
+
let __tylixProgress = 0
|
|
24
|
+
let __tylixVisible = false
|
|
25
|
+
let __tylixHideTimer = null
|
|
26
|
+
let __tylixResetTimer = null
|
|
27
|
+
let __tylixWatchdog = null
|
|
28
|
+
let __tylixFallbackBar = null
|
|
29
|
+
|
|
30
|
+
function __tylixGetFallbackBar() {
|
|
31
|
+
if (window.__TYLIX_CUSTOM_LOADING__) return null
|
|
32
|
+
if (!__tylixFallbackBar) {
|
|
33
|
+
if (!document.body) return null
|
|
34
|
+
const bar = document.createElement('div')
|
|
35
|
+
bar.id = 'tylix-loading-bar'
|
|
36
|
+
bar.setAttribute('aria-hidden', 'true')
|
|
37
|
+
bar.style.cssText =
|
|
38
|
+
'position:fixed;top:0;left:0;height:3px;width:0%;background:#10b981;z-index:99999;pointer-events:none;transition:width 0.2s ease,opacity 0.3s ease;opacity:0;'
|
|
39
|
+
document.body.appendChild(bar)
|
|
40
|
+
__tylixFallbackBar = bar
|
|
41
|
+
}
|
|
42
|
+
return __tylixFallbackBar
|
|
12
43
|
}
|
|
13
44
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
45
|
+
function __tylixApply() {
|
|
46
|
+
__tylixLoading.active = __tylixActive
|
|
47
|
+
__tylixLoading.progress = __tylixProgress
|
|
48
|
+
__tylixLoading.visible = __tylixVisible
|
|
49
|
+
|
|
50
|
+
const bar = __tylixGetFallbackBar()
|
|
51
|
+
if (bar) {
|
|
52
|
+
bar.style.opacity = __tylixVisible ? '1' : '0'
|
|
53
|
+
bar.style.width = __tylixProgress + '%'
|
|
54
|
+
}
|
|
19
55
|
}
|
|
20
56
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
57
|
+
function __tylixFinish() {
|
|
58
|
+
clearTimeout(__tylixHideTimer)
|
|
59
|
+
clearTimeout(__tylixResetTimer)
|
|
60
|
+
__tylixProgress = 100
|
|
61
|
+
__tylixApply()
|
|
62
|
+
__tylixHideTimer = setTimeout(() => {
|
|
63
|
+
if (__tylixActive > 0) return
|
|
64
|
+
__tylixVisible = false
|
|
65
|
+
__tylixApply()
|
|
66
|
+
__tylixResetTimer = setTimeout(() => {
|
|
67
|
+
if (__tylixActive === 0) {
|
|
68
|
+
__tylixProgress = 0
|
|
69
|
+
__tylixApply()
|
|
32
70
|
}
|
|
33
|
-
},
|
|
34
|
-
}
|
|
71
|
+
}, 300)
|
|
72
|
+
}, 150)
|
|
35
73
|
}
|
|
74
|
+
|
|
75
|
+
// Safety net: a request that never settles must not leave the bar up.
|
|
76
|
+
function __tylixForceFinish() {
|
|
77
|
+
__tylixActive = 0
|
|
78
|
+
__tylixFinish()
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function __tylixRequestStart() {
|
|
82
|
+
clearTimeout(__tylixHideTimer)
|
|
83
|
+
clearTimeout(__tylixResetTimer)
|
|
84
|
+
clearTimeout(__tylixWatchdog)
|
|
85
|
+
__tylixActive++
|
|
86
|
+
__tylixVisible = true
|
|
87
|
+
__tylixProgress = 70
|
|
88
|
+
__tylixWatchdog = setTimeout(__tylixForceFinish, 30000)
|
|
89
|
+
__tylixApply()
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function __tylixRequestEnd() {
|
|
93
|
+
__tylixActive = Math.max(0, __tylixActive - 1)
|
|
94
|
+
if (__tylixActive > 0) return
|
|
95
|
+
clearTimeout(__tylixWatchdog)
|
|
96
|
+
__tylixFinish()
|
|
97
|
+
}
|