@unframework.dev/unplugin 0.2.0 → 0.3.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 CHANGED
@@ -88,14 +88,15 @@ integration harness does (`sourceUtilities` in `tests/integration/harness/stylef
88
88
 
89
89
  ## Options
90
90
 
91
- | Option | Meaning |
92
- | ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
93
- | `target` | A built-in target's name (`"react"`, `"vue"`, `"svelte"`, `"solid"`, `"angular"`, `"qwik"`, `"astro"`), `"solid@2"`, or a third-party `Target`. Optional on Vite, which detects a built-in target. Required on every other bundler. |
94
- | `extension` | What module ids append to the `.uf.tsx` path. Built-in targets have one; a third-party target must set it. |
95
- | `onCompile` | Called each time a module loads, with `{ id, file, target, files, diagnostics }`. Returning a string (or a promise of one) fails that module with it: the integration harness's golden guard. |
96
- | `plugins` | Compiler plugins, passed to `compile()` (the harness's canaries). |
97
- | `format` | Formats the output with oxfmt. Defaults to `true`, so a module's code equals the golden file byte for byte. |
98
- | `passThrough` | The specifier prefixes whose imports pass through to every output as written, passed to `compile()`. Defaults to `["virtual:"]`; a list replaces it, as `["virtual:", "styled-system/"]` adds an engine that generates files. A prefix that is empty, relative or absolute fails when the plugin is created. |
91
+ | Option | Meaning |
92
+ | ----------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
93
+ | `target` | A built-in target's name (`"react"`, `"vue"`, `"svelte"`, `"solid"`, `"angular"`, `"qwik"`, `"astro"`), `"solid@2"`, or a third-party `Target`. Optional on Vite, which detects a built-in target. Required on every other bundler. |
94
+ | `extension` | What module ids append to the `.uf.tsx` path. Built-in targets have one; a third-party target must set it. |
95
+ | `onCompile` | Called each time a module loads, with `{ id, file, target, files, diagnostics }`. Returning a string (or a promise of one) fails that module with it: the integration harness's golden guard. |
96
+ | `transformSource` | Called with a `.uf.tsx` or `.uf.ts` file's absolute path, with forward slashes, and its text, each time the plugin reads it, on the first load and on a hot update alike. The string it returns (or a promise of one) is what compiles, and `undefined` compiles the file as written. A hook that throws fails that load, with an error that names the file and the hook's message. `unframework preview` binds its controls to an example's components this way (ADR-0113). |
97
+ | `plugins` | Compiler plugins, passed to `compile()` (the harness's canaries). |
98
+ | `format` | Formats the output with oxfmt. Defaults to `true`, so a module's code equals the golden file byte for byte. |
99
+ | `passThrough` | The specifier prefixes whose imports pass through to every output as written, passed to `compile()`. Defaults to `["virtual:"]`; a list replaces it, as `["virtual:", "styled-system/"]` adds an engine that generates files. A prefix that is empty, relative or absolute fails when the plugin is created. |
99
100
 
100
101
  ## The dev server's allow list
101
102
 
package/dist/plugin.d.ts CHANGED
@@ -42,6 +42,14 @@ export interface UnframeworkOptions {
42
42
  * errors fails anyway, with the message after its diagnostics.
43
43
  */
44
44
  onCompile?(event: CompileEvent): string | void | Promise<string | void>;
45
+ /**
46
+ * Gives the source to compile for a `.uf.tsx` or `.uf.ts` file, in place of its text on disk:
47
+ * called with the file's absolute path, with forward slashes, and its text, each time the plugin
48
+ * reads it, on the first load and on a hot update alike. `undefined` compiles the file as
49
+ * written. `unframework preview` binds its controls to an example's components this way
50
+ * (ADR-0113). None by default.
51
+ */
52
+ transformSource?(file: string, source: string): string | undefined | Promise<string | undefined>;
45
53
  /** Compiler plugins, passed to `compile()` (the harness's canaries). */
46
54
  plugins?: readonly CompilerPlugin[];
47
55
  /** Formats the output with oxfmt. Defaults to `true`, so modules equal the golden files. */
package/dist/plugin.js CHANGED
@@ -112,8 +112,18 @@ const factory = (options = {}, meta) => {
112
112
  ];
113
113
  let root = toPosix(process.cwd());
114
114
  const seen = /* @__PURE__ */ new Map();
115
- const read = async (path) => {
115
+ const readSource = async (path) => {
116
116
  const text = await readFile(path, "utf8").catch(() => void 0);
117
+ if (text === void 0 || !options.transformSource || !isSource(path)) return text;
118
+ try {
119
+ return await options.transformSource(path, text) ?? text;
120
+ } catch (error) {
121
+ const reason = error instanceof Error ? error.message : String(error);
122
+ throw new Error(`transformSource threw while reading ${path}: ${reason}`, { cause: error });
123
+ }
124
+ };
125
+ const read = async (path) => {
126
+ const text = await readSource(path);
117
127
  if (!seen.has(path)) seen.set(path, text);
118
128
  return text;
119
129
  };
@@ -196,6 +206,7 @@ const factory = (options = {}, meta) => {
196
206
  };
197
207
  };
198
208
  let queue = Promise.resolve();
209
+ let generation = 0;
199
210
  const exclusive = (task) => {
200
211
  const run = queue.then(task);
201
212
  queue = run.catch(() => void 0);
@@ -282,12 +293,13 @@ const factory = (options = {}, meta) => {
282
293
  const changed = /* @__PURE__ */ new Map();
283
294
  for (const path of paths) {
284
295
  if (!seen.has(path)) continue;
285
- const text = await readFile(path, "utf8").catch(() => void 0);
296
+ const text = await readSource(path);
286
297
  if (text !== seen.get(path)) changed.set(path, text);
287
298
  }
288
299
  if (!changed.size) return false;
289
300
  const before = new Map([...changed.keys()].map((path) => [path, seen.get(path)]));
290
301
  for (const [path, text] of changed) seen.set(path, text);
302
+ generation += 1;
291
303
  try {
292
304
  for (const graph of graphs.values()) {
293
305
  const names = [...changed.keys()].flatMap((path) => namesOf(graph, path));
@@ -303,14 +315,18 @@ const factory = (options = {}, meta) => {
303
315
  } catch (error) {
304
316
  for (const [path, text] of before) seen.set(path, text);
305
317
  throw error;
318
+ } finally {
319
+ generation += 1;
306
320
  }
307
321
  return true;
308
322
  };
309
- /** Hands the project every change it has not seen among what a module reached. */
310
- const refresh = (graph, filename) => {
323
+ /** The paths of every file a module's output depends on, its own included. */
324
+ const reachedPaths = (graph, filename) => {
311
325
  const { files, packages } = reachedFrom(graph, filename);
312
- return handOver(/* @__PURE__ */ new Set([...files.map((file) => `${graph.root}/${file}`), ...packageFiles(packages)]));
326
+ return /* @__PURE__ */ new Set([...files.map((file) => `${graph.root}/${file}`), ...packageFiles(packages)]);
313
327
  };
328
+ /** Hands the project every change it has not seen among what a module reached. */
329
+ const refresh = (graph, filename) => handOver(reachedPaths(graph, filename));
314
330
  /**
315
331
  * The current compile of a `.uf.tsx` file or a `.uf.ts` module, shared by every id that loads
316
332
  * it, and the source it compiled. The changes the project has not seen go through its `update`
@@ -320,7 +336,14 @@ const factory = (options = {}, meta) => {
320
336
  * compile is taken again. The result's outputs hold the copies of the shared `.ts` modules it
321
337
  * reaches, as `compile()`'s do (ADR-0070): the golden guard judges them.
322
338
  */
323
- const compileFile = (file) => exclusive(async () => {
339
+ const compileFile = async (file) => await currentCompile(file) ?? exclusive(async () => {
340
+ const done = await compileNow(file);
341
+ await warmReach(file, done.result);
342
+ return done;
343
+ });
344
+ const compiles = /* @__PURE__ */ new Map();
345
+ /** The body of `compileFile`, which only a task of `exclusive` runs. */
346
+ const compileNow = async (file) => {
324
347
  const { graph, filename } = locate(file);
325
348
  const { project } = graph;
326
349
  await refresh(graph, filename);
@@ -328,22 +351,74 @@ const factory = (options = {}, meta) => {
328
351
  if (await refresh(graph, filename)) result = await project.compile(filename);
329
352
  const source = seen.get(`${graph.root}/${filename}`) ?? "";
330
353
  const files = result.outputs[target.name] ?? [];
331
- if (!files.length) return {
332
- result,
333
- source
334
- };
335
- const outputs = {
354
+ const outputs = files.length ? {
336
355
  ...result.outputs,
337
356
  [target.name]: [...files, ...project.copies(filename)]
338
- };
339
- return {
340
- result: {
357
+ } : void 0;
358
+ const done = {
359
+ result: outputs ? {
341
360
  ...result,
342
361
  outputs
343
- },
362
+ } : result,
344
363
  source
345
364
  };
346
- });
365
+ compiles.set(file, {
366
+ compiled: done,
367
+ generation
368
+ });
369
+ return done;
370
+ };
371
+ /**
372
+ * The file's last compile, without waiting for `exclusive`, if it is still what `compileNow`
373
+ * would give: no change has been handed over since, and none waits on disk among the files it
374
+ * reached, which `refresh` would hand over. Anything else waits its turn. Qwik loads a
375
+ * component's segments inside a test (ADR-0050), and each resolves its imports through
376
+ * `childId`, so a request that queued behind every other file's compile on a busy server
377
+ * outran the test's timeout.
378
+ */
379
+ const currentCompile = async (file) => {
380
+ const kept = compiles.get(file);
381
+ if (kept?.generation !== generation) return void 0;
382
+ const { graph, filename } = locate(file);
383
+ const paths = [...reachedPaths(graph, filename)].filter((path) => seen.has(path));
384
+ const texts = await Promise.all(paths.map((path) => readFile(path, "utf8").catch(() => void 0)));
385
+ if (paths.some((path, index) => texts[index] !== seen.get(path))) return void 0;
386
+ return compiles.get(file) === kept && kept.generation === generation ? kept.compiled : void 0;
387
+ };
388
+ /**
389
+ * Compiles every `.uf.tsx` and `.uf.ts` file a module's IR imports reach, its children's
390
+ * children too, in the module's own task, so a request for them finds them current. A module's
391
+ * load runs as its importer is imported, while Qwik's segments, the first to import the
392
+ * children, load inside a test (ADR-0050). A file that is gone, or whose compile throws, is
393
+ * left to its own request, which reports it.
394
+ */
395
+ const warmReach = async (file, result) => {
396
+ const visited = /* @__PURE__ */ new Set([file]);
397
+ const pending = [{
398
+ file,
399
+ result
400
+ }];
401
+ for (let next = pending.pop(); next; next = pending.pop()) for (const entry of next.result.ir?.imports ?? []) {
402
+ const child = posix.join(posix.dirname(next.file), entry.file);
403
+ if (visited.has(child)) continue;
404
+ visited.add(child);
405
+ if (!existsSync(child)) continue;
406
+ const kept = compiles.get(child);
407
+ if (kept?.generation === generation) {
408
+ pending.push({
409
+ file: child,
410
+ result: kept.compiled.result
411
+ });
412
+ continue;
413
+ }
414
+ try {
415
+ pending.push({
416
+ file: child,
417
+ result: (await compileNow(child)).result
418
+ });
419
+ } catch {}
420
+ }
421
+ };
347
422
  /** The guard's verdict on a load: the message that fails the module, if any. */
348
423
  const guard = async (event) => {
349
424
  if (!options.onCompile) return void 0;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unframework.dev/unplugin",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "The Unframework bundler plugin: imports of .uf.tsx files compile to the selected framework, and that framework's own plugin finishes the job.",
5
5
  "keywords": [
6
6
  "astro",
@@ -79,9 +79,9 @@
79
79
  "access": "public"
80
80
  },
81
81
  "dependencies": {
82
- "@unframework.dev/codegen": "0.2.0",
83
- "@unframework.dev/compiler": "0.3.0",
84
- "@unframework.dev/diagnostics": "0.3.0",
82
+ "@unframework.dev/codegen": "0.2.1",
83
+ "@unframework.dev/compiler": "0.3.1",
84
+ "@unframework.dev/diagnostics": "0.3.1",
85
85
  "@unframework.dev/parser": "0.1.1",
86
86
  "magic-string": "^1.4.2",
87
87
  "unplugin": "^3.4.0"
@@ -89,12 +89,12 @@
89
89
  "devDependencies": {
90
90
  "@qwik.dev/core": "2.0.0-rc.5",
91
91
  "@types/node": "^24.19.0",
92
- "@unframework.dev/target-angular": "0.2.0",
93
- "@unframework.dev/target-qwik": "0.2.0",
94
- "@unframework.dev/target-react": "0.2.0",
95
- "@unframework.dev/target-solid": "0.2.0",
96
- "@unframework.dev/target-svelte": "0.2.0",
97
- "@unframework.dev/target-vue": "0.2.0",
92
+ "@unframework.dev/target-angular": "0.2.1",
93
+ "@unframework.dev/target-qwik": "0.2.1",
94
+ "@unframework.dev/target-react": "0.2.1",
95
+ "@unframework.dev/target-solid": "0.2.1",
96
+ "@unframework.dev/target-svelte": "0.2.1",
97
+ "@unframework.dev/target-vue": "0.2.1",
98
98
  "@uxfront/typescript-config": "^0.1.0",
99
99
  "astro": "7.3.5",
100
100
  "react": "19.3.0",