@unframework.dev/unplugin 0.3.0 → 0.4.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/LICENSE +22 -0
- package/README.md +28 -10
- package/dist/detect.js +4 -3
- package/dist/ids.js +4 -4
- package/dist/plugin.d.ts +17 -6
- package/dist/plugin.js +126 -13
- package/dist/vite.d.ts +1 -1
- package/package.json +11 -11
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 UXFront
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
package/README.md
CHANGED
|
@@ -10,7 +10,7 @@ import unframework from "@unframework.dev/unplugin/vite";
|
|
|
10
10
|
import vue from "@vitejs/plugin-vue";
|
|
11
11
|
|
|
12
12
|
export default {
|
|
13
|
-
|
|
13
|
+
plugins: [unframework({ target: "vue" }), vue()],
|
|
14
14
|
};
|
|
15
15
|
```
|
|
16
16
|
|
|
@@ -25,7 +25,7 @@ On Vite you can leave `target` out, and the framework's plugin in your configura
|
|
|
25
25
|
|
|
26
26
|
```ts
|
|
27
27
|
export default {
|
|
28
|
-
|
|
28
|
+
plugins: [unframework(), vue()],
|
|
29
29
|
};
|
|
30
30
|
```
|
|
31
31
|
|
|
@@ -73,7 +73,7 @@ plugin first, as its installation guide does:
|
|
|
73
73
|
import styleframe from "styleframe/plugin/vite";
|
|
74
74
|
|
|
75
75
|
export default {
|
|
76
|
-
|
|
76
|
+
plugins: [styleframe(), unframework({ target: "vue" }), vue()],
|
|
77
77
|
};
|
|
78
78
|
```
|
|
79
79
|
|
|
@@ -96,6 +96,7 @@ integration harness does (`sourceUtilities` in `tests/integration/harness/stylef
|
|
|
96
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
97
|
| `plugins` | Compiler plugins, passed to `compile()` (the harness's canaries). |
|
|
98
98
|
| `format` | Formats the output with oxfmt. Defaults to `true`, so a module's code equals the golden file byte for byte. |
|
|
99
|
+
| `helperRoot` | Gives the directory, absolute, whose `_unframework/` holds the shared helper modules of a `.uf.tsx` or `.uf.ts` file's outputs (ADR-0121), given the file's absolute path with forward slashes: its own directory or one above it, inside its graph. Defaults to the graph's root, the project's or the package's, so every module of a graph imports one module of each helper. The integration harness gives each case's directory, where its golden files keep theirs. |
|
|
99
100
|
| `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. |
|
|
100
101
|
|
|
101
102
|
## The dev server's allow list
|
|
@@ -166,6 +167,17 @@ A module id is the absolute path of the `.uf.tsx` file plus the target's suffix
|
|
|
166
167
|
map goes on through its module's, and Astro's load map has a segment at column 0 of each line,
|
|
167
168
|
which is where its compiler traces from (ADR-0074, ADR-0078). L14 covers these in dev only.
|
|
168
169
|
An output with no map loads as `{ code }`. A scoped sheet's load is `{ code }` too, since its map has no segments.
|
|
170
|
+
- A shared helper module (ADR-0121) loads under its own id, `<helper root>/_unframework/<target>/<file>`,
|
|
171
|
+
which no file holds: `resolveId` gives it for a relative import of
|
|
172
|
+
`<helper root>/_unframework/<file>` from one of the plugin's modules, a Qwik segment split off
|
|
173
|
+
one, or another helper module, and for its id or that path coming back, once a compile has
|
|
174
|
+
emitted it. The target's name in the id keeps two plugins for different targets in one server,
|
|
175
|
+
such as React and Solid islands in one Astro app, from sharing a module where both emit a helper
|
|
176
|
+
of one name, like `cx.ts`. `load` gives the compile's text and its map, which names the module
|
|
177
|
+
itself. Every module of a graph imports one id of each helper, so a bundle holds it once per
|
|
178
|
+
target. A project file at `<helper root>/_unframework/<file>` stays the project's: the plugin
|
|
179
|
+
never serves the helper in its place, and the module that would import the helper fails with
|
|
180
|
+
UF1104, as the CLI reports the same layout.
|
|
169
181
|
- An id loads the target's output file with the id's extension (`Hello.vue`, `hello.ts`, and
|
|
170
182
|
`Hello.tsx` for a `.uf.tsx` id). A file with several components emits one such file each:
|
|
171
183
|
`.tsx` and `.ts` files are joined into one module (an import of a file of the join is left out,
|
|
@@ -178,8 +190,9 @@ A module id is the absolute path of the `.uf.tsx` file plus the target's suffix
|
|
|
178
190
|
which ends in the extension the framework's plugin claims.
|
|
179
191
|
- That error stops two components that share a type declared in their own file: each output
|
|
180
192
|
declares the types its props reach, so `joinModules` reports the type as "declared by A.tsx and
|
|
181
|
-
B.tsx" (ADR-0034). Two outputs that print the same inline helper
|
|
182
|
-
|
|
193
|
+
B.tsx" (ADR-0034). Two outputs that print the same inline helper clash the same way. A helper
|
|
194
|
+
shared in `_unframework/`, such as React's `cx`, doesn't: every component of a source imports it
|
|
195
|
+
alike, so the join writes the import once (ADR-0121). So do two imports from one module whose sets differ but share a name
|
|
183
196
|
(Angular's `Component` beside `Component` and `input`, Solid's `Show` beside `Show` and `For`,
|
|
184
197
|
or `{ CardProps }` beside `{ CardProps, Tone }` from a shared `.ts` module): only an identical
|
|
185
198
|
import is written once, and a name two imports bring in is declared twice. Give each such
|
|
@@ -314,8 +327,8 @@ A package can ship its `.uf.tsx` and `.uf.ts` files for its consumers to compile
|
|
|
314
327
|
|
|
315
328
|
```json
|
|
316
329
|
{
|
|
317
|
-
|
|
318
|
-
|
|
330
|
+
"name": "@acme/ui",
|
|
331
|
+
"exports": { "./source/*": "./src/*", "./button": "./src/Button.uf.tsx" }
|
|
319
332
|
}
|
|
320
333
|
```
|
|
321
334
|
|
|
@@ -370,12 +383,17 @@ relative path, so importing a package's component from one reports UF1202.
|
|
|
370
383
|
|
|
371
384
|
On Vite, the pre plugin, named `unframework`, exposes `api.getCompiled(id)`: the code of the last load of
|
|
372
385
|
a module id that finished, in any environment, or `undefined` when the id has not loaded or that
|
|
373
|
-
load failed. A load in progress never clears it.
|
|
374
|
-
|
|
386
|
+
load failed. A load in progress never clears it. For a shared helper module, by its id,
|
|
387
|
+
`<helper root>/_unframework/<target>/<file>`, or by the path its outputs import,
|
|
388
|
+
`<helper root>/_unframework/<file>`, it gives the module's text once a compile has emitted it, as
|
|
389
|
+
`load` serves it, and `undefined` where a project file sits at that path (ADR-0121). Angular's
|
|
390
|
+
`ngtscVirtual` reads the virtual sources of child components and helper modules with it:
|
|
375
391
|
|
|
376
392
|
```ts
|
|
377
393
|
const api = config.plugins.find((plugin) => plugin.name === "unframework")?.api;
|
|
378
394
|
api.getCompiled("/abs/Child.uf.tsx.ts");
|
|
395
|
+
api.getCompiled("/abs/_unframework/ScopeAttributes.ts");
|
|
396
|
+
api.getCompiled("/abs/_unframework/angular/ScopeAttributes.ts");
|
|
379
397
|
```
|
|
380
398
|
|
|
381
399
|
It exposes `api.getSheet(file)` too: the scoped sheet this target's last finished load of the
|
|
@@ -398,7 +416,7 @@ import unframework from "@unframework.dev/unplugin/astro";
|
|
|
398
416
|
import { defineConfig } from "astro/config";
|
|
399
417
|
|
|
400
418
|
export default defineConfig({
|
|
401
|
-
|
|
419
|
+
integrations: [unframework()],
|
|
402
420
|
});
|
|
403
421
|
```
|
|
404
422
|
|
package/dist/detect.js
CHANGED
|
@@ -76,7 +76,7 @@ const vueApiOf = (plugins) => plugins.find(({ name }) => name === VUE_PLUGIN)?.a
|
|
|
76
76
|
* a template out for `"condense"`, and the option can't be set per file (ADR-0026).
|
|
77
77
|
*/
|
|
78
78
|
function hostOptionWarning(plugins, file) {
|
|
79
|
-
if (vueApiOf(plugins)?.options?.template?.compilerOptions?.whitespace !== "preserve") return
|
|
79
|
+
if (vueApiOf(plugins)?.options?.template?.compilerOptions?.whitespace !== "preserve") return;
|
|
80
80
|
const diagnostic = createDiagnostic("UF7010", {
|
|
81
81
|
file,
|
|
82
82
|
span: NO_SPAN,
|
|
@@ -103,7 +103,7 @@ const NODE_SCRIPT_FS = {
|
|
|
103
103
|
try {
|
|
104
104
|
return readFileSync(file, "utf8");
|
|
105
105
|
} catch (error) {
|
|
106
|
-
if (isNoFile(error)) return
|
|
106
|
+
if (isNoFile(error)) return;
|
|
107
107
|
throw error;
|
|
108
108
|
}
|
|
109
109
|
},
|
|
@@ -129,7 +129,8 @@ const NODE_SCRIPT_FS = {
|
|
|
129
129
|
function fillVueScriptFs(plugins) {
|
|
130
130
|
const api = vueApiOf(plugins);
|
|
131
131
|
const options = api?.options;
|
|
132
|
-
|
|
132
|
+
const fs = options?.script?.fs;
|
|
133
|
+
if (!api || !options || fs !== void 0 && fs !== null) return;
|
|
133
134
|
api.options = {
|
|
134
135
|
...options,
|
|
135
136
|
script: {
|
package/dist/ids.js
CHANGED
|
@@ -3,7 +3,7 @@ import { TARGET_NAMES, builtinTargets } from "@unframework.dev/compiler";
|
|
|
3
3
|
// src/ids.ts
|
|
4
4
|
/** The extension of an authored component file. */
|
|
5
5
|
const SOURCE_EXTENSION = ".uf.tsx";
|
|
6
|
-
const escapeRegExp = (text) => text.
|
|
6
|
+
const escapeRegExp = (text) => text.replaceAll(/[.*+?^${}()|[\]\\]/g, String.raw`\$&`);
|
|
7
7
|
/**
|
|
8
8
|
* What `resolveId` looks at, with any query: the module ids of `suffix` coming back, and every
|
|
9
9
|
* specifier that can name a `.uf.tsx` file. That is `X.uf.tsx` itself and the forms TypeScript
|
|
@@ -77,7 +77,7 @@ function composableId(file, suffix, query = "") {
|
|
|
77
77
|
function parseComposableId(id, suffix) {
|
|
78
78
|
const index = id.indexOf("?");
|
|
79
79
|
const path = index === -1 ? id : id.slice(0, index);
|
|
80
|
-
if (!path.endsWith(`.uf.ts${suffix}`)) return
|
|
80
|
+
if (!path.endsWith(`.uf.ts${suffix}`)) return;
|
|
81
81
|
return {
|
|
82
82
|
file: path.slice(0, path.length - suffix.length),
|
|
83
83
|
query: index === -1 ? "" : id.slice(index)
|
|
@@ -123,7 +123,7 @@ function moduleId(file, suffix, query = "") {
|
|
|
123
123
|
function parseModuleId(id, suffix) {
|
|
124
124
|
const index = id.indexOf("?");
|
|
125
125
|
const path = index === -1 ? id : id.slice(0, index);
|
|
126
|
-
if (!path.endsWith(`.uf.tsx${suffix}`)) return
|
|
126
|
+
if (!path.endsWith(`.uf.tsx${suffix}`)) return;
|
|
127
127
|
return {
|
|
128
128
|
file: path.slice(0, path.length - suffix.length),
|
|
129
129
|
query: index === -1 ? "" : id.slice(index)
|
|
@@ -168,7 +168,7 @@ function componentId(file, output, query = "") {
|
|
|
168
168
|
/** A component id taken apart, or `undefined` when the id is not one. */
|
|
169
169
|
function parseComponentId(id) {
|
|
170
170
|
const match = COMPONENT_ID.exec(id);
|
|
171
|
-
if (!match) return
|
|
171
|
+
if (!match) return;
|
|
172
172
|
const index = id.indexOf("?");
|
|
173
173
|
const path = index === -1 ? id : id.slice(0, index);
|
|
174
174
|
const output = match[1];
|
package/dist/plugin.d.ts
CHANGED
|
@@ -11,9 +11,9 @@ export interface CompileEvent {
|
|
|
11
11
|
/** The target's name. */
|
|
12
12
|
target: string;
|
|
13
13
|
/** Every file the target emitted for the module: what the golden files hold. */
|
|
14
|
-
files: OutputFile
|
|
14
|
+
files: Array<OutputFile>;
|
|
15
15
|
/** Every diagnostic of the compile, sorted. */
|
|
16
|
-
diagnostics: Diagnostic
|
|
16
|
+
diagnostics: Array<Diagnostic>;
|
|
17
17
|
/**
|
|
18
18
|
* The compile's IR, absent when the file did not parse: what the module's components declare,
|
|
19
19
|
* such as the events a test listens to (the harness's `ufComponentEvents`).
|
|
@@ -51,7 +51,7 @@ export interface UnframeworkOptions {
|
|
|
51
51
|
*/
|
|
52
52
|
transformSource?(file: string, source: string): string | undefined | Promise<string | undefined>;
|
|
53
53
|
/** Compiler plugins, passed to `compile()` (the harness's canaries). */
|
|
54
|
-
plugins?:
|
|
54
|
+
plugins?: ReadonlyArray<CompilerPlugin>;
|
|
55
55
|
/** Formats the output with oxfmt. Defaults to `true`, so modules equal the golden files. */
|
|
56
56
|
format?: boolean;
|
|
57
57
|
/**
|
|
@@ -59,14 +59,25 @@ export interface UnframeworkOptions {
|
|
|
59
59
|
* `compile()` (ADR-0064): `["virtual:"]` when absent. The plugin never resolves such an import:
|
|
60
60
|
* the plugin that registers the module does, such as a styling engine's.
|
|
61
61
|
*/
|
|
62
|
-
passThrough?:
|
|
62
|
+
passThrough?: ReadonlyArray<string>;
|
|
63
|
+
/**
|
|
64
|
+
* The directory whose `_unframework/` holds the shared helper modules of a `.uf.tsx` or `.uf.ts`
|
|
65
|
+
* file's outputs (ADR-0121), given the file's absolute path with forward slashes: an absolute
|
|
66
|
+
* path, the file's own directory or one above it inside its graph. Defaults to the graph's root,
|
|
67
|
+
* the project's or the package's, so every module of a graph imports one module of each helper.
|
|
68
|
+
* The harness gives each case's directory, where its golden files keep theirs.
|
|
69
|
+
*/
|
|
70
|
+
helperRoot?(file: string): string;
|
|
63
71
|
}
|
|
64
72
|
/** What the pre plugin exposes as `api`, for other plugins that need the compiled sources. */
|
|
65
73
|
export interface UnframeworkApi {
|
|
66
74
|
/**
|
|
67
75
|
* The target source of the last load of a module id that finished, in any environment, or
|
|
68
|
-
* `undefined` when the id has not loaded or that load failed.
|
|
69
|
-
*
|
|
76
|
+
* `undefined` when the id has not loaded or that load failed. For a shared helper module's id
|
|
77
|
+
* (ADR-0121), `<helper root>/_unframework/<target>/<file>`, or the path its outputs import,
|
|
78
|
+
* `<helper root>/_unframework/<file>`, it is the module's text once a compile has emitted it, as
|
|
79
|
+
* `load` serves it, and `undefined` where a project file sits at that path. Angular's
|
|
80
|
+
* `ngtscVirtual` reads child components and helper modules with it.
|
|
70
81
|
*/
|
|
71
82
|
getCompiled(id: string): string | undefined;
|
|
72
83
|
/**
|
package/dist/plugin.js
CHANGED
|
@@ -6,7 +6,7 @@ import { basename, extname, isAbsolute, posix, relative, sep } from "node:path";
|
|
|
6
6
|
import { checkPassThrough, createProject, createSyntacticOracle, isTargetSelector, resolveTarget, targetName } from "@unframework.dev/compiler";
|
|
7
7
|
import { existsSync, readFileSync } from "node:fs";
|
|
8
8
|
import { readFile } from "node:fs/promises";
|
|
9
|
-
import { formatDiagnostic, formatDiagnostics } from "@unframework.dev/diagnostics";
|
|
9
|
+
import { createDiagnostic, formatDiagnostic, formatDiagnostics } from "@unframework.dev/diagnostics";
|
|
10
10
|
import { createUnplugin } from "unplugin";
|
|
11
11
|
// src/plugin.ts
|
|
12
12
|
/** A Qwik optimizer segment of a module: `<module id>_<symbol>.js`, its symbol a name. */
|
|
@@ -22,6 +22,24 @@ const RELATIVE = /^\.\.?\//;
|
|
|
22
22
|
*/
|
|
23
23
|
const SHEET_SUFFIX = ".css";
|
|
24
24
|
const SHEET_ID_FILTER = moduleIdFilter(SHEET_SUFFIX);
|
|
25
|
+
/**
|
|
26
|
+
* A shared helper module's id (ADR-0121): `<helper root>/_unframework/<target>/<file>`, which loads
|
|
27
|
+
* from the compile that emitted it. The target's segment keeps two plugin instances for different
|
|
28
|
+
* targets in one server, such as React and Solid islands in one Astro app, from sharing a module
|
|
29
|
+
* where both emit a helper of one name (`cx.ts`). An output imports the path without it,
|
|
30
|
+
* `<helper root>/_unframework/<file>`, which the filter also takes. A project file at that path is
|
|
31
|
+
* the project's: the plugin never serves the helper in its place, and fails the module that would
|
|
32
|
+
* import it with UF1104 (`helperClash`).
|
|
33
|
+
*/
|
|
34
|
+
const HELPER_ID_FILTER = /\/_unframework\/(?:[^/?]+\/)?[^/?]+(?:\?.*)?$/;
|
|
35
|
+
/** The extensions a helper's specifier may leave out: React's imports name `./_unframework/cx`. */
|
|
36
|
+
const HELPER_EXTENSIONS = [
|
|
37
|
+
"",
|
|
38
|
+
".ts",
|
|
39
|
+
".tsx",
|
|
40
|
+
".js",
|
|
41
|
+
".jsx"
|
|
42
|
+
];
|
|
25
43
|
/** How a project names a package it read types from (ADR-0071): `pkg:<name>`. */
|
|
26
44
|
const PACKAGE = "pkg:";
|
|
27
45
|
/** The output files a script target joins into one module (`.tsx`, `.ts`). */
|
|
@@ -33,11 +51,11 @@ const SCRIPT = /^\.tsx?$/;
|
|
|
33
51
|
*/
|
|
34
52
|
const BARE = /^[\w@#][^:]*$/;
|
|
35
53
|
/** The bundlers whose hook context resolves a bare specifier (see `fileOf`). */
|
|
36
|
-
const RESOLVES_BARE = [
|
|
54
|
+
const RESOLVES_BARE = /* @__PURE__ */ new Set([
|
|
37
55
|
"vite",
|
|
38
56
|
"rollup",
|
|
39
57
|
"rolldown"
|
|
40
|
-
];
|
|
58
|
+
]);
|
|
41
59
|
const NODE_MODULES = "/node_modules/";
|
|
42
60
|
/**
|
|
43
61
|
* A dev server's own allow list, applied to the files our ids name (C21). Vite refuses a request
|
|
@@ -99,7 +117,7 @@ const factory = (options = {}, meta) => {
|
|
|
99
117
|
const composableRequest = (id) => parseComposableId(id, composableSuffix);
|
|
100
118
|
const suffixes = options.target !== void 0 ? [suffix] : options.extension !== void 0 ? [idSuffix("vue", options.extension)] : [...new Set(Object.values(ID_SUFFIXES))];
|
|
101
119
|
const composableFilters = (options.target !== void 0 ? [composableSuffix] : [...new Set(Object.values(COMPOSABLE_ID_SUFFIXES))]).map(composableFilter);
|
|
102
|
-
const bareSpecifiers = RESOLVES_BARE.
|
|
120
|
+
const bareSpecifiers = RESOLVES_BARE.has(meta.framework);
|
|
103
121
|
const moduleIds = [
|
|
104
122
|
...suffixes.map(moduleIdFilter),
|
|
105
123
|
COMPONENT_ID_FILTER,
|
|
@@ -108,7 +126,8 @@ const factory = (options = {}, meta) => {
|
|
|
108
126
|
const loadIds = [
|
|
109
127
|
...moduleIds,
|
|
110
128
|
moduleIdFilter(""),
|
|
111
|
-
SHEET_ID_FILTER
|
|
129
|
+
SHEET_ID_FILTER,
|
|
130
|
+
HELPER_ID_FILTER
|
|
112
131
|
];
|
|
113
132
|
let root = toPosix(process.cwd());
|
|
114
133
|
const seen = /* @__PURE__ */ new Map();
|
|
@@ -188,6 +207,7 @@ const factory = (options = {}, meta) => {
|
|
|
188
207
|
targets: [target],
|
|
189
208
|
format: options.format !== false,
|
|
190
209
|
sourceMap: true,
|
|
210
|
+
helperRoot: (filename) => helperRootOf(graphRoot, filename),
|
|
191
211
|
plugins: options.plugins,
|
|
192
212
|
...options.passThrough ? { passThrough: options.passThrough } : {}
|
|
193
213
|
}),
|
|
@@ -197,6 +217,69 @@ const factory = (options = {}, meta) => {
|
|
|
197
217
|
}
|
|
198
218
|
return graph;
|
|
199
219
|
};
|
|
220
|
+
/**
|
|
221
|
+
* The helper modules' directory of a graph's file, relative to the graph's root (ADR-0121):
|
|
222
|
+
* `helperRoot`'s answer, or the root. An answer outside the graph is passed on as it is, which
|
|
223
|
+
* `compile()` refuses, naming it.
|
|
224
|
+
*/
|
|
225
|
+
const helperRootOf = (graphRoot, filename) => {
|
|
226
|
+
if (!options.helperRoot) return "";
|
|
227
|
+
const directory = toPosix(options.helperRoot(posix.join(graphRoot, filename))).replace(/\/+$/, "");
|
|
228
|
+
if (directory === graphRoot) return "";
|
|
229
|
+
return directory.startsWith(`${graphRoot}/`) ? directory.slice(graphRoot.length + 1) : directory;
|
|
230
|
+
};
|
|
231
|
+
const helperModules = /* @__PURE__ */ new Map();
|
|
232
|
+
/**
|
|
233
|
+
* A helper module's id, given the absolute path its outputs import: the target's name, as one
|
|
234
|
+
* path segment, goes between `_unframework/` and the file, so the file keeps its name and
|
|
235
|
+
* extension, which the framework's own plugin claims it by.
|
|
236
|
+
*/
|
|
237
|
+
const helperIdOf = (path) => {
|
|
238
|
+
const slash = path.lastIndexOf("/");
|
|
239
|
+
const segment = target.name.replaceAll(/[^\w-]/g, "_") || "_";
|
|
240
|
+
return `${path.slice(0, slash)}/${segment}${path.slice(slash)}`;
|
|
241
|
+
};
|
|
242
|
+
/**
|
|
243
|
+
* The id of the helper module a path names, with or without its extension: an output's import
|
|
244
|
+
* of the path, or a helper module's import of its sibling, which is already beside the id.
|
|
245
|
+
*/
|
|
246
|
+
const helperId = (path) => {
|
|
247
|
+
for (const ending of HELPER_EXTENSIONS) {
|
|
248
|
+
const candidate = `${path}${ending}`;
|
|
249
|
+
if (helperModules.has(candidate)) return candidate;
|
|
250
|
+
if (helperModules.has(helperIdOf(candidate))) return helperIdOf(candidate);
|
|
251
|
+
}
|
|
252
|
+
};
|
|
253
|
+
/**
|
|
254
|
+
* The helper module an id or the path its outputs import names, unless a project file has taken
|
|
255
|
+
* the path since, which is the project's to load (`helperClash`).
|
|
256
|
+
*/
|
|
257
|
+
const helperOf = (id) => {
|
|
258
|
+
const helper = helperModules.get(id) ?? helperModules.get(helperIdOf(id));
|
|
259
|
+
return helper && !existsSync(helper.path) ? helper.output : void 0;
|
|
260
|
+
};
|
|
261
|
+
/**
|
|
262
|
+
* UF1104, formatted, where a helper module a compile of `file` emitted would sit at a project
|
|
263
|
+
* file's path (ADR-0121), as the CLI reports it for its tree: the output's import would read
|
|
264
|
+
* that file, so the module fails rather than load it.
|
|
265
|
+
*/
|
|
266
|
+
const helperClash = (file, result, graph, filename, source) => {
|
|
267
|
+
for (const path of result.helpers[target.name] ?? []) {
|
|
268
|
+
const id = posix.join(posix.dirname(file), path);
|
|
269
|
+
if (!existsSync(id)) continue;
|
|
270
|
+
const diagnostic = createDiagnostic("UF1104", {
|
|
271
|
+
file: filename,
|
|
272
|
+
span: {
|
|
273
|
+
start: 0,
|
|
274
|
+
end: 0
|
|
275
|
+
},
|
|
276
|
+
target: target.name,
|
|
277
|
+
message: `${filenameOf(id)} is a file of the project, where the ${target.name} output of ${filename} puts its helper module ${path}: \`_unframework/\` at the root of the output tree holds the helper modules (ADR-0121).`,
|
|
278
|
+
help: "Rename the file or the directory that holds it, or give the plugin another `helperRoot`."
|
|
279
|
+
});
|
|
280
|
+
return formatErrors(graph, filename, [diagnostic], source);
|
|
281
|
+
}
|
|
282
|
+
};
|
|
200
283
|
/** A file's graph and its name there: relative to the graph's root, with forward slashes. */
|
|
201
284
|
const locate = (file) => {
|
|
202
285
|
const graph = graphOf(file);
|
|
@@ -351,6 +434,14 @@ const factory = (options = {}, meta) => {
|
|
|
351
434
|
if (await refresh(graph, filename)) result = await project.compile(filename);
|
|
352
435
|
const source = seen.get(`${graph.root}/${filename}`) ?? "";
|
|
353
436
|
const files = result.outputs[target.name] ?? [];
|
|
437
|
+
const helpers = new Set(result.helpers[target.name] ?? []);
|
|
438
|
+
for (const output of files) {
|
|
439
|
+
const path = posix.join(posix.dirname(file), output.path);
|
|
440
|
+
if (helpers.has(output.path) && !existsSync(path)) helperModules.set(helperIdOf(path), {
|
|
441
|
+
output,
|
|
442
|
+
path
|
|
443
|
+
});
|
|
444
|
+
}
|
|
354
445
|
const outputs = files.length ? {
|
|
355
446
|
...result.outputs,
|
|
356
447
|
[target.name]: [...files, ...project.copies(filename)]
|
|
@@ -378,11 +469,11 @@ const factory = (options = {}, meta) => {
|
|
|
378
469
|
*/
|
|
379
470
|
const currentCompile = async (file) => {
|
|
380
471
|
const kept = compiles.get(file);
|
|
381
|
-
if (kept?.generation !== generation) return
|
|
472
|
+
if (kept?.generation !== generation) return;
|
|
382
473
|
const { graph, filename } = locate(file);
|
|
383
474
|
const paths = [...reachedPaths(graph, filename)].filter((path) => seen.has(path));
|
|
384
475
|
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
|
|
476
|
+
if (paths.some((path, index) => texts[index] !== seen.get(path))) return;
|
|
386
477
|
return compiles.get(file) === kept && kept.generation === generation ? kept.compiled : void 0;
|
|
387
478
|
};
|
|
388
479
|
/**
|
|
@@ -421,7 +512,7 @@ const factory = (options = {}, meta) => {
|
|
|
421
512
|
};
|
|
422
513
|
/** The guard's verdict on a load: the message that fails the module, if any. */
|
|
423
514
|
const guard = async (event) => {
|
|
424
|
-
if (!options.onCompile) return
|
|
515
|
+
if (!options.onCompile) return;
|
|
425
516
|
let verdict;
|
|
426
517
|
try {
|
|
427
518
|
verdict = await options.onCompile(event);
|
|
@@ -429,7 +520,7 @@ const factory = (options = {}, meta) => {
|
|
|
429
520
|
const reason = error instanceof Error ? error.stack ?? error.message : String(error);
|
|
430
521
|
return `onCompile threw while checking ${event.id}: ${reason}`;
|
|
431
522
|
}
|
|
432
|
-
if (typeof verdict !== "string") return
|
|
523
|
+
if (typeof verdict !== "string") return;
|
|
433
524
|
return verdict || `onCompile failed ${event.id} without a message.`;
|
|
434
525
|
};
|
|
435
526
|
/** A module id coming back, as the absolute id `load` receives, if its `.uf.tsx` file exists. */
|
|
@@ -466,7 +557,7 @@ const factory = (options = {}, meta) => {
|
|
|
466
557
|
* or from an importer whose file is gone.
|
|
467
558
|
*/
|
|
468
559
|
const childId = async (file, specifier) => {
|
|
469
|
-
if (!existsSync(file)) return
|
|
560
|
+
if (!existsSync(file)) return;
|
|
470
561
|
const { result } = await compileFile(file);
|
|
471
562
|
const wanted = posix.join(posix.dirname(file), specifier);
|
|
472
563
|
const files = [file, ...(result.ir?.imports ?? []).map((entry) => posix.join(posix.dirname(file), entry.file))];
|
|
@@ -535,6 +626,8 @@ const factory = (options = {}, meta) => {
|
|
|
535
626
|
};
|
|
536
627
|
const errors = result.diagnostics.filter((diagnostic) => diagnostic.severity === "error");
|
|
537
628
|
if (errors.length > 0) return fail(formatErrors(graph, filename, errors, source));
|
|
629
|
+
const clash = helperClash(file, result, graph, filename, source);
|
|
630
|
+
if (clash !== void 0) return fail(clash);
|
|
538
631
|
const owners = result.owners[target.name] ?? {};
|
|
539
632
|
const owned = files.filter((output) => Object.hasOwn(owners, output.path));
|
|
540
633
|
const assembly = assembleModule({
|
|
@@ -579,7 +672,7 @@ const factory = (options = {}, meta) => {
|
|
|
579
672
|
return resolved && !resolved.external ? withoutQuery(resolved.id) : void 0;
|
|
580
673
|
}
|
|
581
674
|
const [named] = moduleIdCandidates(path, root, importer);
|
|
582
|
-
if (named === void 0) return
|
|
675
|
+
if (named === void 0) return;
|
|
583
676
|
const stem = /^(.*\.uf)(?:\.jsx?)?$/.exec(named)?.[1];
|
|
584
677
|
return (stem === void 0 ? [named] : [
|
|
585
678
|
named,
|
|
@@ -594,6 +687,11 @@ const factory = (options = {}, meta) => {
|
|
|
594
687
|
const index = source.indexOf("?");
|
|
595
688
|
const path = index === -1 ? source : source.slice(0, index);
|
|
596
689
|
const query = index === -1 ? "" : source.slice(index);
|
|
690
|
+
const fromOurs = importer !== void 0 && (helperModules.has(withoutQuery(importer)) || (parseModuleId(importer, suffix) ?? componentRequest(importer) ?? composableRequest(importer) ?? splitFrom(importer)) !== void 0);
|
|
691
|
+
for (const candidate of RELATIVE.test(path) && !fromOurs ? [] : moduleIdCandidates(path, root, importer)) {
|
|
692
|
+
const id = helperId(candidate);
|
|
693
|
+
if (id !== void 0) return `${id}${query}`;
|
|
694
|
+
}
|
|
597
695
|
if (parseModuleId(path, SHEET_SUFFIX)) {
|
|
598
696
|
if (RELATIVE.test(path) && !(importer && (parseModuleId(importer, suffix) ?? componentRequest(importer) ?? splitFrom(importer)))) return null;
|
|
599
697
|
const id = moduleIdCandidates(path, root, importer).find((candidate) => existsSync(parseModuleId(candidate, SHEET_SUFFIX).file));
|
|
@@ -629,7 +727,7 @@ const factory = (options = {}, meta) => {
|
|
|
629
727
|
* query. Vite's own file queries (`?raw`, `?url`) read the authored file, so they stay Vite's.
|
|
630
728
|
*/
|
|
631
729
|
const ownerOf = (id) => {
|
|
632
|
-
if (id === null || VITE_FILE_QUERY.test(id)) return
|
|
730
|
+
if (id === null || VITE_FILE_QUERY.test(id)) return;
|
|
633
731
|
return (parseModuleId(id, SHEET_SUFFIX) ?? composableRequest(id) ?? componentRequest(id) ?? parseModuleId(id, suffix) ?? splitFrom(id))?.file;
|
|
634
732
|
};
|
|
635
733
|
const served = /* @__PURE__ */ new Set();
|
|
@@ -696,6 +794,14 @@ const factory = (options = {}, meta) => {
|
|
|
696
794
|
* any of our ids names, queries included, goes through it before anything reads the file.
|
|
697
795
|
*/
|
|
698
796
|
async function loadModule(id, failed, access) {
|
|
797
|
+
const helper = helperModules.get(withoutQuery(id));
|
|
798
|
+
if (helper && !existsSync(helper.path)) {
|
|
799
|
+
const { output } = helper;
|
|
800
|
+
return {
|
|
801
|
+
code: output.contents,
|
|
802
|
+
...output.map ? { map: output.map } : {}
|
|
803
|
+
};
|
|
804
|
+
}
|
|
699
805
|
const requested = parseModuleId(id, SHEET_SUFFIX) ?? componentRequest(id) ?? parseModuleId(id, suffix) ?? parseModuleId(id, "");
|
|
700
806
|
if (access && requested && isAbsolute(requested.file)) await access(requested.file, id);
|
|
701
807
|
const sheet = parseModuleId(id, SHEET_SUFFIX);
|
|
@@ -737,6 +843,8 @@ const factory = (options = {}, meta) => {
|
|
|
737
843
|
};
|
|
738
844
|
const errors = result.diagnostics.filter((diagnostic) => diagnostic.severity === "error");
|
|
739
845
|
if (errors.length > 0) return fail(formatErrors(graph, filename, errors, source));
|
|
846
|
+
const clash = helperClash(request.file, result, graph, filename, source);
|
|
847
|
+
if (clash !== void 0) return fail(clash);
|
|
740
848
|
const owners = result.owners[target.name] ?? {};
|
|
741
849
|
const main = mainComponent(result.ir);
|
|
742
850
|
const owned = files.filter((file) => Object.hasOwn(owners, file.path));
|
|
@@ -790,6 +898,7 @@ const factory = (options = {}, meta) => {
|
|
|
790
898
|
COMPONENT_ID_FILTER,
|
|
791
899
|
RELATIVE,
|
|
792
900
|
SHEET_ID_FILTER,
|
|
901
|
+
HELPER_ID_FILTER,
|
|
793
902
|
...composableFilters,
|
|
794
903
|
...bareSpecifiers ? [BARE] : []
|
|
795
904
|
].map(({ source }) => source).join("|")) };
|
|
@@ -871,7 +980,11 @@ const factory = (options = {}, meta) => {
|
|
|
871
980
|
return target.name === "qwik" && this.environment.name === "client" && update.type === "update" ? reloadQwikClient(this.environment, update, modules) : modules;
|
|
872
981
|
},
|
|
873
982
|
api: {
|
|
874
|
-
getCompiled: (id) =>
|
|
983
|
+
getCompiled: (id) => {
|
|
984
|
+
const own = compiled.get(id);
|
|
985
|
+
if (own !== void 0) return own;
|
|
986
|
+
return helperOf(id)?.contents;
|
|
987
|
+
},
|
|
875
988
|
getSheet: (file) => sheets.get(file)
|
|
876
989
|
}
|
|
877
990
|
}
|
package/dist/vite.d.ts
CHANGED
|
@@ -6,5 +6,5 @@ import { Plugin } from "vite";
|
|
|
6
6
|
* `post` turns a module that `onCompile` failed into `throw new Error(message)`. Place it before
|
|
7
7
|
* the framework's plugins. With no `target`, the framework's plugin decides it (ADR-0075).
|
|
8
8
|
*/
|
|
9
|
-
export default function unframework(options?: UnframeworkOptions): Plugin
|
|
9
|
+
export default function unframework(options?: UnframeworkOptions): Array<Plugin>;
|
|
10
10
|
export type { CompileEvent, UnframeworkApi, UnframeworkOptions };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unframework.dev/unplugin",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.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.
|
|
83
|
-
"@unframework.dev/compiler": "0.
|
|
84
|
-
"@unframework.dev/diagnostics": "0.
|
|
82
|
+
"@unframework.dev/codegen": "0.4.0",
|
|
83
|
+
"@unframework.dev/compiler": "0.4.0",
|
|
84
|
+
"@unframework.dev/diagnostics": "0.5.0",
|
|
85
85
|
"@unframework.dev/parser": "0.1.1",
|
|
86
86
|
"magic-string": "^1.4.2",
|
|
87
87
|
"unplugin": "^3.4.0"
|
|
@@ -89,13 +89,13 @@
|
|
|
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.
|
|
93
|
-
"@unframework.dev/target-qwik": "0.
|
|
94
|
-
"@unframework.dev/target-react": "0.
|
|
95
|
-
"@unframework.dev/target-solid": "0.
|
|
96
|
-
"@unframework.dev/target-svelte": "0.
|
|
97
|
-
"@unframework.dev/target-vue": "0.
|
|
98
|
-
"@uxfront/typescript-config": "^0.
|
|
92
|
+
"@unframework.dev/target-angular": "0.4.0",
|
|
93
|
+
"@unframework.dev/target-qwik": "0.4.0",
|
|
94
|
+
"@unframework.dev/target-react": "0.4.0",
|
|
95
|
+
"@unframework.dev/target-solid": "0.4.0",
|
|
96
|
+
"@unframework.dev/target-svelte": "0.4.0",
|
|
97
|
+
"@unframework.dev/target-vue": "0.3.0",
|
|
98
|
+
"@uxfront/typescript-config": "^0.2.0",
|
|
99
99
|
"astro": "7.3.5",
|
|
100
100
|
"react": "19.3.0",
|
|
101
101
|
"react-dom": "19.3.0",
|