@skathi-native/cli 0.1.0-beta.4 → 0.2.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/aot-inputs.ts +21 -2
- package/build.ts +241 -60
- package/check/bundle.ts +265 -0
- package/check/graph.ts +3 -1
- package/check/index.ts +10 -1
- package/check/program.ts +26 -6
- package/cli/create.ts +29 -7
- package/devtools/commands.ts +25 -0
- package/effects/compile.ts +160 -31
- package/effects/sk.slang +353 -29
- package/package.json +7 -4
- package/prod.ts +40 -7
- package/svelte.ts +76 -0
- package/templates/icon.png +0 -0
- package/templates/showcase/assets/textures/golden.png +0 -0
- package/templates/showcase/assets/textures/hammered.png +0 -0
- package/templates/showcase/assets/textures/noir.png +0 -0
- package/templates/showcase/assets/textures/reeded.png +0 -0
- package/templates/showcase/assets/textures/teal-orange.png +0 -0
- package/templates/showcase/shaders/dissolve.slang +40 -0
- package/templates/showcase/shaders/fireflies.slang +58 -0
- package/templates/showcase/shaders/glass.slang +31 -0
- package/templates/showcase/shaders/grade.slang +34 -0
- package/templates/showcase/shaders/halftone.slang +42 -0
- package/templates/showcase/shaders/lens.slang +51 -0
- package/templates/showcase/shaders/nebula.slang +57 -0
- package/templates/showcase/shaders/orbit.slang +52 -0
- package/templates/showcase/shaders/pond.slang +76 -0
- package/templates/showcase/shaders/ripples.slang +71 -0
- package/templates/showcase/shaders/shatter.slang +116 -0
- package/templates/showcase/shaders/snow.slang +57 -0
- package/templates/showcase/shaders/spotlight.slang +52 -0
- package/templates/showcase/src/App.vue +11 -16
- package/templates/showcase/src/components/FullWindow.vue +77 -0
- package/templates/showcase/src/components/Page.vue +2 -0
- package/templates/showcase/src/fullwindow.ts +36 -0
- package/templates/showcase/src/icons.ts +2 -0
- package/templates/showcase/src/pages/GraphicsPage.vue +122 -0
- package/templates/showcase/src/pages/MotionPage.vue +71 -1
- package/templates/showcase/src/pages/ShadersPage.vue +443 -0
- package/templates/showcase/src/router.ts +32 -1
- package/templates/svelte/.vscode/settings.json +3 -0
- package/templates/svelte/assets/hello.txt +1 -0
- package/templates/svelte/src/App.svelte +142 -0
- package/templates/svelte/src/main.ts +4 -0
- package/vitecompat.ts +2 -0
package/check/bundle.ts
ADDED
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
// What of a source file runs in the production bundle. The bundler keeps code behind a guard it could have folded
|
|
2
|
+
// (`if (DEV)` with esm-env's `DEV` resolved to `false` in another module), so the bundle itself is read: a finding is
|
|
3
|
+
// dropped when the code it maps to sits under a guard that is constant there, or in a function all of whose
|
|
4
|
+
// references in the bundle do. The bundle holds compiled components too, so no caller is out of sight.
|
|
5
|
+
|
|
6
|
+
import { resolve } from "node:path";
|
|
7
|
+
import type ts from "typescript";
|
|
8
|
+
import type { Range } from "./schema";
|
|
9
|
+
import type { ModuleGraph } from "./graph";
|
|
10
|
+
import type { TypeScript } from "./program";
|
|
11
|
+
|
|
12
|
+
type Bundle = NonNullable<ModuleGraph["bundle"]>;
|
|
13
|
+
/** [original line, original column, generated line, generated column], zero-based. */
|
|
14
|
+
type Segment = [number, number, number, number];
|
|
15
|
+
|
|
16
|
+
const B64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
17
|
+
const MAX_DEPTH = 40;
|
|
18
|
+
|
|
19
|
+
function decode(bundle: Bundle, key: (path: string) => string): Map<string, Segment[]> {
|
|
20
|
+
const bySource = new Map<number, Segment[]>();
|
|
21
|
+
let source = 0;
|
|
22
|
+
let line = 0;
|
|
23
|
+
let column = 0;
|
|
24
|
+
const groups = bundle.mappings.split(";");
|
|
25
|
+
for (let genLine = 0; genLine < groups.length; genLine++) {
|
|
26
|
+
let genColumn = 0;
|
|
27
|
+
for (const segment of groups[genLine]!.split(",")) {
|
|
28
|
+
if (!segment) continue;
|
|
29
|
+
const fields: number[] = [];
|
|
30
|
+
for (let i = 0, shift = 0, value = 0; i < segment.length; i++) {
|
|
31
|
+
const digit = B64.indexOf(segment[i]!);
|
|
32
|
+
value += (digit & 31) << shift;
|
|
33
|
+
if (digit & 32) {
|
|
34
|
+
shift += 5;
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
fields.push(value & 1 ? -(value >>> 1) : value >>> 1);
|
|
38
|
+
shift = 0;
|
|
39
|
+
value = 0;
|
|
40
|
+
}
|
|
41
|
+
genColumn += fields[0] ?? 0;
|
|
42
|
+
if (fields.length < 4) continue;
|
|
43
|
+
source += fields[1]!;
|
|
44
|
+
line += fields[2]!;
|
|
45
|
+
column += fields[3]!;
|
|
46
|
+
const list = bySource.get(source) ?? [];
|
|
47
|
+
list.push([line, column, genLine, genColumn]);
|
|
48
|
+
bySource.set(source, list);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
const out = new Map<string, Segment[]>();
|
|
52
|
+
for (const [index, list] of bySource) {
|
|
53
|
+
const path = bundle.sources[index];
|
|
54
|
+
if (!path) continue;
|
|
55
|
+
out.set(
|
|
56
|
+
key(path),
|
|
57
|
+
list.sort((a, b) => a[0] - b[0] || a[1] - b[1]),
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
return out;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export class BundleLiveness {
|
|
64
|
+
private segments: Map<string, Segment[]> | null = null;
|
|
65
|
+
private file: ts.SourceFile | null = null;
|
|
66
|
+
private checker: ts.TypeChecker | null = null;
|
|
67
|
+
private byName: Map<string, ts.Identifier[]> | null = null;
|
|
68
|
+
private readonly functions = new Map<ts.Node, boolean>();
|
|
69
|
+
|
|
70
|
+
constructor(
|
|
71
|
+
private readonly t: TypeScript,
|
|
72
|
+
private readonly bundle: Bundle,
|
|
73
|
+
) {}
|
|
74
|
+
|
|
75
|
+
private key(path: string): string {
|
|
76
|
+
return this.t.sys.useCaseSensitiveFileNames ? resolve(path) : resolve(path).toLowerCase();
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** False when the code at `range` of `path` is in the bundle but cannot run there. */
|
|
80
|
+
live(path: string, range: Range): boolean {
|
|
81
|
+
this.segments ??= decode(this.bundle, (p) => this.key(p));
|
|
82
|
+
const segments = this.segments.get(this.key(path));
|
|
83
|
+
if (!segments) return true;
|
|
84
|
+
const at = this.generated(segments, range);
|
|
85
|
+
if (!at) return true;
|
|
86
|
+
this.parse();
|
|
87
|
+
const offset = this.file!.getPositionOfLineAndCharacter(at[0], at[1]);
|
|
88
|
+
return !this.dead(this.nodeAt(offset), 0);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
private generated(segments: Segment[], range: Range): [number, number] | null {
|
|
92
|
+
const before = (s: Segment, line: number, character: number) => s[0] < line || (s[0] === line && s[1] < character);
|
|
93
|
+
let lo = 0;
|
|
94
|
+
let hi = segments.length;
|
|
95
|
+
while (lo < hi) {
|
|
96
|
+
const mid = (lo + hi) >> 1;
|
|
97
|
+
if (before(segments[mid]!, range.start.line, range.start.character)) lo = mid + 1;
|
|
98
|
+
else hi = mid;
|
|
99
|
+
}
|
|
100
|
+
const inside = segments[lo];
|
|
101
|
+
if (inside && before(inside, range.end.line, range.end.character + 1)) return [inside[2], inside[3]];
|
|
102
|
+
// A position the bundler mapped only at the start of its line.
|
|
103
|
+
const previous = segments[lo - 1];
|
|
104
|
+
return previous && previous[0] === range.start.line ? [previous[2], previous[3]] : null;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
private parse(): void {
|
|
108
|
+
if (this.file) return;
|
|
109
|
+
const t = this.t;
|
|
110
|
+
const name = "/sk-check-bundle.js";
|
|
111
|
+
const file = t.createSourceFile(name, this.bundle.code, t.ScriptTarget.Latest, true, t.ScriptKind.JS);
|
|
112
|
+
const options: ts.CompilerOptions = { allowJs: true, noLib: true, noResolve: true, types: [] };
|
|
113
|
+
const host = t.createCompilerHost(options);
|
|
114
|
+
host.getSourceFile = (path) => (path === name ? file : undefined);
|
|
115
|
+
host.fileExists = (path) => path === name;
|
|
116
|
+
host.readFile = (path) => (path === name ? this.bundle.code : undefined);
|
|
117
|
+
this.checker = t.createProgram({ rootNames: [name], options, host }).getTypeChecker();
|
|
118
|
+
this.file = file;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
private nodeAt(offset: number): ts.Node {
|
|
122
|
+
let node: ts.Node = this.file!;
|
|
123
|
+
for (let found = true; found;) {
|
|
124
|
+
found = false;
|
|
125
|
+
this.t.forEachChild(node, (child) => {
|
|
126
|
+
if (found || offset < child.getStart(this.file!) || offset >= child.end) return;
|
|
127
|
+
node = child;
|
|
128
|
+
found = true;
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
return node;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
private dead(node: ts.Node, depth: number): boolean {
|
|
135
|
+
const t = this.t;
|
|
136
|
+
for (let cur = node; cur.parent; cur = cur.parent) {
|
|
137
|
+
const p = cur.parent;
|
|
138
|
+
if (t.isIfStatement(p) && cur !== p.expression) {
|
|
139
|
+
const test = this.truth(p.expression, depth);
|
|
140
|
+
if (test === (cur === p.elseStatement)) return true;
|
|
141
|
+
} else if (t.isConditionalExpression(p) && cur !== p.condition) {
|
|
142
|
+
const test = this.truth(p.condition, depth);
|
|
143
|
+
if (test === (cur === p.whenFalse)) return true;
|
|
144
|
+
} else if (t.isBinaryExpression(p) && cur === p.right) {
|
|
145
|
+
const op = p.operatorToken.kind;
|
|
146
|
+
const left = this.truth(p.left, depth);
|
|
147
|
+
if (op === t.SyntaxKind.AmpersandAmpersandToken && left === false) return true;
|
|
148
|
+
if (op === t.SyntaxKind.BarBarToken && left === true) return true;
|
|
149
|
+
}
|
|
150
|
+
if (t.isFunctionDeclaration(cur) || t.isFunctionExpression(cur) || t.isArrowFunction(cur)) {
|
|
151
|
+
const verdict = this.uncalled(cur, depth);
|
|
152
|
+
if (verdict !== undefined) return verdict;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return false;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** For a named function: whether every reference to it is dead; undefined when it has no name to follow. */
|
|
159
|
+
private uncalled(fn: ts.FunctionLikeDeclaration, depth: number): boolean | undefined {
|
|
160
|
+
const t = this.t;
|
|
161
|
+
const holder = t.isFunctionDeclaration(fn)
|
|
162
|
+
? fn.name
|
|
163
|
+
: t.isVariableDeclaration(fn.parent) && fn.parent.initializer === fn && t.isIdentifier(fn.parent.name)
|
|
164
|
+
? fn.parent.name
|
|
165
|
+
: undefined;
|
|
166
|
+
if (!holder) return undefined;
|
|
167
|
+
const known = this.functions.get(fn);
|
|
168
|
+
if (known !== undefined) return known;
|
|
169
|
+
if (depth > MAX_DEPTH) return false;
|
|
170
|
+
// While its callers are judged, a recursive call counts as live.
|
|
171
|
+
this.functions.set(fn, false);
|
|
172
|
+
const refs = this.references(holder);
|
|
173
|
+
const verdict = refs.length > 0 && refs.every((ref) => this.dead(ref, depth + 1));
|
|
174
|
+
this.functions.set(fn, verdict);
|
|
175
|
+
return verdict;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
private references(name: ts.Identifier): ts.Identifier[] {
|
|
179
|
+
const t = this.t;
|
|
180
|
+
const checker = this.checker!;
|
|
181
|
+
if (!this.byName) {
|
|
182
|
+
this.byName = new Map();
|
|
183
|
+
const visit = (node: ts.Node): void => {
|
|
184
|
+
if (t.isIdentifier(node)) {
|
|
185
|
+
const list = this.byName!.get(node.text) ?? [];
|
|
186
|
+
list.push(node);
|
|
187
|
+
this.byName!.set(node.text, list);
|
|
188
|
+
}
|
|
189
|
+
t.forEachChild(node, visit);
|
|
190
|
+
};
|
|
191
|
+
visit(this.file!);
|
|
192
|
+
}
|
|
193
|
+
const symbolOf = (id: ts.Identifier) =>
|
|
194
|
+
t.isShorthandPropertyAssignment(id.parent)
|
|
195
|
+
? checker.getShorthandAssignmentValueSymbol(id.parent)
|
|
196
|
+
: checker.getSymbolAtLocation(id);
|
|
197
|
+
const symbol = symbolOf(name);
|
|
198
|
+
if (!symbol) return [];
|
|
199
|
+
return (this.byName.get(name.text) ?? []).filter((id) => id !== name && symbolOf(id) === symbol);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/** The truthiness of `expr` wherever it runs in the bundle, or undefined when it is not constant. */
|
|
203
|
+
private truth(expr: ts.Expression, depth: number): boolean | undefined {
|
|
204
|
+
const value = this.constant(expr, depth);
|
|
205
|
+
return value === NOT_CONSTANT ? undefined : !!value;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
private constant(expr: ts.Expression, depth: number): unknown {
|
|
209
|
+
const t = this.t;
|
|
210
|
+
if (depth > MAX_DEPTH) return NOT_CONSTANT;
|
|
211
|
+
if (t.isParenthesizedExpression(expr)) return this.constant(expr.expression, depth);
|
|
212
|
+
if (expr.kind === t.SyntaxKind.TrueKeyword) return true;
|
|
213
|
+
if (expr.kind === t.SyntaxKind.FalseKeyword) return false;
|
|
214
|
+
if (expr.kind === t.SyntaxKind.NullKeyword) return null;
|
|
215
|
+
if (t.isNumericLiteral(expr)) return Number(expr.text);
|
|
216
|
+
if (t.isStringLiteral(expr) || t.isNoSubstitutionTemplateLiteral(expr)) return expr.text;
|
|
217
|
+
if (t.isVoidExpression(expr)) return undefined;
|
|
218
|
+
if (t.isPrefixUnaryExpression(expr) && expr.operator === t.SyntaxKind.ExclamationToken) {
|
|
219
|
+
const value = this.constant(expr.operand, depth + 1);
|
|
220
|
+
return value === NOT_CONSTANT ? NOT_CONSTANT : !value;
|
|
221
|
+
}
|
|
222
|
+
if (t.isBinaryExpression(expr)) {
|
|
223
|
+
const op = expr.operatorToken.kind;
|
|
224
|
+
const left = this.constant(expr.left, depth + 1);
|
|
225
|
+
if (left === NOT_CONSTANT) return NOT_CONSTANT;
|
|
226
|
+
if (op === t.SyntaxKind.AmpersandAmpersandToken) return left ? this.constant(expr.right, depth + 1) : left;
|
|
227
|
+
if (op === t.SyntaxKind.BarBarToken) return left ? left : this.constant(expr.right, depth + 1);
|
|
228
|
+
const right = this.constant(expr.right, depth + 1);
|
|
229
|
+
if (right === NOT_CONSTANT) return NOT_CONSTANT;
|
|
230
|
+
if (op === t.SyntaxKind.EqualsEqualsEqualsToken) return left === right;
|
|
231
|
+
if (op === t.SyntaxKind.ExclamationEqualsEqualsToken) return left !== right;
|
|
232
|
+
if (op === t.SyntaxKind.EqualsEqualsToken) return left == right;
|
|
233
|
+
if (op === t.SyntaxKind.ExclamationEqualsToken) return left != right;
|
|
234
|
+
return NOT_CONSTANT;
|
|
235
|
+
}
|
|
236
|
+
if (t.isIdentifier(expr)) return this.binding(expr, depth);
|
|
237
|
+
return NOT_CONSTANT;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/** A variable initialised to a constant and never written again. */
|
|
241
|
+
private binding(id: ts.Identifier, depth: number): unknown {
|
|
242
|
+
const t = this.t;
|
|
243
|
+
const symbol = this.checker!.getSymbolAtLocation(id);
|
|
244
|
+
const declarations = symbol?.declarations ?? [];
|
|
245
|
+
const decl = declarations[0];
|
|
246
|
+
if (declarations.length !== 1 || !decl || !t.isVariableDeclaration(decl) || !decl.initializer) {
|
|
247
|
+
return NOT_CONSTANT;
|
|
248
|
+
}
|
|
249
|
+
if (!t.isIdentifier(decl.name)) return NOT_CONSTANT;
|
|
250
|
+
for (const ref of this.references(decl.name)) {
|
|
251
|
+
const p = ref.parent;
|
|
252
|
+
const written =
|
|
253
|
+
(t.isBinaryExpression(p) &&
|
|
254
|
+
p.left === ref &&
|
|
255
|
+
p.operatorToken.kind >= t.SyntaxKind.FirstAssignment &&
|
|
256
|
+
p.operatorToken.kind <= t.SyntaxKind.LastAssignment) ||
|
|
257
|
+
((t.isPrefixUnaryExpression(p) || t.isPostfixUnaryExpression(p)) &&
|
|
258
|
+
(p.operator === t.SyntaxKind.PlusPlusToken || p.operator === t.SyntaxKind.MinusMinusToken));
|
|
259
|
+
if (written) return NOT_CONSTANT;
|
|
260
|
+
}
|
|
261
|
+
return this.constant(decl.initializer, depth + 1);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
const NOT_CONSTANT = Symbol("not constant");
|
package/check/graph.ts
CHANGED
|
@@ -12,8 +12,10 @@ export interface ModuleGraph {
|
|
|
12
12
|
aliases: Record<string, string>;
|
|
13
13
|
/** Ambient declarations the runtime contributes. */
|
|
14
14
|
declarations: string[];
|
|
15
|
-
/** Absolute paths of the .js/.ts/.vue files with code in the bundle. */
|
|
15
|
+
/** Absolute paths of the .js/.ts/.vue/.svelte files with code in the bundle. */
|
|
16
16
|
modules: string[];
|
|
17
|
+
/** The production bundle and its sourcemap; a source is null when its positions are not the file's own. */
|
|
18
|
+
bundle?: { code: string; sources: (string | null)[]; mappings: string };
|
|
17
19
|
errors: GraphError[];
|
|
18
20
|
}
|
|
19
21
|
|
package/check/index.ts
CHANGED
|
@@ -9,6 +9,7 @@ import { Builtins } from "./builtins";
|
|
|
9
9
|
import { codeInfo, REGISTRY } from "./codes";
|
|
10
10
|
import { parseCheckConfig, Suppressions } from "./config";
|
|
11
11
|
import { rangeOf, ZERO_RANGE, type Finding } from "./finding";
|
|
12
|
+
import { BundleLiveness } from "./bundle";
|
|
12
13
|
import { buildGraph, GraphFailure, type ModuleGraph } from "./graph";
|
|
13
14
|
import { Packages } from "./packages";
|
|
14
15
|
import { buildProgram, readUnits, type TypeScript } from "./program";
|
|
@@ -55,6 +56,8 @@ export interface AnalyzeInput {
|
|
|
55
56
|
/** Findings made before the analysis (configuration problems). */
|
|
56
57
|
findings?: Finding[];
|
|
57
58
|
progress?: Progress | undefined;
|
|
59
|
+
/** The production bundle: a rule finding in code it cannot run is not reported. */
|
|
60
|
+
bundle?: ModuleGraph["bundle"];
|
|
58
61
|
}
|
|
59
62
|
|
|
60
63
|
export function loadTypeScript(appRoot: string): TypeScript {
|
|
@@ -138,6 +141,7 @@ export async function runCheck(options: CheckOptions): Promise<CheckReport> {
|
|
|
138
141
|
modules: graph.modules,
|
|
139
142
|
aliases: graph.aliases,
|
|
140
143
|
declarations: graph.declarations,
|
|
144
|
+
bundle: graph.bundle,
|
|
141
145
|
allow: checkConfig.allow,
|
|
142
146
|
deps: options.deps ?? true,
|
|
143
147
|
findings,
|
|
@@ -215,6 +219,7 @@ export function analyze(input: AnalyzeInput): CheckReport {
|
|
|
215
219
|
if (built.configNote) progress.warn(built.configNote);
|
|
216
220
|
const { program, checker } = built;
|
|
217
221
|
const rules = new Rules(typescript, checker, new Builtins(typescript, program, checker));
|
|
222
|
+
const liveness = input.bundle ? new BundleLiveness(typescript, input.bundle) : null;
|
|
218
223
|
progress.step("AOT rules", units.length);
|
|
219
224
|
for (const unit of units) {
|
|
220
225
|
progress.tick();
|
|
@@ -244,7 +249,11 @@ export function analyze(input: AnalyzeInput): CheckReport {
|
|
|
244
249
|
delete unit.sourceFile;
|
|
245
250
|
continue;
|
|
246
251
|
}
|
|
247
|
-
if (deps || !unit.owner)
|
|
252
|
+
if (deps || !unit.owner) {
|
|
253
|
+
const found: Finding[] = [];
|
|
254
|
+
rules.run(unit, file, found);
|
|
255
|
+
findings.push(...found.filter((f) => !liveness || liveness.live(f.file, f.range)));
|
|
256
|
+
}
|
|
248
257
|
}
|
|
249
258
|
progress.step("typing quality");
|
|
250
259
|
const typing = new Typing(
|
package/check/program.ts
CHANGED
|
@@ -8,11 +8,11 @@ export type TypeScript = typeof ts;
|
|
|
8
8
|
|
|
9
9
|
/** One file of the bundle as the analysis sees it. */
|
|
10
10
|
export interface Unit {
|
|
11
|
-
/** The file on disk (.vue for a
|
|
11
|
+
/** The file on disk (.vue or .svelte for a component). */
|
|
12
12
|
path: string;
|
|
13
|
-
/** Its name in the TypeScript program: `<path>.ts`/`.js` for a .
|
|
13
|
+
/** Its name in the TypeScript program: `<path>.ts`/`.js` for a component. */
|
|
14
14
|
programPath: string;
|
|
15
|
-
/** The text positions refer to; for a
|
|
15
|
+
/** The text positions refer to; for a component everything but its scripts is blanked. */
|
|
16
16
|
text: string;
|
|
17
17
|
/** TypeScript rather than JavaScript. */
|
|
18
18
|
typed: boolean;
|
|
@@ -27,6 +27,7 @@ export interface Unreadable {
|
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
const TS_FILE = /\.[cm]?tsx?$/i;
|
|
30
|
+
const COMPONENT_FILE = /\.(vue|svelte)$/i;
|
|
30
31
|
|
|
31
32
|
/** The scripts of an SFC at their own offsets, everything else replaced by spaces. */
|
|
32
33
|
export function vueScripts(source: string, path: string): { text: string; typed: boolean } | null {
|
|
@@ -41,6 +42,25 @@ export function vueScripts(source: string, path: string): { text: string; typed:
|
|
|
41
42
|
return { text: chars.join(""), typed: blocks.some((b) => /^tsx?$/.test(b!.lang ?? "")) };
|
|
42
43
|
}
|
|
43
44
|
|
|
45
|
+
const blank = (text: string) => text.replace(/[^\r\n]/g, " ");
|
|
46
|
+
|
|
47
|
+
/** The `<script>` blocks of a Svelte component at their own offsets, everything else replaced by spaces. */
|
|
48
|
+
export function svelteScripts(source: string): { text: string; typed: boolean } | null {
|
|
49
|
+
// A `<script>` in a comment or in `<svelte:head>` is markup, not the component's script.
|
|
50
|
+
const scan = source.replace(/<!--[\s\S]*?-->|<svelte:head[\s>][\s\S]*?<\/svelte:head\s*>/gi, blank);
|
|
51
|
+
const chars = blank(source).split("");
|
|
52
|
+
let found = false;
|
|
53
|
+
let typed = false;
|
|
54
|
+
for (const m of scan.matchAll(/<script(\s[^>]*)?>([\s\S]*?)<\/script\s*>/gi)) {
|
|
55
|
+
const attrs = m[1] ?? "";
|
|
56
|
+
const start = m.index + "<script".length + attrs.length + 1;
|
|
57
|
+
for (let i = 0; i < m[2]!.length; i++) chars[start + i] = source[start + i]!;
|
|
58
|
+
found = true;
|
|
59
|
+
if (/\blang\s*=\s*["']?(ts|typescript)\b/i.test(attrs)) typed = true;
|
|
60
|
+
}
|
|
61
|
+
return found ? { text: chars.join(""), typed } : null;
|
|
62
|
+
}
|
|
63
|
+
|
|
44
64
|
export function readUnits(
|
|
45
65
|
modules: string[],
|
|
46
66
|
packages: Packages,
|
|
@@ -57,10 +77,10 @@ export function readUnits(
|
|
|
57
77
|
unreadable.push({ path, owner, message: (e as Error).message });
|
|
58
78
|
continue;
|
|
59
79
|
}
|
|
60
|
-
if (
|
|
80
|
+
if (COMPONENT_FILE.test(path)) {
|
|
61
81
|
let scripts: ReturnType<typeof vueScripts>;
|
|
62
82
|
try {
|
|
63
|
-
scripts = vueScripts(source, path);
|
|
83
|
+
scripts = /\.svelte$/i.test(path) ? svelteScripts(source) : vueScripts(source, path);
|
|
64
84
|
} catch (e) {
|
|
65
85
|
unreadable.push({ path, owner, message: (e as Error).message });
|
|
66
86
|
continue;
|
|
@@ -188,7 +208,7 @@ export function buildProgram(input: ProgramInput): BuiltProgram {
|
|
|
188
208
|
}
|
|
189
209
|
const found = typescript.resolveModuleName(specifier, importer, options, host).resolvedModule;
|
|
190
210
|
if (found) return found;
|
|
191
|
-
if (
|
|
211
|
+
if (COMPONENT_FILE.test(specifier) && specifier.startsWith(".")) {
|
|
192
212
|
for (const ext of ["ts", "js"] as const) {
|
|
193
213
|
const path = resolve(dirname(importer), `${specifier}.${ext}`);
|
|
194
214
|
if (own.has(key(path)))
|
package/cli/create.ts
CHANGED
|
@@ -19,15 +19,25 @@ import { colorEnabled, paint } from "./term";
|
|
|
19
19
|
export const PACKAGE_MANAGERS = ["bun", "npm", "pnpm", "yarn"] as const;
|
|
20
20
|
export type PackageManager = (typeof PACKAGE_MANAGERS)[number];
|
|
21
21
|
|
|
22
|
-
export const TEMPLATES = ["showcase", "minimal"] as const;
|
|
22
|
+
export const TEMPLATES = ["showcase", "minimal", "svelte"] as const;
|
|
23
23
|
export type Template = (typeof TEMPLATES)[number];
|
|
24
24
|
|
|
25
|
+
interface Checker {
|
|
26
|
+
script: string;
|
|
27
|
+
devDependencies: Record<string, string>;
|
|
28
|
+
/** The component files the app's tsconfig.json includes besides its .ts sources. */
|
|
29
|
+
sources: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const VUE_TSC: Checker = { script: "vue-tsc --noEmit", devDependencies: { "vue-tsc": "^3" }, sources: "src/**/*.vue" };
|
|
33
|
+
|
|
25
34
|
interface TemplateInfo {
|
|
26
35
|
hint: string;
|
|
27
|
-
/** Taken at the versions the runtime itself depends on. */
|
|
36
|
+
/** Taken at the versions the runtime itself depends on, or accepts as a peer. */
|
|
28
37
|
dependencies: string[];
|
|
29
38
|
window: Record<string, unknown>;
|
|
30
39
|
packs?: { name: string; match?: string[] }[];
|
|
40
|
+
checker: Checker;
|
|
31
41
|
}
|
|
32
42
|
|
|
33
43
|
const TEMPLATE_INFO: Record<Template, TemplateInfo> = {
|
|
@@ -36,11 +46,19 @@ const TEMPLATE_INFO: Record<Template, TemplateInfo> = {
|
|
|
36
46
|
dependencies: ["pinia", "vue-router"],
|
|
37
47
|
window: { width: 1100, height: 720, minWidth: 480, minHeight: 360, titleBarStyle: "hidden", titleBarHeight: 40 },
|
|
38
48
|
packs: [{ name: "media", match: ["assets/media/**"] }],
|
|
49
|
+
checker: VUE_TSC,
|
|
39
50
|
},
|
|
40
51
|
minimal: {
|
|
41
52
|
hint: "one window, one component",
|
|
42
53
|
dependencies: [],
|
|
43
54
|
window: { width: 760, height: 520, titleBarStyle: "default" },
|
|
55
|
+
checker: VUE_TSC,
|
|
56
|
+
},
|
|
57
|
+
svelte: {
|
|
58
|
+
hint: "the minimal app in Svelte 5",
|
|
59
|
+
dependencies: ["svelte"],
|
|
60
|
+
window: { width: 760, height: 520, titleBarStyle: "default" },
|
|
61
|
+
checker: { script: "svelte-check", devDependencies: { "svelte-check": "^4" }, sources: "src/**/*.svelte" },
|
|
44
62
|
},
|
|
45
63
|
};
|
|
46
64
|
|
|
@@ -49,7 +67,7 @@ export const TEMPLATES_DIR = resolve(import.meta.dir, "..", "templates");
|
|
|
49
67
|
export const CONFIG_SCHEMA = "./node_modules/@skathi-native/runtime/sk.config.schema.json";
|
|
50
68
|
|
|
51
69
|
/** Files whose text has its `{{key}}` placeholders filled in; anything else is copied byte for byte. */
|
|
52
|
-
const TEXT_FILES = new Set([".vue", ".ts", ".css", ".txt", ".json", ".html", ".svg"]);
|
|
70
|
+
const TEXT_FILES = new Set([".vue", ".svelte", ".ts", ".css", ".txt", ".json", ".html", ".svg"]);
|
|
53
71
|
|
|
54
72
|
export interface CreateOptions {
|
|
55
73
|
name?: string | undefined;
|
|
@@ -114,7 +132,11 @@ export function create(target: string, options: CreateOptions = {}): string[] {
|
|
|
114
132
|
const template = options.template ?? "showcase";
|
|
115
133
|
const info = TEMPLATE_INFO[template];
|
|
116
134
|
const platforms = options.platforms?.length ? options.platforms : [currentPlatform()];
|
|
117
|
-
const
|
|
135
|
+
const runtimePkg = runtimeRequire("./package.json") as {
|
|
136
|
+
dependencies: Record<string, string>;
|
|
137
|
+
peerDependencies?: Record<string, string>;
|
|
138
|
+
};
|
|
139
|
+
const runtimeDeps = { ...runtimePkg.peerDependencies, ...runtimePkg.dependencies };
|
|
118
140
|
const sdk = `^${CLI_VERSION}`;
|
|
119
141
|
|
|
120
142
|
const files: Record<string, string | Uint8Array> = {
|
|
@@ -129,13 +151,13 @@ export function create(target: string, options: CreateOptions = {}): string[] {
|
|
|
129
151
|
build: "sk build",
|
|
130
152
|
publish: "sk publish",
|
|
131
153
|
check: "sk check",
|
|
132
|
-
typecheck:
|
|
154
|
+
typecheck: info.checker.script,
|
|
133
155
|
},
|
|
134
156
|
dependencies: {
|
|
135
157
|
"@skathi-native/runtime": sdk,
|
|
136
158
|
...Object.fromEntries(info.dependencies.map((dep) => [dep, runtimeDeps[dep]])),
|
|
137
159
|
},
|
|
138
|
-
devDependencies: { "@skathi-native/cli": sdk, typescript: "^5",
|
|
160
|
+
devDependencies: { "@skathi-native/cli": sdk, typescript: "^5", ...info.checker.devDependencies },
|
|
139
161
|
}),
|
|
140
162
|
"sk.config.json": json({
|
|
141
163
|
$schema: CONFIG_SCHEMA,
|
|
@@ -153,7 +175,7 @@ export function create(target: string, options: CreateOptions = {}): string[] {
|
|
|
153
175
|
"icon.png": readFileSync(join(TEMPLATES_DIR, "icon.png")),
|
|
154
176
|
"tsconfig.json": json({
|
|
155
177
|
extends: "@skathi-native/runtime/tsconfig.app.json",
|
|
156
|
-
include: ["src/**/*.ts",
|
|
178
|
+
include: ["src/**/*.ts", info.checker.sources],
|
|
157
179
|
}),
|
|
158
180
|
".gitignore": "node_modules/\ndist/\n",
|
|
159
181
|
};
|
package/devtools/commands.ts
CHANGED
|
@@ -769,6 +769,31 @@ export const COMMANDS: CommandSpec[] = [
|
|
|
769
769
|
name: typeof flags.name === "string" ? flags.name : rest[1],
|
|
770
770
|
}),
|
|
771
771
|
},
|
|
772
|
+
{
|
|
773
|
+
cmd: "effects",
|
|
774
|
+
description:
|
|
775
|
+
"`--shading` effects: the effects.json schemas the host loaded (built-ins and the app's own), and " +
|
|
776
|
+
"every element painting one with its declared text, resolved parameters and the rules that declare " +
|
|
777
|
+
"it. `set` overrides an element's `--shading` inline, live, and answers the advisories the value " +
|
|
778
|
+
"raises; `reset` puts back what the app had.",
|
|
779
|
+
usage: [
|
|
780
|
+
" effects [list] schemas and the elements using them",
|
|
781
|
+
" effects node --node N one element's effect",
|
|
782
|
+
" effects set --node N --value TEXT override live, e.g. --value 'frost(0.6, 14px)'",
|
|
783
|
+
" effects reset --node N drop the override",
|
|
784
|
+
],
|
|
785
|
+
args: {
|
|
786
|
+
op: str('"list" (default), "node", "set" or "reset"'),
|
|
787
|
+
node: NODE,
|
|
788
|
+
value: str("for op=set: the `--shading` value, e.g. frost(0.6, 14px) or none"),
|
|
789
|
+
},
|
|
790
|
+
cli: (rest, flags) =>
|
|
791
|
+
compact({
|
|
792
|
+
op: rest[0] || "list",
|
|
793
|
+
node: num(flags.node ?? rest[1]),
|
|
794
|
+
value: stringFlag(flags.value),
|
|
795
|
+
}),
|
|
796
|
+
},
|
|
772
797
|
{
|
|
773
798
|
cmd: "raise",
|
|
774
799
|
description:
|