clarusjs 0.14.0 → 0.16.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/dist/check/generate.d.ts +2 -5
- package/dist/check/generate.js +119 -5
- package/dist/cli/check.d.ts +10 -2
- package/dist/cli/check.js +38 -12
- package/dist/cli/index.js +4 -1
- package/dist/cli/mapper.js +24 -1
- package/dist/cli/project.d.ts +6 -0
- package/dist/cli/project.js +1 -1
- package/dist/compiler/class-style.js +2 -2
- package/dist/compiler/components.d.ts +61 -0
- package/dist/compiler/components.js +81 -0
- package/dist/compiler/element-attrs.d.ts +7 -0
- package/dist/compiler/element-attrs.js +32 -0
- package/dist/compiler/emit-client.js +23 -3
- package/dist/compiler/emit-server.js +11 -8
- package/dist/compiler/html-expr.d.ts +2 -1
- package/dist/compiler/html-expr.js +15 -9
- package/dist/compiler/index.d.ts +14 -2
- package/dist/compiler/index.js +143 -3
- package/dist/compiler/ir.d.ts +24 -0
- package/dist/compiler/ir.js +81 -11
- package/dist/compiler/lint.d.ts +26 -2
- package/dist/compiler/lint.js +64 -4
- package/dist/compiler/parse.d.ts +37 -0
- package/dist/compiler/parse.js +42 -10
- package/dist/compiler/script.d.ts +11 -0
- package/dist/compiler/script.js +41 -0
- package/dist/compiler/shared.d.ts +6 -0
- package/dist/compiler/shared.js +14 -0
- package/dist/forms/index.js +15 -3
- package/dist/runtime/attrs.d.ts +6 -0
- package/dist/runtime/attrs.js +22 -3
- package/dist/runtime/dom.d.ts +39 -1
- package/dist/runtime/dom.js +159 -23
- package/dist/runtime/html.d.ts +6 -0
- package/dist/runtime/html.js +12 -0
- package/dist/runtime/index.d.ts +1 -1
- package/dist/runtime/index.js +1 -1
- package/dist/runtime/loader.js +53 -7
- package/dist/runtime/router.d.ts +6 -1
- package/dist/runtime/router.js +1 -1
- package/dist/runtime/server.d.ts +8 -2
- package/dist/runtime/server.js +14 -7
- package/dist/server/handler.d.ts +28 -1
- package/dist/server/handler.js +44 -1
- package/dist/server/modes.d.ts +2 -0
- package/dist/server/modes.js +5 -1
- package/dist/vite/build.d.ts +9 -2
- package/dist/vite/build.js +20 -5
- package/dist/vite/libraries.d.ts +32 -0
- package/dist/vite/libraries.js +77 -0
- package/dist/vite/plugin.d.ts +23 -0
- package/dist/vite/plugin.js +52 -24
- package/package.json +1 -1
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
const B = 'boolean';
|
|
2
|
+
const S = 'string';
|
|
3
|
+
const N = 'string | number';
|
|
4
|
+
const union = (...values) => values.map((v) => JSON.stringify(v)).join(' | ');
|
|
5
|
+
const GLOBAL = {
|
|
6
|
+
title: S, accesskey: S, autofocus: B, inert: B, popover: `${union('auto', 'manual', 'hint')} | ''`, spellcheck: `${B} | ${union('true', 'false')}`,
|
|
7
|
+
translate: union('yes', 'no'), draggable: union('true', 'false'), contenteditable: `${B} | ${union('true', 'false', 'plaintext-only')}`,
|
|
8
|
+
enterkeyhint: union('enter', 'done', 'go', 'next', 'previous', 'search', 'send'), inputmode: union('none', 'text', 'decimal', 'numeric', 'tel', 'search', 'email', 'url'),
|
|
9
|
+
autocapitalize: union('off', 'none', 'on', 'sentences', 'words', 'characters'),
|
|
10
|
+
};
|
|
11
|
+
const FORM_FIELD = { disabled: B, form: S, name: S };
|
|
12
|
+
const SUBMIT = { formaction: S, formenctype: S, formmethod: union('get', 'post', 'dialog'), formnovalidate: B, formtarget: S };
|
|
13
|
+
export const ELEMENT_ATTRS = {
|
|
14
|
+
button: { ...FORM_FIELD, ...SUBMIT, type: union('submit', 'reset', 'button'), value: N, popovertarget: S, popovertargetaction: union('toggle', 'show', 'hide'), command: S, commandfor: S },
|
|
15
|
+
input: {
|
|
16
|
+
...FORM_FIELD, ...SUBMIT, type: S, value: N, checked: B, placeholder: S, required: B, readonly: B, multiple: B, min: N, max: N, step: N,
|
|
17
|
+
minlength: N, maxlength: N, pattern: S, size: N, accept: S, alt: S, src: S, width: N, height: N, list: S, autocomplete: S, dirname: S, capture: S,
|
|
18
|
+
popovertarget: S, popovertargetaction: union('toggle', 'show', 'hide'),
|
|
19
|
+
},
|
|
20
|
+
textarea: { ...FORM_FIELD, value: S, placeholder: S, required: B, readonly: B, rows: N, cols: N, minlength: N, maxlength: N, wrap: union('soft', 'hard', 'off'), autocomplete: S, dirname: S },
|
|
21
|
+
select: { ...FORM_FIELD, value: S, required: B, multiple: B, size: N, autocomplete: S },
|
|
22
|
+
option: { disabled: B, label: S, selected: B, value: N },
|
|
23
|
+
a: { href: S, target: S, download: `${S} | ${B}`, rel: S, hreflang: S, type: S, referrerpolicy: S, ping: S },
|
|
24
|
+
img: { src: S, alt: S, srcset: S, sizes: S, width: N, height: N, loading: union('eager', 'lazy'), decoding: union('sync', 'async', 'auto'), fetchpriority: union('high', 'low', 'auto'), crossorigin: S, referrerpolicy: S, usemap: S, ismap: B },
|
|
25
|
+
form: { action: S, method: union('get', 'post', 'dialog'), enctype: S, target: S, novalidate: B, autocomplete: union('on', 'off'), name: S, rel: S, 'accept-charset': S },
|
|
26
|
+
label: { for: S },
|
|
27
|
+
details: { open: B, name: S },
|
|
28
|
+
dialog: { open: B, closedby: union('any', 'closerequest', 'none') },
|
|
29
|
+
};
|
|
30
|
+
export function extendedAttrs(tag) {
|
|
31
|
+
return { ...GLOBAL, ...ELEMENT_ATTRS[tag] };
|
|
32
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { FOR_IMPLICIT } from './parse.js';
|
|
2
2
|
import { attrValueExpr, componentCall, htmlExpr, tagArg, walk, whichExpr, rowNames, rowParams, rowDerived } from './html-expr.js';
|
|
3
3
|
import { formName, rewriteClient, rewriteClientExpr, wrapExpr } from './script.js';
|
|
4
|
-
import { indentScript, inputsLine, renderName } from './shared.js';
|
|
4
|
+
import { contextLines, indentScript, inputsLine, renderName } from './shared.js';
|
|
5
5
|
export function emitClient(ir, hot) {
|
|
6
6
|
if (!ir.stateful)
|
|
7
7
|
return 'export {}\n';
|
|
@@ -27,11 +27,16 @@ export function emitClient(ir, hot) {
|
|
|
27
27
|
all.some((n) => n.kind === 'for' && n.block >= 0 && rowNames(n, ir.events).some((x) => x !== '$count')) && 'derived as __cl_derived',
|
|
28
28
|
hasRegions && 'region as __cl_region',
|
|
29
29
|
all.some((n) => n.kind === 'component' && !!n.models?.length) && 'model as __cl_model',
|
|
30
|
+
all.some((n) => n.kind === 'component' && !!n.listens?.length) && 'listen as __cl_listen',
|
|
31
|
+
all.some((n) => n.kind === 'use') && 'use as __cl_useOn',
|
|
32
|
+
ir.sends && 'emit as __cl_emit',
|
|
33
|
+
ir.sends && 'declare as __cl_declare',
|
|
34
|
+
ir.provides && 'provideOn as __cl_provideOn',
|
|
30
35
|
kinds.has('model') && 'modelInput as __cl_modelInput',
|
|
31
36
|
ir.inputs.some((i) => kinds.has('input') || i.model) && 'batch as __cl_batch',
|
|
32
37
|
rendersHtml && 'escape as __cl_escape',
|
|
33
38
|
rendersHtml && all.some((n) => n.kind === 'attr' && n.name !== '...') && 'attrHtml as __cl_attrHtml',
|
|
34
|
-
rendersHtml && all.some((n) => n.kind === 'attr' && n.name === '...') && 'spreadHtml as __cl_spreadHtml',
|
|
39
|
+
rendersHtml && all.some((n) => n.kind === 'attr' && n.name === '...' && !('expr' in n.value && n.value.expr === '__cl_attrs')) && 'spreadHtml as __cl_spreadHtml',
|
|
35
40
|
rendersHtml && hasBlocks && 'blockHtml as __cl_blockHtml',
|
|
36
41
|
rendersHtml && hasFors && 'forHtml as __cl_forHtml',
|
|
37
42
|
hasRegions && 'regionHtml as __cl_regionHtml',
|
|
@@ -54,6 +59,13 @@ export function emitClient(ir, hot) {
|
|
|
54
59
|
out += `${line}\n`;
|
|
55
60
|
out += `\nexport function attach(__cl_root, __cl_props = {}, __cl_state) {\n`;
|
|
56
61
|
out += ` const __cl_class = __cl_props.__cl_class ? " " + __cl_props.__cl_class : ""\n`;
|
|
62
|
+
if (ir.sends)
|
|
63
|
+
out += ` __cl_declare(__cl_root, ${JSON.stringify(ir.sendNames)})\n`;
|
|
64
|
+
if (ir.sends)
|
|
65
|
+
out += ` const emit = (name, value, info) => __cl_emit(__cl_root, name, value, info)\n`;
|
|
66
|
+
out += contextLines(ir, 'undefined');
|
|
67
|
+
if (ir.provides)
|
|
68
|
+
out += ` __cl_provideOn(__cl_root, __cl_cx)\n`;
|
|
57
69
|
const inputs = ir.inputs.filter((i) => ir.signals.get(i.name) === 'input' || ir.signals.get(i.name) === 'model');
|
|
58
70
|
const value = (i, from) => i.default === undefined ? `${from}.${i.name}` : `${from}.${i.name} === undefined ? (${rewriteClientExpr(i.default, ir.signals)}) : ${from}.${i.name}`;
|
|
59
71
|
for (const i of inputs) {
|
|
@@ -97,6 +109,8 @@ function wiring(ir, nodes, scope, ctx, signals, depth) {
|
|
|
97
109
|
lines.push(`__cl_attr(${ctx}, ${a.index}, ${JSON.stringify(a.name)}, () => ${attrValueExpr(a.value, rw.expr)}${tagArg(a)})`);
|
|
98
110
|
if (a.kind === 'connect')
|
|
99
111
|
lines.push(`__cl_connect(${ctx}, ${a.index}, () => ${wrapExpr(rw.expr(a.expr))})`);
|
|
112
|
+
if (a.kind === 'use')
|
|
113
|
+
lines.push(`__cl_useOn(${ctx}, ${a.index}, () => ${wrapExpr(rw.expr(a.expr))})`);
|
|
100
114
|
}
|
|
101
115
|
for (const e of ir.events.filter((ev) => ev.scope === scope)) {
|
|
102
116
|
const body = rewriteClient(e.handler, signals).trim();
|
|
@@ -107,9 +121,15 @@ function wiring(ir, nodes, scope, ctx, signals, depth) {
|
|
|
107
121
|
for (const n of nodes) {
|
|
108
122
|
if (n.kind === 'component' && n.region >= 0) {
|
|
109
123
|
const content = [...n.children, ...n.fills.flatMap((f) => f.nodes)];
|
|
110
|
-
lines.push(`__cl_region(${ctx}, ${n.region}, () => ${componentCall(n, rw)}, ${fn('', wiring(ir, content, `c${n.region}`, ctx, signals, depth))})`);
|
|
124
|
+
lines.push(`__cl_region(${ctx}, ${n.region}, (__cl_cx) => ${componentCall(n, rw)}, ${fn('', wiring(ir, content, `c${n.region}`, ctx, signals, depth))}, __cl_cx)`);
|
|
111
125
|
for (const m of n.models ?? [])
|
|
112
126
|
lines.push(`__cl_model(${ctx}, ${n.region}, ${JSON.stringify(m.name)}, (__cl_v) => { ${rewriteClient(m.set, signals).trim()} })`);
|
|
127
|
+
for (const l of n.listens ?? []) {
|
|
128
|
+
const body = rewriteClient(l.handler, signals).trim();
|
|
129
|
+
const from = ir.imports.find((i) => i.name === n.name)?.from ?? '';
|
|
130
|
+
const bound = (from.split(/[\\/]/).pop() ?? '').replace(/\.tscl$/, '').replace(/[^\w-]/g, '');
|
|
131
|
+
lines.push(`__cl_listen(${ctx}, ${n.region}, ${JSON.stringify(l.name)}, ($event, $info) => { ${body}${body.includes('//') ? '\n ' : ' '}}, ${JSON.stringify(bound)})`);
|
|
132
|
+
}
|
|
113
133
|
}
|
|
114
134
|
if (n.kind === 'if' && n.block >= 0) {
|
|
115
135
|
const branches = n.branches.map((b, i) => ` { html: () => ${htmlExpr(b.nodes, rw)}, wire: ${fn('', wiring(ir, b.nodes, `${n.block}.${i}`, ctx, signals, depth))} },`);
|
|
@@ -1,24 +1,27 @@
|
|
|
1
1
|
import { asWritten, htmlExpr, walk } from './html-expr.js';
|
|
2
2
|
import { rewriteServer } from './script.js';
|
|
3
|
-
import { indentScript, inputsLine, renderName } from './shared.js';
|
|
3
|
+
import { contextLines, indentScript, inputsLine, renderName } from './shared.js';
|
|
4
4
|
export function emitServer(ir, clientUrl, renderImports = false, loader) {
|
|
5
5
|
const all = walk(ir.nodes);
|
|
6
|
-
const html = htmlExpr(ir.nodes, asWritten, { island: ir.stateful ? ir.name : null });
|
|
6
|
+
const html = htmlExpr(ir.nodes, asWritten, { island: ir.stateful ? ir.name : null, top: true });
|
|
7
7
|
const runtime = [
|
|
8
8
|
'escape as __cl_escape',
|
|
9
9
|
all.some((n) => n.kind === 'hole' && n.json) && 'json as __cl_json',
|
|
10
10
|
ir.stateful && 'island as __cl_island',
|
|
11
11
|
all.some((n) => n.kind === 'attr' && n.name !== '...') && 'attr as __cl_attrHtml',
|
|
12
|
-
|
|
12
|
+
html.includes('__cl_spreadHtml(') && 'spreadHtml as __cl_spreadHtml',
|
|
13
|
+
html.includes('__cl_passedHtml(') && 'passedHtml as __cl_passedHtml',
|
|
13
14
|
all.some((n) => n.kind === 'attr' && JSON.stringify(n.value).includes('__cl_classValue(')) && 'classValue as __cl_classValue',
|
|
14
15
|
all.some((n) => n.kind === 'attr' && JSON.stringify(n.value).includes('__cl_styleValue(')) && 'styleValue as __cl_styleValue',
|
|
15
16
|
all.some((n) => (n.kind === 'if' || n.kind === 'await') && n.block >= 0) && 'blockHtml as __cl_blockHtml',
|
|
16
17
|
all.some((n) => n.kind === 'await' && n.block < 0) && 'awaitHtml as __cl_awaitHtml',
|
|
17
18
|
all.some((n) => n.kind === 'for' && n.block >= 0) && 'forHtml as __cl_forHtml',
|
|
18
19
|
all.some((n) => n.kind === 'component' && n.region >= 0) && 'regionHtml as __cl_regionHtml',
|
|
20
|
+
all.some((n) => n.kind === 'slot') && 'slot as __cl_slot',
|
|
19
21
|
loader && 'loaded as __cl_loaded',
|
|
20
22
|
html.includes('__cl_join(') && 'join as __cl_join',
|
|
21
23
|
html.includes('__cl_sync(') && 'sync as __cl_sync',
|
|
24
|
+
html.includes('__cl_attrs') && 'passed as __cl_passed',
|
|
22
25
|
].filter(Boolean).join(', ');
|
|
23
26
|
let out = `import { ${runtime} } from 'clarusjs/runtime/server'\n`;
|
|
24
27
|
for (const imp of ir.imports)
|
|
@@ -31,16 +34,16 @@ export function emitServer(ir, clientUrl, renderImports = false, loader) {
|
|
|
31
34
|
out += `${line}\n`;
|
|
32
35
|
const props = `{ ${[...ir.inputs.map((i) => i.name), '...(__cl_opts?.class && { __cl_class: __cl_opts.class })'].join(', ')} }`;
|
|
33
36
|
out += `\nexport ${ir.server?.awaits ? 'async ' : ''}function render(__cl_props = {}, __cl_slots, __cl_opts) {\n`;
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
else {
|
|
37
|
-
out += unknownSlotCheck(ir);
|
|
37
|
+
out += unknownSlotCheck(ir);
|
|
38
|
+
if (!ir.stateful)
|
|
38
39
|
out += ` if (__cl_opts?.attach) throw new Error(${JSON.stringify(`[clarusjs] <${ir.name}> has no <script>, so attach="`)} + __cl_opts.attach + '" does nothing')\n`;
|
|
39
|
-
}
|
|
40
40
|
if (loader)
|
|
41
41
|
out += ` __cl_props = { ...__cl_loaded(${JSON.stringify(loader)}), ...__cl_props }\n`;
|
|
42
42
|
out += inputsLine(ir);
|
|
43
43
|
out += ` const __cl_class = __cl_opts?.class ? " " + __cl_opts.class : ""\n`;
|
|
44
|
+
if (html.includes('__cl_attrs'))
|
|
45
|
+
out += ` const __cl_attrs = __cl_passed(__cl_props, ${JSON.stringify(ir.inputs.map((i) => i.name))}${ir.extends ? `, ${JSON.stringify(ir.extends)}` : ''})\n`;
|
|
46
|
+
out += contextLines(ir, '__cl_opts?.cx');
|
|
44
47
|
if (ir.script)
|
|
45
48
|
out += indentScript(rewriteServer(ir.script));
|
|
46
49
|
if (ir.server)
|
|
@@ -15,8 +15,9 @@ export declare const asWritten: Rewriter;
|
|
|
15
15
|
*/
|
|
16
16
|
export type ServerCtx = {
|
|
17
17
|
island: string | null;
|
|
18
|
+
top?: boolean;
|
|
18
19
|
};
|
|
19
|
-
export declare function htmlExpr(nodes: IRNode[], rw?: Rewriter,
|
|
20
|
+
export declare function htmlExpr(nodes: IRNode[], rw?: Rewriter, top?: ServerCtx): string;
|
|
20
21
|
/** `render_child({ props }, { slots })` for a component node. */
|
|
21
22
|
export declare function componentCall(n: Extract<IRNode, {
|
|
22
23
|
kind: 'component';
|
|
@@ -3,14 +3,20 @@ import { renderName } from './shared.js';
|
|
|
3
3
|
import { wrapExpr } from './script.js';
|
|
4
4
|
export const asWritten = { expr: (e) => e, without: () => asWritten };
|
|
5
5
|
const mayWait = (nodes) => walk(nodes).some((n) => n.kind === 'component' || n.kind === 'slot' || (n.kind === 'await' && n.block < 0));
|
|
6
|
-
export function htmlExpr(nodes, rw = asWritten,
|
|
6
|
+
export function htmlExpr(nodes, rw = asWritten, top) {
|
|
7
|
+
const ctx = top?.top ? { island: top.island } : top;
|
|
7
8
|
const parts = nodes.map((n) => {
|
|
8
9
|
if (n.kind === 'static')
|
|
9
10
|
return JSON.stringify(n.html);
|
|
10
|
-
if (n.kind === 'slot')
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
if (n.kind === 'slot') {
|
|
12
|
+
const data = n.props.length ? `{ ${n.props.map((p) => `${p.name === '__proto__' ? '["__proto__"]' : p.name}: ${wrapExpr(rw.expr(p.expr))}`).join(', ')} }` : 'undefined';
|
|
13
|
+
const placed = `__cl_slot(__cl_slots?.[${JSON.stringify(n.name)}], ${data}, () => ${n.fallback.length ? htmlExpr(n.fallback, rw, ctx) : '""'}, __cl_cx)`;
|
|
14
|
+
return ctx?.island ? `__cl_join(["<!--[s-->", ${placed}, "<!--]s-->"])` : placed;
|
|
15
|
+
}
|
|
16
|
+
if (n.kind === 'connect' || n.kind === 'use')
|
|
13
17
|
return '""';
|
|
18
|
+
if (n.kind === 'attr' && n.name === '...' && 'expr' in n.value && n.value.expr === '__cl_attrs')
|
|
19
|
+
return `__cl_passedHtml(__cl_attrs, ${JSON.stringify((n.skip ?? []).filter((x) => x !== 'engine' && x !== 'use' && !x.includes('.')))})`;
|
|
14
20
|
if (n.kind === 'attr' && n.name === '...')
|
|
15
21
|
return `__cl_spreadHtml(${attrValueExpr(n.value, rw.expr)}, ${JSON.stringify(n.skip ?? [])}, ${JSON.stringify(n.tag)}${rw === asWritten && n.where ? `, ${JSON.stringify(n.where)}` : ''})`;
|
|
16
22
|
if (n.kind === 'attr')
|
|
@@ -52,18 +58,18 @@ export function htmlExpr(nodes, rw = asWritten, ctx) {
|
|
|
52
58
|
const value = `__cl_escape(${wrapExpr(rw.expr(n.expr))})`;
|
|
53
59
|
return n.reactive ? `"<!--[${n.index}-->" + ${value} + "<!--]-->"` : value;
|
|
54
60
|
});
|
|
55
|
-
if (ctx && !ctx.island && mayWait(nodes))
|
|
61
|
+
if (ctx && (!ctx.island || (top?.top && nodes.some((n) => n.kind === 'slot'))) && mayWait(nodes))
|
|
56
62
|
return `__cl_join([${parts.join(', ')}])`;
|
|
57
63
|
return parts.length ? parts.join(' + ') : '""';
|
|
58
64
|
}
|
|
59
65
|
export function componentCall(n, rw, ctx) {
|
|
60
|
-
const props = n.props.map((p) => (p.name === '...' ? `...${wrapExpr(rw.expr(p.expr))}` : `${p.name}: ${wrapExpr(rw.expr(p.expr))}`)).join(', ');
|
|
66
|
+
const props = n.props.map((p) => (p.name === '...' ? `...${wrapExpr(rw.expr(p.expr))}` : `${/^[A-Za-z_$][\w$]*$/.test(p.name) ? p.name : JSON.stringify(p.name)}: ${wrapExpr(rw.expr(p.expr))}`)).join(', ');
|
|
61
67
|
const slots = [
|
|
62
|
-
...(n.children.length ? [`"default": ${htmlExpr(n.children, rw, ctx)}`] : []),
|
|
63
|
-
...n.fills.map((f) => `${JSON.stringify(f.name)}: ${htmlExpr(f.nodes, rw, ctx)}`),
|
|
68
|
+
...(n.children.length ? [`"default": (_, __cl_cx) => ${htmlExpr(n.children, rw, ctx)}`] : []),
|
|
69
|
+
...n.fills.map((f) => `${JSON.stringify(f.name)}: (${f.params ?? '_'}, __cl_cx) => ${htmlExpr(f.nodes, rw, ctx)}`),
|
|
64
70
|
];
|
|
65
71
|
const slotsArg = slots.length ? `{ ${slots.join(', ')} }` : undefined;
|
|
66
|
-
const opts = [...(n.attach ? [`attach: ${JSON.stringify(n.attach)}`] : []), ...(n.class ? [`class: ${JSON.stringify(n.class)}`] : []), ...(n.paths ? [`paths: { ${n.paths.map((p) => `${JSON.stringify(p.name)}: ${wrapExpr(rw.expr(p.expr))}`).join(', ')} }`] : [])];
|
|
72
|
+
const opts = ['cx: __cl_cx', ...(n.attach ? [`attach: ${JSON.stringify(n.attach)}`] : []), ...(n.class ? [`class: ${JSON.stringify(n.class)}`] : []), ...(n.paths ? [`paths: { ${n.paths.map((p) => `${JSON.stringify(p.name)}: ${wrapExpr(rw.expr(p.expr))}`).join(', ')} }`] : [])];
|
|
67
73
|
if (opts.length)
|
|
68
74
|
return `${renderName(n.name)}({ ${props} }, ${slotsArg ?? 'undefined'}, { ${opts.join(', ')} })`;
|
|
69
75
|
return slotsArg ? `${renderName(n.name)}({ ${props} }, ${slotsArg})` : `${renderName(n.name)}({ ${props} })`;
|
package/dist/compiler/index.d.ts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { ClarusError } from './errors.ts';
|
|
2
|
+
import { type ComponentNames } from './components.ts';
|
|
2
3
|
export { ClarusError } from './errors.ts';
|
|
3
4
|
export { parse } from './parse.ts';
|
|
5
|
+
export { componentIndex, COMPONENT_DIRS, libraryNames, withLibraries, type ComponentNames, type Library } from './components.ts';
|
|
4
6
|
export { checkIcons, type IconResolver } from './icon.ts';
|
|
5
7
|
import type { IconResolver } from './icon.ts';
|
|
6
|
-
export { lint, lintRules, type LintRule, type LintLevel, type LintConfig, type LintMessage, type LintRuleDef, type LintContext, type LintElement } from './lint.ts';
|
|
7
|
-
export type { ParsedFile, InputDecl, ImportDecl, TemplateNode, Attr } from './parse.ts';
|
|
8
|
+
export { applyFixes, lint, lintRules, type LintFix, type LintRule, type LintLevel, type LintConfig, type LintMessage, type LintRuleDef, type LintContext, type LintElement } from './lint.ts';
|
|
9
|
+
export type { ParsedFile, InputDecl, EventDecl, ImportDecl, TemplateNode, Attr } from './parse.ts';
|
|
8
10
|
/**
|
|
9
11
|
* `renderImports`: import child components' render functions with `?render`, for a render module that runs in the
|
|
10
12
|
* browser (a client route's page, or a component re-rendered inside an island).
|
|
@@ -16,6 +18,16 @@ export type { ParsedFile, InputDecl, ImportDecl, TemplateNode, Attr } from './pa
|
|
|
16
18
|
* @see https://clarusjs.com/reference/compiler#compile
|
|
17
19
|
*/
|
|
18
20
|
export type CompileOptions = {
|
|
21
|
+
/**
|
|
22
|
+
* The page's layouts (#456), outermost first, as import specifiers: its folder and config layouts, which the plugin
|
|
23
|
+
* finds. The page's template is wrapped in them (each gets the route's `path` and `params`); `@layout none` skips them.
|
|
24
|
+
*/
|
|
25
|
+
layouts?: string[];
|
|
26
|
+
/**
|
|
27
|
+
* Automatic imports (#458): tag names → component paths, from `componentIndex()`. A tag the template uses that's
|
|
28
|
+
* here and not imported by the file is imported; other hyphenated tags stay HTML.
|
|
29
|
+
*/
|
|
30
|
+
components?: ComponentNames;
|
|
19
31
|
file: string;
|
|
20
32
|
target: 'server' | 'client';
|
|
21
33
|
clientUrl?: string;
|
package/dist/compiler/index.js
CHANGED
|
@@ -1,17 +1,28 @@
|
|
|
1
1
|
import { emitClient } from './emit-client.js';
|
|
2
2
|
import { ClarusError } from './errors.js';
|
|
3
3
|
import { emitServer } from './emit-server.js';
|
|
4
|
-
import { lower } from './ir.js';
|
|
5
|
-
import {
|
|
4
|
+
import { lower, SPREAD_REFUSED } from './ir.js';
|
|
5
|
+
import { autoImport } from './components.js';
|
|
6
|
+
import { ELEMENT_ATTRS } from './element-attrs.js';
|
|
7
|
+
import { awaitLists, fragmentRoot, parse, rootNodes } from './parse.js';
|
|
6
8
|
export { ClarusError } from './errors.js';
|
|
7
9
|
export { parse } from './parse.js';
|
|
10
|
+
export { componentIndex, COMPONENT_DIRS, libraryNames, withLibraries } from './components.js';
|
|
8
11
|
import { foldClass } from './class-style.js';
|
|
9
12
|
export { checkIcons } from './icon.js';
|
|
10
|
-
export { lint, lintRules } from './lint.js';
|
|
13
|
+
export { applyFixes, lint, lintRules } from './lint.js';
|
|
11
14
|
export function compile(source, options) {
|
|
12
15
|
const parsed = parse(source, options.file);
|
|
16
|
+
checkExtends(parsed, options.file, source);
|
|
17
|
+
const fragmentRoot = unwrapFragments(parsed, options.file, source);
|
|
18
|
+
if (options.components)
|
|
19
|
+
autoImport(parsed, options.components, options.file, source);
|
|
20
|
+
if (options.layouts?.length && parsed.layout !== 'none')
|
|
21
|
+
wrapInLayouts(parsed, options.layouts, options.file, source);
|
|
13
22
|
const warnings = [];
|
|
14
23
|
const id = options.scope ?? scopeId(options.file);
|
|
24
|
+
if (!fragmentRoot)
|
|
25
|
+
fallThrough(parsed);
|
|
15
26
|
foldClass(parsed.nodes, new Set(parsed.imports.map((imp) => imp.name)));
|
|
16
27
|
markRoots(parsed.nodes, `cl-c-${id}`, new Set(parsed.imports.map((imp) => imp.name)), parsed.styles.some((st) => st.mode === 'scoped'));
|
|
17
28
|
let css = '';
|
|
@@ -76,6 +87,119 @@ function markSlotContent(nodes, cls, components) {
|
|
|
76
87
|
}
|
|
77
88
|
}
|
|
78
89
|
}
|
|
90
|
+
const ID_LISTS = ['aria-describedby', 'aria-labelledby', 'aria-controls', 'aria-owns'];
|
|
91
|
+
function checkExtends(parsed, file, source) {
|
|
92
|
+
const ext = parsed.extends;
|
|
93
|
+
if (!ext)
|
|
94
|
+
return;
|
|
95
|
+
const fail = (text, at) => { throw new ClarusError(text, file, source, at); };
|
|
96
|
+
if (!ELEMENT_ATTRS[ext.tag])
|
|
97
|
+
fail(`@extends ${ext.tag}: clarusjs knows the attributes of ${Object.keys(ELEMENT_ATTRS).join(', ')}`, ext.start);
|
|
98
|
+
const top = rootNodes;
|
|
99
|
+
const nodes = top(parsed.nodes);
|
|
100
|
+
const is = (list) => list.length === 1 && list[0].type === 'element' && list[0].tag === ext.tag;
|
|
101
|
+
const spread = (el) => el.type === 'element' && el.attrs.some((a) => a.kind === 'prop' && a.name === '...');
|
|
102
|
+
for (const r of [nodes, ...(nodes[0]?.type === 'if' ? nodes[0].branches.map((b) => top(b.children)) : [])])
|
|
103
|
+
if (is(r) && spread(r[0]))
|
|
104
|
+
fail(`@extends ${ext.tag}: its root has its own [...] spread, which takes the place of the attributes passed to it; pass what it spreads as inputs instead`, r[0].start);
|
|
105
|
+
if (is(nodes))
|
|
106
|
+
return;
|
|
107
|
+
const first = nodes[0];
|
|
108
|
+
const wrong = `@extends ${ext.tag}: the root must be a <${ext.tag}>, where the attributes land`;
|
|
109
|
+
if (nodes.length !== 1 || first.type !== 'if')
|
|
110
|
+
return fail(wrong, (nodes[0] ?? { start: ext.start }).start);
|
|
111
|
+
if (parsed.script || parsed.inputs.some((i) => i.model))
|
|
112
|
+
fail(`@extends ${ext.tag}: an island's root must be one <${ext.tag}> (it re-renders an @if in the browser, without the passed attributes)`, first.start);
|
|
113
|
+
if (first.branches.at(-1).cond !== null || !first.branches.every((b) => is(top(b.children))))
|
|
114
|
+
fail(wrong, first.start);
|
|
115
|
+
}
|
|
116
|
+
function unwrapFragments(parsed, file, source) {
|
|
117
|
+
const reserved = parsed.imports.find((i) => i.name === 'fragment');
|
|
118
|
+
if (reserved)
|
|
119
|
+
throw new ClarusError('fragment is reserved: <fragment> groups elements without rendering one; import the component under another name', file, source, reserved.start);
|
|
120
|
+
const isNode = (x) => !!x && typeof x === 'object' && 'type' in x;
|
|
121
|
+
const holders = (n) => {
|
|
122
|
+
const out = [];
|
|
123
|
+
for (const v of Object.values(n)) {
|
|
124
|
+
const items = Array.isArray(v) ? v : [v];
|
|
125
|
+
for (const x of items)
|
|
126
|
+
if (x && typeof x === 'object' && !isNode(x) && Array.isArray(x.children))
|
|
127
|
+
out.push(x);
|
|
128
|
+
}
|
|
129
|
+
return out;
|
|
130
|
+
};
|
|
131
|
+
const flat = (nodes) => nodes.flatMap((n) => {
|
|
132
|
+
if ('children' in n)
|
|
133
|
+
n.children = flat(n.children);
|
|
134
|
+
const r = n;
|
|
135
|
+
for (const key of ['pending', 'empty'])
|
|
136
|
+
if (Array.isArray(r[key]))
|
|
137
|
+
r[key] = flat(r[key]);
|
|
138
|
+
for (const h of holders(n))
|
|
139
|
+
h.children = flat(h.children);
|
|
140
|
+
if (n.type !== 'element' || n.tag !== 'fragment')
|
|
141
|
+
return [n];
|
|
142
|
+
if (n.attrs.length)
|
|
143
|
+
throw new ClarusError('<fragment> renders no element: attributes and events can\'t land on it', file, source, n.start);
|
|
144
|
+
return n.children;
|
|
145
|
+
});
|
|
146
|
+
const root = fragmentRoot(parsed.nodes);
|
|
147
|
+
parsed.nodes = parsed.nodes.flatMap((n) => {
|
|
148
|
+
if (n.type !== 'element' || n.tag !== 'fragment' || n.attrs.length)
|
|
149
|
+
return [n];
|
|
150
|
+
const kept = n.children.filter((c) => !(c.type === 'text' && !c.value.trim()));
|
|
151
|
+
const head = kept[0];
|
|
152
|
+
if (head?.type === 'text')
|
|
153
|
+
kept[0] = { ...head, value: head.value.replace(/^[ \t]*\r?\n/, '') };
|
|
154
|
+
return kept;
|
|
155
|
+
});
|
|
156
|
+
parsed.nodes = flat(parsed.nodes);
|
|
157
|
+
return root;
|
|
158
|
+
}
|
|
159
|
+
function fallThrough(parsed) {
|
|
160
|
+
const meaningfulOf = (nodes) => nodes.filter((n) => !(n.type === 'text' && !n.value.replace(/<!--[\s\S]*?-->/g, '').trim()) && !(n.type === 'element' && UNMARKED.has(n.tag)));
|
|
161
|
+
const meaningful = meaningfulOf(parsed.nodes);
|
|
162
|
+
if (meaningful.length !== 1)
|
|
163
|
+
return;
|
|
164
|
+
const top = meaningful[0];
|
|
165
|
+
if (top.type === 'if') {
|
|
166
|
+
const roots = top.branches.map((b) => meaningfulOf(b.children));
|
|
167
|
+
if (parsed.script || parsed.inputs.some((i) => i.model) || top.branches.at(-1).cond !== null || roots.some((r) => r.length !== 1 || r[0].type !== 'element'))
|
|
168
|
+
return;
|
|
169
|
+
for (const [r] of roots)
|
|
170
|
+
fallOnto(parsed, r);
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
if (top.type === 'element')
|
|
174
|
+
fallOnto(parsed, top);
|
|
175
|
+
}
|
|
176
|
+
function fallOnto(parsed, root) {
|
|
177
|
+
if (SPREAD_REFUSED.has(root.tag.toLowerCase()))
|
|
178
|
+
return;
|
|
179
|
+
if (root.attrs.some((a) => a.kind === 'prop' && a.name === '...'))
|
|
180
|
+
return;
|
|
181
|
+
const island = !!parsed.script || parsed.inputs.some((i) => i.model);
|
|
182
|
+
if (island && parsed.imports.some((i) => i.name === root.tag))
|
|
183
|
+
return;
|
|
184
|
+
root.attrs.push({ kind: 'prop', name: '...', expr: '__cl_attrs', start: root.start });
|
|
185
|
+
for (const [i, a] of root.attrs.entries()) {
|
|
186
|
+
if (a.kind !== 'static' || typeof a.value !== 'string' || !ID_LISTS.includes(a.name.toLowerCase()))
|
|
187
|
+
continue;
|
|
188
|
+
const more = `__cl_attrs[${JSON.stringify(a.name)}] == null ? "" : " " + __cl_attrs[${JSON.stringify(a.name)}]`;
|
|
189
|
+
root.attrs[i] = { kind: 'interp', name: a.name, parts: [a.value, { expr: more, start: a.start }], start: a.start };
|
|
190
|
+
}
|
|
191
|
+
if (parsed.imports.some((i) => i.name === root.tag))
|
|
192
|
+
return;
|
|
193
|
+
if (root.attrs.some((a) => (a.name === 'style' || a.name.startsWith('style.')) && a.kind !== 'event' && a.kind !== 'static'))
|
|
194
|
+
return;
|
|
195
|
+
const style = root.attrs.findIndex((a) => a.name === 'style' && a.kind !== 'event');
|
|
196
|
+
const ownStyle = root.attrs[style];
|
|
197
|
+
if (ownStyle?.kind === 'static' && typeof ownStyle.value === 'string') {
|
|
198
|
+
root.attrs[style] = { kind: 'interp', name: 'style', parts: [ownStyle.value, { expr: '__cl_attrs.style == null ? "" : "; " + __cl_attrs.style', start: ownStyle.start }], start: ownStyle.start };
|
|
199
|
+
}
|
|
200
|
+
else if (!ownStyle)
|
|
201
|
+
root.attrs.push({ kind: 'prop', name: 'style', expr: '__cl_attrs.style', start: root.start });
|
|
202
|
+
}
|
|
79
203
|
function markRoots(nodes, cls, components, slots = false) {
|
|
80
204
|
for (const node of nodes) {
|
|
81
205
|
if (node.type === 'element' && !components.has(node.tag) && !UNMARKED.has(node.tag)) {
|
|
@@ -110,3 +234,19 @@ function markRoots(nodes, cls, components, slots = false) {
|
|
|
110
234
|
}
|
|
111
235
|
}
|
|
112
236
|
}
|
|
237
|
+
function wrapInLayouts(parsed, layouts, file, source) {
|
|
238
|
+
if (parsed.script || parsed.inputs.some((i) => i.model)) {
|
|
239
|
+
throw new ClarusError('this page is an island (it has a <script> or a @model input), and folder or config layouts can\'t wrap an island yet: move the script into a component the page uses, or add @layout none', file, source, 0);
|
|
240
|
+
}
|
|
241
|
+
let nodes = parsed.nodes;
|
|
242
|
+
for (let k = layouts.length - 1; k >= 0; k--) {
|
|
243
|
+
const name = `__cl_layout_${k}`;
|
|
244
|
+
parsed.imports.push({ name, from: layouts[k], start: 0 });
|
|
245
|
+
const attrs = [
|
|
246
|
+
{ kind: 'prop', name: 'path', expr: "__cl_opts?.route?.path ?? ''", start: 0 },
|
|
247
|
+
{ kind: 'prop', name: 'params', expr: '__cl_opts?.route?.params ?? {}', start: 0 },
|
|
248
|
+
];
|
|
249
|
+
nodes = [{ type: 'element', tag: name, attrs, children: nodes, start: 0 }];
|
|
250
|
+
}
|
|
251
|
+
parsed.nodes = nodes;
|
|
252
|
+
}
|
package/dist/compiler/ir.d.ts
CHANGED
|
@@ -22,9 +22,14 @@ export type IRNode = {
|
|
|
22
22
|
name: string;
|
|
23
23
|
set: string;
|
|
24
24
|
}[];
|
|
25
|
+
listens?: {
|
|
26
|
+
name: string;
|
|
27
|
+
handler: string;
|
|
28
|
+
}[];
|
|
25
29
|
children: IRNode[];
|
|
26
30
|
fills: {
|
|
27
31
|
name: string;
|
|
32
|
+
params?: string;
|
|
28
33
|
nodes: IRNode[];
|
|
29
34
|
}[];
|
|
30
35
|
region: number;
|
|
@@ -37,6 +42,10 @@ export type IRNode = {
|
|
|
37
42
|
} | {
|
|
38
43
|
kind: 'slot';
|
|
39
44
|
name: string;
|
|
45
|
+
props: {
|
|
46
|
+
name: string;
|
|
47
|
+
expr: string;
|
|
48
|
+
}[];
|
|
40
49
|
fallback: IRNode[];
|
|
41
50
|
} | {
|
|
42
51
|
kind: 'attr';
|
|
@@ -57,6 +66,10 @@ export type IRNode = {
|
|
|
57
66
|
kind: 'connect';
|
|
58
67
|
expr: string;
|
|
59
68
|
index: number;
|
|
69
|
+
} | {
|
|
70
|
+
kind: 'use';
|
|
71
|
+
expr: string;
|
|
72
|
+
index: number;
|
|
60
73
|
} | {
|
|
61
74
|
kind: 'if';
|
|
62
75
|
branches: {
|
|
@@ -96,6 +109,8 @@ export type ComponentIR = {
|
|
|
96
109
|
file: string;
|
|
97
110
|
name: string;
|
|
98
111
|
inputs: InputDecl[];
|
|
112
|
+
/** `@extends tag`: the attribute names passed through to the root besides the usual ones. */
|
|
113
|
+
extends?: string[];
|
|
99
114
|
imports: ImportDecl[];
|
|
100
115
|
script?: string;
|
|
101
116
|
scriptImports: string[];
|
|
@@ -110,9 +125,18 @@ export type ComponentIR = {
|
|
|
110
125
|
stateful: boolean;
|
|
111
126
|
nodes: IRNode[];
|
|
112
127
|
events: IREvent[];
|
|
128
|
+
/** The component sends events to its parent (@event, #442): its script gets emit(). */
|
|
129
|
+
sends: boolean;
|
|
130
|
+
/** Their names: the parent's (name) listens for these as component events, not DOM events. */
|
|
131
|
+
sendNames: string[];
|
|
132
|
+
/** @context declarations (#465): values read from the components around this one. */
|
|
133
|
+
contexts: InputDecl[];
|
|
134
|
+
/** The script calls provide() (#465): this component adds to the context its children render with. */
|
|
135
|
+
provides: boolean;
|
|
113
136
|
/** Things that compile but likely aren't meant (located, not thrown). */
|
|
114
137
|
warnings: ClarusError[];
|
|
115
138
|
};
|
|
139
|
+
export declare const SPREAD_REFUSED: Set<string>;
|
|
116
140
|
export declare function lower(parsed: ParsedFile, file: string, source: string, options?: {
|
|
117
141
|
collapseWhitespace?: boolean;
|
|
118
142
|
icon?: IconResolver;
|