@unframework.dev/unplugin 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/README.md +420 -2
- package/dist/assemble.js +133 -0
- package/dist/astro.d.ts +11 -0
- package/dist/astro.js +38 -0
- package/dist/bun.d.ts +2 -0
- package/dist/bun.js +2 -0
- package/dist/chain.js +50 -0
- package/dist/detect.js +117 -0
- package/dist/esbuild.d.ts +2 -0
- package/dist/esbuild.js +2 -0
- package/dist/farm.d.ts +2 -0
- package/dist/farm.js +2 -0
- package/dist/ids.d.ts +63 -0
- package/dist/ids.js +180 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/plugin.d.ts +87 -0
- package/dist/plugin.js +858 -0
- package/dist/rolldown.d.ts +2 -0
- package/dist/rolldown.js +2 -0
- package/dist/rollup.d.ts +2 -0
- package/dist/rollup.js +2 -0
- package/dist/rsbuild.d.ts +2 -0
- package/dist/rsbuild.js +2 -0
- package/dist/rspack.d.ts +2 -0
- package/dist/rspack.js +2 -0
- package/dist/unloader.d.ts +2 -0
- package/dist/unloader.js +2 -0
- package/dist/vite.d.ts +12 -0
- package/dist/vite.js +13 -0
- package/dist/webpack.d.ts +2 -0
- package/dist/webpack.js +2 -0
- package/package.json +124 -3
package/dist/chain.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { chainMap } from "@unframework.dev/codegen";
|
|
2
|
+
//#region src/chain.ts
|
|
3
|
+
/** Vite's name for the Svelte plugin's compile step. */
|
|
4
|
+
const SVELTE_COMPILE = "vite-plugin-svelte:compile";
|
|
5
|
+
/**
|
|
6
|
+
* Keeps Svelte's compile from merging a module's load map itself. It passes
|
|
7
|
+
* `getCombinedSourcemap()` to the compiler, whose result already reaches the `.uf.tsx`, and Vite
|
|
8
|
+
* then chains that map through the load map a second time, so most positions trace to nothing.
|
|
9
|
+
* Without the merge, the compile's map stops at the `.svelte` text and Vite's own chain goes on.
|
|
10
|
+
* `isOwn` limits this to the modules this plugin loaded. A later release of the Svelte plugin
|
|
11
|
+
* that renames the step leaves the double chain, which L14's stack checks then fail on.
|
|
12
|
+
*/
|
|
13
|
+
function chainSvelteOnce(plugins, isOwn) {
|
|
14
|
+
const hook = plugins.find((plugin) => plugin.name === SVELTE_COMPILE)?.transform;
|
|
15
|
+
if (!hook || typeof hook === "function") return;
|
|
16
|
+
const handler = hook.handler;
|
|
17
|
+
hook.handler = function(code, id, options) {
|
|
18
|
+
if (!isOwn(id)) return handler.call(this, code, id, options);
|
|
19
|
+
const context = Object.create(this, { getCombinedSourcemap: { value: () => void 0 } });
|
|
20
|
+
return handler.call(context, code, id, options);
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
/** Vite's name for Qwik's plugin, whose `load` serves the optimizer's segments. */
|
|
24
|
+
const QWIK_PLUGIN = "vite-plugin-qwik";
|
|
25
|
+
/**
|
|
26
|
+
* Chains a Qwik segment's map on through its module's load map. The optimizer splits a module
|
|
27
|
+
* into segments that Vite loads as modules of their own, so Vite never chains the module's load
|
|
28
|
+
* map onto them, and a segment's map stops at the Qwik output. `mapOf` gives the load map of the
|
|
29
|
+
* segment's module, by the segment's id.
|
|
30
|
+
*/
|
|
31
|
+
function chainQwikSegments(plugins, mapOf) {
|
|
32
|
+
const qwik = plugins.find((plugin) => plugin.name === QWIK_PLUGIN);
|
|
33
|
+
const hook = qwik?.load;
|
|
34
|
+
if (!qwik || !hook) return;
|
|
35
|
+
const handler = typeof hook === "function" ? hook : hook.handler;
|
|
36
|
+
const load = async function(id, options) {
|
|
37
|
+
const result = await handler.call(this, id, options);
|
|
38
|
+
const parent = mapOf(id);
|
|
39
|
+
if (!parent || typeof result !== "object" || result === null) return result;
|
|
40
|
+
if (!result.map) return result;
|
|
41
|
+
return {
|
|
42
|
+
...result,
|
|
43
|
+
map: chainMap(result.map, parent)
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
if (typeof hook === "function") qwik.load = load;
|
|
47
|
+
else hook.handler = load;
|
|
48
|
+
}
|
|
49
|
+
//#endregion
|
|
50
|
+
export { chainQwikSegments, chainSvelteOnce };
|
package/dist/detect.js
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { readFileSync, realpathSync, statSync } from "node:fs";
|
|
2
|
+
import { createDiagnostic, formatDiagnostic } from "@unframework.dev/diagnostics";
|
|
3
|
+
//#region src/detect.ts
|
|
4
|
+
/** The name plugin-vue registers. */
|
|
5
|
+
const VUE_PLUGIN = "vite:vue";
|
|
6
|
+
/** Whether a plugin of this name belongs to each framework a built-in target compiles to. */
|
|
7
|
+
const FRAMEWORK_PLUGINS = {
|
|
8
|
+
react: (name) => name.startsWith("vite:react-"),
|
|
9
|
+
vue: (name) => name === VUE_PLUGIN,
|
|
10
|
+
svelte: (name) => name === "vite-plugin-svelte",
|
|
11
|
+
solid: (name) => name === "solid",
|
|
12
|
+
angular: (name) => name === "@analogjs/vite-plugin-angular",
|
|
13
|
+
qwik: (name) => name === "vite-plugin-qwik",
|
|
14
|
+
astro: (name) => name.startsWith("astro:")
|
|
15
|
+
};
|
|
16
|
+
const NO_SPAN = {
|
|
17
|
+
start: 0,
|
|
18
|
+
end: 0
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* The one target whose framework's plugin is among `plugins`, or the UF7005 or UF7006 message that
|
|
22
|
+
* says why there isn't one. `file` names the configuration in the message.
|
|
23
|
+
*/
|
|
24
|
+
function detectTarget(plugins, file) {
|
|
25
|
+
const names = plugins.flatMap(({ name }) => typeof name === "string" ? [name] : []);
|
|
26
|
+
const found = Object.entries(FRAMEWORK_PLUGINS).flatMap(([target, belongs]) => {
|
|
27
|
+
const plugin = names.find((name) => belongs(name));
|
|
28
|
+
return plugin ? [{
|
|
29
|
+
target,
|
|
30
|
+
plugin
|
|
31
|
+
}] : [];
|
|
32
|
+
});
|
|
33
|
+
if (found.length === 1) return { target: found[0].target };
|
|
34
|
+
const diagnostic = found.length ? createDiagnostic("UF7006", {
|
|
35
|
+
file,
|
|
36
|
+
span: NO_SPAN,
|
|
37
|
+
message: `The plugin has no \`target\` option, and the Vite configuration holds the plugins of ${found.map(({ target, plugin }) => `${target} (${plugin})`).join(" and ")}, so it can't choose a target.`,
|
|
38
|
+
help: "Pass `target` to the Unframework plugin."
|
|
39
|
+
}) : createDiagnostic("UF7005", {
|
|
40
|
+
file,
|
|
41
|
+
span: NO_SPAN,
|
|
42
|
+
message: "The plugin has no `target` option, and no plugin in the Vite configuration belongs to a framework it knows.",
|
|
43
|
+
help: "Add the framework's Vite plugin to the configuration, or pass `target` to the Unframework plugin."
|
|
44
|
+
});
|
|
45
|
+
return { error: formatDiagnostic(diagnostic, void 0) };
|
|
46
|
+
}
|
|
47
|
+
/** plugin-vue's `api`, found by the name it registers. */
|
|
48
|
+
const vueApiOf = (plugins) => plugins.find(({ name }) => name === VUE_PLUGIN)?.api;
|
|
49
|
+
/**
|
|
50
|
+
* The UF7010 message for plugin-vue's whitespace option, when it is `"preserve"`: the printer lays
|
|
51
|
+
* a template out for `"condense"`, and the option can't be set per file (ADR-0026).
|
|
52
|
+
*/
|
|
53
|
+
function hostOptionWarning(plugins, file) {
|
|
54
|
+
if (vueApiOf(plugins)?.options?.template?.compilerOptions?.whitespace !== "preserve") return void 0;
|
|
55
|
+
const diagnostic = createDiagnostic("UF7010", {
|
|
56
|
+
file,
|
|
57
|
+
span: NO_SPAN,
|
|
58
|
+
message: "plugin-vue's `template.compilerOptions.whitespace` is \"preserve\", and outputs assume \"condense\", so a component can render different whitespace from the other targets.",
|
|
59
|
+
help: "Remove the option, or set it to \"condense\"."
|
|
60
|
+
});
|
|
61
|
+
return formatDiagnostic(diagnostic, void 0);
|
|
62
|
+
}
|
|
63
|
+
/** The errors that mean a path names no file to read, which TypeScript's `sys` reports as none. */
|
|
64
|
+
const NO_FILE = /* @__PURE__ */ new Set([
|
|
65
|
+
"ENOENT",
|
|
66
|
+
"ENOTDIR",
|
|
67
|
+
"EISDIR"
|
|
68
|
+
]);
|
|
69
|
+
const isNoFile = (error) => error instanceof Error && "code" in error && NO_FILE.has(error.code);
|
|
70
|
+
/**
|
|
71
|
+
* Node's file system, in the shape TypeScript's `sys` gives `compileScript`: a directory is no
|
|
72
|
+
* file, a missing file reads as `undefined` and a path that can't be resolved is its own real path.
|
|
73
|
+
* Any other error, such as a file it may not read, throws.
|
|
74
|
+
*/
|
|
75
|
+
const NODE_SCRIPT_FS = {
|
|
76
|
+
fileExists: (file) => statSync(file, { throwIfNoEntry: false })?.isFile() ?? false,
|
|
77
|
+
readFile(file) {
|
|
78
|
+
try {
|
|
79
|
+
return readFileSync(file, "utf8");
|
|
80
|
+
} catch (error) {
|
|
81
|
+
if (isNoFile(error)) return void 0;
|
|
82
|
+
throw error;
|
|
83
|
+
}
|
|
84
|
+
},
|
|
85
|
+
realpath(file) {
|
|
86
|
+
try {
|
|
87
|
+
return realpathSync(file);
|
|
88
|
+
} catch (error) {
|
|
89
|
+
if (isNoFile(error)) return file;
|
|
90
|
+
throw error;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
/**
|
|
95
|
+
* Gives plugin-vue Node's file system for imported types, unless the app gave its own (ADR-0082).
|
|
96
|
+
* Vue's `compileScript` reads a props type imported from another module through `script.fs`, or
|
|
97
|
+
* through TypeScript's `sys` without one, and TypeScript 7 doesn't export `sys`. plugin-vue passes
|
|
98
|
+
* no `fs`, so on TypeScript 7 every component whose props type comes from another module fails to
|
|
99
|
+
* compile: the outputs, and the app's own `.vue` files too. The options are replaced through
|
|
100
|
+
* plugin-vue's setter, never changed in place, so the app's own object stays as it wrote it.
|
|
101
|
+
* plugin-vue's `configResolved` rebuilds its options from the current ones, so the fill holds
|
|
102
|
+
* whichever of the two plugins' hooks runs first.
|
|
103
|
+
*/
|
|
104
|
+
function fillVueScriptFs(plugins) {
|
|
105
|
+
const api = vueApiOf(plugins);
|
|
106
|
+
const options = api?.options;
|
|
107
|
+
if (!api || !options || options.script?.fs != null) return;
|
|
108
|
+
api.options = {
|
|
109
|
+
...options,
|
|
110
|
+
script: {
|
|
111
|
+
...options.script,
|
|
112
|
+
fs: NODE_SCRIPT_FS
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
//#endregion
|
|
117
|
+
export { NODE_SCRIPT_FS, detectTarget, fillVueScriptFs, hostOptionWarning };
|
package/dist/esbuild.js
ADDED
package/dist/farm.d.ts
ADDED
package/dist/farm.js
ADDED
package/dist/ids.d.ts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { TargetName } from "@unframework.dev/compiler";
|
|
2
|
+
import { Target } from "@unframework.dev/codegen";
|
|
3
|
+
//#region src/ids.d.ts
|
|
4
|
+
/** The extension of an authored component file. */
|
|
5
|
+
export declare const SOURCE_EXTENSION = ".uf.tsx";
|
|
6
|
+
/**
|
|
7
|
+
* What each built-in target appends to the absolute `.uf.tsx` path to form its module ids.
|
|
8
|
+
* React, Solid and Qwik keep the `.tsx` id, which their plugins claim. Vue, Svelte and Astro
|
|
9
|
+
* claim `.vue`, `.svelte` and `.astro`. Angular output is TypeScript, and Analog only claims
|
|
10
|
+
* `.ts` (`/\.[cm]?ts(?![a-z])/`), so its ids end in `.uf.tsx.ts`.
|
|
11
|
+
*/
|
|
12
|
+
export declare const ID_SUFFIXES: Readonly<Record<TargetName, string>>;
|
|
13
|
+
/**
|
|
14
|
+
* What each built-in target appends to the absolute `.uf.ts` path to form the id of a module of
|
|
15
|
+
* composables (ADR-0069). A composable compiles to a plain TypeScript module on React, Vue, Solid,
|
|
16
|
+
* Astro and Qwik, which Vite's own transform and Qwik's optimizer claim under the `.uf.ts` id.
|
|
17
|
+
* vite-plugin-svelte compiles runes only in a `.svelte.ts` module, and Angular's `ngtscVirtual`
|
|
18
|
+
* collects `<file>.ts` ids, so their ids end in `.uf.ts.svelte.ts` and `.uf.ts.ts`.
|
|
19
|
+
*/
|
|
20
|
+
export declare const COMPOSABLE_ID_SUFFIXES: Readonly<Record<TargetName, string>>;
|
|
21
|
+
/**
|
|
22
|
+
* The id suffix of a target's modules of composables: a built-in target's, or `""` for a
|
|
23
|
+
* third-party target, whose composables load as plain TypeScript modules.
|
|
24
|
+
*/
|
|
25
|
+
export declare function composableIdSuffix(target: TargetName | Target): string;
|
|
26
|
+
/** The id of the module of composables `file` (an absolute `.uf.ts` path) for an id suffix. */
|
|
27
|
+
export declare function composableId(file: string, suffix: string, query?: string): string;
|
|
28
|
+
/**
|
|
29
|
+
* Takes the id of a module of composables for an id suffix apart, or returns `undefined` when the
|
|
30
|
+
* id is not one.
|
|
31
|
+
*/
|
|
32
|
+
export declare function parseComposableId(id: string, suffix: string): ModuleRequest | undefined;
|
|
33
|
+
/**
|
|
34
|
+
* The prefix of the id a `.uf.tsx` import resolves to during Vite's dependency scan. The
|
|
35
|
+
* scanner reads whatever id it gets from disk with its own loaders, so a module id that only
|
|
36
|
+
* `load` can produce fails the whole scan, and the authored file would expose its compile-time
|
|
37
|
+
* imports (`@uxfront/unframework`, or a rejected framework import) as dependencies to pre-bundle. The
|
|
38
|
+
* scanner externalises ids that contain `\0` and never loads them. Each target's toolchain
|
|
39
|
+
* lists the runtime imports of its compiled output in `optimizeDeps.include` instead.
|
|
40
|
+
*/
|
|
41
|
+
export declare const SCAN_ID_PREFIX = "\0uf-scan:";
|
|
42
|
+
/**
|
|
43
|
+
* The id suffix for a target: `extension` when given, otherwise the built-in target's suffix.
|
|
44
|
+
* A third-party target has no built-in suffix, so it must name the extension its framework's
|
|
45
|
+
* Vite plugin claims.
|
|
46
|
+
*/
|
|
47
|
+
export declare function idSuffix(target: TargetName | Target, extension?: string): string;
|
|
48
|
+
/** The module id that loads `file` (an absolute `.uf.tsx` path) for an id suffix. */
|
|
49
|
+
export declare function moduleId(file: string, suffix: string, query?: string): string;
|
|
50
|
+
/** A module id taken apart. */
|
|
51
|
+
export interface ModuleRequest {
|
|
52
|
+
/** The authored `.uf.tsx` file, or `.uf.ts` file for a module of composables. */
|
|
53
|
+
file: string;
|
|
54
|
+
/** The query, with its `?`, or `""`. */
|
|
55
|
+
query: string;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Takes a module id for an id suffix apart, or returns `undefined` when the id is not one.
|
|
59
|
+
* The query is not part of the match, so `X.uf.tsx.vue?vue&type=style&index=0&lang.css` is
|
|
60
|
+
* recognised; whether a query belongs to the plugin is the caller's decision.
|
|
61
|
+
*/
|
|
62
|
+
export declare function parseModuleId(id: string, suffix: string): ModuleRequest | undefined;
|
|
63
|
+
//#endregion
|
package/dist/ids.js
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import { posix } from "node:path";
|
|
2
|
+
import { TARGET_NAMES, builtinTargets } from "@unframework.dev/compiler";
|
|
3
|
+
//#region src/ids.ts
|
|
4
|
+
/** The extension of an authored component file. */
|
|
5
|
+
const SOURCE_EXTENSION = ".uf.tsx";
|
|
6
|
+
const escapeRegExp = (text) => text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
7
|
+
/**
|
|
8
|
+
* What `resolveId` looks at, with any query: the module ids of `suffix` coming back, and every
|
|
9
|
+
* specifier that can name a `.uf.tsx` file. That is `X.uf.tsx` itself and the forms TypeScript
|
|
10
|
+
* and Vite's resolver map to it: `X.uf` (extensionless, `moduleResolution: "bundler"`) and
|
|
11
|
+
* `X.uf.js` or `X.uf.jsx` (what `"nodenext"` requires). Whether a specifier is a component is
|
|
12
|
+
* decided by the file it resolves to, never by its spelling: `X.uf` may be a composable, `X.uf.ts`.
|
|
13
|
+
*/
|
|
14
|
+
function specifierFilter(suffix) {
|
|
15
|
+
return new RegExp(`\\.uf(?:\\.tsx(?:${escapeRegExp(suffix)})?|\\.jsx?)?(?:\\?.*)?$`);
|
|
16
|
+
}
|
|
17
|
+
/** The module ids of `suffix`, with any query. */
|
|
18
|
+
function moduleIdFilter(suffix) {
|
|
19
|
+
return new RegExp(`${escapeRegExp(`${SOURCE_EXTENSION}${suffix}`)}(?:\\?.*)?$`);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* What each built-in target appends to the absolute `.uf.tsx` path to form its module ids.
|
|
23
|
+
* React, Solid and Qwik keep the `.tsx` id, which their plugins claim. Vue, Svelte and Astro
|
|
24
|
+
* claim `.vue`, `.svelte` and `.astro`. Angular output is TypeScript, and Analog only claims
|
|
25
|
+
* `.ts` (`/\.[cm]?ts(?![a-z])/`), so its ids end in `.uf.tsx.ts`.
|
|
26
|
+
*/
|
|
27
|
+
const ID_SUFFIXES = {
|
|
28
|
+
react: "",
|
|
29
|
+
vue: ".vue",
|
|
30
|
+
svelte: ".svelte",
|
|
31
|
+
solid: "",
|
|
32
|
+
angular: ".ts",
|
|
33
|
+
qwik: "",
|
|
34
|
+
astro: ".astro"
|
|
35
|
+
};
|
|
36
|
+
/** The extension of an authored module of composables (ADR-0069). */
|
|
37
|
+
const COMPOSABLE_EXTENSION = ".uf.ts";
|
|
38
|
+
/**
|
|
39
|
+
* What each built-in target appends to the absolute `.uf.ts` path to form the id of a module of
|
|
40
|
+
* composables (ADR-0069). A composable compiles to a plain TypeScript module on React, Vue, Solid,
|
|
41
|
+
* Astro and Qwik, which Vite's own transform and Qwik's optimizer claim under the `.uf.ts` id.
|
|
42
|
+
* vite-plugin-svelte compiles runes only in a `.svelte.ts` module, and Angular's `ngtscVirtual`
|
|
43
|
+
* collects `<file>.ts` ids, so their ids end in `.uf.ts.svelte.ts` and `.uf.ts.ts`.
|
|
44
|
+
*/
|
|
45
|
+
const COMPOSABLE_ID_SUFFIXES = {
|
|
46
|
+
react: "",
|
|
47
|
+
vue: "",
|
|
48
|
+
svelte: ".svelte.ts",
|
|
49
|
+
solid: "",
|
|
50
|
+
angular: ".ts",
|
|
51
|
+
qwik: "",
|
|
52
|
+
astro: ""
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* The id suffix of a target's modules of composables: a built-in target's, or `""` for a
|
|
56
|
+
* third-party target, whose composables load as plain TypeScript modules.
|
|
57
|
+
*/
|
|
58
|
+
function composableIdSuffix(target) {
|
|
59
|
+
const builtin = typeof target === "string" ? target : TARGET_NAMES.find((name) => builtinTargets[name] === target);
|
|
60
|
+
return builtin !== void 0 && Object.hasOwn(COMPOSABLE_ID_SUFFIXES, builtin) ? COMPOSABLE_ID_SUFFIXES[builtin] : "";
|
|
61
|
+
}
|
|
62
|
+
/** A `.uf.ts` specifier, or a module id of composables for `suffix` coming back, with any query. */
|
|
63
|
+
function composableFilter(suffix) {
|
|
64
|
+
const tail = suffix ? `(?:${escapeRegExp(suffix)})?` : "";
|
|
65
|
+
return new RegExp(`${escapeRegExp(COMPOSABLE_EXTENSION)}${tail}(?:\\?.*)?$`);
|
|
66
|
+
}
|
|
67
|
+
/** The id of the module of composables `file` (an absolute `.uf.ts` path) for an id suffix. */
|
|
68
|
+
function composableId(file, suffix, query = "") {
|
|
69
|
+
return `${file}${suffix}${query}`;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Takes the id of a module of composables for an id suffix apart, or returns `undefined` when the
|
|
73
|
+
* id is not one.
|
|
74
|
+
*/
|
|
75
|
+
function parseComposableId(id, suffix) {
|
|
76
|
+
const index = id.indexOf("?");
|
|
77
|
+
const path = index === -1 ? id : id.slice(0, index);
|
|
78
|
+
if (!path.endsWith(`.uf.ts${suffix}`)) return void 0;
|
|
79
|
+
return {
|
|
80
|
+
file: path.slice(0, path.length - suffix.length),
|
|
81
|
+
query: index === -1 ? "" : id.slice(index)
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* The prefix of the id a `.uf.tsx` import resolves to during Vite's dependency scan. The
|
|
86
|
+
* scanner reads whatever id it gets from disk with its own loaders, so a module id that only
|
|
87
|
+
* `load` can produce fails the whole scan, and the authored file would expose its compile-time
|
|
88
|
+
* imports (`@uxfront/unframework`, or a rejected framework import) as dependencies to pre-bundle. The
|
|
89
|
+
* scanner externalises ids that contain `\0` and never loads them. Each target's toolchain
|
|
90
|
+
* lists the runtime imports of its compiled output in `optimizeDeps.include` instead.
|
|
91
|
+
*/
|
|
92
|
+
const SCAN_ID_PREFIX = "\0uf-scan:";
|
|
93
|
+
const EXTENSION = /^(?:\.[\w-]+)*$/;
|
|
94
|
+
/**
|
|
95
|
+
* The id suffix for a target: `extension` when given, otherwise the built-in target's suffix.
|
|
96
|
+
* A third-party target has no built-in suffix, so it must name the extension its framework's
|
|
97
|
+
* Vite plugin claims.
|
|
98
|
+
*/
|
|
99
|
+
function idSuffix(target, extension) {
|
|
100
|
+
if (extension !== void 0) {
|
|
101
|
+
if (!EXTENSION.test(extension)) throw new TypeError(`The extension ${JSON.stringify(extension)} is not a file extension: use "" to keep the .uf.tsx id, or a suffix such as ".vue".`);
|
|
102
|
+
return extension;
|
|
103
|
+
}
|
|
104
|
+
if (typeof target === "string") {
|
|
105
|
+
if (!Object.hasOwn(ID_SUFFIXES, target)) throw new TypeError(`Unknown target "${target}".`);
|
|
106
|
+
return ID_SUFFIXES[target];
|
|
107
|
+
}
|
|
108
|
+
const builtin = TARGET_NAMES.find((name) => builtinTargets[name] === target);
|
|
109
|
+
if (builtin === void 0) throw new TypeError(`The "${target.name}" target is not built in, so its module ids need an explicit \`extension\`: the file extension its framework's Vite plugin claims, such as ".vue", or "" to keep the .uf.tsx id.`);
|
|
110
|
+
return ID_SUFFIXES[builtin];
|
|
111
|
+
}
|
|
112
|
+
/** The module id that loads `file` (an absolute `.uf.tsx` path) for an id suffix. */
|
|
113
|
+
function moduleId(file, suffix, query = "") {
|
|
114
|
+
return `${file}${suffix}${query}`;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Takes a module id for an id suffix apart, or returns `undefined` when the id is not one.
|
|
118
|
+
* The query is not part of the match, so `X.uf.tsx.vue?vue&type=style&index=0&lang.css` is
|
|
119
|
+
* recognised; whether a query belongs to the plugin is the caller's decision.
|
|
120
|
+
*/
|
|
121
|
+
function parseModuleId(id, suffix) {
|
|
122
|
+
const index = id.indexOf("?");
|
|
123
|
+
const path = index === -1 ? id : id.slice(0, index);
|
|
124
|
+
if (!path.endsWith(`.uf.tsx${suffix}`)) return void 0;
|
|
125
|
+
return {
|
|
126
|
+
file: path.slice(0, path.length - suffix.length),
|
|
127
|
+
query: index === -1 ? "" : id.slice(index)
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
const DRIVE_PATH = /^[A-Za-z]:\//;
|
|
131
|
+
/**
|
|
132
|
+
* The absolute ids a module id that comes back to `resolveId` (its path, without the query) may
|
|
133
|
+
* stand for, most likely first; the caller keeps the first whose `.uf.tsx` file exists.
|
|
134
|
+
*
|
|
135
|
+
* - `./X.uf.tsx.vue` and `../X.uf.tsx.vue` are relative to their importer.
|
|
136
|
+
* - `/X.uf.tsx.vue` is a root-relative URL from the browser, or, outside the root, an absolute id.
|
|
137
|
+
* - `C:/X.uf.tsx.vue` is an absolute id on Windows.
|
|
138
|
+
*
|
|
139
|
+
* Paths join with forward slashes, as Vite writes ids on every platform (and its root), so one
|
|
140
|
+
* module never gets a second id that differs only in its separators. A bare specifier names a
|
|
141
|
+
* package, never one of these ids.
|
|
142
|
+
*/
|
|
143
|
+
function moduleIdCandidates(path, root, importer) {
|
|
144
|
+
if (path.startsWith("./") || path.startsWith("../")) {
|
|
145
|
+
if (importer === void 0) return [];
|
|
146
|
+
const index = importer.indexOf("?");
|
|
147
|
+
const importerPath = index === -1 ? importer : importer.slice(0, index);
|
|
148
|
+
return [posix.join(posix.dirname(importerPath), path)];
|
|
149
|
+
}
|
|
150
|
+
if (path.startsWith(`${root}/`)) return [path];
|
|
151
|
+
if (path.startsWith("/")) return [posix.join(root, path), path];
|
|
152
|
+
return DRIVE_PATH.test(path) ? [path] : [];
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* A component id (ADR-0053): `<abs>/X.uf.tsx.<output file>`, such as `/src/Card.uf.tsx.CardIcon.vue`,
|
|
156
|
+
* which loads a component of the file other than its main one, under the extension its
|
|
157
|
+
* framework's plugin claims. A main id's suffix is an extension alone (`.vue`), with no dot in it.
|
|
158
|
+
*/
|
|
159
|
+
const COMPONENT_ID = /\.uf\.tsx\.([^/?]+\.[^/.?]+)(?:\?.*)?$/;
|
|
160
|
+
/** What `resolveId` and `load` look at for component ids, with any query. */
|
|
161
|
+
const COMPONENT_ID_FILTER = COMPONENT_ID;
|
|
162
|
+
/** The component id of `output`, a file the target emits for a component of `file`. */
|
|
163
|
+
function componentId(file, output, query = "") {
|
|
164
|
+
return `${file}.${output}${query}`;
|
|
165
|
+
}
|
|
166
|
+
/** A component id taken apart, or `undefined` when the id is not one. */
|
|
167
|
+
function parseComponentId(id) {
|
|
168
|
+
const match = COMPONENT_ID.exec(id);
|
|
169
|
+
if (!match) return void 0;
|
|
170
|
+
const index = id.indexOf("?");
|
|
171
|
+
const path = index === -1 ? id : id.slice(0, index);
|
|
172
|
+
const output = match[1];
|
|
173
|
+
return {
|
|
174
|
+
file: path.slice(0, path.length - output.length - 1),
|
|
175
|
+
output,
|
|
176
|
+
query: index === -1 ? "" : id.slice(index)
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
//#endregion
|
|
180
|
+
export { COMPONENT_ID_FILTER, COMPOSABLE_EXTENSION, COMPOSABLE_ID_SUFFIXES, ID_SUFFIXES, SCAN_ID_PREFIX, SOURCE_EXTENSION, componentId, composableFilter, composableId, composableIdSuffix, idSuffix, moduleId, moduleIdCandidates, moduleIdFilter, parseComponentId, parseComposableId, parseModuleId, specifierFilter };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { CompileEvent, PLUGIN_NAME, UnframeworkApi, UnframeworkOptions, unframeworkUnplugin } from "./plugin.js";
|
|
2
|
+
import { COMPOSABLE_ID_SUFFIXES, ID_SUFFIXES, ModuleRequest, SCAN_ID_PREFIX, SOURCE_EXTENSION, composableId, composableIdSuffix, idSuffix, moduleId, parseComposableId, parseModuleId } from "./ids.js";
|
|
3
|
+
export { COMPOSABLE_ID_SUFFIXES, type CompileEvent, ID_SUFFIXES, type ModuleRequest, PLUGIN_NAME, SCAN_ID_PREFIX, SOURCE_EXTENSION, type UnframeworkApi, type UnframeworkOptions, composableId, composableIdSuffix, idSuffix, moduleId, parseComposableId, parseModuleId, unframeworkUnplugin };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { COMPOSABLE_ID_SUFFIXES, ID_SUFFIXES, SCAN_ID_PREFIX, SOURCE_EXTENSION, composableId, composableIdSuffix, idSuffix, moduleId, parseComposableId, parseModuleId } from "./ids.js";
|
|
2
|
+
import { PLUGIN_NAME, unframeworkUnplugin } from "./plugin.js";
|
|
3
|
+
export { COMPOSABLE_ID_SUFFIXES, ID_SUFFIXES, PLUGIN_NAME, SCAN_ID_PREFIX, SOURCE_EXTENSION, composableId, composableIdSuffix, idSuffix, moduleId, parseComposableId, parseModuleId, unframeworkUnplugin };
|
package/dist/plugin.d.ts
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { CompilerPlugin, TargetName, UfModule } from "@unframework.dev/compiler";
|
|
2
|
+
import { Diagnostic } from "@unframework.dev/diagnostics";
|
|
3
|
+
import { UnpluginInstance } from "unplugin";
|
|
4
|
+
import { OutputFile, Target } from "@unframework.dev/codegen";
|
|
5
|
+
//#region src/plugin.d.ts
|
|
6
|
+
/** What {@link UnframeworkOptions.onCompile} receives for every module it loads. */
|
|
7
|
+
export interface CompileEvent {
|
|
8
|
+
/** The module id, such as `/abs/Hello.uf.tsx.vue`. */
|
|
9
|
+
id: string;
|
|
10
|
+
/** The absolute path of the authored `.uf.tsx` file, or `.uf.ts` file of composables. */
|
|
11
|
+
file: string;
|
|
12
|
+
/** The target's name. */
|
|
13
|
+
target: string;
|
|
14
|
+
/** Every file the target emitted for the module: what the golden files hold. */
|
|
15
|
+
files: OutputFile[];
|
|
16
|
+
/** Every diagnostic of the compile, sorted. */
|
|
17
|
+
diagnostics: Diagnostic[];
|
|
18
|
+
/**
|
|
19
|
+
* The compile's IR, absent when the file did not parse: what the module's components declare,
|
|
20
|
+
* such as the events a test listens to (the harness's `ufComponentEvents`).
|
|
21
|
+
*/
|
|
22
|
+
ir?: UfModule;
|
|
23
|
+
}
|
|
24
|
+
/** Options of the unframework plugin. */
|
|
25
|
+
export interface UnframeworkOptions {
|
|
26
|
+
/**
|
|
27
|
+
* The framework to compile to: a built-in target's name, or a third-party target. On Vite it may
|
|
28
|
+
* be left out, and the framework's plugin in the configuration decides (ADR-0075). Every other
|
|
29
|
+
* bundler needs it.
|
|
30
|
+
*/
|
|
31
|
+
target?: TargetName | Target;
|
|
32
|
+
/**
|
|
33
|
+
* What module ids append to the `.uf.tsx` path: the extension the framework's own Vite plugin
|
|
34
|
+
* claims, such as `".vue"`, or `""` to keep the `.uf.tsx` id. Defaults to the built-in
|
|
35
|
+
* target's suffix (`ID_SUFFIXES`); a third-party target must set it.
|
|
36
|
+
*/
|
|
37
|
+
extension?: string;
|
|
38
|
+
/**
|
|
39
|
+
* Called each time a module loads, with the compile it loads from, errors included. Returning
|
|
40
|
+
* a string fails that module with the string as its error (the harness's golden guard): the
|
|
41
|
+
* module still goes through the framework's plugin, and its final code becomes
|
|
42
|
+
* `throw new Error(message)`, so the message reaches the test report. A compile that has
|
|
43
|
+
* errors fails anyway, with the message after its diagnostics.
|
|
44
|
+
*/
|
|
45
|
+
onCompile?(event: CompileEvent): string | void | Promise<string | void>;
|
|
46
|
+
/** Compiler plugins, passed to `compile()` (the harness's canaries). */
|
|
47
|
+
plugins?: readonly CompilerPlugin[];
|
|
48
|
+
/** Formats the output with oxfmt. Defaults to `true`, so modules equal the golden files. */
|
|
49
|
+
format?: boolean;
|
|
50
|
+
/**
|
|
51
|
+
* The specifier prefixes whose imports pass through to every output as written, passed to
|
|
52
|
+
* `compile()` (ADR-0064): `["virtual:"]` when absent. The plugin never resolves such an import:
|
|
53
|
+
* the plugin that registers the module does, such as a styling engine's.
|
|
54
|
+
*/
|
|
55
|
+
passThrough?: readonly string[];
|
|
56
|
+
}
|
|
57
|
+
/** What the pre plugin exposes as `api`, for other plugins that need the compiled sources. */
|
|
58
|
+
export interface UnframeworkApi {
|
|
59
|
+
/**
|
|
60
|
+
* The target source of the last load of a module id that finished, in any environment, or
|
|
61
|
+
* `undefined` when the id has not loaded or that load failed. Angular's `ngtscVirtual` reads
|
|
62
|
+
* child components with it.
|
|
63
|
+
*/
|
|
64
|
+
getCompiled(id: string): string | undefined;
|
|
65
|
+
/**
|
|
66
|
+
* The scoped sheet this target's last finished load of the `.uf.tsx` file emitted (ADR-0063),
|
|
67
|
+
* after the plugins' `output` hooks, or `undefined` when no load of it finished or it imports
|
|
68
|
+
* no stylesheet. Angular's `ngtscVirtual` hands it to ngtsc, which reads a `styleUrl` itself:
|
|
69
|
+
* Vite never loads the sheet there.
|
|
70
|
+
*/
|
|
71
|
+
getSheet(file: string): string | undefined;
|
|
72
|
+
}
|
|
73
|
+
/** The name of the pre plugin, by which other plugins find its {@link UnframeworkApi}. */
|
|
74
|
+
export declare const PLUGIN_NAME = "unframework";
|
|
75
|
+
/** The unframework plugin for every bundler unplugin offers. Only Vite runs end to end. */
|
|
76
|
+
export declare const unframeworkUnplugin: UnpluginInstance<UnframeworkOptions | undefined, true>;
|
|
77
|
+
type Instance = UnpluginInstance<UnframeworkOptions | undefined, true>;
|
|
78
|
+
export declare const bun: Instance["bun"];
|
|
79
|
+
export declare const esbuild: Instance["esbuild"];
|
|
80
|
+
export declare const farm: Instance["farm"];
|
|
81
|
+
export declare const rolldown: Instance["rolldown"];
|
|
82
|
+
export declare const rollup: Instance["rollup"];
|
|
83
|
+
export declare const rsbuild: Instance["rsbuild"];
|
|
84
|
+
export declare const rspack: Instance["rspack"];
|
|
85
|
+
export declare const unloader: Instance["unloader"];
|
|
86
|
+
export declare const webpack: Instance["webpack"];
|
|
87
|
+
//#endregion
|