clarusjs 0.19.1 → 0.21.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/README.md +1 -1
- package/dist/check/generate.js +124 -25
- package/dist/cli/index.js +17 -0
- package/dist/cli/migrate-files.d.ts +5 -0
- package/dist/cli/migrate-files.js +22 -0
- package/dist/cli/migrate.d.ts +8 -0
- package/dist/cli/migrate.js +317 -0
- package/dist/compiler/emit-client.js +7 -4
- package/dist/compiler/emit-server.js +10 -3
- package/dist/compiler/factories.d.ts +38 -0
- package/dist/compiler/factories.js +273 -0
- package/dist/compiler/html-expr.d.ts +4 -0
- package/dist/compiler/html-expr.js +5 -1
- package/dist/compiler/index.js +8 -9
- package/dist/compiler/ir.d.ts +13 -0
- package/dist/compiler/ir.js +41 -12
- package/dist/compiler/jsdoc.d.ts +15 -0
- package/dist/compiler/jsdoc.js +81 -0
- package/dist/compiler/lint.js +9 -4
- package/dist/compiler/parse.d.ts +35 -9
- package/dist/compiler/parse.js +77 -8
- package/dist/compiler/script.d.ts +15 -5
- package/dist/compiler/script.js +112 -18
- package/dist/compiler/shared.d.ts +5 -0
- package/dist/compiler/shared.js +5 -3
- package/dist/compiler/strip-types.d.ts +20 -0
- package/dist/compiler/strip-types.js +148 -0
- package/dist/forms/index.js +26 -5
- package/dist/runtime/context.d.ts +39 -0
- package/dist/runtime/context.js +4 -0
- package/dist/runtime/fragments.js +6 -2
- package/dist/runtime/index.d.ts +1 -0
- package/dist/runtime/index.js +1 -0
- package/dist/runtime/navigate.js +2 -1
- package/dist/runtime/signals.d.ts +11 -6
- package/dist/runtime/signals.js +12 -3
- package/dist/vite/base-clash.d.ts +9 -0
- package/dist/vite/base-clash.js +54 -0
- package/dist/vite/build-app.d.ts +5 -0
- package/dist/vite/build-app.js +185 -0
- package/dist/vite/dev-server.d.ts +5 -0
- package/dist/vite/dev-server.js +67 -0
- package/dist/vite/graph.d.ts +45 -0
- package/dist/vite/graph.js +125 -0
- package/dist/vite/hmr.d.ts +19 -0
- package/dist/vite/hmr.js +53 -0
- package/dist/vite/links.d.ts +2 -0
- package/dist/vite/links.js +2 -0
- package/dist/vite/options.d.ts +147 -0
- package/dist/vite/options.js +1 -0
- package/dist/vite/output.d.ts +25 -0
- package/dist/vite/output.js +49 -0
- package/dist/vite/plugin.d.ts +4 -150
- package/dist/vite/plugin.js +68 -624
- package/dist/vite/preview.d.ts +3 -0
- package/dist/vite/preview.js +100 -0
- package/dist/vite/server-entry.d.ts +18 -0
- package/dist/vite/server-entry.js +83 -0
- package/dist/vite/shared.d.ts +34 -0
- package/dist/vite/shared.js +47 -0
- package/dist/vite/styles.d.ts +16 -0
- package/dist/vite/styles.js +56 -0
- package/package.json +6 -4
package/README.md
CHANGED
|
@@ -14,7 +14,7 @@ Write components in a familiar template language. clarusjs compiles them to plai
|
|
|
14
14
|
only for the parts that are interactive. Static sites, server rendering and client apps share one component model,
|
|
15
15
|
and you can always see where each line runs.
|
|
16
16
|
|
|
17
|
-
> Status:
|
|
17
|
+
> Status: **0.x, with stable syntax.** The template syntax and the script form (`props()`, `model()`, `events()`, `context()`) are stable: changes to them are additive, or come with `clarusjs migrate` and a deprecation that lasts at least one minor version, marked **Breaking** in the changelog. Other APIs (the Vite plugin's options, runtime internals, the HTML contract) can still change between minor versions.
|
|
18
18
|
|
|
19
19
|
## Install
|
|
20
20
|
|
package/dist/check/generate.js
CHANGED
|
@@ -7,7 +7,7 @@ import { ClarusError } from '../compiler/errors.js';
|
|
|
7
7
|
import { modifierError, splitEvent } from '../compiler/events.js';
|
|
8
8
|
import { outerIndexIn } from '../compiler/loops.js';
|
|
9
9
|
import { FOR_IMPLICIT, parse } from '../compiler/parse.js';
|
|
10
|
-
import { checkScript, providesIn, referencesNames, splitImports,
|
|
10
|
+
import { checkScript, providesIn, referencesNames, scriptAnnotations, scriptTypeImports, splitImports, usesRuntime } from '../compiler/script.js';
|
|
11
11
|
const ENUMERATED = new Set(['draggable', 'spellcheck', 'translate', 'autocorrect', 'contenteditable']);
|
|
12
12
|
const PROPERTY = { readonly: 'readOnly', novalidate: 'noValidate', formnovalidate: 'formNoValidate', allowfullscreen: 'allowFullscreen', playsinline: 'playsInline', nomodule: 'noModule', ismap: 'isMap' };
|
|
13
13
|
function patternNames(params) {
|
|
@@ -67,6 +67,8 @@ export function generateTs(source, file, options = {}) {
|
|
|
67
67
|
emit('declare function state<T>(value: T): T extends never[] ? any[] : T\n');
|
|
68
68
|
emit('declare function derived<T>(fn: () => T): T\n');
|
|
69
69
|
emit('declare function shared<T>(store: { get(): T }): T\n');
|
|
70
|
+
if (parsed.script && usesRuntime(parsed.script.code, [], 'effect'))
|
|
71
|
+
emit('declare function effect(fn: () => void | (() => void)): () => void\n');
|
|
70
72
|
emit('declare function __cl_use(value: unknown): void\n');
|
|
71
73
|
emit('declare const __cl_bool: boolean\n');
|
|
72
74
|
const transitions = (nodes) => nodes.some((n) => {
|
|
@@ -85,8 +87,12 @@ export function generateTs(source, file, options = {}) {
|
|
|
85
87
|
emit(`export type __cl_Style = ${text} | __cl_Style[] | { [property: string]: ${text} }\n`);
|
|
86
88
|
emit(`type __cl_AttrOf<T extends string, P extends string> = P extends '...' ? { [name: string]: unknown } | null | undefined : P extends 'class' ? __cl_Class : P extends \`class.\${string}\` ? unknown : P extends 'style' ? __cl_Style : T extends keyof HTMLElementTagNameMap ? P extends keyof HTMLElementTagNameMap[T] ? (HTMLElementTagNameMap[T][P] extends boolean ? boolean | null | undefined : ${text}) : ${text} : ${text}\n`);
|
|
87
89
|
emit('declare function __cl_attr<T extends string, P extends string>(value: __cl_AttrOf<T, P>): void\n');
|
|
90
|
+
const eventType = (type, tag) => {
|
|
91
|
+
const k = JSON.stringify(type), t = JSON.stringify(tag);
|
|
92
|
+
const el = `T extends keyof HTMLElementTagNameMap ? HTMLElementTagNameMap[T] : T extends keyof SVGElementTagNameMap ? SVGElementTagNameMap[T] : HTMLElement`;
|
|
93
|
+
return `{ [K in ${k}]: { [T in ${t}]: (K extends keyof HTMLElementEventMap ? HTMLElementEventMap[K] : CustomEvent) & { target: any; currentTarget: ${el} } }[${t}] }[${k}]`;
|
|
94
|
+
};
|
|
88
95
|
emit('export type __cl_El<T extends string> = T extends keyof HTMLElementTagNameMap ? HTMLElementTagNameMap[T] : T extends keyof SVGElementTagNameMap ? SVGElementTagNameMap[T] : HTMLElement\n');
|
|
89
|
-
emit('export type __cl_Event<K extends string, E = HTMLElement> = (K extends keyof HTMLElementEventMap ? HTMLElementEventMap[K] : CustomEvent) & { target: any; currentTarget: E }\n');
|
|
90
96
|
for (const t of parsed.typeImports) {
|
|
91
97
|
emit('import type ');
|
|
92
98
|
copy(t.clause, t.clauseStart);
|
|
@@ -102,21 +108,48 @@ export function generateTs(source, file, options = {}) {
|
|
|
102
108
|
if (options.loader)
|
|
103
109
|
emit(`import { load as __cl_load } from ${JSON.stringify(options.loader)}\n`);
|
|
104
110
|
const script = parsed.script ? splitImports(parsed.script.code, file, source, parsed.script.start) : null;
|
|
105
|
-
const provides = !!parsed.script && !!script && providesIn(script, file, source, parsed.script.start)
|
|
111
|
+
const provides = (!!parsed.script && !!script && providesIn(script, file, source, parsed.script.start))
|
|
112
|
+
|| (!!parsed.serverScript && providesIn(splitImports(parsed.serverScript.code, file, source, parsed.serverScript.start, true), file, source, parsed.serverScript.start));
|
|
106
113
|
if (parsed.script && script) {
|
|
107
114
|
for (const imp of script.imports) {
|
|
108
115
|
copy(imp.text, parsed.script.start + imp.start);
|
|
109
116
|
emit('\n');
|
|
110
117
|
}
|
|
118
|
+
if (parsed.script.ts) {
|
|
119
|
+
for (const t of [...(parsed.script.typeImports ?? []), ...(parsed.script.typeDecls ?? [])]) {
|
|
120
|
+
if (parsed.script.ts.startsWith(t.text, t.start))
|
|
121
|
+
copy(t.text, parsed.script.start + t.start);
|
|
122
|
+
else
|
|
123
|
+
emit(t.text);
|
|
124
|
+
emit('\n');
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
else
|
|
128
|
+
for (const line of scriptTypeImports(parsed.script.code))
|
|
129
|
+
emit(`${line}\n`);
|
|
111
130
|
}
|
|
112
131
|
const serverScript = parsed.serverScript ? splitImports(parsed.serverScript.code, file, source, parsed.serverScript.start, true) : null;
|
|
113
132
|
if (parsed.serverScript && serverScript) {
|
|
133
|
+
if (parsed.serverScript.ts) {
|
|
134
|
+
for (const t of [...(parsed.serverScript.typeImports ?? []), ...(parsed.serverScript.typeDecls ?? [])]) {
|
|
135
|
+
if (parsed.serverScript.ts.startsWith(t.text, t.start))
|
|
136
|
+
copy(t.text, parsed.serverScript.start + t.start);
|
|
137
|
+
else
|
|
138
|
+
emit(t.text);
|
|
139
|
+
emit('\n');
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
else
|
|
143
|
+
for (const line of scriptTypeImports(parsed.serverScript.code))
|
|
144
|
+
emit(`${line}\n`);
|
|
114
145
|
for (const imp of serverScript.imports) {
|
|
115
146
|
copy(imp.text, parsed.serverScript.start + imp.start);
|
|
116
147
|
emit('\n');
|
|
117
148
|
}
|
|
118
149
|
}
|
|
119
150
|
for (const t of parsed.types) {
|
|
151
|
+
if (t.doc)
|
|
152
|
+
emit(t.doc + '\n');
|
|
120
153
|
emit(t.kind === 'interface' ? `interface ${t.name}${t.params ?? ''} ` : `type ${t.name}${t.params ?? ''} = `);
|
|
121
154
|
copy(t.type, t.typeStart);
|
|
122
155
|
emit('\n');
|
|
@@ -125,9 +158,11 @@ export function generateTs(source, file, options = {}) {
|
|
|
125
158
|
if (!parsed.inputs.length)
|
|
126
159
|
emit(' __cl_none?: never\n');
|
|
127
160
|
for (const input of parsed.inputs) {
|
|
161
|
+
if (input.doc)
|
|
162
|
+
emit(` ${input.doc}\n`);
|
|
128
163
|
emit(' ');
|
|
129
164
|
copy(input.name, input.nameStart);
|
|
130
|
-
emit(input.default !== undefined ? '?: ' : ': ');
|
|
165
|
+
emit(input.default !== undefined || input.optional ? '?: ' : ': ');
|
|
131
166
|
copy(input.type, input.typeStart);
|
|
132
167
|
emit('\n');
|
|
133
168
|
}
|
|
@@ -150,7 +185,13 @@ export function generateTs(source, file, options = {}) {
|
|
|
150
185
|
}
|
|
151
186
|
emit('}\n');
|
|
152
187
|
if (provides)
|
|
153
|
-
emit('declare function provide(key: string, value:
|
|
188
|
+
emit('declare function provide<T>(key: string | { readonly name: string; readonly __clContext?: T }, value: NoInfer<T>): void\n');
|
|
189
|
+
if (parsed.contexts.length)
|
|
190
|
+
emit('declare function __cl_context<T>(fallback?: () => T): T\n');
|
|
191
|
+
if (parsed.contexts.some((c) => c.key))
|
|
192
|
+
emit('declare function __cl_publicKey(key: { readonly public: true }): void\n');
|
|
193
|
+
if (parsed.contexts.some((c) => c.key))
|
|
194
|
+
emit('type __cl_ContextOf<K> = K extends { readonly __clContext?: infer T } ? T : unknown\n');
|
|
154
195
|
if (parsed.events.length)
|
|
155
196
|
emit('declare const emit: <K extends keyof Events>(name: K, ...args: Events[K] extends void ? [value?: undefined, info?: { reason?: string; event?: Event }] : [value: Events[K], info?: { reason?: string; event?: Event }]) => boolean\n');
|
|
156
197
|
let loaderSpan;
|
|
@@ -180,31 +221,53 @@ export function generateTs(source, file, options = {}) {
|
|
|
180
221
|
copy(input.name, input.nameStart);
|
|
181
222
|
if (input.default !== undefined && input.defaultStart !== undefined) {
|
|
182
223
|
emit(' = ');
|
|
183
|
-
copy(input.default, input.defaultStart);
|
|
224
|
+
copy(input.defaultTs ?? input.default, input.defaultStart);
|
|
184
225
|
}
|
|
185
226
|
});
|
|
186
227
|
emit(' } = __cl_props\n');
|
|
187
228
|
emit(` __cl_use([${list.map((x) => x.name).join(', ')}])\n`);
|
|
188
229
|
}
|
|
230
|
+
if (parsed.props) {
|
|
231
|
+
const defaulted = parsed.inputs.filter((i) => parsed.props.keys.includes(i.name) && i.default !== undefined).map((i) => JSON.stringify(i.name));
|
|
232
|
+
const keys = parsed.props.keys.map((k) => JSON.stringify(k)).join(' | ') || 'never';
|
|
233
|
+
const d = defaulted.join(' | ') || 'never';
|
|
234
|
+
emit(` const ${parsed.props.name}: __cl_Readonly<Omit<Pick<Inputs, ${keys}>, ${d}> & Required<Pick<Inputs, ${d}>>> = __cl_props as any\n __cl_use(${parsed.props.name})\n`);
|
|
235
|
+
}
|
|
236
|
+
const island = (!!script && (/\S/.test(script.body) || parsed.events.length > 0)) || parsed.inputs.some((i) => i.model);
|
|
189
237
|
for (const c of parsed.contexts) {
|
|
238
|
+
if (island && c.key) {
|
|
239
|
+
emit(' __cl_publicKey(');
|
|
240
|
+
copy(c.key, c.typeStart);
|
|
241
|
+
emit(')\n');
|
|
242
|
+
}
|
|
243
|
+
if (c.doc)
|
|
244
|
+
emit(` ${c.doc}\n`);
|
|
190
245
|
emit(' const ');
|
|
191
246
|
copy(c.name, c.nameStart);
|
|
192
247
|
emit(': ');
|
|
193
248
|
copy(c.type, c.typeStart);
|
|
194
|
-
emit(
|
|
195
|
-
if (c.default !== undefined && c.defaultStart !== undefined)
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
emit('
|
|
199
|
-
|
|
249
|
+
emit(` = __cl_context<${c.type}>(`);
|
|
250
|
+
if (c.default !== undefined && c.defaultStart !== undefined) {
|
|
251
|
+
emit('() => (');
|
|
252
|
+
copy(c.defaultTs ?? c.default, c.defaultStart);
|
|
253
|
+
emit(')');
|
|
254
|
+
}
|
|
255
|
+
emit(')\n');
|
|
200
256
|
}
|
|
201
257
|
if (parsed.contexts.length)
|
|
202
258
|
emit(` __cl_use([${parsed.contexts.map((c) => c.name).join(', ')}])\n`);
|
|
203
259
|
const copyScript = (from, split) => {
|
|
204
|
-
|
|
260
|
+
let body = split.body;
|
|
261
|
+
const moved = from.ts ? [...(from.typeImports ?? []), ...(from.typeDecls ?? [])].filter((t) => from.ts.startsWith(t.text, t.start)) : [];
|
|
262
|
+
for (const [s, e] of from.factories ?? [])
|
|
263
|
+
moved.push({ text: (from.ts ?? from.code).slice(s, e), start: s });
|
|
264
|
+
if (from.ts)
|
|
265
|
+
body = body.split('').map((c, i) => (c === from.code[i] ? from.ts[i] : c)).join('');
|
|
205
266
|
const cuts = [
|
|
206
|
-
...
|
|
207
|
-
...
|
|
267
|
+
...moved.map((t) => ({ at: t.start, end: t.start + t.text.length, text: t.text.replace(/[^\n]/g, ' ') })),
|
|
268
|
+
...(from.ts ? (from.stayingExports ?? []).map((at) => ({ at, end: at + 'export'.length, text: ' ' })) : []),
|
|
269
|
+
...split.imports.map((imp) => ({ at: imp.start, end: imp.start + imp.text.length, text: imp.text.replace(/[^\n]/g, ' ') })),
|
|
270
|
+
...(from.ts ? [] : scriptAnnotations(from.code).map((e) => ({ ...e, end: e.at }))),
|
|
208
271
|
].sort((a, b) => a.at - b.at);
|
|
209
272
|
let pos = 0;
|
|
210
273
|
for (const cut of cuts) {
|
|
@@ -470,7 +533,7 @@ export function generateTs(source, file, options = {}) {
|
|
|
470
533
|
const wrong = modifierError(type, mods);
|
|
471
534
|
if (wrong)
|
|
472
535
|
throw new ClarusError(wrong, file, source, a.start);
|
|
473
|
-
emit(` ;(($event:
|
|
536
|
+
emit(` ;(($event: ${eventType(type, node.tag.toLowerCase())}, $values: Record<string, any>) => { `);
|
|
474
537
|
guarded();
|
|
475
538
|
emit('{ ');
|
|
476
539
|
copy(a.expr, at(a.expr, attrValueStart(a)));
|
|
@@ -511,6 +574,12 @@ function joinPath(dir, rel) {
|
|
|
511
574
|
export function viewsDeclaration(views) {
|
|
512
575
|
const lines = ['// Generated by clarusjs check: view name → inputs, for render() from clarusjs/views. Declarations only.'];
|
|
513
576
|
const entries = [];
|
|
577
|
+
const modules = new Map();
|
|
578
|
+
const moduleAlias = (from) => {
|
|
579
|
+
if (!modules.has(from))
|
|
580
|
+
modules.set(from, `__cl_i${modules.size}`);
|
|
581
|
+
return modules.get(from);
|
|
582
|
+
};
|
|
514
583
|
views.forEach(({ file, source }, i) => {
|
|
515
584
|
let parsed;
|
|
516
585
|
try {
|
|
@@ -521,30 +590,60 @@ export function viewsDeclaration(views) {
|
|
|
521
590
|
}
|
|
522
591
|
const ns = `__cl_v${i}`;
|
|
523
592
|
const body = [];
|
|
524
|
-
|
|
525
|
-
|
|
593
|
+
const renames = new Map();
|
|
594
|
+
const scriptImports = [parsed.script, parsed.serverScript].flatMap((s) => s?.typeImports ?? [])
|
|
595
|
+
.map((imp) => /^import\s+type\s+(\{[^}]*\}|\*\s+as\s+[\w$]+|[\w$]+)\s+from\s+(['"])(.+?)\2/.exec(imp.text))
|
|
596
|
+
.filter((m) => !!m).map((m) => ({ clause: m[1], from: m[3] }));
|
|
597
|
+
const fromHere = (spec) => (spec.startsWith('.') ? `../${joinPath(file.split('/').slice(0, -1).join('/'), spec)}` : spec.startsWith('~/') ? `../src/${spec.slice(2)}` : spec);
|
|
598
|
+
const scriptTypes = [parsed.script, parsed.serverScript].flatMap((s) => (s?.typeDecls ?? []).map((d) => d.text)).join('\n') + parsed.inputs.map((i) => i.type).join('\n');
|
|
599
|
+
for (const s of [parsed.script, parsed.serverScript]) {
|
|
600
|
+
if (!s?.ts)
|
|
601
|
+
continue;
|
|
602
|
+
for (const m of s.code.matchAll(/^\s*import\s+(\{[^}]*\}|\*\s+as\s+[\w$]+|[\w$]+)\s+from\s+(['"])(.+?)\2/gm)) {
|
|
603
|
+
if (m[1].startsWith('*')) {
|
|
604
|
+
const local = m[1].replace(/^\*\s+as\s+/, '');
|
|
605
|
+
if (new RegExp(`\\btypeof\\s+${local.replace(/\$/g, '\\$')}\\b`).test(scriptTypes))
|
|
606
|
+
body.push(` const ${local}: typeof import(${JSON.stringify(fromHere(m[3]))})`);
|
|
607
|
+
continue;
|
|
608
|
+
}
|
|
609
|
+
const names = m[1].startsWith('{')
|
|
610
|
+
? m[1].slice(1, -1).split(',').map((p) => p.trim()).filter(Boolean).map((p) => { const [name, local = name] = p.split(/\s+as\s+/).map((x) => x.trim()); return { name: name, local: local }; })
|
|
611
|
+
: [{ name: 'default', local: m[1] }];
|
|
612
|
+
for (const { name, local } of names)
|
|
613
|
+
if (new RegExp(`\\btypeof\\s+${local.replace(/\$/g, '\\$')}\\b`).test(scriptTypes))
|
|
614
|
+
body.push(` const ${local}: typeof import(${JSON.stringify(fromHere(m[3]))}).${name}`);
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
for (const t of [...parsed.typeImports, ...scriptImports]) {
|
|
618
|
+
const mod = moduleAlias(fromHere(t.from));
|
|
526
619
|
const clause = t.clause.trim();
|
|
527
620
|
if (clause.startsWith('{')) {
|
|
528
621
|
for (const part of clause.slice(1, -1).split(',').map((p) => p.trim().replace(/^type\s+/, '')).filter(Boolean)) {
|
|
529
622
|
const [name, local = name] = part.split(/\s+as\s+/).map((x) => x.trim());
|
|
530
|
-
body.push(`
|
|
623
|
+
body.push(` import ${local} = ${mod}.${name}`);
|
|
531
624
|
}
|
|
532
625
|
}
|
|
533
|
-
else if (
|
|
534
|
-
body.push(`
|
|
535
|
-
|
|
626
|
+
else if (clause.startsWith('*'))
|
|
627
|
+
body.push(` import ${clause.replace(/^\*\s+as\s+/, '')} = ${mod}`);
|
|
628
|
+
else
|
|
629
|
+
renames.set(clause, `${mod}.default`);
|
|
536
630
|
}
|
|
631
|
+
for (const s of [parsed.script, parsed.serverScript])
|
|
632
|
+
for (const d of s?.typeDecls ?? [])
|
|
633
|
+
body.push(` ${d.text}`);
|
|
537
634
|
for (const t of parsed.types)
|
|
538
|
-
body.push(t.kind === 'interface' ? ` interface ${t.name}${t.params ?? ''} ${t.type}` : ` type ${t.name}${t.params ?? ''} = ${t.type}`);
|
|
635
|
+
body.push(...(t.doc ? [` ${t.doc}`] : []), t.kind === 'interface' ? ` interface ${t.name}${t.params ?? ''} ${t.type}` : ` type ${t.name}${t.params ?? ''} = ${t.type}`);
|
|
539
636
|
body.push(` interface Inputs {`);
|
|
540
637
|
if (!parsed.inputs.length)
|
|
541
638
|
body.push(' __cl_none?: never');
|
|
542
639
|
for (const input of parsed.inputs)
|
|
543
|
-
body.push(` ${input.name}${input.default !== undefined ? '?' : ''}: ${input.type}`);
|
|
640
|
+
body.push(...(input.doc ? [` ${input.doc}`] : []), ` ${input.name}${input.default !== undefined || input.optional ? '?' : ''}: ${input.type}`);
|
|
544
641
|
body.push(' }');
|
|
545
|
-
|
|
642
|
+
const renamed = (line) => [...renames].reduce((l, [local, to]) => l.replace(new RegExp(`(?<![\\w$.])${local.replace(/\$/g, '\\$')}(?![\\w$]|\\s*\\??:)`, 'g'), to), line);
|
|
643
|
+
lines.push(`declare namespace ${ns} {`, ...body.map((l) => (l.startsWith(' import ') ? l : renamed(l))), '}');
|
|
546
644
|
entries.push(` ${JSON.stringify(file.replace(/^src\//, '').replace(/\.[^./]+$/, ''))}: ${ns}.Inputs`);
|
|
547
645
|
});
|
|
646
|
+
lines.splice(1, 0, ...[...modules].map(([from, alias]) => `import * as ${alias} from ${JSON.stringify(from)}`));
|
|
548
647
|
lines.push("declare module 'clarusjs/views' {", ' interface Views {', ...entries, ' }', '}', 'export {}', '');
|
|
549
648
|
return lines.join('\n');
|
|
550
649
|
}
|
package/dist/cli/index.js
CHANGED
|
@@ -3,6 +3,7 @@ import { Command } from 'commander';
|
|
|
3
3
|
import { check } from './check.js';
|
|
4
4
|
import { loadProject } from './project.js';
|
|
5
5
|
import { explain } from './explain.js';
|
|
6
|
+
import { migrateFiles } from './migrate-files.js';
|
|
6
7
|
import { serve as mapper } from './mapper.js';
|
|
7
8
|
import { serve } from '../node/serve.js';
|
|
8
9
|
import { writeEnvTypes } from '../vite/env.js';
|
|
@@ -47,6 +48,22 @@ program
|
|
|
47
48
|
console.log(options.json ? result.json : result.text);
|
|
48
49
|
process.exitCode = result.exitCode;
|
|
49
50
|
});
|
|
51
|
+
program
|
|
52
|
+
.command('migrate')
|
|
53
|
+
.description('move @input, @model and @event (and @type, @interface, @import type) into each component\'s <script lang="ts"> as props(), model() and events(); reads of an input become props.name. Without --write it only lists what would change')
|
|
54
|
+
.argument('[paths...]', '.tscl files or folders (default: src)', ['src'])
|
|
55
|
+
.option('--write', 'write the changed files')
|
|
56
|
+
.action((paths, options) => {
|
|
57
|
+
const result = migrateFiles(paths, !!options.write);
|
|
58
|
+
for (const file of result.changed)
|
|
59
|
+
console.log(`${options.write ? 'migrated' : 'would migrate'} ${file}`);
|
|
60
|
+
for (const note of result.notes)
|
|
61
|
+
console.log(`note: ${note}`);
|
|
62
|
+
if (!result.changed.length)
|
|
63
|
+
console.log('Nothing to migrate.');
|
|
64
|
+
else if (!options.write)
|
|
65
|
+
console.log('\nRun again with --write to change them, then clarusjs check.');
|
|
66
|
+
});
|
|
50
67
|
program
|
|
51
68
|
.command('mapper')
|
|
52
69
|
.description('the TypeScript 7.1 content mapper for .tscl files (TypeScript starts it; see https://clarusjs.com/guide/editor)')
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { readdirSync, readFileSync, statSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { join, relative } from 'node:path';
|
|
3
|
+
import { migrate } from './migrate.js';
|
|
4
|
+
const tsclUnder = (path) => statSync(path).isDirectory()
|
|
5
|
+
? readdirSync(path).filter((n) => n !== 'node_modules' && !n.startsWith('.')).flatMap((n) => tsclUnder(join(path, n)))
|
|
6
|
+
: path.endsWith('.tscl') ? [path] : [];
|
|
7
|
+
export function migrateFiles(paths, write) {
|
|
8
|
+
const changed = [];
|
|
9
|
+
const notes = [];
|
|
10
|
+
for (const file of paths.flatMap(tsclUnder)) {
|
|
11
|
+
const source = readFileSync(file, 'utf8');
|
|
12
|
+
const name = relative(process.cwd(), file);
|
|
13
|
+
const out = migrate(source, name);
|
|
14
|
+
notes.push(...out.notes);
|
|
15
|
+
if (out.code === source)
|
|
16
|
+
continue;
|
|
17
|
+
changed.push(name);
|
|
18
|
+
if (write)
|
|
19
|
+
writeFileSync(file, out.code);
|
|
20
|
+
}
|
|
21
|
+
return { changed, notes };
|
|
22
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `source` in the script form, or unchanged when there's nothing to move. `notes` says what it left (an @context,
|
|
3
|
+
* which reads by name) and what may need a hand (a script that became TypeScript may need parameter types).
|
|
4
|
+
*/
|
|
5
|
+
export declare function migrate(source: string, file: string): {
|
|
6
|
+
code: string;
|
|
7
|
+
notes: string[];
|
|
8
|
+
};
|