@unframework.dev/compiler 0.0.0-stage → 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/README.md +195 -2
- package/dist/capabilities.d.ts +15 -0
- package/dist/capabilities.js +44 -0
- package/dist/compile.d.ts +128 -0
- package/dist/compile.js +568 -0
- package/dist/config-types.d.ts +63 -0
- package/dist/config-types.js +1 -0
- package/dist/config.d.ts +54 -0
- package/dist/config.js +277 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +9 -0
- package/dist/plugin-ir.js +310 -0
- package/dist/project.d.ts +74 -0
- package/dist/project.js +464 -0
- package/dist/resolver.d.ts +89 -0
- package/dist/resolver.js +86 -0
- package/dist/targets.d.ts +11 -0
- package/dist/targets.js +36 -0
- package/package.json +66 -3
package/dist/project.js
ADDED
|
@@ -0,0 +1,464 @@
|
|
|
1
|
+
import { componentImportNames, directoryOf, joinPath, modulePath, relativePath, stylesheetSource, takesComponent } from "./resolver.js";
|
|
2
|
+
import { compile } from "./compile.js";
|
|
3
|
+
import { analyze, frameworkOf, isAuthoringModule, moduleSpecifiers, shapeQueries, stylesheetImports, unfollowedImports } from "@unframework.dev/analyzer";
|
|
4
|
+
import { parseModule } from "@unframework.dev/parser";
|
|
5
|
+
//#region src/project.ts
|
|
6
|
+
/**
|
|
7
|
+
* The project graph (ADR-0071) over the files `readFile` gives. A module's cache key is a hash of
|
|
8
|
+
* its source and of everything it read through the project: its children's APIs and the shapes
|
|
9
|
+
* they were analysed with, its stylesheets, the files of its shared modules, its import cycles
|
|
10
|
+
* and its own types' shapes. A shared module's text is no part of it: its copy is the module's own
|
|
11
|
+
* node, so an edit that changes only a copy recompiles nothing. The caller owns `typeOracle`.
|
|
12
|
+
*/
|
|
13
|
+
function createProject(options) {
|
|
14
|
+
const root = options.root.replace(/\/+$/, "");
|
|
15
|
+
const { typeOracle } = options;
|
|
16
|
+
const compileOptions = { ...options };
|
|
17
|
+
for (const key of [
|
|
18
|
+
"root",
|
|
19
|
+
"readFile",
|
|
20
|
+
"typeOracle"
|
|
21
|
+
]) Reflect.deleteProperty(compileOptions, key);
|
|
22
|
+
const readPath = (path) => path === void 0 ? Promise.resolve(void 0) : options.readFile(`${root}/${path}`);
|
|
23
|
+
/** Each child's analysis, by its file, with the key it was analysed under. */
|
|
24
|
+
const children = /* @__PURE__ */ new Map();
|
|
25
|
+
/** What each child's analysis read: its stylesheets and its types' files and packages. */
|
|
26
|
+
const childEdges = /* @__PURE__ */ new Map();
|
|
27
|
+
/** Each file's `.uf.tsx` imports, by its text, which the cycle search follows. */
|
|
28
|
+
const imported = /* @__PURE__ */ new Map();
|
|
29
|
+
const compiled = /* @__PURE__ */ new Map();
|
|
30
|
+
const copies = /* @__PURE__ */ new Map();
|
|
31
|
+
const resolveChild = async (request) => {
|
|
32
|
+
const path = joinPath(directoryOf(request.importer), request.specifier);
|
|
33
|
+
const source = await readPath(path);
|
|
34
|
+
if (path === void 0 || source === void 0) return void 0;
|
|
35
|
+
const parsed = parseModule(path, source);
|
|
36
|
+
const edges = /* @__PURE__ */ new Set();
|
|
37
|
+
const stylesheets = /* @__PURE__ */ new Map();
|
|
38
|
+
for (const specifier of stylesheetImports(parsed)) {
|
|
39
|
+
const sheet = joinPath(directoryOf(path), specifier);
|
|
40
|
+
if (sheet !== void 0) edges.add(sheet);
|
|
41
|
+
const text = await readPath(sheet);
|
|
42
|
+
stylesheets.set(specifier, text === void 0 ? void 0 : stylesheetSource(sheet, text));
|
|
43
|
+
}
|
|
44
|
+
const shapes = /* @__PURE__ */ new Map();
|
|
45
|
+
for (const query of typeOracle ? shapeQueries(parsed) : []) {
|
|
46
|
+
const shape = await typeOracle.resolveShape(query);
|
|
47
|
+
shapes.set(`${query.span.start}:${query.span.end}`, shape);
|
|
48
|
+
for (const dependency of shape.dependencies) edges.add(dependency);
|
|
49
|
+
}
|
|
50
|
+
childEdges.set(path, edges);
|
|
51
|
+
const key = JSON.stringify([
|
|
52
|
+
source,
|
|
53
|
+
...[...stylesheets.values()].map((sheet) => sheet?.source ?? null),
|
|
54
|
+
...shapes
|
|
55
|
+
]);
|
|
56
|
+
let cached = children.get(path);
|
|
57
|
+
if (cached?.key !== key) {
|
|
58
|
+
cached = {
|
|
59
|
+
key,
|
|
60
|
+
child: {
|
|
61
|
+
api: analyze(parsed, {
|
|
62
|
+
stylesheets,
|
|
63
|
+
...typeOracle ? { shapes: (span) => shapes.get(`${span.start}:${span.end}`) } : {}
|
|
64
|
+
}).api,
|
|
65
|
+
shapes: [...shapes]
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
children.set(path, cached);
|
|
69
|
+
}
|
|
70
|
+
const { api } = cached.child;
|
|
71
|
+
return {
|
|
72
|
+
api: api && {
|
|
73
|
+
...api,
|
|
74
|
+
file: relativePath(directoryOf(request.importer), path)
|
|
75
|
+
},
|
|
76
|
+
shapes: cached.child.shapes
|
|
77
|
+
};
|
|
78
|
+
};
|
|
79
|
+
const readStylesheet = async (request) => {
|
|
80
|
+
const path = joinPath(directoryOf(request.importer), request.specifier);
|
|
81
|
+
const source = await readPath(path);
|
|
82
|
+
return source === void 0 ? void 0 : {
|
|
83
|
+
file: path,
|
|
84
|
+
source
|
|
85
|
+
};
|
|
86
|
+
};
|
|
87
|
+
const readModule = async (path) => {
|
|
88
|
+
const source = await readPath(path);
|
|
89
|
+
return source === void 0 ? void 0 : {
|
|
90
|
+
file: path,
|
|
91
|
+
source
|
|
92
|
+
};
|
|
93
|
+
};
|
|
94
|
+
/**
|
|
95
|
+
* A shared module and every `.ts` module it reaches (`module`), and each relative `.ts` file the
|
|
96
|
+
* walk looked for and could not read: an edge too, so creating one recompiles the modules that
|
|
97
|
+
* reach it.
|
|
98
|
+
*/
|
|
99
|
+
const followModules = async (request) => {
|
|
100
|
+
const first = await readModule(modulePath(request));
|
|
101
|
+
if (!first) return void 0;
|
|
102
|
+
const files = [first];
|
|
103
|
+
const seen = /* @__PURE__ */ new Set([first.file]);
|
|
104
|
+
const missing = [];
|
|
105
|
+
const resolved = { files };
|
|
106
|
+
for (let index = 0; index < files.length; index++) {
|
|
107
|
+
const { file, source } = files[index];
|
|
108
|
+
const parsed = parseModule(file, source);
|
|
109
|
+
const [unfollowed] = unfollowedImports(parsed);
|
|
110
|
+
if (unfollowed !== void 0) resolved.unresolved ??= {
|
|
111
|
+
specifier: unfollowed,
|
|
112
|
+
file
|
|
113
|
+
};
|
|
114
|
+
for (const specifier of moduleSpecifiers(parsed)) {
|
|
115
|
+
if (frameworkOf(specifier) || isAuthoringModule(specifier)) {
|
|
116
|
+
resolved.framework ??= {
|
|
117
|
+
specifier,
|
|
118
|
+
file
|
|
119
|
+
};
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
if (!specifier.startsWith("./") && !specifier.startsWith("../")) continue;
|
|
123
|
+
const path = modulePath({
|
|
124
|
+
specifier,
|
|
125
|
+
importer: file
|
|
126
|
+
});
|
|
127
|
+
if (path !== void 0 && seen.has(path)) continue;
|
|
128
|
+
const next = await readModule(path);
|
|
129
|
+
if (!next) {
|
|
130
|
+
resolved.unresolved ??= {
|
|
131
|
+
specifier,
|
|
132
|
+
file
|
|
133
|
+
};
|
|
134
|
+
if (path !== void 0) missing.push(path);
|
|
135
|
+
} else {
|
|
136
|
+
seen.add(next.file);
|
|
137
|
+
files.push(next);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return {
|
|
142
|
+
resolved,
|
|
143
|
+
missing
|
|
144
|
+
};
|
|
145
|
+
};
|
|
146
|
+
const readModules = async (request) => (await followModules(request))?.resolved;
|
|
147
|
+
/**
|
|
148
|
+
* The components a module imports to render, by file: its component edges (ADR-0071), each
|
|
149
|
+
* read by the API of the module it imports, which the project's analysis caches by content.
|
|
150
|
+
*/
|
|
151
|
+
const componentEdgesOf = async (file, read) => {
|
|
152
|
+
const source = await readPath(file);
|
|
153
|
+
if (source === void 0) return [];
|
|
154
|
+
let cached = imported.get(file);
|
|
155
|
+
if (cached?.source !== source) {
|
|
156
|
+
cached = {
|
|
157
|
+
source,
|
|
158
|
+
imports: componentImportNames(parseModule(file, source))
|
|
159
|
+
};
|
|
160
|
+
imported.set(file, cached);
|
|
161
|
+
}
|
|
162
|
+
const files = [];
|
|
163
|
+
for (const { specifier, names } of cached.imports) {
|
|
164
|
+
const request = {
|
|
165
|
+
specifier,
|
|
166
|
+
importer: file
|
|
167
|
+
};
|
|
168
|
+
const path = joinPath(directoryOf(file), specifier);
|
|
169
|
+
if (path === void 0) continue;
|
|
170
|
+
read.add(path);
|
|
171
|
+
if (takesComponent((await resolveChild(request))?.api, names)) files.push(path);
|
|
172
|
+
}
|
|
173
|
+
return files;
|
|
174
|
+
};
|
|
175
|
+
/**
|
|
176
|
+
* The shortest cycle of component edges a component edge closes, from the importer back to it,
|
|
177
|
+
* and every file the search read. A component that renders itself is UF1203's, not a cycle
|
|
178
|
+
* (ADR-0053), and a cycle that a key, a type or a shared module closes is none (ADR-0071).
|
|
179
|
+
*/
|
|
180
|
+
const findCycle = async (request) => {
|
|
181
|
+
const start = joinPath(directoryOf(request.importer), request.specifier);
|
|
182
|
+
if (start === void 0 || start === request.importer) return { read: [] };
|
|
183
|
+
const read = /* @__PURE__ */ new Set([start]);
|
|
184
|
+
const previous = /* @__PURE__ */ new Map([[start, request.importer]]);
|
|
185
|
+
const queue = [start];
|
|
186
|
+
for (let index = 0; index < queue.length; index++) {
|
|
187
|
+
const file = queue[index];
|
|
188
|
+
for (const next of await componentEdgesOf(file, read)) {
|
|
189
|
+
if (next === request.importer) {
|
|
190
|
+
const chain = [file];
|
|
191
|
+
while (chain[0] !== start) chain.unshift(previous.get(chain[0]));
|
|
192
|
+
return {
|
|
193
|
+
cycle: [
|
|
194
|
+
request.importer,
|
|
195
|
+
...chain,
|
|
196
|
+
request.importer
|
|
197
|
+
],
|
|
198
|
+
read: [...read]
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
if (next === file || previous.has(next)) continue;
|
|
202
|
+
previous.set(next, file);
|
|
203
|
+
queue.push(next);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
return { read: [...read] };
|
|
207
|
+
};
|
|
208
|
+
const resolve = Object.assign(async (request) => (await resolveChild(request))?.api, {
|
|
209
|
+
stylesheet: readStylesheet,
|
|
210
|
+
module: readModules,
|
|
211
|
+
cycle: async (request) => (await findCycle(request)).cycle
|
|
212
|
+
});
|
|
213
|
+
/**
|
|
214
|
+
* Asks one question again: its answer, what of it a dependent's key holds, and the files it
|
|
215
|
+
* read, which become the asking module's edges.
|
|
216
|
+
*/
|
|
217
|
+
const ask = async (call) => {
|
|
218
|
+
if (call.kind === "shape") {
|
|
219
|
+
const shape = await typeOracle.resolveShape(call.query);
|
|
220
|
+
return {
|
|
221
|
+
value: shape,
|
|
222
|
+
held: shape,
|
|
223
|
+
edges: shape.dependencies
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
const { request } = call;
|
|
227
|
+
const path = joinPath(directoryOf(request.importer), request.specifier);
|
|
228
|
+
if (call.kind === "api") {
|
|
229
|
+
const child = await resolveChild(request);
|
|
230
|
+
return {
|
|
231
|
+
value: child?.api,
|
|
232
|
+
held: child ?? null,
|
|
233
|
+
edges: path === void 0 ? [] : [path]
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
if (call.kind === "stylesheet") {
|
|
237
|
+
const sheet = await readStylesheet(request);
|
|
238
|
+
return {
|
|
239
|
+
value: sheet,
|
|
240
|
+
held: sheet ?? null,
|
|
241
|
+
edges: path === void 0 ? [] : [path]
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
if (call.kind === "module") {
|
|
245
|
+
const followed = await followModules(request);
|
|
246
|
+
const resolved = followed?.resolved;
|
|
247
|
+
const files = resolved?.files.map(({ file }) => file) ?? [];
|
|
248
|
+
const first = modulePath(request);
|
|
249
|
+
return {
|
|
250
|
+
value: resolved,
|
|
251
|
+
held: resolved ? {
|
|
252
|
+
...resolved,
|
|
253
|
+
files
|
|
254
|
+
} : null,
|
|
255
|
+
edges: [
|
|
256
|
+
...first === void 0 ? [] : [first],
|
|
257
|
+
...files,
|
|
258
|
+
...followed?.missing ?? []
|
|
259
|
+
]
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
const { cycle, read } = await findCycle(request);
|
|
263
|
+
return {
|
|
264
|
+
value: cycle,
|
|
265
|
+
held: cycle ?? null,
|
|
266
|
+
edges: read
|
|
267
|
+
};
|
|
268
|
+
};
|
|
269
|
+
/** The key a module compiles under: a hash of its source and of every answer it was given. */
|
|
270
|
+
/**
|
|
271
|
+
* The key a module compiles under: a hash of its source and of every answer it was given. It
|
|
272
|
+
* also gives the files the answers read now, which become the module's edges when the key is
|
|
273
|
+
* unchanged: an import chain the answers no longer follow stops being watched.
|
|
274
|
+
*/
|
|
275
|
+
const keyOf = async (source, calls) => {
|
|
276
|
+
const held = [];
|
|
277
|
+
const edges = /* @__PURE__ */ new Set();
|
|
278
|
+
for (const call of calls) try {
|
|
279
|
+
const answer = await ask(call);
|
|
280
|
+
held.push(answer.held);
|
|
281
|
+
for (const edge of answer.edges) edges.add(edge);
|
|
282
|
+
} catch (error) {
|
|
283
|
+
held.push({ threw: error instanceof Error ? error.message : String(error) });
|
|
284
|
+
const named = namedFile(call);
|
|
285
|
+
if (named !== void 0) edges.add(named);
|
|
286
|
+
}
|
|
287
|
+
return {
|
|
288
|
+
key: await hash(JSON.stringify([source, held])),
|
|
289
|
+
edges
|
|
290
|
+
};
|
|
291
|
+
};
|
|
292
|
+
const build = async (file, source) => {
|
|
293
|
+
const calls = [];
|
|
294
|
+
const held = [];
|
|
295
|
+
const edges = /* @__PURE__ */ new Set();
|
|
296
|
+
const copied = /* @__PURE__ */ new Set();
|
|
297
|
+
const reached = /* @__PURE__ */ new Set();
|
|
298
|
+
const record = async (call) => {
|
|
299
|
+
calls.push(call);
|
|
300
|
+
try {
|
|
301
|
+
const answer = await ask(call);
|
|
302
|
+
held.push(answer.held);
|
|
303
|
+
for (const edge of answer.edges) edges.add(edge);
|
|
304
|
+
if (call.kind === "module") for (const module of answer.value?.files ?? []) {
|
|
305
|
+
copies.set(module.file, module.source);
|
|
306
|
+
reached.add(module.file);
|
|
307
|
+
copied.add(relativePath(directoryOf(file), module.file));
|
|
308
|
+
}
|
|
309
|
+
return answer.value;
|
|
310
|
+
} catch (error) {
|
|
311
|
+
held.push({ threw: error instanceof Error ? error.message : String(error) });
|
|
312
|
+
const named = namedFile(call);
|
|
313
|
+
if (named !== void 0) edges.add(named);
|
|
314
|
+
throw error;
|
|
315
|
+
}
|
|
316
|
+
};
|
|
317
|
+
const recording = Object.assign((request) => record({
|
|
318
|
+
kind: "api",
|
|
319
|
+
request
|
|
320
|
+
}), {
|
|
321
|
+
stylesheet: (request) => record({
|
|
322
|
+
kind: "stylesheet",
|
|
323
|
+
request
|
|
324
|
+
}),
|
|
325
|
+
module: (request) => record({
|
|
326
|
+
kind: "module",
|
|
327
|
+
request
|
|
328
|
+
}),
|
|
329
|
+
cycle: (request) => record({
|
|
330
|
+
kind: "cycle",
|
|
331
|
+
request
|
|
332
|
+
})
|
|
333
|
+
});
|
|
334
|
+
const oracle = typeOracle && {
|
|
335
|
+
resolveShape: (query) => record({
|
|
336
|
+
kind: "shape",
|
|
337
|
+
query
|
|
338
|
+
}),
|
|
339
|
+
printType: (query) => typeOracle.printType(query),
|
|
340
|
+
update: (changes) => typeOracle.update(changes),
|
|
341
|
+
dispose: () => typeOracle.dispose()
|
|
342
|
+
};
|
|
343
|
+
const result = await compile(source, {
|
|
344
|
+
...compileOptions,
|
|
345
|
+
filename: file,
|
|
346
|
+
resolve: recording,
|
|
347
|
+
...oracle ? { typeOracle: oracle } : {}
|
|
348
|
+
});
|
|
349
|
+
for (const dependency of result.dependencies) edges.add(dependency);
|
|
350
|
+
edges.delete(file);
|
|
351
|
+
const outputs = Object.fromEntries(Object.entries(result.outputs).map(([target, files]) => [target, files.filter(({ path }) => !copied.has(path))]));
|
|
352
|
+
const own = {
|
|
353
|
+
...result,
|
|
354
|
+
outputs
|
|
355
|
+
};
|
|
356
|
+
compiled.set(file, {
|
|
357
|
+
result: own,
|
|
358
|
+
key: await hash(JSON.stringify([source, held])),
|
|
359
|
+
calls,
|
|
360
|
+
edges,
|
|
361
|
+
copies: reached
|
|
362
|
+
});
|
|
363
|
+
return own;
|
|
364
|
+
};
|
|
365
|
+
const edgesOf = (file) => /* @__PURE__ */ new Set([...compiled.get(file)?.edges ?? [], ...childEdges.get(file) ?? []]);
|
|
366
|
+
return {
|
|
367
|
+
resolve,
|
|
368
|
+
compile: async (file) => {
|
|
369
|
+
const cached = compiled.get(file);
|
|
370
|
+
if (cached) return cached.result;
|
|
371
|
+
const source = await readPath(file);
|
|
372
|
+
if (source === void 0) throw new Error(`The project has no file "${file}".`);
|
|
373
|
+
return build(file, source);
|
|
374
|
+
},
|
|
375
|
+
copy: (file) => copies.get(file),
|
|
376
|
+
copies: (file) => [...compiled.get(file)?.copies ?? []].flatMap((module) => {
|
|
377
|
+
const contents = copies.get(module);
|
|
378
|
+
if (contents === void 0) return [];
|
|
379
|
+
return [{
|
|
380
|
+
path: relativePath(directoryOf(file), module),
|
|
381
|
+
contents
|
|
382
|
+
}];
|
|
383
|
+
}),
|
|
384
|
+
dependencies: (file) => [...edgesOf(file)].toSorted(),
|
|
385
|
+
update: async (files) => {
|
|
386
|
+
await typeOracle?.update({ files: [...files] });
|
|
387
|
+
const before = new Map(copies);
|
|
388
|
+
for (const file of files) {
|
|
389
|
+
if (!copies.has(file)) continue;
|
|
390
|
+
const source = await readPath(file);
|
|
391
|
+
if (source === void 0) copies.delete(file);
|
|
392
|
+
else copies.set(file, source);
|
|
393
|
+
}
|
|
394
|
+
const dependents = /* @__PURE__ */ new Map();
|
|
395
|
+
for (const file of /* @__PURE__ */ new Set([...compiled.keys(), ...childEdges.keys()])) for (const edge of edgesOf(file)) {
|
|
396
|
+
if (!dependents.has(edge)) dependents.set(edge, /* @__PURE__ */ new Set());
|
|
397
|
+
dependents.get(edge).add(file);
|
|
398
|
+
}
|
|
399
|
+
const affected = new Set(files);
|
|
400
|
+
for (const file of affected) for (const dependent of dependents.get(file) ?? []) affected.add(dependent);
|
|
401
|
+
const recompiled = [];
|
|
402
|
+
for (const file of [...affected].toSorted()) {
|
|
403
|
+
const node = compiled.get(file);
|
|
404
|
+
if (!node) continue;
|
|
405
|
+
const source = await readPath(file);
|
|
406
|
+
if (source === void 0) {
|
|
407
|
+
compiled.delete(file);
|
|
408
|
+
continue;
|
|
409
|
+
}
|
|
410
|
+
const { key, edges } = await keyOf(source, node.calls);
|
|
411
|
+
if (key === node.key) {
|
|
412
|
+
node.edges = /* @__PURE__ */ new Set([...edges, ...node.result.dependencies]);
|
|
413
|
+
node.edges.delete(file);
|
|
414
|
+
continue;
|
|
415
|
+
}
|
|
416
|
+
await build(file, source);
|
|
417
|
+
recompiled.push(file);
|
|
418
|
+
}
|
|
419
|
+
const live = new Set([...compiled.values()].flatMap((node) => [...node.copies]));
|
|
420
|
+
for (const file of copies.keys()) if (!live.has(file)) copies.delete(file);
|
|
421
|
+
return {
|
|
422
|
+
compiled: recompiled,
|
|
423
|
+
copied: [.../* @__PURE__ */ new Set([...before.keys(), ...copies.keys()])].filter((file) => before.get(file) !== copies.get(file)).toSorted()
|
|
424
|
+
};
|
|
425
|
+
}
|
|
426
|
+
};
|
|
427
|
+
}
|
|
428
|
+
/**
|
|
429
|
+
* A resolver over files (ADR-0053): a project's (ADR-0071), which reads the imported file,
|
|
430
|
+
* analyses it up to its declarations, its types read through the type oracle (ADR-0067), and
|
|
431
|
+
* gives its API with its `file` relative to the importer. The analysis is cached by the contents
|
|
432
|
+
* of the file, its stylesheets and its shapes, so an edit that leaves the API alone resolves to
|
|
433
|
+
* the same API, and the parent compiles to the same bytes. A child's API is read from its
|
|
434
|
+
* declarations alone, never from its own `.uf.tsx` imports, so a cycle cannot make it loop: only
|
|
435
|
+
* `cycle` resolves those, for their APIs.
|
|
436
|
+
* Its stylesheets are read, for the scope id its API carries (ADR-0063). It reads a stylesheet for
|
|
437
|
+
* `stylesheet` too, and for `module` a shared `.ts` module, a specifier without an extension
|
|
438
|
+
* naming its `.ts`, and follows its relative imports through every `.ts` module they reach, each
|
|
439
|
+
* read once (ADR-0070). Its `cycle` follows the component edges from an import, each a value
|
|
440
|
+
* import of a component from a `.uf.tsx` module, for the shortest cycle back to the importer
|
|
441
|
+
* (UF1205).
|
|
442
|
+
*/
|
|
443
|
+
function createFileResolver(options) {
|
|
444
|
+
return createProject({
|
|
445
|
+
...options,
|
|
446
|
+
targets: []
|
|
447
|
+
}).resolve;
|
|
448
|
+
}
|
|
449
|
+
/**
|
|
450
|
+
* The file a question named, so a question that threw is still an edge: a fix to that file
|
|
451
|
+
* recompiles the module, whose UF9001 would otherwise stay until its own source changed. A shape
|
|
452
|
+
* query names the module's own file, which is no edge.
|
|
453
|
+
*/
|
|
454
|
+
function namedFile(call) {
|
|
455
|
+
if (call.kind === "shape") return void 0;
|
|
456
|
+
return call.kind === "module" ? modulePath(call.request) : joinPath(directoryOf(call.request.importer), call.request.specifier);
|
|
457
|
+
}
|
|
458
|
+
/** A SHA-256 of a text, in hex. */
|
|
459
|
+
async function hash(text) {
|
|
460
|
+
const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(text));
|
|
461
|
+
return [...new Uint8Array(digest)].map((byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
462
|
+
}
|
|
463
|
+
//#endregion
|
|
464
|
+
export { createFileResolver, createProject };
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import "@unframework.dev/analyzer";
|
|
2
|
+
import { ModuleApi } from "@unframework.dev/ir";
|
|
3
|
+
import "@unframework.dev/parser";
|
|
4
|
+
import { TypeOracle } from "@unframework.dev/type-oracle";
|
|
5
|
+
//#region src/resolver.d.ts
|
|
6
|
+
/** What {@link compile}'s `resolve` option receives: an import's specifier, and its importer. */
|
|
7
|
+
export interface ResolveRequest {
|
|
8
|
+
/**
|
|
9
|
+
* The specifier as written, which ends in `.uf.tsx`, in `.css` for a stylesheet, or names a
|
|
10
|
+
* shared `.ts` module, with its extension or without one.
|
|
11
|
+
*/
|
|
12
|
+
specifier: string;
|
|
13
|
+
/** The importing file's `filename`, as `compile()` was given it. */
|
|
14
|
+
importer: string;
|
|
15
|
+
}
|
|
16
|
+
/** A stylesheet a resolver read (ADR-0063). */
|
|
17
|
+
export interface ResolvedStylesheet {
|
|
18
|
+
/** The sheet's file, named as `compile()`'s `filename` is: diagnostics in the sheet name it. */
|
|
19
|
+
file: string;
|
|
20
|
+
/** The sheet's text. */
|
|
21
|
+
source: string;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* A shared `.ts` module a resolver read (ADR-0070), with every relative `.ts` module it reaches:
|
|
25
|
+
* each output tree holds a copy of each, unchanged.
|
|
26
|
+
*/
|
|
27
|
+
export interface ResolvedModule {
|
|
28
|
+
/**
|
|
29
|
+
* The module, then each module it reaches, in the order the resolver first reached them: each
|
|
30
|
+
* file named as `compile()`'s `filename` is, and its text.
|
|
31
|
+
*/
|
|
32
|
+
files: {
|
|
33
|
+
file: string;
|
|
34
|
+
source: string;
|
|
35
|
+
}[];
|
|
36
|
+
/** The first import of a framework or `@uxfront/unframework` they make, and its file (UF1204). */
|
|
37
|
+
framework?: {
|
|
38
|
+
specifier: string;
|
|
39
|
+
file: string;
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* The first relative import among them the resolver could not read, or the first load it
|
|
43
|
+
* cannot follow (an `import()` of no string, `import.meta.glob`), as written, and its file
|
|
44
|
+
* (UF1202).
|
|
45
|
+
*/
|
|
46
|
+
unresolved?: {
|
|
47
|
+
specifier: string;
|
|
48
|
+
file: string;
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Resolves an imported `.uf.tsx` module to its public API (ADR-0053). Its `stylesheet` reads a
|
|
53
|
+
* component's stylesheet, `import "./Card.css"` (ADR-0063), or gives `undefined` for one that does
|
|
54
|
+
* not exist: without it, no sheet is read (UF5005). Its `module` reads a shared `.ts` module and
|
|
55
|
+
* the modules it reaches (ADR-0070), or gives `undefined` for one that does not exist: without it,
|
|
56
|
+
* a value import of one is UF1202. Its `cycle` gives the files of the cycle of component edges an
|
|
57
|
+
* import closes, from the importer back to it, or `undefined` where there is none (UF1205,
|
|
58
|
+
* ADR-0071): `compile()` asks it only for its own component edges (`componentImportNames` and
|
|
59
|
+
* `takesComponent`), and without it, no cycle is found.
|
|
60
|
+
*/
|
|
61
|
+
export interface Resolver {
|
|
62
|
+
(request: ResolveRequest): Promise<ModuleApi | undefined>;
|
|
63
|
+
stylesheet?(request: ResolveRequest): Promise<ResolvedStylesheet | undefined>;
|
|
64
|
+
module?(request: ResolveRequest): Promise<ResolvedModule | undefined>;
|
|
65
|
+
cycle?(request: ResolveRequest): Promise<string[] | undefined>;
|
|
66
|
+
}
|
|
67
|
+
/** Options for `createFileResolver`, and the files of `createProject` (ADR-0071). */
|
|
68
|
+
export interface FileResolverOptions {
|
|
69
|
+
/**
|
|
70
|
+
* The directory the importers' file names are relative to, as `compile()`'s `filename` is. A
|
|
71
|
+
* file name or an import may leave it (`../packages/ui/B.uf.tsx`): `readFile` decides what may
|
|
72
|
+
* be read.
|
|
73
|
+
*/
|
|
74
|
+
root: string;
|
|
75
|
+
/**
|
|
76
|
+
* Reads a file by its path under `root` (`root`, a `/` and the path relative to it), or gives
|
|
77
|
+
* `undefined` for one that does not exist.
|
|
78
|
+
*/
|
|
79
|
+
readFile(path: string): Promise<string | undefined>;
|
|
80
|
+
/**
|
|
81
|
+
* The type oracle a child's declarations are read through (ADR-0067), over the same `root`: a
|
|
82
|
+
* prop, event, slot or model whose type the child imports, extends or builds with a utility
|
|
83
|
+
* type reaches the parent's API, which also resolves the literals behind an alias (UF3039).
|
|
84
|
+
* Without it, a child's types are read from its own module alone (`localShapes`), and what it
|
|
85
|
+
* imports is unresolved. The caller owns it, and disposes of it.
|
|
86
|
+
*/
|
|
87
|
+
typeOracle?: TypeOracle;
|
|
88
|
+
}
|
|
89
|
+
//#endregion
|
package/dist/resolver.js
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { isSharedModuleSpecifier } from "@unframework.dev/analyzer";
|
|
2
|
+
import { scopeStylesheet } from "@unframework.dev/codegen";
|
|
3
|
+
//#region src/resolver.ts
|
|
4
|
+
/** A shared module's file: the specifier as written, or with `.ts` where it has no extension. */
|
|
5
|
+
function modulePath(request) {
|
|
6
|
+
return isSharedModuleSpecifier(request.specifier) ? joinPath(directoryOf(request.importer), request.specifier.endsWith(".ts") ? request.specifier : `${request.specifier}.ts`) : void 0;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* A stylesheet as the analyser reads it: with whether its selectors use `:slotted()`, which only
|
|
10
|
+
* the codegen scoper can tell, so the compiler asks it before analysis (ADR-0063).
|
|
11
|
+
*/
|
|
12
|
+
function stylesheetSource(file, source) {
|
|
13
|
+
return {
|
|
14
|
+
file,
|
|
15
|
+
source,
|
|
16
|
+
slotted: scopeStylesheet(file, source, "00000000").slotted
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
/** The directory part of a forward-slashed path: `""` for a file at the root. */
|
|
20
|
+
function directoryOf(path) {
|
|
21
|
+
const index = path.lastIndexOf("/");
|
|
22
|
+
return index === -1 ? "" : path.slice(0, index);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* `specifier` joined to `directory`, normalised with forward slashes, or `undefined` when it is
|
|
26
|
+
* not relative. A `..` that leaves the root stays (`../b`), and one after it adds another.
|
|
27
|
+
*/
|
|
28
|
+
function joinPath(directory, specifier) {
|
|
29
|
+
if (!specifier.startsWith("./") && !specifier.startsWith("../")) return void 0;
|
|
30
|
+
const parts = directory ? directory.split("/") : [];
|
|
31
|
+
for (const part of specifier.split("/")) {
|
|
32
|
+
if (part === "." || part === "") continue;
|
|
33
|
+
if (part === ".." && parts.length && parts.at(-1) !== "..") parts.pop();
|
|
34
|
+
else parts.push(part);
|
|
35
|
+
}
|
|
36
|
+
return parts.join("/");
|
|
37
|
+
}
|
|
38
|
+
/** `path` relative to `directory`, both under one root, with forward slashes. */
|
|
39
|
+
function relativePath(directory, path) {
|
|
40
|
+
const from = directory ? directory.split("/") : [];
|
|
41
|
+
const to = path.split("/");
|
|
42
|
+
let shared = 0;
|
|
43
|
+
while (shared < from.length && shared < to.length - 1 && from[shared] === to[shared]) shared++;
|
|
44
|
+
return [...from.slice(shared).map(() => ".."), ...to.slice(shared)].join("/");
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* A module's imports that may be component edges (ADR-0071): each `.uf.tsx` import with the names
|
|
48
|
+
* it takes by value (`default` for a default import), and the span of its first specifier. An
|
|
49
|
+
* import is a component edge when one of its names is a component of the module's API
|
|
50
|
+
* (`takesComponent`): a key, a type or another value closes no cycle (UF1205).
|
|
51
|
+
*/
|
|
52
|
+
function componentImportNames(parsed) {
|
|
53
|
+
const found = /* @__PURE__ */ new Map();
|
|
54
|
+
for (const statement of parsed.program.body) {
|
|
55
|
+
if (statement.type !== "ImportDeclaration" || statement.importKind === "type") continue;
|
|
56
|
+
const specifier = statement.source.value;
|
|
57
|
+
if (!COMPONENT_MODULE.test(specifier)) continue;
|
|
58
|
+
const names = statement.specifiers.flatMap((imported) => {
|
|
59
|
+
if (imported.type === "ImportDefaultSpecifier") return ["default"];
|
|
60
|
+
if (imported.type !== "ImportSpecifier" || imported.importKind === "type") return [];
|
|
61
|
+
const name = imported.imported;
|
|
62
|
+
return [name.type === "Identifier" ? name.name : String(name.value)];
|
|
63
|
+
});
|
|
64
|
+
const entry = found.get(specifier);
|
|
65
|
+
if (entry) entry.names.push(...names);
|
|
66
|
+
else found.set(specifier, {
|
|
67
|
+
span: {
|
|
68
|
+
start: statement.source.start,
|
|
69
|
+
end: statement.source.end
|
|
70
|
+
},
|
|
71
|
+
names
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
return [...found].map(([specifier, entry]) => ({
|
|
75
|
+
specifier,
|
|
76
|
+
...entry
|
|
77
|
+
}));
|
|
78
|
+
}
|
|
79
|
+
/** Whether a module's API exports a component under one of the names an import takes. */
|
|
80
|
+
function takesComponent(api, names) {
|
|
81
|
+
return names.some((name) => api?.components.some((component) => name === "default" ? component.export === "default" : component.export === "named" && component.name === name));
|
|
82
|
+
}
|
|
83
|
+
/** A relative import of a `.uf.tsx` module. */
|
|
84
|
+
const COMPONENT_MODULE = /^\.\.?\/.*\.uf\.tsx$/;
|
|
85
|
+
//#endregion
|
|
86
|
+
export { componentImportNames, directoryOf, joinPath, modulePath, relativePath, stylesheetSource, takesComponent };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { TargetName } from "./config-types.js";
|
|
2
|
+
import { Target } from "@unframework.dev/codegen";
|
|
3
|
+
//#region src/targets.d.ts
|
|
4
|
+
/** The built-in targets, in the order the docs and reports list them. */
|
|
5
|
+
export declare const TARGET_NAMES: readonly TargetName[];
|
|
6
|
+
/** The built-in targets by name. */
|
|
7
|
+
export declare const builtinTargets: Readonly<Record<TargetName, Target>>;
|
|
8
|
+
/** Resolves a target name, or passes a target object through (third-party targets). */
|
|
9
|
+
export declare function resolveTarget(target: TargetName | Target): Target;
|
|
10
|
+
//#endregion
|
|
11
|
+
export type { TargetName };
|
package/dist/targets.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import angular from "@unframework.dev/target-angular";
|
|
2
|
+
import astro from "@unframework.dev/target-astro";
|
|
3
|
+
import qwik from "@unframework.dev/target-qwik";
|
|
4
|
+
import react from "@unframework.dev/target-react";
|
|
5
|
+
import solid from "@unframework.dev/target-solid";
|
|
6
|
+
import svelte from "@unframework.dev/target-svelte";
|
|
7
|
+
import vue from "@unframework.dev/target-vue";
|
|
8
|
+
//#region src/targets.ts
|
|
9
|
+
/** The built-in targets, in the order the docs and reports list them. */
|
|
10
|
+
const TARGET_NAMES = [
|
|
11
|
+
"react",
|
|
12
|
+
"vue",
|
|
13
|
+
"svelte",
|
|
14
|
+
"solid",
|
|
15
|
+
"angular",
|
|
16
|
+
"qwik",
|
|
17
|
+
"astro"
|
|
18
|
+
];
|
|
19
|
+
/** The built-in targets by name. */
|
|
20
|
+
const builtinTargets = {
|
|
21
|
+
react,
|
|
22
|
+
vue,
|
|
23
|
+
svelte,
|
|
24
|
+
solid,
|
|
25
|
+
angular,
|
|
26
|
+
qwik,
|
|
27
|
+
astro
|
|
28
|
+
};
|
|
29
|
+
/** Resolves a target name, or passes a target object through (third-party targets). */
|
|
30
|
+
function resolveTarget(target) {
|
|
31
|
+
if (typeof target !== "string") return target;
|
|
32
|
+
if (!Object.hasOwn(builtinTargets, target)) throw new TypeError(`Unknown target "${target}".`);
|
|
33
|
+
return builtinTargets[target];
|
|
34
|
+
}
|
|
35
|
+
//#endregion
|
|
36
|
+
export { TARGET_NAMES, builtinTargets, resolveTarget };
|