@unframework.dev/compiler 0.3.2 → 0.4.1
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 +22 -8
- package/dist/capabilities.d.ts +1 -1
- package/dist/compile.d.ts +28 -8
- package/dist/compile.js +67 -6
- package/dist/config-types.d.ts +5 -5
- package/dist/config.d.ts +5 -5
- package/dist/config.js +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/plugin-ir.js +1 -0
- package/dist/project.d.ts +18 -10
- package/dist/project.js +189 -66
- package/dist/resolver.d.ts +3 -3
- package/dist/resolver.js +1 -1
- package/dist/targets.d.ts +3 -3
- package/package.json +14 -14
package/README.md
CHANGED
|
@@ -6,8 +6,8 @@ The Unframework compiler (plan §5.1):
|
|
|
6
6
|
import { compile } from "@unframework.dev/compiler";
|
|
7
7
|
|
|
8
8
|
const result = await compile(source, {
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
filename: "src/Hello.uf.tsx",
|
|
10
|
+
targets: ["react", "vue", "svelte", "solid", "angular", "qwik", "astro"],
|
|
11
11
|
});
|
|
12
12
|
result.outputs.vue; // [{ path: "Hello.vue", contents: "<template>…</template>\n" }]
|
|
13
13
|
result.diagnostics; // sorted, with stable UF codes
|
|
@@ -46,6 +46,17 @@ scoped sheet, `<source>.css` (`Button.uf.tsx.css`): its sheets scoped and joined
|
|
|
46
46
|
which the `output` hooks see and formatting keeps as printed. `CompileResult.owners` names the
|
|
47
47
|
component each output file is written for, by target, which the unplugin loads files by; the
|
|
48
48
|
sheet is no component's module, and is left out.
|
|
49
|
+
**Shared helper modules** (ADR-0121). A target puts each helper it emits for a missing primitive
|
|
50
|
+
through `EmitContext.helper`, into one module per helper, `_unframework/<file>` at the root of the
|
|
51
|
+
output tree, which each output imports by a relative path. `helperRoot` names that root, in the
|
|
52
|
+
frame of `filename`: the source's directory or one above it, and the source's own directory by
|
|
53
|
+
default, so one compile's outputs are a complete tree. Any other directory throws. The modules
|
|
54
|
+
follow the components and the copies, sorted and formatted, and each maps onto itself under
|
|
55
|
+
`sourceMap`. `owners` leaves them out, and `CompileResult.helpers` lists their paths by target.
|
|
56
|
+
A module's text depends only on the target and the helper, so every compile that emits it writes
|
|
57
|
+
the same file, and a caller that writes a tree writes each once. A caller must write them beside
|
|
58
|
+
the components. A target that gives one module two texts is UF9001, and a shared module whose
|
|
59
|
+
copy would take a helper module's place is UF1104.
|
|
49
60
|
The `typeOracle` option is the type oracle (plan §5.5, ADR-0067): before it analyses, `compile()`
|
|
50
61
|
asks it for the shape of each type the analyser reads (`shapeQueries`), and the analyser lowers
|
|
51
62
|
props from those shapes, so a props type may come from another file or a package. Its queries name
|
|
@@ -86,7 +97,8 @@ map as it is. Maps don't change the files' text, and they never enter the golden
|
|
|
86
97
|
**The project graph** (ADR-0071). `createProject({ root, readFile, targets, typeOracle })` takes
|
|
87
98
|
the files `createFileResolver` does and the options each compile takes, but `filename` and
|
|
88
99
|
`resolve`. Its `compile(file)` compiles a module, named relative to `root`, through its own
|
|
89
|
-
resolver and the caller's oracle, and keeps the result.
|
|
100
|
+
resolver and the caller's oracle, and keeps the result. Its `helperRoot` is one directory for every
|
|
101
|
+
module, or a function that gives each module's from its file. The result keeps its helper modules. The result leaves out the copies of shared
|
|
90
102
|
`.ts` modules: each copy is its module's own node, which `copy(file)` gives. `copies(file)` gives
|
|
91
103
|
a compiled module's copies at their paths relative to it, each with its module's current text: with
|
|
92
104
|
the result's outputs, they are what `compile()` emits (the unplugin's golden guard reads them), but
|
|
@@ -103,7 +115,9 @@ update renews them even when the key holds. It returns the modules it compiled a
|
|
|
103
115
|
that were added, changed or went.
|
|
104
116
|
|
|
105
117
|
`dependencies(file)` lists what a module read. A caller that changes a file without `update` gets
|
|
106
|
-
the last result. `
|
|
118
|
+
the last result. A project calls `readFile` once for each file while a compile, an update, a
|
|
119
|
+
`resolve()` or a `resolve.cycle()` is in flight, and again in the next one, so each sees one
|
|
120
|
+
version of each file. `createFileResolver` is a project's `resolve`.
|
|
107
121
|
|
|
108
122
|
**Cycles.** The resolver's `cycle` gives the files of the cycle of component edges an import
|
|
109
123
|
closes, from the importer back to it: a component edge is a value import of a component from
|
|
@@ -149,10 +163,10 @@ bundler plugin doesn't call them: it takes explicit options.
|
|
|
149
163
|
import { loadConfig, resolveConfig } from "@unframework.dev/compiler";
|
|
150
164
|
|
|
151
165
|
async function readConfig(root: string) {
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
166
|
+
const loaded = await loadConfig(root);
|
|
167
|
+
// A file that failed to load leaves nothing to check.
|
|
168
|
+
if (loaded.diagnostics.length > 0) return { config: undefined, diagnostics: loaded.diagnostics };
|
|
169
|
+
return resolveConfig(loaded.raw, loaded);
|
|
156
170
|
}
|
|
157
171
|
```
|
|
158
172
|
|
package/dist/capabilities.d.ts
CHANGED
|
@@ -10,4 +10,4 @@ import { UfModule } from "@unframework.dev/ir";
|
|
|
10
10
|
* `nextTick`, refine `interactivity`: `CAPABILITY_PREREQUISITES`) is not reported again: the
|
|
11
11
|
* one report says the listener is inert, where three would say it at the same span.
|
|
12
12
|
*/
|
|
13
|
-
export declare function checkCapabilities(module: UfModule, target: Target): Diagnostic
|
|
13
|
+
export declare function checkCapabilities(module: UfModule, target: Target): Array<Diagnostic>;
|
package/dist/compile.d.ts
CHANGED
|
@@ -25,7 +25,7 @@ export interface CompilerPlugin {
|
|
|
25
25
|
/** Transforms the IR after analysis. Return a new module, or nothing to keep it. */
|
|
26
26
|
ir?(module: UfModule): UfModule | void;
|
|
27
27
|
/** Transforms one component's files for one target, before formatting. */
|
|
28
|
-
output?(files:
|
|
28
|
+
output?(files: ReadonlyArray<OutputFile>, context: OutputHookContext): Array<OutputFile> | void;
|
|
29
29
|
}
|
|
30
30
|
/** Options for {@link compile}. */
|
|
31
31
|
export interface CompileOptions {
|
|
@@ -38,7 +38,7 @@ export interface CompileOptions {
|
|
|
38
38
|
* The targets to emit, by selector (`solid@2`, ADR-0102) or as target objects. Outputs are keyed
|
|
39
39
|
* by each target's id, so `solid@2`'s are under `solid2`.
|
|
40
40
|
*/
|
|
41
|
-
targets:
|
|
41
|
+
targets: ReadonlyArray<TargetSelector | Target>;
|
|
42
42
|
/** Formats the output with oxfmt. Defaults to `true`; the dev server may skip it. */
|
|
43
43
|
format?: boolean;
|
|
44
44
|
/**
|
|
@@ -48,7 +48,7 @@ export interface CompileOptions {
|
|
|
48
48
|
* without one.
|
|
49
49
|
*/
|
|
50
50
|
sourceMap?: boolean;
|
|
51
|
-
plugins?:
|
|
51
|
+
plugins?: ReadonlyArray<CompilerPlugin>;
|
|
52
52
|
/** Each target's options, by target name. */
|
|
53
53
|
targetOptions?: Readonly<Record<string, unknown>>;
|
|
54
54
|
/**
|
|
@@ -70,7 +70,7 @@ export interface CompileOptions {
|
|
|
70
70
|
* absolute throws (`checkPassThrough`). An import the analyser classifies itself (the
|
|
71
71
|
* authoring API, a framework, a `.uf.tsx` or relative module) never passes through.
|
|
72
72
|
*/
|
|
73
|
-
passThrough?:
|
|
73
|
+
passThrough?: ReadonlyArray<string>;
|
|
74
74
|
/**
|
|
75
75
|
* The type oracle the props, events, models, slots and injection keys' types are resolved
|
|
76
76
|
* through (plan §5.5, ADR-0067): `compile()` asks it for each type the analyser reads before it
|
|
@@ -81,26 +81,46 @@ export interface CompileOptions {
|
|
|
81
81
|
* too, so it must hold the text given here. The caller owns it, and disposes of it.
|
|
82
82
|
*/
|
|
83
83
|
typeOracle?: TypeOracle;
|
|
84
|
+
/**
|
|
85
|
+
* The directory whose output tree holds `_unframework/`, the shared helper modules (ADR-0121),
|
|
86
|
+
* named as `filename` is: the directory of `filename` or one above it, with forward slashes, and
|
|
87
|
+
* `""` for the root. It defaults to the directory of `filename`, so one compile's outputs are a
|
|
88
|
+
* complete tree. Any other directory throws (`checkHelperRoot`).
|
|
89
|
+
*/
|
|
90
|
+
helperRoot?: string;
|
|
84
91
|
}
|
|
92
|
+
/**
|
|
93
|
+
* The helper modules' directory, relative to the directory of `filename`: `""` for its own,
|
|
94
|
+
* `"../.."` for the directory two above. Throws when `helperRoot` is not the directory of
|
|
95
|
+
* `filename` or one above it, since a helper module there would sit outside the output tree.
|
|
96
|
+
*/
|
|
97
|
+
export declare function checkHelperRoot(filename: string, helperRoot: string | undefined): string;
|
|
85
98
|
/**
|
|
86
99
|
* Checks the `passThrough` option (ADR-0064): throws when a prefix is empty, or starts with `.`,
|
|
87
100
|
* `/`, `\\` or a drive letter, since such a specifier names a file of the project, a component's
|
|
88
101
|
* own sheet or a child, never a module a plugin registers.
|
|
89
102
|
*/
|
|
90
|
-
export declare function checkPassThrough(prefixes:
|
|
103
|
+
export declare function checkPassThrough(prefixes: ReadonlyArray<string>): void;
|
|
91
104
|
/** The result of {@link compile}. */
|
|
92
105
|
export interface CompileResult {
|
|
93
106
|
filename: string;
|
|
94
107
|
/** The module's IR (frozen), or `undefined` when module-level errors stop compilation. */
|
|
95
108
|
ir: UfModule | undefined;
|
|
96
109
|
/** Every diagnostic, sorted. Portability diagnostics carry their target. */
|
|
97
|
-
diagnostics: Diagnostic
|
|
110
|
+
diagnostics: Array<Diagnostic>;
|
|
98
111
|
/**
|
|
99
112
|
* Each target's files, by target name, in the order of `targets`: each component's, then a copy
|
|
100
113
|
* of each shared `.ts` module the source reaches, unchanged, at its path relative to the source
|
|
101
114
|
* (ADR-0070).
|
|
102
115
|
*/
|
|
103
|
-
outputs: Record<string, OutputFile
|
|
116
|
+
outputs: Record<string, Array<OutputFile>>;
|
|
117
|
+
/**
|
|
118
|
+
* The paths in each target's `outputs` that are shared helper modules (ADR-0121), by target
|
|
119
|
+
* name, sorted: `_unframework/<file>` in the `helperRoot` directory. Each one's text depends only
|
|
120
|
+
* on the target and the helper, so every compile that emits it writes the same file, and a
|
|
121
|
+
* caller that writes a tree writes it once. A caller must write them beside the components.
|
|
122
|
+
*/
|
|
123
|
+
helpers: Record<string, Array<string>>;
|
|
104
124
|
/**
|
|
105
125
|
* The component each output file is written for, by target name, then by the file's path, or
|
|
106
126
|
* the composable for a `.uf.ts` source's: the unplugin loads a file's components under ids of
|
|
@@ -115,7 +135,7 @@ export interface CompileResult {
|
|
|
115
135
|
* too, one that is missing included (ADR-0069), and the packages, as `pkg:<name>`, one that is
|
|
116
136
|
* not installed included. Never an absolute path (P8).
|
|
117
137
|
*/
|
|
118
|
-
dependencies: string
|
|
138
|
+
dependencies: Array<string>;
|
|
119
139
|
}
|
|
120
140
|
/**
|
|
121
141
|
* Compiles one `.uf.tsx` source, or a `.uf.ts` module of composables (ADR-0069), to every
|
package/dist/compile.js
CHANGED
|
@@ -3,13 +3,26 @@ import { isSpanIn, pluginIrProblems } from "./plugin-ir.js";
|
|
|
3
3
|
import { componentImportNames, directoryOf, joinPath, relativePath, stylesheetSource, takesComponent } from "./resolver.js";
|
|
4
4
|
import { resolveTarget } from "./targets.js";
|
|
5
5
|
import { analyze, componentImports, directiveValueQueries, shapeQueries, sharedModuleImports, stylesheetImports } from "@unframework.dev/analyzer";
|
|
6
|
-
import { anchorOutput, formatOutput, identityMapped, kebabCase, remapFormatted, scopeStylesheet } from "@unframework.dev/codegen";
|
|
6
|
+
import { anchorOutput, createHelperModules, formatOutput, identityMapped, kebabCase, remapFormatted, scopeStylesheet } from "@unframework.dev/codegen";
|
|
7
7
|
import { createDiagnostic, sortDiagnostics } from "@unframework.dev/diagnostics";
|
|
8
8
|
import { checkInvariants, validateModule } from "@unframework.dev/ir";
|
|
9
9
|
import { parseModule } from "@unframework.dev/parser";
|
|
10
10
|
import { createSyntacticOracle } from "@unframework.dev/type-oracle";
|
|
11
11
|
// src/compile.ts
|
|
12
12
|
/**
|
|
13
|
+
* The helper modules' directory, relative to the directory of `filename`: `""` for its own,
|
|
14
|
+
* `"../.."` for the directory two above. Throws when `helperRoot` is not the directory of
|
|
15
|
+
* `filename` or one above it, since a helper module there would sit outside the output tree.
|
|
16
|
+
*/
|
|
17
|
+
function checkHelperRoot(filename, helperRoot) {
|
|
18
|
+
const parts = (path) => path.split("/").filter((part) => part && part !== ".");
|
|
19
|
+
const directory = parts(directoryOf(filename));
|
|
20
|
+
if (helperRoot === void 0) return "";
|
|
21
|
+
const root = typeof helperRoot === "string" ? parts(helperRoot) : [];
|
|
22
|
+
if (!(typeof helperRoot === "string" && helperRoot.startsWith("/") === filename.startsWith("/") && root.length <= directory.length && root.every((part, index) => part === directory[index]))) throw new TypeError(`The helperRoot ${JSON.stringify(helperRoot)} is invalid: the helper modules' directory is the directory of ${filename} or one above it, named as the file is (ADR-0121).`);
|
|
23
|
+
return directory.slice(root.length).map(() => "..").join("/");
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
13
26
|
* Checks the `passThrough` option (ADR-0064): throws when a prefix is empty, or starts with `.`,
|
|
14
27
|
* `/`, `\\` or a drive letter, since such a specifier names a file of the project, a component's
|
|
15
28
|
* own sheet or a child, never a module a plugin registers.
|
|
@@ -29,14 +42,17 @@ function checkPassThrough(prefixes) {
|
|
|
29
42
|
async function compile(source, options) {
|
|
30
43
|
const file = options.filename;
|
|
31
44
|
if (options.passThrough) checkPassThrough(options.passThrough);
|
|
45
|
+
const helperBase = checkHelperRoot(file, options.helperRoot);
|
|
32
46
|
const diagnostics = [];
|
|
33
47
|
const targets = options.targets.map(resolveTarget);
|
|
34
48
|
const outputs = {};
|
|
35
49
|
const owners = {};
|
|
50
|
+
const helpers = {};
|
|
36
51
|
for (const target of targets) {
|
|
37
52
|
if (Object.hasOwn(outputs, target.name)) throw new TypeError(`Two targets are named "${target.name}".`);
|
|
38
53
|
outputs[target.name] = [];
|
|
39
54
|
owners[target.name] = {};
|
|
55
|
+
helpers[target.name] = [];
|
|
40
56
|
}
|
|
41
57
|
let analysed;
|
|
42
58
|
/** Each stylesheet's file, by its specifier, as the resolver named it. */
|
|
@@ -194,9 +210,10 @@ async function compile(source, options) {
|
|
|
194
210
|
if (component.styles?.length) sheets.set(component.name, joined);
|
|
195
211
|
}
|
|
196
212
|
if (module) for (const target of targets) try {
|
|
197
|
-
const emitted = await emitTarget(target, module, source, options, diagnostics, sheets, copies);
|
|
213
|
+
const emitted = await emitTarget(target, module, source, options, diagnostics, sheets, copies, helperBase);
|
|
198
214
|
outputs[target.name] = emitted.files;
|
|
199
215
|
owners[target.name] = emitted.owners;
|
|
216
|
+
helpers[target.name] = emitted.helpers;
|
|
200
217
|
} catch (error) {
|
|
201
218
|
diagnostics.push(internal(file, error, `The ${target.name} target failed`, target.name));
|
|
202
219
|
}
|
|
@@ -205,6 +222,7 @@ async function compile(source, options) {
|
|
|
205
222
|
ir: module,
|
|
206
223
|
diagnostics: sortDiagnostics(diagnostics),
|
|
207
224
|
outputs,
|
|
225
|
+
helpers,
|
|
208
226
|
owners,
|
|
209
227
|
dependencies: [...dependencies].toSorted()
|
|
210
228
|
};
|
|
@@ -289,9 +307,13 @@ function importCycle(file, specifier, span, files) {
|
|
|
289
307
|
* may not share a path, compared without case: case-insensitive file systems would write one
|
|
290
308
|
* over the other. Each shared `.ts` module's copy follows, unchanged, where a component's files
|
|
291
309
|
* were written: neither the hooks nor formatting see it, and `owners` leaves it out (ADR-0070).
|
|
310
|
+
* Each helper module the target put comes last, sorted, formatted and identity-mapped, and
|
|
311
|
+
* `owners` leaves it out too (ADR-0121).
|
|
292
312
|
*/
|
|
293
|
-
async function emitTarget(target, module, source, options, diagnostics, sheets, copies) {
|
|
313
|
+
async function emitTarget(target, module, source, options, diagnostics, sheets, copies, helperBase) {
|
|
294
314
|
const file = options.filename;
|
|
315
|
+
const helperModules = createHelperModules(helperBase);
|
|
316
|
+
const helper = helperModules.helper;
|
|
295
317
|
const sheetPath = `${file.slice(file.lastIndexOf("/") + 1)}.css`;
|
|
296
318
|
diagnostics.push(...checkCapabilities(module, target));
|
|
297
319
|
const written = [];
|
|
@@ -302,7 +324,8 @@ async function emitTarget(target, module, source, options, diagnostics, sheets,
|
|
|
302
324
|
const emitted = readFiles(target.emit(component, {
|
|
303
325
|
module,
|
|
304
326
|
options: options.targetOptions?.[target.name],
|
|
305
|
-
report: (diagnostic) => diagnostics.push(reported(diagnostic, source, file, target.name))
|
|
327
|
+
report: (diagnostic) => diagnostics.push(reported(diagnostic, source, file, target.name)),
|
|
328
|
+
helper
|
|
306
329
|
}));
|
|
307
330
|
if ("problem" in emitted) throw new TypeError(`emit returned ${emitted.problem}`);
|
|
308
331
|
const sheet = sheets.get(component.name);
|
|
@@ -402,7 +425,8 @@ async function emitTarget(target, module, source, options, diagnostics, sheets,
|
|
|
402
425
|
const emitted = readFiles(each.emit({
|
|
403
426
|
module,
|
|
404
427
|
options: options.targetOptions?.[target.name],
|
|
405
|
-
report: (diagnostic) => diagnostics.push(reported(diagnostic, source, file, target.name))
|
|
428
|
+
report: (diagnostic) => diagnostics.push(reported(diagnostic, source, file, target.name)),
|
|
429
|
+
helper
|
|
406
430
|
}));
|
|
407
431
|
if ("problem" in emitted) throw new TypeError(`emitting ${each.name} returned ${emitted.problem}`);
|
|
408
432
|
files = emitted.files;
|
|
@@ -463,7 +487,44 @@ async function emitTarget(target, module, source, options, diagnostics, sheets,
|
|
|
463
487
|
contents
|
|
464
488
|
});
|
|
465
489
|
}
|
|
490
|
+
const helperPaths = [];
|
|
491
|
+
for (const module of written.length ? helperModules.files() : []) {
|
|
492
|
+
let output = module;
|
|
493
|
+
if (options.format !== false) {
|
|
494
|
+
const outcome = await formatOutput(module);
|
|
495
|
+
if (outcome.error) {
|
|
496
|
+
const what = `The ${target.name} target emitted the helper module ${module.path}, which does not parse`;
|
|
497
|
+
diagnostics.push(internal(file, outcome.error, what, target.name));
|
|
498
|
+
}
|
|
499
|
+
output = {
|
|
500
|
+
path: module.path,
|
|
501
|
+
contents: outcome.file.contents
|
|
502
|
+
};
|
|
503
|
+
}
|
|
504
|
+
const place = (path) => joinPath(directoryOf(file), `./${path}`)?.toLowerCase();
|
|
505
|
+
const key = place(output.path);
|
|
506
|
+
const owner = [...owners.values()].find(({ path }) => place(path) === key);
|
|
507
|
+
const copy = [...copies].find(([path]) => place(path) === key);
|
|
508
|
+
if (owner || copy) {
|
|
509
|
+
const span = copy?.[1].span ?? {
|
|
510
|
+
start: 0,
|
|
511
|
+
end: 0
|
|
512
|
+
};
|
|
513
|
+
const what = copy ? `The shared module ${copy[0]}` : `${owner.component}'s output, ${owner.path},`;
|
|
514
|
+
diagnostics.push(createDiagnostic("UF1104", {
|
|
515
|
+
file,
|
|
516
|
+
span,
|
|
517
|
+
message: `${what} and the ${target.name} helper module ${output.path} are one file: \`_unframework/\` at the root of each output tree holds the helper modules (ADR-0121).`,
|
|
518
|
+
help: "Rename the shared module, or the directory that holds it.",
|
|
519
|
+
target: target.name
|
|
520
|
+
}));
|
|
521
|
+
continue;
|
|
522
|
+
}
|
|
523
|
+
written.push(options.sourceMap ? identityMapped(output) : output);
|
|
524
|
+
helperPaths.push(output.path);
|
|
525
|
+
}
|
|
466
526
|
return {
|
|
527
|
+
helpers: helperPaths,
|
|
467
528
|
files: written,
|
|
468
529
|
owners: Object.fromEntries([...owners.values()].filter(({ path }) => path !== sheetPath).map(({ path, component }) => [path, component]))
|
|
469
530
|
};
|
|
@@ -596,4 +657,4 @@ function pluginFailed(file, plugin, hook, what, target) {
|
|
|
596
657
|
...target ? { target } : {}
|
|
597
658
|
});
|
|
598
659
|
}
|
|
599
|
-
export { checkPassThrough, compile };
|
|
660
|
+
export { checkHelperRoot, checkPassThrough, compile };
|
package/dist/config-types.d.ts
CHANGED
|
@@ -10,7 +10,7 @@ export type TargetName = "react" | "vue" | "svelte" | "solid" | "solid2" | "angu
|
|
|
10
10
|
export type TargetSelector = TargetName | "solid@1" | "solid@2";
|
|
11
11
|
/** The targets packaging covers, and where each packager resolves from (ADR-0076). */
|
|
12
12
|
export interface PackageConfig {
|
|
13
|
-
targets?:
|
|
13
|
+
targets?: ReadonlyArray<TargetSelector>;
|
|
14
14
|
/** One directory, or one per target. */
|
|
15
15
|
resolveFrom?: string | Partial<Record<TargetName, string>>;
|
|
16
16
|
}
|
|
@@ -21,7 +21,7 @@ export interface PackageConfig {
|
|
|
21
21
|
*/
|
|
22
22
|
export type VitePluginOption = {
|
|
23
23
|
readonly name: string;
|
|
24
|
-
} | false | null | undefined |
|
|
24
|
+
} | false | null | undefined | ReadonlyArray<VitePluginOption> | PromiseLike<VitePluginOption>;
|
|
25
25
|
/** The kind of Vite server a project's `vite` config is for (ADR-0104). */
|
|
26
26
|
export type ViteMode = "browser" | "ssr" | "preview";
|
|
27
27
|
/** What a project's `vite` function is called with: the server's target and its kind. */
|
|
@@ -36,7 +36,7 @@ export interface ViteContext {
|
|
|
36
36
|
* (ADR-0104).
|
|
37
37
|
*/
|
|
38
38
|
export type ProjectViteConfig = {
|
|
39
|
-
plugins?:
|
|
39
|
+
plugins?: ReadonlyArray<VitePluginOption>;
|
|
40
40
|
} & object;
|
|
41
41
|
/** The configuration as a project writes it, with `defineConfig` from `@uxfront/unframework/config`. */
|
|
42
42
|
export interface UnframeworkConfig {
|
|
@@ -44,7 +44,7 @@ export interface UnframeworkConfig {
|
|
|
44
44
|
* Defaults to the seven targets of `DEFAULT_TARGETS`. `solid@2` is opt-in, since a project that
|
|
45
45
|
* selects both Solid majors needs two Solid installs (ADR-0102).
|
|
46
46
|
*/
|
|
47
|
-
targets?:
|
|
47
|
+
targets?: ReadonlyArray<TargetSelector>;
|
|
48
48
|
/** Defaults to `src`, relative to the config's directory. */
|
|
49
49
|
srcDir?: string;
|
|
50
50
|
/** Defaults to `dist`, relative to the config's directory. */
|
|
@@ -68,5 +68,5 @@ export interface UnframeworkConfig {
|
|
|
68
68
|
* The font families that may draw text when pixels compare live, besides the bundled Inter, as
|
|
69
69
|
* the `font-fallback` message names them. Committed baselines take Inter alone (ADR-0105).
|
|
70
70
|
*/
|
|
71
|
-
liveFonts?:
|
|
71
|
+
liveFonts?: ReadonlyArray<string>;
|
|
72
72
|
}
|
package/dist/config.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { PackageConfig, TargetName, UnframeworkConfig } from "./config-types.js"
|
|
|
2
2
|
import { Diagnostic } from "@unframework.dev/diagnostics";
|
|
3
3
|
/** The configuration with its defaults applied, and its paths absolute. */
|
|
4
4
|
export interface ResolvedConfig {
|
|
5
|
-
targets:
|
|
5
|
+
targets: ReadonlyArray<TargetName>;
|
|
6
6
|
srcDir: string;
|
|
7
7
|
outDir: string;
|
|
8
8
|
targetOptions: Partial<Record<TargetName, unknown>>;
|
|
@@ -12,11 +12,11 @@ export interface ResolvedConfig {
|
|
|
12
12
|
/** The project's Vite config, as a function each server calls (ADR-0104). */
|
|
13
13
|
vite?: UnframeworkConfig["vite"];
|
|
14
14
|
/** The font families that may draw text when pixels compare live, besides Inter (ADR-0105). */
|
|
15
|
-
liveFonts:
|
|
15
|
+
liveFonts: ReadonlyArray<string>;
|
|
16
16
|
}
|
|
17
17
|
/** The packaging config with its selectors resolved to target ids (ADR-0102). */
|
|
18
18
|
export interface ResolvedPackageConfig extends Omit<PackageConfig, "targets"> {
|
|
19
|
-
targets?:
|
|
19
|
+
targets?: ReadonlyArray<TargetName>;
|
|
20
20
|
}
|
|
21
21
|
/** Where a configuration came from: what {@link loadConfig} gives {@link resolveConfig}. */
|
|
22
22
|
export interface ConfigOrigin {
|
|
@@ -33,13 +33,13 @@ export interface LoadedConfig extends ConfigOrigin {
|
|
|
33
33
|
directory: string;
|
|
34
34
|
/** The file relative to the root, when there is one. */
|
|
35
35
|
file?: string;
|
|
36
|
-
diagnostics: Diagnostic
|
|
36
|
+
diagnostics: Array<Diagnostic>;
|
|
37
37
|
}
|
|
38
38
|
/** The result of {@link resolveConfig}. */
|
|
39
39
|
export interface ResolveConfigResult {
|
|
40
40
|
/** `undefined` when any field is invalid. */
|
|
41
41
|
config: ResolvedConfig | undefined;
|
|
42
|
-
diagnostics: Diagnostic
|
|
42
|
+
diagnostics: Array<Diagnostic>;
|
|
43
43
|
}
|
|
44
44
|
/**
|
|
45
45
|
* Imports `unframework.config.{ts,mts,js,mjs}` from `root`, or `file` relative to it, through
|
package/dist/config.js
CHANGED
|
@@ -24,7 +24,7 @@ const FIELDS = [
|
|
|
24
24
|
"liveFonts"
|
|
25
25
|
];
|
|
26
26
|
const PACKAGE_FIELDS = ["targets", "resolveFrom"];
|
|
27
|
-
const TYPE_ORACLES = ["syntactic", "tsgo"];
|
|
27
|
+
const TYPE_ORACLES = /* @__PURE__ */ new Set(["syntactic", "tsgo"]);
|
|
28
28
|
/** A configuration has no source span to point at: the message names the file, or the field. */
|
|
29
29
|
const NO_SPAN = {
|
|
30
30
|
start: 0,
|
|
@@ -107,7 +107,7 @@ async function checkConfig(raw, directory, report) {
|
|
|
107
107
|
const srcDir = pathField(raw.srcDir === void 0 ? "src" : raw.srcDir, "/srcDir", directory, report);
|
|
108
108
|
const outDir = pathField(raw.outDir === void 0 ? "dist" : raw.outDir, "/outDir", directory, report);
|
|
109
109
|
const targetOptions = await resolveTargetOptions(raw.targetOptions, report);
|
|
110
|
-
if (raw.typeOracle !== void 0 && !TYPE_ORACLES.
|
|
110
|
+
if (raw.typeOracle !== void 0 && !TYPE_ORACLES.has(raw.typeOracle)) report("UF7002", "/typeOracle", `must be "syntactic" or "tsgo".`);
|
|
111
111
|
if (raw.strict !== void 0 && typeof raw.strict !== "boolean") report("UF7002", "/strict", "must be a boolean.");
|
|
112
112
|
const packaging = raw.package === void 0 ? void 0 : resolvePackage(raw.package, targets, directory, report);
|
|
113
113
|
if (raw.vite !== void 0 && typeof raw.vite !== "function") report("UF7002", "/vite", "must be a function that returns a Vite config, such as `() => ({ plugins: [engine()] })`: each Vite server calls it for a fresh set of plugins.");
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { CompileOptions, CompileResult, CompilerPlugin, OutputHookContext, checkPassThrough, compile } from "./compile.js";
|
|
1
|
+
import { CompileOptions, CompileResult, CompilerPlugin, OutputHookContext, checkHelperRoot, checkPassThrough, compile } from "./compile.js";
|
|
2
2
|
import { Project, ProjectOptions, ProjectUpdate, createFileResolver, createProject } from "./project.js";
|
|
3
3
|
import { FileResolverOptions, ResolveRequest, ResolvedModule, ResolvedStylesheet, Resolver } from "./resolver.js";
|
|
4
4
|
import { ConfigOrigin, LoadedConfig, ResolveConfigResult, ResolvedConfig, ResolvedPackageConfig, loadConfig, resolveConfig } from "./config.js";
|
|
@@ -10,4 +10,4 @@ import { OutputFile, Target, requiredCapabilities } from "@unframework.dev/codeg
|
|
|
10
10
|
import { Diagnostic } from "@unframework.dev/diagnostics";
|
|
11
11
|
import { UfModule } from "@unframework.dev/ir";
|
|
12
12
|
import { SyntacticOracleOptions, TypeOracle, createSyntacticOracle } from "@unframework.dev/type-oracle";
|
|
13
|
-
export { type CompileOptions, type CompileResult, type CompilerPlugin, type ConfigOrigin, DEFAULT_PASS_THROUGH, DEFAULT_TARGETS, type Diagnostic, type FileResolverOptions, type LoadedConfig, type OutputFile, type OutputHookContext, type PackageConfig, type Project, type ProjectOptions, type ProjectUpdate, type ProjectViteConfig, type ResolveConfigResult, type ResolveRequest, type ResolvedConfig, type ResolvedModule, type ResolvedPackageConfig, type ResolvedStylesheet, type Resolver, type SyntacticOracleOptions, TARGET_NAMES, TARGET_SELECTORS, type Target, type TargetName, type TargetSelector, type TypeOracle, type UfModule, type UnframeworkConfig, type ViteContext, type ViteMode, type VitePluginOption, builtinTargets, checkCapabilities, checkPassThrough, compile, createFileResolver, createProject, createSyntacticOracle, isTargetSelector, loadConfig, requiredCapabilities, resolveConfig, resolveTarget, targetName };
|
|
13
|
+
export { type CompileOptions, type CompileResult, type CompilerPlugin, type ConfigOrigin, DEFAULT_PASS_THROUGH, DEFAULT_TARGETS, type Diagnostic, type FileResolverOptions, type LoadedConfig, type OutputFile, type OutputHookContext, type PackageConfig, type Project, type ProjectOptions, type ProjectUpdate, type ProjectViteConfig, type ResolveConfigResult, type ResolveRequest, type ResolvedConfig, type ResolvedModule, type ResolvedPackageConfig, type ResolvedStylesheet, type Resolver, type SyntacticOracleOptions, TARGET_NAMES, TARGET_SELECTORS, type Target, type TargetName, type TargetSelector, type TypeOracle, type UfModule, type UnframeworkConfig, type ViteContext, type ViteMode, type VitePluginOption, builtinTargets, checkCapabilities, checkHelperRoot, checkPassThrough, compile, createFileResolver, createProject, createSyntacticOracle, isTargetSelector, loadConfig, requiredCapabilities, resolveConfig, resolveTarget, targetName };
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { checkPassThrough, compile } from "./compile.js";
|
|
1
|
+
import { checkHelperRoot, checkPassThrough, compile } from "./compile.js";
|
|
2
2
|
import { createFileResolver, createProject } from "./project.js";
|
|
3
3
|
import { loadConfig, resolveConfig } from "./config.js";
|
|
4
4
|
import { DEFAULT_TARGETS, TARGET_NAMES, TARGET_SELECTORS, builtinTargets, isTargetSelector, resolveTarget, targetName } from "./targets.js";
|
|
@@ -6,4 +6,4 @@ import { checkCapabilities } from "./capabilities.js";
|
|
|
6
6
|
import { DEFAULT_PASS_THROUGH } from "@unframework.dev/analyzer";
|
|
7
7
|
import { requiredCapabilities } from "@unframework.dev/codegen";
|
|
8
8
|
import { createSyntacticOracle } from "@unframework.dev/type-oracle";
|
|
9
|
-
export { DEFAULT_PASS_THROUGH, DEFAULT_TARGETS, TARGET_NAMES, TARGET_SELECTORS, builtinTargets, checkCapabilities, checkPassThrough, compile, createFileResolver, createProject, createSyntacticOracle, isTargetSelector, loadConfig, requiredCapabilities, resolveConfig, resolveTarget, targetName };
|
|
9
|
+
export { DEFAULT_PASS_THROUGH, DEFAULT_TARGETS, TARGET_NAMES, TARGET_SELECTORS, builtinTargets, checkCapabilities, checkHelperRoot, checkPassThrough, compile, createFileResolver, createProject, createSyntacticOracle, isTargetSelector, loadConfig, requiredCapabilities, resolveConfig, resolveTarget, targetName };
|
package/dist/plugin-ir.js
CHANGED
package/dist/project.d.ts
CHANGED
|
@@ -5,13 +5,20 @@ import { OutputFile } from "@unframework.dev/codegen";
|
|
|
5
5
|
* Options for {@link createProject}: its files, as `createFileResolver`'s, and the options each of
|
|
6
6
|
* its compiles takes, but the file name and the resolver, which are the project's.
|
|
7
7
|
*/
|
|
8
|
-
export type ProjectOptions = FileResolverOptions & Omit<CompileOptions, "filename" | "resolve" | "typeOracle"
|
|
8
|
+
export type ProjectOptions = FileResolverOptions & Omit<CompileOptions, "filename" | "resolve" | "typeOracle" | "helperRoot"> & {
|
|
9
|
+
/**
|
|
10
|
+
* The directory whose output tree holds `_unframework/`, the shared helper modules
|
|
11
|
+
* (ADR-0121), relative to the root: one for every module, or one per module, given its file.
|
|
12
|
+
* Defaults to each module's own directory, as `compile()`'s does.
|
|
13
|
+
*/
|
|
14
|
+
helperRoot?: string | ((file: string) => string);
|
|
15
|
+
};
|
|
9
16
|
/** What {@link Project.update} did. */
|
|
10
17
|
export interface ProjectUpdate {
|
|
11
18
|
/** The modules compiled again, sorted, each once. */
|
|
12
|
-
compiled: string
|
|
19
|
+
compiled: Array<string>;
|
|
13
20
|
/** The shared `.ts` modules whose copy was added, changed or went, sorted. */
|
|
14
|
-
copied: string
|
|
21
|
+
copied: Array<string>;
|
|
15
22
|
}
|
|
16
23
|
/** A project's module graph (ADR-0071). Every file is named relative to its root. */
|
|
17
24
|
export interface Project {
|
|
@@ -36,15 +43,15 @@ export interface Project {
|
|
|
36
43
|
* relative to it, each with its module's current text: what `compile()` adds to every output
|
|
37
44
|
* tree beside the module's own files (ADR-0070). Empty for a module the project has not compiled.
|
|
38
45
|
*/
|
|
39
|
-
copies(file: string): OutputFile
|
|
46
|
+
copies(file: string): Array<OutputFile>;
|
|
40
47
|
/** What a module read, sorted: files, and packages as `pkg:<name>` (ADR-0071). */
|
|
41
|
-
dependencies(file: string): string
|
|
48
|
+
dependencies(file: string): Array<string>;
|
|
42
49
|
/**
|
|
43
50
|
* Routes changed, created or deleted files through the project: it tells the type oracle, reads
|
|
44
51
|
* again the copies among them, then checks each compiled module that depends on a file,
|
|
45
52
|
* directly or not, and compiles it again only where what it read changed.
|
|
46
53
|
*/
|
|
47
|
-
update(files:
|
|
54
|
+
update(files: ReadonlyArray<string>): Promise<ProjectUpdate>;
|
|
48
55
|
}
|
|
49
56
|
/**
|
|
50
57
|
* The project graph (ADR-0071) over the files `readFile` gives. A module's cache key is a hash of
|
|
@@ -58,10 +65,11 @@ export declare function createProject(options: ProjectOptions): Project;
|
|
|
58
65
|
* A resolver over files (ADR-0053): a project's (ADR-0071), which reads the imported file,
|
|
59
66
|
* analyses it up to its declarations, its types read through the type oracle (ADR-0067), and
|
|
60
67
|
* gives its API with its `file` relative to the importer. The analysis is cached by the contents
|
|
61
|
-
* of the file, its stylesheets and its
|
|
62
|
-
* the same API, and the parent compiles to the same bytes. A child's
|
|
63
|
-
*
|
|
64
|
-
*
|
|
68
|
+
* of the file, its stylesheets, its shapes and its own children's APIs, so an edit that leaves
|
|
69
|
+
* the API alone resolves to the same API, and the parent compiles to the same bytes. A child's
|
|
70
|
+
* API is read from its declarations and its own children's APIs, which give a root component's
|
|
71
|
+
* tags (ADR-0118), each child read once while a resolve is in flight: an import back to a child
|
|
72
|
+
* being read on the same path reads no API, and only `cycle` reports it.
|
|
65
73
|
* Its stylesheets are read, for the scope id its API carries (ADR-0063). It reads a stylesheet for
|
|
66
74
|
* `stylesheet` too, and for `module` a shared `.ts` module, a specifier without an extension
|
|
67
75
|
* naming its `.ts`, and follows its relative imports through every `.ts` module they reach, each
|
package/dist/project.js
CHANGED
|
@@ -4,6 +4,12 @@ import { analyze, frameworkOf, isAuthoringModule, moduleSpecifiers, shapeQueries
|
|
|
4
4
|
import { parseModule } from "@unframework.dev/parser";
|
|
5
5
|
// src/project.ts
|
|
6
6
|
/**
|
|
7
|
+
* What an import reads where its child imports, through its own children, the importer back:
|
|
8
|
+
* no API (UF1205). Every edge of a cycle is cut, whatever imports the cycle first or when, so a
|
|
9
|
+
* child's API depends on the graph alone (P8).
|
|
10
|
+
*/
|
|
11
|
+
const CYCLE = Symbol("cycle");
|
|
12
|
+
/**
|
|
7
13
|
* The project graph (ADR-0071) over the files `readFile` gives. A module's cache key is a hash of
|
|
8
14
|
* its source and of everything it read through the project: its children's APIs and the shapes
|
|
9
15
|
* they were analysed with, its stylesheets, the files of its shared modules, its import cycles
|
|
@@ -13,12 +19,14 @@ import { parseModule } from "@unframework.dev/parser";
|
|
|
13
19
|
function createProject(options) {
|
|
14
20
|
const root = options.root.replace(/\/+$/, "");
|
|
15
21
|
const { typeOracle } = options;
|
|
16
|
-
const
|
|
22
|
+
const { helperRoot: _helperRoot, ...rest } = options;
|
|
23
|
+
const compileOptions = { ...rest };
|
|
17
24
|
for (const key of [
|
|
18
25
|
"root",
|
|
19
26
|
"readFile",
|
|
20
27
|
"typeOracle"
|
|
21
28
|
]) Reflect.deleteProperty(compileOptions, key);
|
|
29
|
+
const helperRootOf = (file) => typeof options.helperRoot === "function" ? options.helperRoot(file) : options.helperRoot;
|
|
22
30
|
const readPath = (path) => path === void 0 ? Promise.resolve(void 0) : options.readFile(`${root}/${path}`);
|
|
23
31
|
/** Each child's analysis, by its file, with the key it was analysed under. */
|
|
24
32
|
const children = /* @__PURE__ */ new Map();
|
|
@@ -28,17 +36,127 @@ function createProject(options) {
|
|
|
28
36
|
const imported = /* @__PURE__ */ new Map();
|
|
29
37
|
const compiled = /* @__PURE__ */ new Map();
|
|
30
38
|
const copies = /* @__PURE__ */ new Map();
|
|
39
|
+
/**
|
|
40
|
+
* The children analysed in the current session, by file (ADR-0118): a parent reads each child
|
|
41
|
+
* with its own children, so without it a child that many paths reach is read once per path.
|
|
42
|
+
* A session lasts while a compile, an update or a resolve is in flight, so it sees each file
|
|
43
|
+
* as it is then, and an update starts a fresh one. It reads each file's text once too: the
|
|
44
|
+
* children, the cycle searches and the compile itself share it, so a compile waits on one read
|
|
45
|
+
* per file, not one per path that reaches it.
|
|
46
|
+
*/
|
|
47
|
+
let session;
|
|
48
|
+
let active = 0;
|
|
49
|
+
const newSession = () => ({
|
|
50
|
+
analyses: /* @__PURE__ */ new Map(),
|
|
51
|
+
imports: /* @__PURE__ */ new Map(),
|
|
52
|
+
texts: /* @__PURE__ */ new Map()
|
|
53
|
+
});
|
|
54
|
+
const inSession = async (run, fresh = false) => {
|
|
55
|
+
if (fresh || !session) session = newSession();
|
|
56
|
+
active++;
|
|
57
|
+
try {
|
|
58
|
+
return await run();
|
|
59
|
+
} finally {
|
|
60
|
+
if (--active === 0) session = void 0;
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
const memoOf = () => session ?? newSession();
|
|
64
|
+
/**
|
|
65
|
+
* A file's text, read once in the session `memo` (the current one by default), or read again
|
|
66
|
+
* outside a session. A read that fails is forgotten, so the next one tries again.
|
|
67
|
+
*/
|
|
68
|
+
const readText = (path, memo = session) => {
|
|
69
|
+
if (path === void 0 || !memo) return readPath(path);
|
|
70
|
+
let text = memo.texts.get(path);
|
|
71
|
+
if (!text) {
|
|
72
|
+
const read = readPath(path);
|
|
73
|
+
memo.texts.set(path, read);
|
|
74
|
+
read.catch(() => {
|
|
75
|
+
if (memo.texts.get(path) === read) memo.texts.delete(path);
|
|
76
|
+
});
|
|
77
|
+
text = read;
|
|
78
|
+
}
|
|
79
|
+
return text;
|
|
80
|
+
};
|
|
81
|
+
/** The files a child imports components from, read once in the session. */
|
|
82
|
+
const importsOf = (path, memo) => {
|
|
83
|
+
let found = memo.imports.get(path);
|
|
84
|
+
if (!found) {
|
|
85
|
+
found = readText(path, memo).then((source) => source === void 0 ? [] : componentImportNames(parseModule(path, source)).flatMap(({ specifier }) => {
|
|
86
|
+
const file = joinPath(directoryOf(path), specifier);
|
|
87
|
+
return file === void 0 ? [] : [file];
|
|
88
|
+
}));
|
|
89
|
+
memo.imports.set(path, found);
|
|
90
|
+
}
|
|
91
|
+
return found;
|
|
92
|
+
};
|
|
93
|
+
/** Whether `from` imports `to`, through its children's imports (or is it). */
|
|
94
|
+
const reaches = async (from, to, memo) => {
|
|
95
|
+
const queue = [from];
|
|
96
|
+
const seen = new Set(queue);
|
|
97
|
+
for (let index = 0; index < queue.length; index++) {
|
|
98
|
+
const each = queue[index];
|
|
99
|
+
if (each === to) return true;
|
|
100
|
+
for (const next of await importsOf(each, memo)) {
|
|
101
|
+
if (seen.has(next)) continue;
|
|
102
|
+
seen.add(next);
|
|
103
|
+
queue.push(next);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return false;
|
|
107
|
+
};
|
|
108
|
+
/** A child's API, with its own children's (ADR-0118), as the session reads it. */
|
|
31
109
|
const resolveChild = async (request) => {
|
|
32
110
|
const path = joinPath(directoryOf(request.importer), request.specifier);
|
|
33
|
-
const
|
|
34
|
-
if (
|
|
111
|
+
const analysed = path === void 0 ? void 0 : await analysisOf(path, memoOf());
|
|
112
|
+
if (!analysed) return;
|
|
113
|
+
const { api } = analysed.child;
|
|
114
|
+
return {
|
|
115
|
+
api: api && {
|
|
116
|
+
...api,
|
|
117
|
+
file: relativePath(directoryOf(request.importer), path)
|
|
118
|
+
},
|
|
119
|
+
shapes: analysed.child.shapes
|
|
120
|
+
};
|
|
121
|
+
};
|
|
122
|
+
/**
|
|
123
|
+
* A child's analysis in the session `memo`: the one another path is reading or has read, or a
|
|
124
|
+
* new one. Awaiting another path's never deadlocks: a child waits only on children outside its
|
|
125
|
+
* own cycles, which `analyseChild` cuts.
|
|
126
|
+
*/
|
|
127
|
+
const analysisOf = (path, memo) => {
|
|
128
|
+
let entry = memo.analyses.get(path);
|
|
129
|
+
if (!entry) {
|
|
130
|
+
entry = { pending: readText(path, memo).then((source) => source === void 0 ? void 0 : analyseChild(path, source, memo)) };
|
|
131
|
+
memo.analyses.set(path, entry);
|
|
132
|
+
const created = entry;
|
|
133
|
+
created.pending.catch(() => {
|
|
134
|
+
if (memo.analyses.get(path) === created) memo.analyses.delete(path);
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
return entry.pending;
|
|
138
|
+
};
|
|
139
|
+
const analyseChild = async (path, source, memo) => {
|
|
35
140
|
const parsed = parseModule(path, source);
|
|
36
141
|
const edges = /* @__PURE__ */ new Set();
|
|
142
|
+
const imports = /* @__PURE__ */ new Map();
|
|
143
|
+
for (const { specifier } of componentImportNames(parsed)) {
|
|
144
|
+
const file = joinPath(directoryOf(path), specifier);
|
|
145
|
+
const cyclic = file !== void 0 && await reaches(file, path, memo);
|
|
146
|
+
const analysed = file === void 0 || cyclic ? CYCLE : await analysisOf(file, memo);
|
|
147
|
+
const api = analysed && analysed !== CYCLE ? analysed.child.api : void 0;
|
|
148
|
+
imports.set(specifier, api && {
|
|
149
|
+
...api,
|
|
150
|
+
file: relativePath(directoryOf(path), file)
|
|
151
|
+
});
|
|
152
|
+
if (file !== void 0) edges.add(file);
|
|
153
|
+
for (const edge of file !== void 0 && childEdges.get(file) || []) edges.add(edge);
|
|
154
|
+
}
|
|
37
155
|
const stylesheets = /* @__PURE__ */ new Map();
|
|
38
156
|
for (const specifier of stylesheetImports(parsed)) {
|
|
39
157
|
const sheet = joinPath(directoryOf(path), specifier);
|
|
40
158
|
if (sheet !== void 0) edges.add(sheet);
|
|
41
|
-
const text = await
|
|
159
|
+
const text = await readText(sheet, memo);
|
|
42
160
|
stylesheets.set(specifier, text === void 0 ? void 0 : stylesheetSource(sheet, text));
|
|
43
161
|
}
|
|
44
162
|
const shapes = /* @__PURE__ */ new Map();
|
|
@@ -51,7 +169,8 @@ function createProject(options) {
|
|
|
51
169
|
const key = JSON.stringify([
|
|
52
170
|
source,
|
|
53
171
|
...[...stylesheets.values()].map((sheet) => sheet?.source ?? null),
|
|
54
|
-
...shapes
|
|
172
|
+
...shapes,
|
|
173
|
+
...imports
|
|
55
174
|
]);
|
|
56
175
|
let cached = children.get(path);
|
|
57
176
|
if (cached?.key !== key) {
|
|
@@ -60,6 +179,7 @@ function createProject(options) {
|
|
|
60
179
|
child: {
|
|
61
180
|
api: analyze(parsed, {
|
|
62
181
|
stylesheets,
|
|
182
|
+
imports,
|
|
63
183
|
...typeOracle ? { shapes: (span) => shapes.get(`${span.start}:${span.end}`) } : {}
|
|
64
184
|
}).api,
|
|
65
185
|
shapes: [...shapes]
|
|
@@ -67,25 +187,18 @@ function createProject(options) {
|
|
|
67
187
|
};
|
|
68
188
|
children.set(path, cached);
|
|
69
189
|
}
|
|
70
|
-
|
|
71
|
-
return {
|
|
72
|
-
api: api && {
|
|
73
|
-
...api,
|
|
74
|
-
file: relativePath(directoryOf(request.importer), path)
|
|
75
|
-
},
|
|
76
|
-
shapes: cached.child.shapes
|
|
77
|
-
};
|
|
190
|
+
return { child: cached.child };
|
|
78
191
|
};
|
|
79
192
|
const readStylesheet = async (request) => {
|
|
80
193
|
const path = joinPath(directoryOf(request.importer), request.specifier);
|
|
81
|
-
const source = await
|
|
194
|
+
const source = await readText(path);
|
|
82
195
|
return source === void 0 ? void 0 : {
|
|
83
196
|
file: path,
|
|
84
197
|
source
|
|
85
198
|
};
|
|
86
199
|
};
|
|
87
200
|
const readModule = async (path) => {
|
|
88
|
-
const source = await
|
|
201
|
+
const source = await readText(path);
|
|
89
202
|
return source === void 0 ? void 0 : {
|
|
90
203
|
file: path,
|
|
91
204
|
source
|
|
@@ -98,7 +211,7 @@ function createProject(options) {
|
|
|
98
211
|
*/
|
|
99
212
|
const followModules = async (request) => {
|
|
100
213
|
const first = await readModule(modulePath(request));
|
|
101
|
-
if (!first) return
|
|
214
|
+
if (!first) return;
|
|
102
215
|
const files = [first];
|
|
103
216
|
const seen = /* @__PURE__ */ new Set([first.file]);
|
|
104
217
|
const missing = [];
|
|
@@ -149,7 +262,7 @@ function createProject(options) {
|
|
|
149
262
|
* read by the API of the module it imports, which the project's analysis caches by content.
|
|
150
263
|
*/
|
|
151
264
|
const componentEdgesOf = async (file, read) => {
|
|
152
|
-
const source = await
|
|
265
|
+
const source = await readText(file);
|
|
153
266
|
if (source === void 0) return [];
|
|
154
267
|
let cached = imported.get(file);
|
|
155
268
|
if (cached?.source !== source) {
|
|
@@ -205,10 +318,10 @@ function createProject(options) {
|
|
|
205
318
|
}
|
|
206
319
|
return { read: [...read] };
|
|
207
320
|
};
|
|
208
|
-
const resolve = Object.assign(async (
|
|
321
|
+
const resolve = Object.assign((request) => inSession(async () => (await resolveChild(request))?.api), {
|
|
209
322
|
stylesheet: readStylesheet,
|
|
210
323
|
module: readModules,
|
|
211
|
-
cycle: async (
|
|
324
|
+
cycle: (request) => inSession(async () => (await findCycle(request)).cycle)
|
|
212
325
|
});
|
|
213
326
|
/**
|
|
214
327
|
* Asks one question again: its answer, what of it a dependent's key holds, and the files it
|
|
@@ -354,8 +467,10 @@ function createProject(options) {
|
|
|
354
467
|
update: (changes) => typeOracle.update(changes),
|
|
355
468
|
dispose: () => typeOracle.dispose()
|
|
356
469
|
};
|
|
470
|
+
const helperRoot = helperRootOf(file);
|
|
357
471
|
const result = await compile(source, {
|
|
358
472
|
...compileOptions,
|
|
473
|
+
...helperRoot === void 0 ? {} : { helperRoot },
|
|
359
474
|
filename: file,
|
|
360
475
|
resolve: recording,
|
|
361
476
|
...oracle ? { typeOracle: oracle } : {}
|
|
@@ -382,9 +497,11 @@ function createProject(options) {
|
|
|
382
497
|
compile: async (file) => {
|
|
383
498
|
const cached = compiled.get(file);
|
|
384
499
|
if (cached) return cached.result;
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
500
|
+
return inSession(async () => {
|
|
501
|
+
const source = await readText(file);
|
|
502
|
+
if (source === void 0) throw new Error(`The project has no file "${file}".`);
|
|
503
|
+
return build(file, source);
|
|
504
|
+
});
|
|
388
505
|
},
|
|
389
506
|
copy: (file) => copies.get(file),
|
|
390
507
|
copies: (file) => [...compiled.get(file)?.copies ?? []].flatMap((module) => {
|
|
@@ -396,57 +513,63 @@ function createProject(options) {
|
|
|
396
513
|
}];
|
|
397
514
|
}),
|
|
398
515
|
dependencies: (file) => [...edgesOf(file)].toSorted(),
|
|
399
|
-
update:
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
516
|
+
update: (files) => inSession(() => updateFiles(files), true)
|
|
517
|
+
};
|
|
518
|
+
/**
|
|
519
|
+
* Routes changed files through the project (`Project.update`), in a fresh session: what it
|
|
520
|
+
* reads of a child is the child as it is now.
|
|
521
|
+
*/
|
|
522
|
+
async function updateFiles(files) {
|
|
523
|
+
await typeOracle?.update({ files: [...files] });
|
|
524
|
+
const before = new Map(copies);
|
|
525
|
+
for (const file of files) {
|
|
526
|
+
if (!copies.has(file)) continue;
|
|
527
|
+
const source = await readText(file);
|
|
528
|
+
if (source === void 0) copies.delete(file);
|
|
529
|
+
else copies.set(file, source);
|
|
530
|
+
}
|
|
531
|
+
const dependents = /* @__PURE__ */ new Map();
|
|
532
|
+
for (const file of /* @__PURE__ */ new Set([...compiled.keys(), ...childEdges.keys()])) for (const edge of edgesOf(file)) {
|
|
533
|
+
if (!dependents.has(edge)) dependents.set(edge, /* @__PURE__ */ new Set());
|
|
534
|
+
dependents.get(edge).add(file);
|
|
535
|
+
}
|
|
536
|
+
const affected = new Set(files);
|
|
537
|
+
for (const file of affected) for (const dependent of dependents.get(file) ?? []) affected.add(dependent);
|
|
538
|
+
const recompiled = [];
|
|
539
|
+
for (const file of [...affected].toSorted()) {
|
|
540
|
+
const node = compiled.get(file);
|
|
541
|
+
if (!node) continue;
|
|
542
|
+
const source = await readText(file);
|
|
543
|
+
if (source === void 0) {
|
|
544
|
+
compiled.delete(file);
|
|
545
|
+
continue;
|
|
412
546
|
}
|
|
413
|
-
const
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
if (!node) continue;
|
|
419
|
-
const source = await readPath(file);
|
|
420
|
-
if (source === void 0) {
|
|
421
|
-
compiled.delete(file);
|
|
422
|
-
continue;
|
|
423
|
-
}
|
|
424
|
-
const { key, edges } = await keyOf(source, node.calls);
|
|
425
|
-
if (key === node.key) {
|
|
426
|
-
node.edges = /* @__PURE__ */ new Set([...edges, ...node.result.dependencies]);
|
|
427
|
-
node.edges.delete(file);
|
|
428
|
-
continue;
|
|
429
|
-
}
|
|
430
|
-
await build(file, source);
|
|
431
|
-
recompiled.push(file);
|
|
547
|
+
const { key, edges } = await keyOf(source, node.calls);
|
|
548
|
+
if (key === node.key) {
|
|
549
|
+
node.edges = /* @__PURE__ */ new Set([...edges, ...node.result.dependencies]);
|
|
550
|
+
node.edges.delete(file);
|
|
551
|
+
continue;
|
|
432
552
|
}
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
return {
|
|
436
|
-
compiled: recompiled,
|
|
437
|
-
copied: [.../* @__PURE__ */ new Set([...before.keys(), ...copies.keys()])].filter((file) => before.get(file) !== copies.get(file)).toSorted()
|
|
438
|
-
};
|
|
553
|
+
await build(file, source);
|
|
554
|
+
recompiled.push(file);
|
|
439
555
|
}
|
|
440
|
-
|
|
556
|
+
const live = new Set([...compiled.values()].flatMap((node) => [...node.copies]));
|
|
557
|
+
for (const file of copies.keys()) if (!live.has(file)) copies.delete(file);
|
|
558
|
+
return {
|
|
559
|
+
compiled: recompiled,
|
|
560
|
+
copied: [.../* @__PURE__ */ new Set([...before.keys(), ...copies.keys()])].filter((file) => before.get(file) !== copies.get(file)).toSorted()
|
|
561
|
+
};
|
|
562
|
+
}
|
|
441
563
|
}
|
|
442
564
|
/**
|
|
443
565
|
* A resolver over files (ADR-0053): a project's (ADR-0071), which reads the imported file,
|
|
444
566
|
* analyses it up to its declarations, its types read through the type oracle (ADR-0067), and
|
|
445
567
|
* gives its API with its `file` relative to the importer. The analysis is cached by the contents
|
|
446
|
-
* of the file, its stylesheets and its
|
|
447
|
-
* the same API, and the parent compiles to the same bytes. A child's
|
|
448
|
-
*
|
|
449
|
-
*
|
|
568
|
+
* of the file, its stylesheets, its shapes and its own children's APIs, so an edit that leaves
|
|
569
|
+
* the API alone resolves to the same API, and the parent compiles to the same bytes. A child's
|
|
570
|
+
* API is read from its declarations and its own children's APIs, which give a root component's
|
|
571
|
+
* tags (ADR-0118), each child read once while a resolve is in flight: an import back to a child
|
|
572
|
+
* being read on the same path reads no API, and only `cycle` reports it.
|
|
450
573
|
* Its stylesheets are read, for the scope id its API carries (ADR-0063). It reads a stylesheet for
|
|
451
574
|
* `stylesheet` too, and for `module` a shared `.ts` module, a specifier without an extension
|
|
452
575
|
* naming its `.ts`, and follows its relative imports through every `.ts` module they reach, each
|
|
@@ -466,7 +589,7 @@ function createFileResolver(options) {
|
|
|
466
589
|
* query, or a check of the directives' values, names the module's own file, which is no edge.
|
|
467
590
|
*/
|
|
468
591
|
function namedFile(call) {
|
|
469
|
-
if (call.kind === "shape" || call.kind === "directive-values") return
|
|
592
|
+
if (call.kind === "shape" || call.kind === "directive-values") return;
|
|
470
593
|
return call.kind === "module" ? modulePath(call.request) : joinPath(directoryOf(call.request.importer), call.request.specifier);
|
|
471
594
|
}
|
|
472
595
|
/** A SHA-256 of a text, in hex. */
|
package/dist/resolver.d.ts
CHANGED
|
@@ -28,10 +28,10 @@ export interface ResolvedModule {
|
|
|
28
28
|
* The module, then each module it reaches, in the order the resolver first reached them: each
|
|
29
29
|
* file named as `compile()`'s `filename` is, and its text.
|
|
30
30
|
*/
|
|
31
|
-
files: {
|
|
31
|
+
files: Array<{
|
|
32
32
|
file: string;
|
|
33
33
|
source: string;
|
|
34
|
-
}
|
|
34
|
+
}>;
|
|
35
35
|
/** The first import of a framework or `@uxfront/unframework` they make, and its file (UF1204). */
|
|
36
36
|
framework?: {
|
|
37
37
|
specifier: string;
|
|
@@ -61,7 +61,7 @@ export interface Resolver {
|
|
|
61
61
|
(request: ResolveRequest): Promise<ModuleApi | undefined>;
|
|
62
62
|
stylesheet?(request: ResolveRequest): Promise<ResolvedStylesheet | undefined>;
|
|
63
63
|
module?(request: ResolveRequest): Promise<ResolvedModule | undefined>;
|
|
64
|
-
cycle?(request: ResolveRequest): Promise<string
|
|
64
|
+
cycle?(request: ResolveRequest): Promise<Array<string> | undefined>;
|
|
65
65
|
}
|
|
66
66
|
/** Options for `createFileResolver`, and the files of `createProject` (ADR-0071). */
|
|
67
67
|
export interface FileResolverOptions {
|
package/dist/resolver.js
CHANGED
|
@@ -26,7 +26,7 @@ function directoryOf(path) {
|
|
|
26
26
|
* not relative. A `..` that leaves the root stays (`../b`), and one after it adds another.
|
|
27
27
|
*/
|
|
28
28
|
function joinPath(directory, specifier) {
|
|
29
|
-
if (!specifier.startsWith("./") && !specifier.startsWith("../")) return
|
|
29
|
+
if (!specifier.startsWith("./") && !specifier.startsWith("../")) return;
|
|
30
30
|
const parts = directory ? directory.split("/") : [];
|
|
31
31
|
for (const part of specifier.split("/")) {
|
|
32
32
|
if (part === "." || part === "") continue;
|
package/dist/targets.d.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import { TargetName, TargetSelector } from "./config-types.js";
|
|
2
2
|
import { Target } from "@unframework.dev/codegen";
|
|
3
3
|
/** The built-in targets, in the order the docs and reports list them. */
|
|
4
|
-
export declare const TARGET_NAMES:
|
|
4
|
+
export declare const TARGET_NAMES: ReadonlyArray<TargetName>;
|
|
5
5
|
/**
|
|
6
6
|
* The targets a configuration without `targets` compiles to: every built-in but `solid2`, which
|
|
7
7
|
* is opt-in, since a project that selects both Solid majors needs two Solid installs (ADR-0102).
|
|
8
8
|
*/
|
|
9
|
-
export declare const DEFAULT_TARGETS:
|
|
9
|
+
export declare const DEFAULT_TARGETS: ReadonlyArray<TargetName>;
|
|
10
10
|
/** Every selector a configuration or the CLI accepts: the ids, then the versioned forms. */
|
|
11
|
-
export declare const TARGET_SELECTORS:
|
|
11
|
+
export declare const TARGET_SELECTORS: ReadonlyArray<TargetSelector>;
|
|
12
12
|
/** Whether a string selects a built-in target. Own names only: `toString` is not a target. */
|
|
13
13
|
export declare function isTargetSelector(value: string): value is TargetSelector;
|
|
14
14
|
/** The id of the target a selector selects: `solid@2` is `solid2`, `solid@1` is `solid`. */
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unframework.dev/compiler",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.1",
|
|
4
4
|
"description": "The Unframework compiler: compile() turns one .uf.tsx source into native React, Vue, Svelte, Solid, Angular, Qwik and Astro components.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"angular",
|
|
@@ -41,24 +41,24 @@
|
|
|
41
41
|
"access": "public"
|
|
42
42
|
},
|
|
43
43
|
"dependencies": {
|
|
44
|
-
"@unframework.dev/analyzer": "0.
|
|
45
|
-
"@unframework.dev/codegen": "0.
|
|
46
|
-
"@unframework.dev/diagnostics": "0.
|
|
47
|
-
"@unframework.dev/ir": "0.
|
|
44
|
+
"@unframework.dev/analyzer": "0.4.0",
|
|
45
|
+
"@unframework.dev/codegen": "0.4.0",
|
|
46
|
+
"@unframework.dev/diagnostics": "0.5.0",
|
|
47
|
+
"@unframework.dev/ir": "0.4.0",
|
|
48
48
|
"@unframework.dev/parser": "0.1.1",
|
|
49
|
-
"@unframework.dev/target-angular": "0.
|
|
50
|
-
"@unframework.dev/target-astro": "0.
|
|
51
|
-
"@unframework.dev/target-qwik": "0.
|
|
52
|
-
"@unframework.dev/target-react": "0.
|
|
53
|
-
"@unframework.dev/target-solid": "0.
|
|
54
|
-
"@unframework.dev/target-solid2": "0.
|
|
55
|
-
"@unframework.dev/target-svelte": "0.
|
|
56
|
-
"@unframework.dev/target-vue": "0.
|
|
49
|
+
"@unframework.dev/target-angular": "0.4.1",
|
|
50
|
+
"@unframework.dev/target-astro": "0.4.1",
|
|
51
|
+
"@unframework.dev/target-qwik": "0.4.0",
|
|
52
|
+
"@unframework.dev/target-react": "0.4.0",
|
|
53
|
+
"@unframework.dev/target-solid": "0.4.0",
|
|
54
|
+
"@unframework.dev/target-solid2": "0.3.0",
|
|
55
|
+
"@unframework.dev/target-svelte": "0.4.0",
|
|
56
|
+
"@unframework.dev/target-vue": "0.3.0",
|
|
57
57
|
"@unframework.dev/type-oracle": "0.2.0"
|
|
58
58
|
},
|
|
59
59
|
"devDependencies": {
|
|
60
60
|
"@types/node": "^24.19.0",
|
|
61
|
-
"@uxfront/typescript-config": "^0.
|
|
61
|
+
"@uxfront/typescript-config": "^0.2.0",
|
|
62
62
|
"tsdown": "^0.23.7",
|
|
63
63
|
"vitest": "5.0.3"
|
|
64
64
|
},
|