@xessa/cli 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +42 -2
- package/dist/args.d.ts +43 -0
- package/dist/args.d.ts.map +1 -0
- package/dist/args.js +202 -0
- package/dist/args.js.map +1 -0
- package/dist/commands/build.d.ts +44 -0
- package/dist/commands/build.d.ts.map +1 -0
- package/dist/commands/build.js +533 -0
- package/dist/commands/build.js.map +1 -0
- package/dist/commands/check.d.ts +11 -0
- package/dist/commands/check.d.ts.map +1 -0
- package/dist/commands/check.js +35 -0
- package/dist/commands/check.js.map +1 -0
- package/dist/commands/clean.d.ts +12 -0
- package/dist/commands/clean.d.ts.map +1 -0
- package/dist/commands/clean.js +140 -0
- package/dist/commands/clean.js.map +1 -0
- package/dist/commands/format.d.ts +41 -0
- package/dist/commands/format.d.ts.map +1 -0
- package/dist/commands/format.js +127 -0
- package/dist/commands/format.js.map +1 -0
- package/dist/commands/watch.d.ts +38 -0
- package/dist/commands/watch.d.ts.map +1 -0
- package/dist/commands/watch.js +338 -0
- package/dist/commands/watch.js.map +1 -0
- package/dist/compiler.d.ts +20 -0
- package/dist/compiler.d.ts.map +1 -0
- package/dist/compiler.js +57 -0
- package/dist/compiler.js.map +1 -0
- package/dist/config.d.ts +93 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +331 -0
- package/dist/config.js.map +1 -0
- package/dist/formatter.d.ts +21 -0
- package/dist/formatter.d.ts.map +1 -0
- package/dist/formatter.js +38 -0
- package/dist/formatter.js.map +1 -0
- package/dist/glob.d.ts +52 -0
- package/dist/glob.d.ts.map +1 -0
- package/dist/glob.js +214 -0
- package/dist/glob.js.map +1 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +114 -0
- package/dist/index.js.map +1 -0
- package/dist/manifest.d.ts +56 -0
- package/dist/manifest.d.ts.map +1 -0
- package/dist/manifest.js +138 -0
- package/dist/manifest.js.map +1 -0
- package/dist/paths.d.ts +24 -0
- package/dist/paths.d.ts.map +1 -0
- package/dist/paths.js +42 -0
- package/dist/paths.js.map +1 -0
- package/dist/project.d.ts +44 -0
- package/dist/project.d.ts.map +1 -0
- package/dist/project.js +92 -0
- package/dist/project.js.map +1 -0
- package/dist/report.d.ts +128 -0
- package/dist/report.d.ts.map +1 -0
- package/dist/report.js +128 -0
- package/dist/report.js.map +1 -0
- package/dist/setup.d.ts +58 -0
- package/dist/setup.d.ts.map +1 -0
- package/dist/setup.js +181 -0
- package/dist/setup.js.map +1 -0
- package/dist/writer.d.ts +32 -0
- package/dist/writer.d.ts.map +1 -0
- package/dist/writer.js +93 -0
- package/dist/writer.js.map +1 -0
- package/package.json +40 -4
- package/src/args.ts +246 -0
- package/src/commands/build.ts +682 -0
- package/src/commands/check.ts +45 -0
- package/src/commands/clean.ts +168 -0
- package/src/commands/format.ts +169 -0
- package/src/commands/watch.ts +366 -0
- package/src/compiler-api.d.ts +261 -0
- package/src/compiler.ts +65 -0
- package/src/config.ts +443 -0
- package/src/formatter.ts +56 -0
- package/src/glob.ts +230 -0
- package/src/index.ts +137 -0
- package/src/manifest.ts +176 -0
- package/src/paths.ts +50 -0
- package/src/project.ts +122 -0
- package/src/report.ts +230 -0
- package/src/setup.ts +218 -0
- package/src/writer.ts +102 -0
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `xessa watch` — incremental rebuilds until interrupted.
|
|
3
|
+
*
|
|
4
|
+
* The watcher performs an initial full build, then watches the directories that
|
|
5
|
+
* hold entry sources and their import dependencies. Events are debounced, mapped
|
|
6
|
+
* through a reverse import graph (built from `CompileResult.dependencies`) to the
|
|
7
|
+
* affected entries, and serialized: one build runs at a time and any event that
|
|
8
|
+
* arrives mid-build invalidates it through a generation counter, so a stale
|
|
9
|
+
* result never lands on disk.
|
|
10
|
+
*
|
|
11
|
+
* Sources (`.cssx`), the configuration file and directory events trigger work.
|
|
12
|
+
* Generated CSS, JS, declarations, maps, writer temporaries and the manifest are
|
|
13
|
+
* matched against the known artifact set and ignored, so the watcher cannot feed
|
|
14
|
+
* back on its own outputs. Deletions and unhandled paths trigger a full
|
|
15
|
+
* rediscovery through the include globs.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { type FSWatcher, watch } from "node:fs";
|
|
19
|
+
import { stat } from "node:fs/promises";
|
|
20
|
+
import type { CliInvocation } from "../args.js";
|
|
21
|
+
import type { CliConfig } from "../config.js";
|
|
22
|
+
import { staticDirectoryPrefix } from "../glob.js";
|
|
23
|
+
import { MANIFEST_FILENAME } from "../manifest.js";
|
|
24
|
+
import { dirnamePosix, joinPosix, relativePosix } from "../paths.js";
|
|
25
|
+
import { type ProjectContext, prepareProject } from "../project.js";
|
|
26
|
+
import { invocationFailure, writeErrorLine, writeLine } from "../report.js";
|
|
27
|
+
import { emitReport, executeBuild, toJsonReport } from "./build.js";
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Debounce window for filesystem events in milliseconds.
|
|
31
|
+
*
|
|
32
|
+
* Exported so integration tests can reason about event coalescing instead of
|
|
33
|
+
* guessing at a sleep, and reported in the JSON `watch` ready event. Changing it
|
|
34
|
+
* only affects latency, never correctness: every build is still awaited through
|
|
35
|
+
* its emitted build event.
|
|
36
|
+
*/
|
|
37
|
+
export const WATCH_DEBOUNCE_MS = 40;
|
|
38
|
+
|
|
39
|
+
/** Options for {@link runWatch}. */
|
|
40
|
+
export interface WatchOptions {
|
|
41
|
+
/** Parsed invocation with the optional single-entry file argument. */
|
|
42
|
+
readonly invocation: CliInvocation;
|
|
43
|
+
/** Working directory used to resolve relative paths. */
|
|
44
|
+
readonly cwd: string;
|
|
45
|
+
/** Abort signal that stops the watcher; defaults to SIGINT/SIGTERM. */
|
|
46
|
+
readonly signal?: AbortSignal;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
interface WatchState {
|
|
50
|
+
context: ProjectContext;
|
|
51
|
+
config: CliConfig | null;
|
|
52
|
+
/** Source path (entry or dependency) to the entries depending on it. */
|
|
53
|
+
reverse: Map<string, Set<string>>;
|
|
54
|
+
/** Every known source file: entries plus dependencies. */
|
|
55
|
+
sources: Set<string>;
|
|
56
|
+
/** Workspace-relative artifact paths, so the watcher ignores its own writes. */
|
|
57
|
+
artifacts: Set<string>;
|
|
58
|
+
/** Build generation, bumped by every filesystem event. */
|
|
59
|
+
generation: number;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Run `xessa watch [file]` until aborted. */
|
|
63
|
+
export async function runWatch(options: WatchOptions): Promise<number> {
|
|
64
|
+
const { invocation, cwd } = options;
|
|
65
|
+
const controller = options.signal === undefined ? new AbortController() : undefined;
|
|
66
|
+
const signal = options.signal ?? controller?.signal;
|
|
67
|
+
if (controller !== undefined) {
|
|
68
|
+
process.once("SIGINT", () => controller.abort());
|
|
69
|
+
process.once("SIGTERM", () => controller.abort());
|
|
70
|
+
}
|
|
71
|
+
if (signal === undefined) return 2;
|
|
72
|
+
|
|
73
|
+
const prepared = await prepareProject(invocation, cwd);
|
|
74
|
+
if (!prepared.ok) return invocationFailure(invocation.command, prepared.message, invocation.json);
|
|
75
|
+
if (!prepared.context.singleEntry && prepared.context.entries.length === 0) {
|
|
76
|
+
return invocationFailure(
|
|
77
|
+
invocation.command,
|
|
78
|
+
"No .cssx sources matched the project include globs.",
|
|
79
|
+
invocation.json,
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const state: WatchState = {
|
|
84
|
+
context: prepared.context,
|
|
85
|
+
config: prepared.context.config,
|
|
86
|
+
reverse: new Map(),
|
|
87
|
+
sources: new Set(),
|
|
88
|
+
artifacts: new Set(),
|
|
89
|
+
generation: 0,
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
const watchers = new Map<string, FSWatcher>();
|
|
93
|
+
const pending = new Set<string>();
|
|
94
|
+
let pendingUnknown = false;
|
|
95
|
+
let pendingConfig = false;
|
|
96
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
97
|
+
let wake: (() => void) | undefined;
|
|
98
|
+
/**
|
|
99
|
+
* Latched wakeup. `notify()` runs while no waiter exists (the build cycle is
|
|
100
|
+
* in flight), so the request is remembered here and the next `waitForWork()`
|
|
101
|
+
* returns immediately instead of sleeping until an unrelated event arrives.
|
|
102
|
+
*/
|
|
103
|
+
let pendingWake = false;
|
|
104
|
+
|
|
105
|
+
const notify = (): void => {
|
|
106
|
+
const resolve = wake;
|
|
107
|
+
wake = undefined;
|
|
108
|
+
if (resolve === undefined) pendingWake = true;
|
|
109
|
+
else resolve();
|
|
110
|
+
};
|
|
111
|
+
const waitForWork = (): Promise<void> => {
|
|
112
|
+
if (signal.aborted) return Promise.resolve();
|
|
113
|
+
if (pendingWake) {
|
|
114
|
+
pendingWake = false;
|
|
115
|
+
return Promise.resolve();
|
|
116
|
+
}
|
|
117
|
+
return new Promise<void>((resolve) => {
|
|
118
|
+
wake = resolve;
|
|
119
|
+
});
|
|
120
|
+
};
|
|
121
|
+
const clearTimer = (): void => {
|
|
122
|
+
if (timer !== undefined) {
|
|
123
|
+
clearTimeout(timer);
|
|
124
|
+
timer = undefined;
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
const schedule = (absolute: string | null): void => {
|
|
128
|
+
if (absolute === null) {
|
|
129
|
+
pendingUnknown = true;
|
|
130
|
+
} else {
|
|
131
|
+
const relative = relativePosix(state.context.root, absolute);
|
|
132
|
+
if (relative === "" || relative.startsWith("../")) pendingUnknown = true;
|
|
133
|
+
else pending.add(relative);
|
|
134
|
+
}
|
|
135
|
+
// Any event invalidates an in-flight build generation.
|
|
136
|
+
state.generation += 1;
|
|
137
|
+
clearTimer();
|
|
138
|
+
timer = setTimeout(() => {
|
|
139
|
+
timer = undefined;
|
|
140
|
+
notify();
|
|
141
|
+
}, WATCH_DEBOUNCE_MS);
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
const closeAll = (): void => {
|
|
145
|
+
clearTimer();
|
|
146
|
+
for (const watcher of watchers.values()) watcher.close();
|
|
147
|
+
watchers.clear();
|
|
148
|
+
};
|
|
149
|
+
const onAbort = (): void => {
|
|
150
|
+
closeAll();
|
|
151
|
+
notify();
|
|
152
|
+
};
|
|
153
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
154
|
+
|
|
155
|
+
const watchDirectory = (directory: string): void => {
|
|
156
|
+
if (watchers.has(directory)) return;
|
|
157
|
+
try {
|
|
158
|
+
const watcher = watch(directory, { persistent: true }, (_event, filename) => {
|
|
159
|
+
if (typeof filename === "string") {
|
|
160
|
+
if (filename === "") return;
|
|
161
|
+
const absolute = joinPosix(directory, filename);
|
|
162
|
+
if (state.config !== null && absolute === state.config.file) {
|
|
163
|
+
pendingConfig = true;
|
|
164
|
+
schedule(absolute);
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
const relative = relativePosix(state.context.root, absolute);
|
|
168
|
+
if (relative === "" || relative.startsWith("../")) {
|
|
169
|
+
schedule(null);
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
// Generated artifacts, the manifest and the writer's temporaries are
|
|
173
|
+
// our own output: never rebuild because we wrote them.
|
|
174
|
+
if (relative === MANIFEST_FILENAME) return;
|
|
175
|
+
if (relative.endsWith(".cssx-tmp")) return;
|
|
176
|
+
if (state.artifacts.has(relative)) return;
|
|
177
|
+
if (relative.endsWith(".cssx")) {
|
|
178
|
+
schedule(absolute);
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
// A directory creation/removal or an unknown entry: rediscover the
|
|
182
|
+
// include globs so a brand-new source tree becomes visible without a
|
|
183
|
+
// restart. This replaces the old "only `.cssx` filenames matter" rule.
|
|
184
|
+
schedule(null);
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
schedule(null);
|
|
188
|
+
});
|
|
189
|
+
watcher.on("error", () => {
|
|
190
|
+
watchers.delete(directory);
|
|
191
|
+
});
|
|
192
|
+
watchers.set(directory, watcher);
|
|
193
|
+
} catch {
|
|
194
|
+
// Missing or unwatchable directories are skipped; rediscovery retries.
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
const syncWatchers = (): void => {
|
|
199
|
+
const desired = new Set<string>();
|
|
200
|
+
desired.add(state.context.root);
|
|
201
|
+
if (state.config !== null) desired.add(dirnamePosix(state.config.file));
|
|
202
|
+
for (const source of state.sources) {
|
|
203
|
+
desired.add(dirnamePosix(joinPosix(state.context.root, source)));
|
|
204
|
+
}
|
|
205
|
+
for (const pattern of state.config?.include ?? []) {
|
|
206
|
+
const prefix = staticDirectoryPrefix(pattern);
|
|
207
|
+
desired.add(prefix === "" ? state.context.root : joinPosix(state.context.root, prefix));
|
|
208
|
+
}
|
|
209
|
+
for (const directory of desired) watchDirectory(directory);
|
|
210
|
+
for (const [directory, watcher] of watchers) {
|
|
211
|
+
if (desired.has(directory)) continue;
|
|
212
|
+
watcher.close();
|
|
213
|
+
watchers.delete(directory);
|
|
214
|
+
}
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
const updateGraph = (report: Awaited<ReturnType<typeof executeBuild>>["report"]): void => {
|
|
218
|
+
const reverse = new Map<string, Set<string>>();
|
|
219
|
+
const sources = new Set<string>();
|
|
220
|
+
for (const entry of report.entries) {
|
|
221
|
+
sources.add(entry.file);
|
|
222
|
+
const self = reverse.get(entry.file) ?? new Set<string>();
|
|
223
|
+
self.add(entry.file);
|
|
224
|
+
reverse.set(entry.file, self);
|
|
225
|
+
for (const dependency of entry.dependencies) {
|
|
226
|
+
sources.add(dependency);
|
|
227
|
+
const affected = reverse.get(dependency) ?? new Set<string>();
|
|
228
|
+
affected.add(entry.file);
|
|
229
|
+
reverse.set(dependency, affected);
|
|
230
|
+
}
|
|
231
|
+
for (const output of entry.outputs) state.artifacts.add(output.path);
|
|
232
|
+
}
|
|
233
|
+
state.reverse = reverse;
|
|
234
|
+
state.sources = sources;
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
const runCycle = async (entries: readonly string[], prune: boolean): Promise<void> => {
|
|
238
|
+
const generation = state.generation;
|
|
239
|
+
let outcome: Awaited<ReturnType<typeof executeBuild>> | undefined;
|
|
240
|
+
try {
|
|
241
|
+
outcome = await executeBuild({
|
|
242
|
+
context: { ...state.context, entries },
|
|
243
|
+
write: true,
|
|
244
|
+
dryRun: false,
|
|
245
|
+
prune,
|
|
246
|
+
...(invocation.minify === undefined ? {} : { minify: invocation.minify }),
|
|
247
|
+
isCurrent: () => state.generation === generation,
|
|
248
|
+
});
|
|
249
|
+
} catch (error) {
|
|
250
|
+
writeErrorLine(`xessa watch: build failed: ${describeError(error)}`);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
if (outcome !== undefined && !outcome.stale) {
|
|
254
|
+
updateGraph(outcome.report);
|
|
255
|
+
}
|
|
256
|
+
// Watchers must be attached (or refreshed) before the build event is
|
|
257
|
+
// reported, so consumers that react to the event cannot race attachment.
|
|
258
|
+
syncWatchers();
|
|
259
|
+
if (outcome === undefined) return;
|
|
260
|
+
if (outcome.stale) {
|
|
261
|
+
writeLine("xessa watch: discarded a stale build generation");
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
if (invocation.json) {
|
|
265
|
+
// True NDJSON: one compact JSON object per line, so consumers can parse
|
|
266
|
+
// the stream incrementally.
|
|
267
|
+
writeLine(JSON.stringify({ type: "build", ...toJsonReport(outcome.report) }));
|
|
268
|
+
} else {
|
|
269
|
+
emitReport(outcome.report, false);
|
|
270
|
+
}
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
// Initial full build.
|
|
274
|
+
await runCycle(state.context.entries, !state.context.singleEntry);
|
|
275
|
+
|
|
276
|
+
if (invocation.json) {
|
|
277
|
+
// Readiness handshake: emitted only after the initial build and watcher
|
|
278
|
+
// attachment, so tests never guess when it is safe to touch files.
|
|
279
|
+
writeLine(
|
|
280
|
+
JSON.stringify({
|
|
281
|
+
type: "watch",
|
|
282
|
+
ready: true,
|
|
283
|
+
sources: state.sources.size,
|
|
284
|
+
debounceMs: WATCH_DEBOUNCE_MS,
|
|
285
|
+
}),
|
|
286
|
+
);
|
|
287
|
+
} else {
|
|
288
|
+
writeLine(
|
|
289
|
+
`xessa watch: watching ${state.sources.size} source${state.sources.size === 1 ? "" : "s"} (Ctrl+C to stop)`,
|
|
290
|
+
);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
while (!signal.aborted) {
|
|
294
|
+
await waitForWork();
|
|
295
|
+
if (signal.aborted) break;
|
|
296
|
+
|
|
297
|
+
const changes = [...pending];
|
|
298
|
+
pending.clear();
|
|
299
|
+
const configChanged = pendingConfig;
|
|
300
|
+
pendingConfig = false;
|
|
301
|
+
const unknown = pendingUnknown;
|
|
302
|
+
pendingUnknown = false;
|
|
303
|
+
|
|
304
|
+
if (changes.length === 0 && !configChanged && !unknown) continue;
|
|
305
|
+
|
|
306
|
+
let full = configChanged || unknown;
|
|
307
|
+
if (!full) {
|
|
308
|
+
for (const change of changes) {
|
|
309
|
+
if (!(await isFile(joinPosix(state.context.root, change)))) {
|
|
310
|
+
full = true;
|
|
311
|
+
break;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
if (full) {
|
|
317
|
+
const refreshed = await prepareProject(invocation, cwd);
|
|
318
|
+
if (!refreshed.ok) {
|
|
319
|
+
writeErrorLine(`xessa watch: configuration error: ${refreshed.message}`);
|
|
320
|
+
continue;
|
|
321
|
+
}
|
|
322
|
+
state.context = refreshed.context;
|
|
323
|
+
state.config = refreshed.context.config;
|
|
324
|
+
await runCycle(state.context.entries, !state.context.singleEntry);
|
|
325
|
+
continue;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
const affected = new Set<string>();
|
|
329
|
+
for (const change of changes) {
|
|
330
|
+
const dependents = state.reverse.get(change);
|
|
331
|
+
if (dependents === undefined) {
|
|
332
|
+
full = true;
|
|
333
|
+
break;
|
|
334
|
+
}
|
|
335
|
+
for (const entry of dependents) affected.add(entry);
|
|
336
|
+
}
|
|
337
|
+
if (full) {
|
|
338
|
+
const refreshed = await prepareProject(invocation, cwd);
|
|
339
|
+
if (!refreshed.ok) continue;
|
|
340
|
+
state.context = refreshed.context;
|
|
341
|
+
state.config = refreshed.context.config;
|
|
342
|
+
await runCycle(state.context.entries, !state.context.singleEntry);
|
|
343
|
+
continue;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
const entries = [...affected].filter((entry) => state.sources.has(entry)).sort();
|
|
347
|
+
if (entries.length === 0) continue;
|
|
348
|
+
await runCycle(entries, false);
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
signal.removeEventListener("abort", onAbort);
|
|
352
|
+
closeAll();
|
|
353
|
+
return 0;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
async function isFile(path: string): Promise<boolean> {
|
|
357
|
+
try {
|
|
358
|
+
return (await stat(path)).isFile();
|
|
359
|
+
} catch {
|
|
360
|
+
return false;
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
function describeError(error: unknown): string {
|
|
365
|
+
return error instanceof Error ? error.message : String(error);
|
|
366
|
+
}
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ambient declaration of the compiler surface the CLI consumes.
|
|
3
|
+
*
|
|
4
|
+
* The monorepo does not link workspace packages into `node_modules` and the
|
|
5
|
+
* compiler intentionally publishes only `dist` declarations, so the CLI
|
|
6
|
+
* typechecks against this mirror of the public barrel
|
|
7
|
+
* (`packages/compiler/src/index.ts`) instead of reaching into compiler
|
|
8
|
+
* internals. When a real `@xessa/compiler` installation resolves (or the
|
|
9
|
+
* compiler's `dist` is present with `paths`/`node_modules` wiring), TypeScript
|
|
10
|
+
* uses the package's own declarations instead and this mirror is ignored.
|
|
11
|
+
*
|
|
12
|
+
* Keep this file limited to the symbols the CLI imports. The Node smoke test
|
|
13
|
+
* exercises every mirrored function against the built compiler, so drift
|
|
14
|
+
* between this mirror and the real exports fails a gate.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
declare module "@xessa/compiler" {
|
|
18
|
+
/** Half-open range of offsets into one source file. */
|
|
19
|
+
export interface Span {
|
|
20
|
+
readonly start: number;
|
|
21
|
+
readonly end: number;
|
|
22
|
+
readonly file: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Stable XESSA#### diagnostic with severity and origin span. */
|
|
26
|
+
export interface Diagnostic {
|
|
27
|
+
readonly code: string;
|
|
28
|
+
readonly severity: "error" | "warning";
|
|
29
|
+
readonly message: string;
|
|
30
|
+
readonly span: Span;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** One authored setting inside a `@setup` block. */
|
|
34
|
+
export interface SetupSetting {
|
|
35
|
+
readonly kind: "setting";
|
|
36
|
+
readonly key: string;
|
|
37
|
+
readonly value: string;
|
|
38
|
+
readonly valueSpan: Span;
|
|
39
|
+
readonly span: Span;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** A `@setup { ... }` block as returned by `parse`. */
|
|
43
|
+
export interface SetupBlock {
|
|
44
|
+
readonly kind: "setup";
|
|
45
|
+
readonly settings: readonly SetupSetting[];
|
|
46
|
+
readonly span: Span;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Any top-level stylesheet item; only `kind === "setup"` is inspected. */
|
|
50
|
+
export interface StylesheetItem {
|
|
51
|
+
readonly kind: string;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Parsed stylesheet; the CLI only reads top-level `@setup` blocks. */
|
|
55
|
+
export interface Stylesheet {
|
|
56
|
+
readonly kind: "stylesheet";
|
|
57
|
+
readonly file: string;
|
|
58
|
+
readonly items: readonly StylesheetItem[];
|
|
59
|
+
readonly span: Span;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Result of the tolerant `parse` entry point. */
|
|
63
|
+
export interface ParseResult {
|
|
64
|
+
readonly ast: Stylesheet;
|
|
65
|
+
readonly diagnostics: readonly Diagnostic[];
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Map from authored class name to emitted class name. */
|
|
69
|
+
export type ClassMap = Readonly<Record<string, string>>;
|
|
70
|
+
|
|
71
|
+
/** Artifacts and diagnostics for one compiled source. */
|
|
72
|
+
export interface CompileResult {
|
|
73
|
+
readonly css?: string;
|
|
74
|
+
readonly js?: string;
|
|
75
|
+
readonly types?: string;
|
|
76
|
+
readonly sourceMap?: string;
|
|
77
|
+
readonly classes: ClassMap;
|
|
78
|
+
readonly diagnostics: readonly Diagnostic[];
|
|
79
|
+
readonly dependencies: readonly string[];
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Class naming strategies available in v0.1. */
|
|
83
|
+
export type NamingStrategy = "none" | "prefix";
|
|
84
|
+
|
|
85
|
+
/** Class naming configuration. */
|
|
86
|
+
export interface NamingOptions {
|
|
87
|
+
readonly strategy: NamingStrategy;
|
|
88
|
+
readonly prefix?: string;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Media grouping modes accepted by v0.1. */
|
|
92
|
+
export type MediaGroupingMode = "safe" | "none" | "merge";
|
|
93
|
+
|
|
94
|
+
/** Output settings shared by `@setup`, project config and CLI overrides. */
|
|
95
|
+
export interface OutputOptions {
|
|
96
|
+
readonly cssDir?: string;
|
|
97
|
+
readonly cssName?: string;
|
|
98
|
+
readonly mapDir?: string;
|
|
99
|
+
readonly mapName?: string;
|
|
100
|
+
readonly emitCss?: boolean;
|
|
101
|
+
readonly emitMap?: boolean;
|
|
102
|
+
readonly mapTypes?: boolean;
|
|
103
|
+
readonly emitTypes?: boolean;
|
|
104
|
+
readonly sourceMap?: boolean;
|
|
105
|
+
readonly minify?: boolean;
|
|
106
|
+
readonly mapInherit?: boolean;
|
|
107
|
+
readonly importCss?: boolean;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Options accepted by `compile`. */
|
|
111
|
+
export interface CompileOptions extends OutputOptions {
|
|
112
|
+
readonly file: string;
|
|
113
|
+
readonly prefix?: string;
|
|
114
|
+
readonly naming?: NamingOptions;
|
|
115
|
+
readonly media?: Readonly<Record<string, string>>;
|
|
116
|
+
/** Strict validation. Default `true` (ADR-0023). */
|
|
117
|
+
readonly strict?: boolean;
|
|
118
|
+
/** Media grouping. Default `"safe"`. */
|
|
119
|
+
readonly mediaGrouping?: MediaGroupingMode;
|
|
120
|
+
readonly workspaceRoot?: string;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** Compiler behaviour settings from `xessa.config.json` (`compiler`). */
|
|
124
|
+
export interface CompilerConfig {
|
|
125
|
+
/** Reject unknown configuration keys. Default `true` (ADR-0023). */
|
|
126
|
+
readonly strict?: boolean;
|
|
127
|
+
/** Media grouping. Default `"safe"`. */
|
|
128
|
+
readonly mediaGrouping?: MediaGroupingMode;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** Declarative project configuration shape. */
|
|
132
|
+
export interface ProjectConfig {
|
|
133
|
+
readonly include?: readonly string[];
|
|
134
|
+
readonly exclude?: readonly string[];
|
|
135
|
+
readonly output?: OutputOptions;
|
|
136
|
+
readonly classes?: NamingOptions;
|
|
137
|
+
readonly media?: Readonly<Record<string, string>>;
|
|
138
|
+
readonly compiler?: CompilerConfig;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** Options accepted by `compileProject`. */
|
|
142
|
+
export interface CompileProjectOptions {
|
|
143
|
+
readonly projectRoot?: string;
|
|
144
|
+
readonly configFile?: string;
|
|
145
|
+
readonly output?: OutputOptions;
|
|
146
|
+
readonly naming?: NamingOptions;
|
|
147
|
+
readonly media?: Readonly<Record<string, string>>;
|
|
148
|
+
readonly strict?: boolean;
|
|
149
|
+
readonly mediaGrouping?: MediaGroupingMode;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** Effective output settings after merging every layer. */
|
|
153
|
+
export interface ResolvedSetup
|
|
154
|
+
extends Required<Omit<OutputOptions, "mapTypes" | "mapInherit" | "importCss">> {
|
|
155
|
+
readonly prefix: string;
|
|
156
|
+
readonly mapInherit?: boolean;
|
|
157
|
+
readonly importCss?: boolean;
|
|
158
|
+
readonly cssDirs?: readonly string[];
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** One decoded item in a comma-separated `@setup` value list. */
|
|
162
|
+
export interface SetupListEntry {
|
|
163
|
+
readonly value: string;
|
|
164
|
+
readonly span: Span;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** Result of {@link decodeSetupList}. */
|
|
168
|
+
export interface SetupListResult {
|
|
169
|
+
readonly entries: readonly SetupListEntry[];
|
|
170
|
+
readonly isList: boolean;
|
|
171
|
+
readonly diagnostics: readonly Diagnostic[];
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** Decode a comma-separated `@setup` value list and its per-item diagnostics. */
|
|
175
|
+
export function decodeSetupList(raw: string, span: Span): SetupListResult;
|
|
176
|
+
|
|
177
|
+
/** Explicit CLI overrides; partial by design. */
|
|
178
|
+
export type CliOutputOverrides = OutputOptions & { readonly prefix?: string };
|
|
179
|
+
|
|
180
|
+
/** Filesystem and policy operations the compiler needs from its host. */
|
|
181
|
+
export interface ModuleHost {
|
|
182
|
+
readFile(path: string): Promise<string>;
|
|
183
|
+
fileExists(path: string): Promise<boolean>;
|
|
184
|
+
canonicalize(path: string): string;
|
|
185
|
+
resolvePath(from: string, to: string): string;
|
|
186
|
+
workspaceRoot(): string;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** Artifact kinds the planner can plan. */
|
|
190
|
+
export type OutputKind = "css" | "js" | "types" | "cssSourceMap";
|
|
191
|
+
|
|
192
|
+
/** One planned artifact; the planner never writes it. */
|
|
193
|
+
export interface PlannedOutput {
|
|
194
|
+
readonly kind: OutputKind;
|
|
195
|
+
readonly absolutePath: string;
|
|
196
|
+
readonly workspacePath: string;
|
|
197
|
+
readonly sourceFile: string;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** One compiler-owned artifact recorded in the build manifest. */
|
|
201
|
+
export interface ManifestEntry {
|
|
202
|
+
readonly kind: OutputKind;
|
|
203
|
+
readonly workspacePath: string;
|
|
204
|
+
readonly sourceFile: string;
|
|
205
|
+
readonly contentHash?: string;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** Ordered, validated artifact set for one entry. */
|
|
209
|
+
export interface OutputPlan {
|
|
210
|
+
readonly outputs: readonly PlannedOutput[];
|
|
211
|
+
readonly manifest: readonly ManifestEntry[];
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** Inputs to the pure output planner. */
|
|
215
|
+
export interface PlanOutputsInput {
|
|
216
|
+
readonly sourceFile: string;
|
|
217
|
+
readonly setup: ResolvedSetup;
|
|
218
|
+
readonly projectConfig?: ProjectConfig;
|
|
219
|
+
readonly cliOverrides?: CliOutputOverrides;
|
|
220
|
+
readonly workspaceRoot: string;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/** Planner result: valid plan plus XESSA5001 diagnostics. */
|
|
224
|
+
export interface PlanOutputsResult {
|
|
225
|
+
readonly plan: OutputPlan;
|
|
226
|
+
readonly diagnostics: readonly Diagnostic[];
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/** Canonicalization adapter accepted by `planOutputs`. */
|
|
230
|
+
export interface OutputPlannerHost {
|
|
231
|
+
canonicalize(path: string): string;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/** Optional planner context for symlink and cross-entry collision checks. */
|
|
235
|
+
export interface OutputPlannerContext {
|
|
236
|
+
readonly host?: OutputPlannerHost;
|
|
237
|
+
readonly occupiedTargets?: readonly string[];
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/** Tolerantly parse one source string. */
|
|
241
|
+
export function parse(source: string, options: { readonly file: string }): ParseResult;
|
|
242
|
+
|
|
243
|
+
/** Compile one source string without writing files. */
|
|
244
|
+
export function compile(source: string, options: CompileOptions): Promise<CompileResult>;
|
|
245
|
+
|
|
246
|
+
/** Compile several entries with one shared module graph. */
|
|
247
|
+
export function compileProject(
|
|
248
|
+
entries: string[],
|
|
249
|
+
host: ModuleHost,
|
|
250
|
+
options: CompileProjectOptions,
|
|
251
|
+
): Promise<Record<string, CompileResult>>;
|
|
252
|
+
|
|
253
|
+
/** Validate and return the complete output plan without writing. */
|
|
254
|
+
export function planOutputs(
|
|
255
|
+
input: PlanOutputsInput,
|
|
256
|
+
context?: OutputPlannerContext,
|
|
257
|
+
): PlanOutputsResult;
|
|
258
|
+
|
|
259
|
+
/** Default Node/Bun filesystem host. */
|
|
260
|
+
export function createNodeHost(root?: string): ModuleHost;
|
|
261
|
+
}
|
package/src/compiler.ts
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Compiler boundary for the CLI.
|
|
3
|
+
*
|
|
4
|
+
* The CLI consumes only the compiler's public barrel. Because the monorepo does
|
|
5
|
+
* not link workspace packages into `node_modules`, the loader tries, in order:
|
|
6
|
+
*
|
|
7
|
+
* 1. the bare `@xessa/compiler` specifier (published installs, or a linked
|
|
8
|
+
* workspace with a built `dist`);
|
|
9
|
+
* 2. `packages/compiler/dist/index.js` relative to this module (Bun and Node
|
|
10
|
+
* runs from the repository);
|
|
11
|
+
* 3. `packages/compiler/src/index.ts` under Bun only (development and tests
|
|
12
|
+
* without a build step).
|
|
13
|
+
*
|
|
14
|
+
* The module is cached after the first successful load.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/** Public compiler module surface used by the CLI. */
|
|
18
|
+
export type CompilerApi = typeof import("@xessa/compiler");
|
|
19
|
+
|
|
20
|
+
let cached: Promise<CompilerApi> | undefined;
|
|
21
|
+
|
|
22
|
+
/** Load (and cache) the compiler public API. */
|
|
23
|
+
export function loadCompiler(): Promise<CompilerApi> {
|
|
24
|
+
cached ??= resolveCompiler();
|
|
25
|
+
return cached;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function isBun(): boolean {
|
|
29
|
+
return typeof (globalThis as { Bun?: unknown }).Bun !== "undefined";
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function candidates(): readonly (() => Promise<unknown>)[] {
|
|
33
|
+
const list: (() => Promise<unknown>)[] = [
|
|
34
|
+
() => import("@xessa/compiler"),
|
|
35
|
+
() => import(new URL("../../compiler/dist/index.js", import.meta.url).href),
|
|
36
|
+
];
|
|
37
|
+
if (isBun()) {
|
|
38
|
+
list.push(() => import(new URL("../../compiler/src/index.ts", import.meta.url).href));
|
|
39
|
+
}
|
|
40
|
+
return list;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async function resolveCompiler(): Promise<CompilerApi> {
|
|
44
|
+
const errors: string[] = [];
|
|
45
|
+
for (const candidate of candidates()) {
|
|
46
|
+
try {
|
|
47
|
+
const loaded = (await candidate()) as CompilerApi;
|
|
48
|
+
if (typeof loaded.compileProject !== "function" || typeof loaded.planOutputs !== "function") {
|
|
49
|
+
errors.push("module loaded without compileProject/planOutputs exports");
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
return loaded;
|
|
53
|
+
} catch (error) {
|
|
54
|
+
errors.push(describeError(error));
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
throw new Error(
|
|
58
|
+
`Unable to load @xessa/compiler. Build it first with "bun run build" in packages/compiler. Tried: ${errors.join(" | ")}`,
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function describeError(error: unknown): string {
|
|
63
|
+
if (error instanceof Error) return error.message;
|
|
64
|
+
return String(error);
|
|
65
|
+
}
|