@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/compile.js
ADDED
|
@@ -0,0 +1,568 @@
|
|
|
1
|
+
import { checkCapabilities } from "./capabilities.js";
|
|
2
|
+
import { isSpanIn, pluginIrProblems } from "./plugin-ir.js";
|
|
3
|
+
import { componentImportNames, directoryOf, joinPath, relativePath, stylesheetSource, takesComponent } from "./resolver.js";
|
|
4
|
+
import { resolveTarget } from "./targets.js";
|
|
5
|
+
import { analyze, componentImports, shapeQueries, sharedModuleImports, stylesheetImports } from "@unframework.dev/analyzer";
|
|
6
|
+
import { anchorOutput, formatOutput, identityMapped, kebabCase, remapFormatted, scopeStylesheet } from "@unframework.dev/codegen";
|
|
7
|
+
import { createDiagnostic, sortDiagnostics } from "@unframework.dev/diagnostics";
|
|
8
|
+
import { checkInvariants, validateModule } from "@unframework.dev/ir";
|
|
9
|
+
import { parseModule } from "@unframework.dev/parser";
|
|
10
|
+
import { createSyntacticOracle } from "@unframework.dev/type-oracle";
|
|
11
|
+
//#region src/compile.ts
|
|
12
|
+
/**
|
|
13
|
+
* Checks the `passThrough` option (ADR-0064): throws when a prefix is empty, or starts with `.`,
|
|
14
|
+
* `/`, `\\` or a drive letter, since such a specifier names a file of the project, a component's
|
|
15
|
+
* own sheet or a child, never a module a plugin registers.
|
|
16
|
+
*/
|
|
17
|
+
function checkPassThrough(prefixes) {
|
|
18
|
+
for (const prefix of prefixes) if (typeof prefix !== "string" || !prefix || /^(?:[./\\]|[A-Za-z]:)/.test(prefix)) throw new TypeError(`The passThrough prefix ${JSON.stringify(prefix)} is invalid: a prefix that passes through names a module a bundler plugin registers, such as "virtual:", so it is not empty, relative or absolute.`);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Compiles one `.uf.tsx` source, or a `.uf.ts` module of composables (ADR-0069), to every
|
|
22
|
+
* selected target. A pure function of the source and
|
|
23
|
+
* the options: the same input and versions give byte-identical output (P8). It throws only
|
|
24
|
+
* for invalid options (an unknown target name, two targets with one name, an invalid
|
|
25
|
+
* `passThrough` prefix); every other
|
|
26
|
+
* problem, a misbehaving plugin or target included, is a diagnostic, and an error in one
|
|
27
|
+
* component or target never stops the others.
|
|
28
|
+
*/
|
|
29
|
+
async function compile(source, options) {
|
|
30
|
+
const file = options.filename;
|
|
31
|
+
if (options.passThrough) checkPassThrough(options.passThrough);
|
|
32
|
+
const diagnostics = [];
|
|
33
|
+
const targets = options.targets.map(resolveTarget);
|
|
34
|
+
const outputs = {};
|
|
35
|
+
const owners = {};
|
|
36
|
+
for (const target of targets) {
|
|
37
|
+
if (Object.hasOwn(outputs, target.name)) throw new TypeError(`Two targets are named "${target.name}".`);
|
|
38
|
+
outputs[target.name] = [];
|
|
39
|
+
owners[target.name] = {};
|
|
40
|
+
}
|
|
41
|
+
let analysed;
|
|
42
|
+
/** Each stylesheet's file, by its specifier, as the resolver named it. */
|
|
43
|
+
let sheetFiles = /* @__PURE__ */ new Map();
|
|
44
|
+
const dependencies = /* @__PURE__ */ new Set();
|
|
45
|
+
/**
|
|
46
|
+
* Each shared `.ts` module's text, by its path relative to the source, with the specifier of
|
|
47
|
+
* the source's import that first reaches it (ADR-0070).
|
|
48
|
+
*/
|
|
49
|
+
const copies = /* @__PURE__ */ new Map();
|
|
50
|
+
try {
|
|
51
|
+
const parsed = parseModule(file, source);
|
|
52
|
+
const oracle = options.typeOracle ?? sourceOracle(file, source);
|
|
53
|
+
const shapes = /* @__PURE__ */ new Map();
|
|
54
|
+
for (const query of shapeQueries(parsed)) try {
|
|
55
|
+
const shape = await oracle.resolveShape(query);
|
|
56
|
+
shapes.set(`${query.span.start}:${query.span.end}`, shape);
|
|
57
|
+
for (const dependency of shape.dependencies) dependencies.add(dependency);
|
|
58
|
+
} catch (error) {
|
|
59
|
+
const type = source.slice(query.span.start, query.span.end);
|
|
60
|
+
diagnostics.push(internal(file, error, `The type oracle failed on \`${type}\``));
|
|
61
|
+
}
|
|
62
|
+
const imports = /* @__PURE__ */ new Map();
|
|
63
|
+
for (const specifier of options.resolve ? componentImports(parsed) : []) {
|
|
64
|
+
const composable = COMPOSABLE_SPECIFIER.test(specifier) ? joinPath(directoryOf(file), specifier) : void 0;
|
|
65
|
+
if (composable !== void 0) dependencies.add(composable);
|
|
66
|
+
try {
|
|
67
|
+
const api = await options.resolve({
|
|
68
|
+
specifier,
|
|
69
|
+
importer: file
|
|
70
|
+
});
|
|
71
|
+
imports.set(specifier, api);
|
|
72
|
+
} catch (error) {
|
|
73
|
+
imports.set(specifier, void 0);
|
|
74
|
+
diagnostics.push(internal(file, error, `The resolver failed on "${specifier}"`));
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
const stylesheets = /* @__PURE__ */ new Map();
|
|
78
|
+
for (const specifier of stylesheetImports(parsed)) try {
|
|
79
|
+
const sheet = await options.resolve?.stylesheet?.({
|
|
80
|
+
specifier,
|
|
81
|
+
importer: file
|
|
82
|
+
});
|
|
83
|
+
stylesheets.set(specifier, sheet && stylesheetSource(sheet.file, sheet.source));
|
|
84
|
+
} catch (error) {
|
|
85
|
+
stylesheets.set(specifier, void 0);
|
|
86
|
+
diagnostics.push(internal(file, error, `The resolver failed on "${specifier}"`));
|
|
87
|
+
}
|
|
88
|
+
sheetFiles = new Map([...stylesheets].flatMap(([specifier, sheet]) => sheet ? [[specifier, sheet.file]] : []));
|
|
89
|
+
const modules = /* @__PURE__ */ new Map();
|
|
90
|
+
const fromSource = (path) => relativePath(directoryOf(file), path);
|
|
91
|
+
for (const specifier of options.resolve?.module ? sharedModuleImports(parsed) : []) try {
|
|
92
|
+
const resolved = await options.resolve.module({
|
|
93
|
+
specifier,
|
|
94
|
+
importer: file
|
|
95
|
+
});
|
|
96
|
+
if (!resolved?.files.length) {
|
|
97
|
+
modules.set(specifier, void 0);
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
const { framework, unresolved } = resolved;
|
|
101
|
+
modules.set(specifier, {
|
|
102
|
+
file: fromSource(resolved.files[0].file),
|
|
103
|
+
...framework ? { framework: {
|
|
104
|
+
...framework,
|
|
105
|
+
file: fromSource(framework.file)
|
|
106
|
+
} } : {},
|
|
107
|
+
...unresolved ? { unresolved: {
|
|
108
|
+
...unresolved,
|
|
109
|
+
file: fromSource(unresolved.file)
|
|
110
|
+
} } : {}
|
|
111
|
+
});
|
|
112
|
+
const at = relativeImports(parsed).find((entry) => entry.specifier === specifier).span;
|
|
113
|
+
for (const { file: path, source: text } of resolved.files) {
|
|
114
|
+
dependencies.add(path);
|
|
115
|
+
if (!copies.has(fromSource(path))) copies.set(fromSource(path), {
|
|
116
|
+
contents: text,
|
|
117
|
+
span: at
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
} catch (error) {
|
|
121
|
+
modules.set(specifier, void 0);
|
|
122
|
+
diagnostics.push(internal(file, error, `The resolver failed on "${specifier}"`));
|
|
123
|
+
}
|
|
124
|
+
const cycles = [];
|
|
125
|
+
const edges = componentImportNames(parsed).filter(({ specifier, names }) => takesComponent(imports.get(specifier), names));
|
|
126
|
+
for (const { specifier, span } of options.resolve?.cycle ? edges : []) try {
|
|
127
|
+
const files = await options.resolve.cycle({
|
|
128
|
+
specifier,
|
|
129
|
+
importer: file
|
|
130
|
+
});
|
|
131
|
+
if (files) cycles.push(importCycle(file, specifier, span, files));
|
|
132
|
+
} catch (error) {
|
|
133
|
+
diagnostics.push(internal(file, error, `The resolver failed on "${specifier}"`));
|
|
134
|
+
}
|
|
135
|
+
const analysis = analyze(parsed, {
|
|
136
|
+
imports,
|
|
137
|
+
modules,
|
|
138
|
+
stylesheets,
|
|
139
|
+
shapes: (span) => shapes.get(`${span.start}:${span.end}`),
|
|
140
|
+
...options.passThrough ? { passThrough: options.passThrough } : {}
|
|
141
|
+
});
|
|
142
|
+
diagnostics.push(...analysis.diagnostics);
|
|
143
|
+
const broken = analysis.module && checkInvariants(analysis.module);
|
|
144
|
+
if (broken?.length) diagnostics.push(internal(file, describeProblems(broken), "The analyser lowered invalid IR"));
|
|
145
|
+
else analysed = analysis.module && deepFreeze(analysis.module);
|
|
146
|
+
const collisions = analysed ? copyCollisions(analysed, copies, file) : [];
|
|
147
|
+
diagnostics.push(...collisions, ...cycles);
|
|
148
|
+
if (collisions.length || cycles.length) analysed = void 0;
|
|
149
|
+
} catch (error) {
|
|
150
|
+
diagnostics.push(internal(file, error, "The analyser failed"));
|
|
151
|
+
}
|
|
152
|
+
let module = analysed;
|
|
153
|
+
for (const plugin of options.plugins ?? []) {
|
|
154
|
+
if (!module || !analysed || !plugin.ir) continue;
|
|
155
|
+
try {
|
|
156
|
+
const result = plugin.ir(module);
|
|
157
|
+
if (result === void 0) continue;
|
|
158
|
+
const problems = validateModule(result);
|
|
159
|
+
if (!problems.length) problems.push(...checkInvariants(result), ...pluginIrProblems(result, analysed, source));
|
|
160
|
+
if (problems.length) {
|
|
161
|
+
const what = `returned invalid IR: ${describeProblems(problems)}`;
|
|
162
|
+
diagnostics.push(pluginFailed(file, plugin.name, "ir", what));
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
module = deepFreeze(result);
|
|
166
|
+
} catch (error) {
|
|
167
|
+
diagnostics.push(pluginFailed(file, plugin.name, "ir", `threw: ${messageOf(error)}`));
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
/** Each styled component's scoped sheets, joined in import order, by its name. */
|
|
171
|
+
const sheets = /* @__PURE__ */ new Map();
|
|
172
|
+
for (const component of module?.components ?? []) {
|
|
173
|
+
let joined = "";
|
|
174
|
+
for (const sheet of component.styles ?? []) {
|
|
175
|
+
const sheetFile = sheetFiles.get(sheet.specifier) ?? sheet.specifier;
|
|
176
|
+
const scoped = scopeStylesheet(sheetFile, sheet.source, component.scopeId);
|
|
177
|
+
diagnostics.push(...scoped.diagnostics);
|
|
178
|
+
if (scoped.diagnostics.length) module = void 0;
|
|
179
|
+
joined += joined && !joined.endsWith("\n") ? `\n${scoped.code}` : scoped.code;
|
|
180
|
+
}
|
|
181
|
+
if (component.styles?.length) sheets.set(component.name, joined);
|
|
182
|
+
}
|
|
183
|
+
if (module) for (const target of targets) try {
|
|
184
|
+
const emitted = await emitTarget(target, module, source, options, diagnostics, sheets, copies);
|
|
185
|
+
outputs[target.name] = emitted.files;
|
|
186
|
+
owners[target.name] = emitted.owners;
|
|
187
|
+
} catch (error) {
|
|
188
|
+
diagnostics.push(internal(file, error, `The ${target.name} target failed`, target.name));
|
|
189
|
+
}
|
|
190
|
+
return {
|
|
191
|
+
filename: file,
|
|
192
|
+
ir: module,
|
|
193
|
+
diagnostics: sortDiagnostics(diagnostics),
|
|
194
|
+
outputs,
|
|
195
|
+
owners,
|
|
196
|
+
dependencies: [...dependencies].toSorted()
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
/** An import of a `.uf.ts` module's composables (ADR-0069). */
|
|
200
|
+
const COMPOSABLE_SPECIFIER = /\.uf\.ts$/;
|
|
201
|
+
/**
|
|
202
|
+
* The type oracle `compile()` uses without one (ADR-0067): the syntactic adapter over the one
|
|
203
|
+
* source, at the root of a project that holds nothing else, so it reads no file and resolves
|
|
204
|
+
* the module's own types only.
|
|
205
|
+
*/
|
|
206
|
+
function sourceOracle(file, source) {
|
|
207
|
+
return createSyntacticOracle({
|
|
208
|
+
root: "",
|
|
209
|
+
readFile: async (path) => path === `/${file}` ? source : void 0
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* The shared modules whose copy would take a component's output file (UF1104, ADR-0070): an
|
|
214
|
+
* output imports a component's file by its name without the extension (`./CardIcon`), React,
|
|
215
|
+
* Solid and Qwik name it as the component and Angular in kebab-case, and the output tree holds
|
|
216
|
+
* the module's own components and every child it imports. So a copy whose path, without `.ts`,
|
|
217
|
+
* is one of those names, compared without case, is reported for every target, since the module
|
|
218
|
+
* compiles to each.
|
|
219
|
+
*/
|
|
220
|
+
function copyCollisions(module, copies, file) {
|
|
221
|
+
const names = /* @__PURE__ */ new Map();
|
|
222
|
+
const add = (directory, name) => {
|
|
223
|
+
for (const stem of [name, kebabCase(name)]) names.set(`${directory}${stem}`.toLowerCase(), name);
|
|
224
|
+
};
|
|
225
|
+
for (const component of module.components) add("", component.name);
|
|
226
|
+
for (const entry of module.imports ?? []) {
|
|
227
|
+
const slash = entry.file.lastIndexOf("/");
|
|
228
|
+
for (const component of entry.api.components) add(slash === -1 ? "" : entry.file.slice(0, slash + 1), component.name);
|
|
229
|
+
}
|
|
230
|
+
return [...copies].flatMap(([path, { span }]) => {
|
|
231
|
+
const name = names.get(path.replace(/\.ts$/, "").toLowerCase());
|
|
232
|
+
if (name === void 0) return [];
|
|
233
|
+
return [createDiagnostic("UF1104", {
|
|
234
|
+
file,
|
|
235
|
+
span,
|
|
236
|
+
message: `The shared module ${path} is named like ${name}'s output file, which an output imports without its extension (\`${name}\`, and \`${kebabCase(name)}\` on Angular): each output tree holds the module's copy beside the components.`,
|
|
237
|
+
help: "Rename the shared module, or the component."
|
|
238
|
+
})];
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* The source's relative imports and re-exports, each specifier once with the span of its first
|
|
243
|
+
* declaration's specifier.
|
|
244
|
+
*/
|
|
245
|
+
function relativeImports(parsed) {
|
|
246
|
+
const found = /* @__PURE__ */ new Map();
|
|
247
|
+
for (const statement of parsed.program.body) {
|
|
248
|
+
const from = statement.type === "ImportDeclaration" || statement.type === "ExportAllDeclaration" || statement.type === "ExportNamedDeclaration" ? statement.source : null;
|
|
249
|
+
if (!from || found.has(from.value)) continue;
|
|
250
|
+
if (!from.value.startsWith("./") && !from.value.startsWith("../")) continue;
|
|
251
|
+
found.set(from.value, {
|
|
252
|
+
start: from.start,
|
|
253
|
+
end: from.end
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
return [...found].map(([specifier, span]) => ({
|
|
257
|
+
specifier,
|
|
258
|
+
span
|
|
259
|
+
}));
|
|
260
|
+
}
|
|
261
|
+
/** UF1205 at an import that closes a cycle, which names its files relative to the source. */
|
|
262
|
+
function importCycle(file, specifier, span, files) {
|
|
263
|
+
const names = files.map((path) => relativePath(directoryOf(file), path));
|
|
264
|
+
return createDiagnostic("UF1205", {
|
|
265
|
+
file,
|
|
266
|
+
span,
|
|
267
|
+
message: `"${specifier}" closes an import cycle of components, ${names.join(" → ")}: each imports the next to render it, so none of them can load before the others.`,
|
|
268
|
+
help: "Render the components of the cycle from one module, which declares them all."
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* Checks a target's capabilities, then emits, transforms and formats each component's files.
|
|
273
|
+
* A styled component's files end with its scoped sheet, `<source>.css` (ADR-0063), which the
|
|
274
|
+
* `output` hooks see and formatting keeps as printed, and which `owners` leaves out: the
|
|
275
|
+
* unplugin serves it on an id of its own. The files of two components, or two files of one,
|
|
276
|
+
* may not share a path, compared without case: case-insensitive file systems would write one
|
|
277
|
+
* over the other. Each shared `.ts` module's copy follows, unchanged, where a component's files
|
|
278
|
+
* were written: neither the hooks nor formatting see it, and `owners` leaves it out (ADR-0070).
|
|
279
|
+
*/
|
|
280
|
+
async function emitTarget(target, module, source, options, diagnostics, sheets, copies) {
|
|
281
|
+
const file = options.filename;
|
|
282
|
+
const sheetPath = `${file.slice(file.lastIndexOf("/") + 1)}.css`;
|
|
283
|
+
diagnostics.push(...checkCapabilities(module, target));
|
|
284
|
+
const written = [];
|
|
285
|
+
const owners = /* @__PURE__ */ new Map();
|
|
286
|
+
for (const component of module.components) {
|
|
287
|
+
let files;
|
|
288
|
+
try {
|
|
289
|
+
const emitted = readFiles(target.emit(component, {
|
|
290
|
+
module,
|
|
291
|
+
options: options.targetOptions?.[target.name],
|
|
292
|
+
report: (diagnostic) => diagnostics.push(reported(diagnostic, source, file, target.name))
|
|
293
|
+
}));
|
|
294
|
+
if ("problem" in emitted) throw new TypeError(`emit returned ${emitted.problem}`);
|
|
295
|
+
const sheet = sheets.get(component.name);
|
|
296
|
+
if (sheet !== void 0) emitted.files.push({
|
|
297
|
+
path: sheetPath,
|
|
298
|
+
contents: sheet
|
|
299
|
+
});
|
|
300
|
+
files = deepFreeze(emitted.files);
|
|
301
|
+
} catch (error) {
|
|
302
|
+
const what = `The ${target.name} target failed on ${component.name}`;
|
|
303
|
+
diagnostics.push(internal(file, error, what, target.name));
|
|
304
|
+
continue;
|
|
305
|
+
}
|
|
306
|
+
for (const plugin of options.plugins ?? []) {
|
|
307
|
+
if (!plugin.output) continue;
|
|
308
|
+
try {
|
|
309
|
+
const result = plugin.output(files, {
|
|
310
|
+
target: target.name,
|
|
311
|
+
component,
|
|
312
|
+
module
|
|
313
|
+
});
|
|
314
|
+
if (result === void 0) continue;
|
|
315
|
+
const returned = readFiles(result);
|
|
316
|
+
if ("problem" in returned) {
|
|
317
|
+
const what = `returned ${returned.problem}`;
|
|
318
|
+
diagnostics.push(pluginFailed(file, plugin.name, "output", what, target.name));
|
|
319
|
+
continue;
|
|
320
|
+
}
|
|
321
|
+
files = deepFreeze(returned.files);
|
|
322
|
+
} catch (error) {
|
|
323
|
+
diagnostics.push(pluginFailed(file, plugin.name, "output", `threw: ${messageOf(error)}`, target.name));
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
if (options.sourceMap) {
|
|
327
|
+
const own = {
|
|
328
|
+
...module,
|
|
329
|
+
components: [component],
|
|
330
|
+
composables: []
|
|
331
|
+
};
|
|
332
|
+
files = files.map((output) => anchorOutput(output, own, source));
|
|
333
|
+
}
|
|
334
|
+
if (options.format !== false) {
|
|
335
|
+
const unformatted = files;
|
|
336
|
+
files = (await Promise.all(unformatted.map((output) => formatOutput(output)))).map((outcome, index) => {
|
|
337
|
+
if (outcome.error) {
|
|
338
|
+
const what = `The ${target.name} target emitted ${outcome.file.path}, which does not parse`;
|
|
339
|
+
diagnostics.push(internal(file, outcome.error, what, target.name));
|
|
340
|
+
}
|
|
341
|
+
return remapped(unformatted[index], outcome, file, target.name, diagnostics);
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
for (const output of files) {
|
|
345
|
+
const key = output.path.toLowerCase();
|
|
346
|
+
const owner = owners.get(key);
|
|
347
|
+
if (owner) {
|
|
348
|
+
diagnostics.push(internal(file, `${owner.path} (${owner.component}) and ${output.path} (${component.name}) are one file on a case-insensitive file system`, `The ${target.name} target wrote two files at one path`, target.name));
|
|
349
|
+
continue;
|
|
350
|
+
}
|
|
351
|
+
owners.set(key, {
|
|
352
|
+
path: output.path,
|
|
353
|
+
component: component.name
|
|
354
|
+
});
|
|
355
|
+
written.push(output);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
for (const composable of module.composables ?? []) {
|
|
359
|
+
if (!target.emitComposable) {
|
|
360
|
+
diagnostics.push(createDiagnostic("UF1002", {
|
|
361
|
+
file,
|
|
362
|
+
span: composable.span,
|
|
363
|
+
message: `The ${target.name} target does not emit composables: it has no \`emitComposable\`.`,
|
|
364
|
+
target: target.name
|
|
365
|
+
}));
|
|
366
|
+
continue;
|
|
367
|
+
}
|
|
368
|
+
let files;
|
|
369
|
+
try {
|
|
370
|
+
const emitted = readFiles(target.emitComposable(composable, {
|
|
371
|
+
module,
|
|
372
|
+
options: options.targetOptions?.[target.name],
|
|
373
|
+
report: (diagnostic) => diagnostics.push(reported(diagnostic, source, file, target.name))
|
|
374
|
+
}));
|
|
375
|
+
if ("problem" in emitted) throw new TypeError(`emitComposable returned ${emitted.problem}`);
|
|
376
|
+
files = emitted.files;
|
|
377
|
+
} catch (error) {
|
|
378
|
+
const what = `The ${target.name} target failed on ${composable.name}`;
|
|
379
|
+
diagnostics.push(internal(file, error, what, target.name));
|
|
380
|
+
continue;
|
|
381
|
+
}
|
|
382
|
+
if (options.sourceMap) {
|
|
383
|
+
const own = {
|
|
384
|
+
...module,
|
|
385
|
+
components: [],
|
|
386
|
+
composables: [composable]
|
|
387
|
+
};
|
|
388
|
+
files = files.map((output) => anchorOutput(output, own, source));
|
|
389
|
+
}
|
|
390
|
+
if (options.format !== false) {
|
|
391
|
+
const unformatted = files;
|
|
392
|
+
files = (await Promise.all(unformatted.map((output) => formatOutput(output)))).map((outcome, index) => {
|
|
393
|
+
if (outcome.error) {
|
|
394
|
+
const what = `The ${target.name} target emitted ${outcome.file.path}, which does not parse`;
|
|
395
|
+
diagnostics.push(internal(file, outcome.error, what, target.name));
|
|
396
|
+
}
|
|
397
|
+
return remapped(unformatted[index], outcome, file, target.name, diagnostics);
|
|
398
|
+
});
|
|
399
|
+
}
|
|
400
|
+
for (const output of files) {
|
|
401
|
+
const key = output.path.toLowerCase();
|
|
402
|
+
const owner = owners.get(key);
|
|
403
|
+
if (owner) {
|
|
404
|
+
diagnostics.push(internal(file, `${owner.path} (${owner.component}) and ${output.path} (${composable.name}) are one file on a case-insensitive file system`, `The ${target.name} target wrote two files at one path`, target.name));
|
|
405
|
+
continue;
|
|
406
|
+
}
|
|
407
|
+
owners.set(key, {
|
|
408
|
+
path: output.path,
|
|
409
|
+
component: composable.name
|
|
410
|
+
});
|
|
411
|
+
written.push(output);
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
for (const [path, { contents, span }] of written.length ? copies : []) {
|
|
415
|
+
const owner = owners.get(path.toLowerCase());
|
|
416
|
+
if (owner) {
|
|
417
|
+
diagnostics.push(createDiagnostic("UF1104", {
|
|
418
|
+
file,
|
|
419
|
+
span,
|
|
420
|
+
message: `The shared module ${path} and ${owner.component}'s ${target.name} output, ${owner.path}, are one file: each output tree holds a copy of the module beside the components.`,
|
|
421
|
+
help: "Rename the shared module, or the component.",
|
|
422
|
+
target: target.name
|
|
423
|
+
}));
|
|
424
|
+
continue;
|
|
425
|
+
}
|
|
426
|
+
written.push(options.sourceMap ? identityMapped({
|
|
427
|
+
path,
|
|
428
|
+
contents
|
|
429
|
+
}) : {
|
|
430
|
+
path,
|
|
431
|
+
contents
|
|
432
|
+
});
|
|
433
|
+
}
|
|
434
|
+
return {
|
|
435
|
+
files: written,
|
|
436
|
+
owners: Object.fromEntries([...owners.values()].filter(({ path }) => path !== sheetPath).map(({ path, component }) => [path, component]))
|
|
437
|
+
};
|
|
438
|
+
}
|
|
439
|
+
/**
|
|
440
|
+
* A formatted file with its map carried across. Code that didn't parse stays as printed, and so
|
|
441
|
+
* does its map. A map that can't follow formatting is UF9002, and one whose diff passes its cap
|
|
442
|
+
* UF9003: the file goes without one.
|
|
443
|
+
*/
|
|
444
|
+
function remapped(before, { file: after, error }, file, target, diagnostics) {
|
|
445
|
+
if (!before.map || error) return after;
|
|
446
|
+
const { map: _stale, ...formatted } = after;
|
|
447
|
+
const result = remapFormatted(before, after, before.map);
|
|
448
|
+
if ("map" in result) return {
|
|
449
|
+
...formatted,
|
|
450
|
+
map: result.map
|
|
451
|
+
};
|
|
452
|
+
const { code, ...init } = result.diagnostic;
|
|
453
|
+
diagnostics.push(createDiagnostic(code, {
|
|
454
|
+
...init,
|
|
455
|
+
file,
|
|
456
|
+
target
|
|
457
|
+
}));
|
|
458
|
+
return formatted;
|
|
459
|
+
}
|
|
460
|
+
/**
|
|
461
|
+
* A diagnostic a target reported, for this file and target. A malformed one (no catalogued
|
|
462
|
+
* code, a span outside the source) becomes UF9001: it could neither be sorted nor shown.
|
|
463
|
+
*/
|
|
464
|
+
function reported(value, source, file, target) {
|
|
465
|
+
const problem = diagnosticProblem(value, source.length);
|
|
466
|
+
if (problem) return internal(file, problem, `The ${target} target reported a malformed diagnostic`, target);
|
|
467
|
+
const { code, ...init } = value;
|
|
468
|
+
return createDiagnostic(code, {
|
|
469
|
+
...init,
|
|
470
|
+
file,
|
|
471
|
+
target
|
|
472
|
+
});
|
|
473
|
+
}
|
|
474
|
+
/** Why a value is not a diagnostic whose spans lie in a source of `length` characters. */
|
|
475
|
+
function diagnosticProblem(value, length) {
|
|
476
|
+
if (!isRecord(value)) return `${describe(value)} instead of a diagnostic`;
|
|
477
|
+
const { code, message } = value;
|
|
478
|
+
if (typeof code !== "string" || typeof message !== "string") return "a diagnostic without a code or a message";
|
|
479
|
+
const malformed = `${code}, which is not a well-formed diagnostic: ${message}`;
|
|
480
|
+
const related = value.related ?? [];
|
|
481
|
+
const fixes = value.fixes ?? [];
|
|
482
|
+
if (!SEVERITIES.has(value.severity) || !(value.help === void 0 || typeof value.help === "string") || !isRecordList(related) || !isRecordList(fixes)) return malformed;
|
|
483
|
+
const edits = [];
|
|
484
|
+
for (const fix of fixes) {
|
|
485
|
+
const { title, confidence, edits: written } = fix;
|
|
486
|
+
if (typeof title !== "string" || !CONFIDENCES.has(confidence) || !isRecordList(written)) return malformed;
|
|
487
|
+
edits.push(...written);
|
|
488
|
+
}
|
|
489
|
+
if (related.some((item) => typeof item.message !== "string") || edits.some((edit) => typeof edit.text !== "string")) return malformed;
|
|
490
|
+
if (![
|
|
491
|
+
value.span,
|
|
492
|
+
...related.map((item) => item.span),
|
|
493
|
+
...edits.map((edit) => edit.span)
|
|
494
|
+
].every((span) => isSpanIn(span, length))) return `${code}, which points outside the source (${length} characters): ${message}`;
|
|
495
|
+
}
|
|
496
|
+
const SEVERITIES = /* @__PURE__ */ new Set([
|
|
497
|
+
"error",
|
|
498
|
+
"warning",
|
|
499
|
+
"info"
|
|
500
|
+
]);
|
|
501
|
+
const CONFIDENCES = /* @__PURE__ */ new Set(["safe", "likely"]);
|
|
502
|
+
function isRecordList(value) {
|
|
503
|
+
return Array.isArray(value) && value.every(isRecord);
|
|
504
|
+
}
|
|
505
|
+
function isRecord(value) {
|
|
506
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
507
|
+
}
|
|
508
|
+
/** A target's or a plugin's files, copied, or why the value is not a list of output files. */
|
|
509
|
+
function readFiles(value) {
|
|
510
|
+
if (!Array.isArray(value)) return { problem: `${describe(value)} instead of a list of files` };
|
|
511
|
+
const files = [];
|
|
512
|
+
for (const [index, item] of value.entries()) {
|
|
513
|
+
if (typeof item !== "object" || item === null || !("path" in item) || !("contents" in item) || typeof item.path !== "string" || typeof item.contents !== "string") return { problem: `a list whose item ${index} is not a file ({ path, contents } strings)` };
|
|
514
|
+
files.push({
|
|
515
|
+
path: item.path,
|
|
516
|
+
contents: item.contents
|
|
517
|
+
});
|
|
518
|
+
}
|
|
519
|
+
return { files };
|
|
520
|
+
}
|
|
521
|
+
/** `undefined`, `an array`, `a string`, … */
|
|
522
|
+
function describe(value) {
|
|
523
|
+
if (value === null || value === void 0) return String(value);
|
|
524
|
+
if (Array.isArray(value)) return "an array";
|
|
525
|
+
const type = typeof value;
|
|
526
|
+
return `${/^[aeiou]/.test(type) ? "an" : "a"} ${type}`;
|
|
527
|
+
}
|
|
528
|
+
/** The first problems with a module, by JSON Pointer: `/components/0/render/tag must …`. */
|
|
529
|
+
function describeProblems(problems) {
|
|
530
|
+
const first = problems.slice(0, 3).map(({ path, message }) => `${path || "/"} ${message}`);
|
|
531
|
+
const more = problems.length > 3 ? ` (and ${problems.length - 3} more)` : "";
|
|
532
|
+
return `${first.join("; ")}${more}`;
|
|
533
|
+
}
|
|
534
|
+
/** Freezes a plain-data value and everything in it, so a plugin cannot edit it in place. */
|
|
535
|
+
function deepFreeze(value) {
|
|
536
|
+
if (typeof value === "object" && value !== null && !Object.isFrozen(value)) {
|
|
537
|
+
Object.freeze(value);
|
|
538
|
+
for (const item of Object.values(value)) deepFreeze(item);
|
|
539
|
+
}
|
|
540
|
+
return value;
|
|
541
|
+
}
|
|
542
|
+
function messageOf(error) {
|
|
543
|
+
return error instanceof Error ? error.message : String(error);
|
|
544
|
+
}
|
|
545
|
+
function internal(file, error, what, target) {
|
|
546
|
+
return createDiagnostic("UF9001", {
|
|
547
|
+
file,
|
|
548
|
+
span: {
|
|
549
|
+
start: 0,
|
|
550
|
+
end: 0
|
|
551
|
+
},
|
|
552
|
+
message: `${what}: ${messageOf(error)}`,
|
|
553
|
+
...target ? { target } : {}
|
|
554
|
+
});
|
|
555
|
+
}
|
|
556
|
+
function pluginFailed(file, plugin, hook, what, target) {
|
|
557
|
+
return createDiagnostic("UF8001", {
|
|
558
|
+
file,
|
|
559
|
+
span: {
|
|
560
|
+
start: 0,
|
|
561
|
+
end: 0
|
|
562
|
+
},
|
|
563
|
+
message: `The "${plugin}" plugin's ${hook} hook ${what}`,
|
|
564
|
+
...target ? { target } : {}
|
|
565
|
+
});
|
|
566
|
+
}
|
|
567
|
+
//#endregion
|
|
568
|
+
export { checkPassThrough, compile };
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
//#region src/config-types.d.ts
|
|
2
|
+
/** The names of the built-in targets. */
|
|
3
|
+
export type TargetName = "react" | "vue" | "svelte" | "solid" | "angular" | "qwik" | "astro";
|
|
4
|
+
/** The targets packaging covers, and where each packager resolves from (ADR-0076). */
|
|
5
|
+
export interface PackageConfig {
|
|
6
|
+
targets?: readonly TargetName[];
|
|
7
|
+
/** One directory, or one per target. */
|
|
8
|
+
resolveFrom?: string | Partial<Record<TargetName, string>>;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* One entry of Vite's `plugins`, as far as the configuration reads it: a plugin, which has a
|
|
12
|
+
* name, nothing, a list of them, or a promise of one. This file imports nothing, so Vite's own
|
|
13
|
+
* `PluginOption`, which every Vite plugin matches, stands here as its shape (ADR-0104).
|
|
14
|
+
*/
|
|
15
|
+
export type VitePluginOption = {
|
|
16
|
+
readonly name: string;
|
|
17
|
+
} | false | null | undefined | readonly VitePluginOption[] | PromiseLike<VitePluginOption>;
|
|
18
|
+
/** The kind of Vite server a project's `vite` config is for (ADR-0104). */
|
|
19
|
+
export type ViteMode = "browser" | "ssr" | "preview";
|
|
20
|
+
/** What a project's `vite` function is called with: the server's target and its kind. */
|
|
21
|
+
export interface ViteContext {
|
|
22
|
+
readonly target: TargetName;
|
|
23
|
+
readonly mode: ViteMode;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* The partial Vite config a project's `vite` function returns, as far as this file can say
|
|
27
|
+
* without importing Vite (ADR-0076): its `plugins` are checked, and any other key Vite's
|
|
28
|
+
* `UserConfig` has passes, whether the function returns an object literal or Vite's own type
|
|
29
|
+
* (ADR-0104).
|
|
30
|
+
*/
|
|
31
|
+
export type ProjectViteConfig = {
|
|
32
|
+
plugins?: readonly VitePluginOption[];
|
|
33
|
+
} & object;
|
|
34
|
+
/** The configuration as a project writes it, with `defineConfig` from `@uxfront/unframework/config`. */
|
|
35
|
+
export interface UnframeworkConfig {
|
|
36
|
+
/** Defaults to all seven targets. */
|
|
37
|
+
targets?: readonly TargetName[];
|
|
38
|
+
/** Defaults to `src`, relative to the config's directory. */
|
|
39
|
+
srcDir?: string;
|
|
40
|
+
/** Defaults to `dist`, relative to the config's directory. */
|
|
41
|
+
outDir?: string;
|
|
42
|
+
/** Each target's options, validated through its Standard Schema. A target without one takes none. */
|
|
43
|
+
targetOptions?: Partial<Record<TargetName, unknown>>;
|
|
44
|
+
/** Defaults to `syntactic`. */
|
|
45
|
+
typeOracle?: "syntactic" | "tsgo";
|
|
46
|
+
/** Plan §8.1: `true` makes portability warnings errors, in the CLI. Defaults to `false`. */
|
|
47
|
+
strict?: boolean;
|
|
48
|
+
package?: PackageConfig;
|
|
49
|
+
/**
|
|
50
|
+
* The project's Vite config for the servers `unframework test`, `visual` and `preview` start,
|
|
51
|
+
* such as the plugin of a styling engine that serves `virtual:` modules (ADR-0064). Each server
|
|
52
|
+
* calls it with its target and kind, for a fresh set of plugins, since a plugin may hold one
|
|
53
|
+
* server's state. Its plugins are listed before the unframework plugin, and the other keys merge into
|
|
54
|
+
* the server's own config, which it never replaces (ADR-0104).
|
|
55
|
+
*/
|
|
56
|
+
vite?: (context: ViteContext) => ProjectViteConfig | Promise<ProjectViteConfig>;
|
|
57
|
+
/**
|
|
58
|
+
* The font families that may draw text when pixels compare live, besides the bundled Inter, as
|
|
59
|
+
* the `font-fallback` message names them. Committed baselines take Inter alone (ADR-0105).
|
|
60
|
+
*/
|
|
61
|
+
liveFonts?: readonly string[];
|
|
62
|
+
}
|
|
63
|
+
//#endregion
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { PackageConfig, TargetName, UnframeworkConfig } from "./config-types.js";
|
|
2
|
+
import { Diagnostic } from "@unframework.dev/diagnostics";
|
|
3
|
+
//#region src/config.d.ts
|
|
4
|
+
/** The configuration with its defaults applied, and its paths absolute. */
|
|
5
|
+
export interface ResolvedConfig {
|
|
6
|
+
targets: readonly TargetName[];
|
|
7
|
+
srcDir: string;
|
|
8
|
+
outDir: string;
|
|
9
|
+
targetOptions: Partial<Record<TargetName, unknown>>;
|
|
10
|
+
typeOracle: "syntactic" | "tsgo";
|
|
11
|
+
strict: boolean;
|
|
12
|
+
package?: PackageConfig;
|
|
13
|
+
/** The project's Vite config, as a function each server calls (ADR-0104). */
|
|
14
|
+
vite?: UnframeworkConfig["vite"];
|
|
15
|
+
/** The font families that may draw text when pixels compare live, besides Inter (ADR-0105). */
|
|
16
|
+
liveFonts: readonly string[];
|
|
17
|
+
}
|
|
18
|
+
/** Where a configuration came from: what {@link loadConfig} gives {@link resolveConfig}. */
|
|
19
|
+
export interface ConfigOrigin {
|
|
20
|
+
/** The directory relative paths resolve against. Defaults to the working directory. */
|
|
21
|
+
directory?: string;
|
|
22
|
+
/** The file as diagnostics name it. Defaults to `unframework.config.ts`. */
|
|
23
|
+
file?: string;
|
|
24
|
+
}
|
|
25
|
+
/** The result of {@link loadConfig}. */
|
|
26
|
+
export interface LoadedConfig extends ConfigOrigin {
|
|
27
|
+
/** The file's default export, `{}` when there is no file, `undefined` when it failed to load. */
|
|
28
|
+
raw: unknown;
|
|
29
|
+
/** The config's directory, or the root when there is no file. */
|
|
30
|
+
directory: string;
|
|
31
|
+
/** The file relative to the root, when there is one. */
|
|
32
|
+
file?: string;
|
|
33
|
+
diagnostics: Diagnostic[];
|
|
34
|
+
}
|
|
35
|
+
/** The result of {@link resolveConfig}. */
|
|
36
|
+
export interface ResolveConfigResult {
|
|
37
|
+
/** `undefined` when any field is invalid. */
|
|
38
|
+
config: ResolvedConfig | undefined;
|
|
39
|
+
diagnostics: Diagnostic[];
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Imports `unframework.config.{ts,mts,js,mjs}` from `root`, or `file` relative to it, through
|
|
43
|
+
* Node's type stripping. A project without a config file takes the defaults, so that is no error.
|
|
44
|
+
* It never throws: a file that cannot be imported, or has no default export, is UF7001.
|
|
45
|
+
*/
|
|
46
|
+
export declare function loadConfig(root: string, file?: string): Promise<LoadedConfig>;
|
|
47
|
+
/**
|
|
48
|
+
* Checks a configuration and applies its defaults. It never throws: a field of the wrong shape is
|
|
49
|
+
* UF7002, a name that is no built-in target UF7003, and options the target's schema rejects, or a
|
|
50
|
+
* target without a schema is given, UF7004, each with the field's JSON pointer. It is asynchronous
|
|
51
|
+
* because a Standard Schema may be.
|
|
52
|
+
*/
|
|
53
|
+
export declare function resolveConfig(raw: unknown, origin?: ConfigOrigin): Promise<ResolveConfigResult>;
|
|
54
|
+
//#endregion
|