@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/plugin.js
ADDED
|
@@ -0,0 +1,858 @@
|
|
|
1
|
+
import { 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 } from "./ids.js";
|
|
2
|
+
import { assembleModule } from "./assemble.js";
|
|
3
|
+
import { chainQwikSegments, chainSvelteOnce } from "./chain.js";
|
|
4
|
+
import { detectTarget, fillVueScriptFs, hostOptionWarning } from "./detect.js";
|
|
5
|
+
import { basename, extname, isAbsolute, posix, relative, sep } from "node:path";
|
|
6
|
+
import { checkPassThrough, createProject, createSyntacticOracle, resolveTarget } from "@unframework.dev/compiler";
|
|
7
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
8
|
+
import { readFile } from "node:fs/promises";
|
|
9
|
+
import { formatDiagnostic, formatDiagnostics } from "@unframework.dev/diagnostics";
|
|
10
|
+
import { createUnplugin } from "unplugin";
|
|
11
|
+
//#region src/plugin.ts
|
|
12
|
+
/** A Qwik optimizer segment of a module: `<module id>_<symbol>.js`, its symbol a name. */
|
|
13
|
+
const SEGMENT = /^(.+\.uf\.tsx(?:\.[^/_]+)?)_[\w$]+\.js$/;
|
|
14
|
+
/** The name of the pre plugin, by which other plugins find its {@link UnframeworkApi}. */
|
|
15
|
+
const PLUGIN_NAME = "unframework";
|
|
16
|
+
const VITE_FILE_QUERY = /[?&](?:worker|sharedworker|raw|url)\b/;
|
|
17
|
+
/** A relative specifier: an output's import of another output (`./Field.vue`, ADR-0053). */
|
|
18
|
+
const RELATIVE = /^\.\.?\//;
|
|
19
|
+
/**
|
|
20
|
+
* What a `.uf.tsx` path appends to name its scoped sheet, `X.uf.tsx.css` (ADR-0063): every
|
|
21
|
+
* target's output imports it as `./X.uf.tsx.css`, never the author's `./X.css`.
|
|
22
|
+
*/
|
|
23
|
+
const SHEET_SUFFIX = ".css";
|
|
24
|
+
const SHEET_ID_FILTER = moduleIdFilter(SHEET_SUFFIX);
|
|
25
|
+
/** How a project names a package it read types from (ADR-0071): `pkg:<name>`. */
|
|
26
|
+
const PACKAGE = "pkg:";
|
|
27
|
+
/** The output files a script target joins into one module (`.tsx`, `.ts`). */
|
|
28
|
+
const SCRIPT = /^\.tsx?$/;
|
|
29
|
+
/**
|
|
30
|
+
* A specifier that names a package or a subpath import rather than a path or a scheme: `pkg`,
|
|
31
|
+
* `@scope/pkg/source/Button.uf.tsx`, `#ui/Button`. Which file it names only its resolution can say
|
|
32
|
+
* (ADR-0075).
|
|
33
|
+
*/
|
|
34
|
+
const BARE = /^[\w@#][^:]*$/;
|
|
35
|
+
/** The bundlers whose hook context resolves a bare specifier (see `fileOf`). */
|
|
36
|
+
const RESOLVES_BARE = [
|
|
37
|
+
"vite",
|
|
38
|
+
"rollup",
|
|
39
|
+
"rolldown"
|
|
40
|
+
];
|
|
41
|
+
const NODE_MODULES = "/node_modules/";
|
|
42
|
+
/**
|
|
43
|
+
* A dev server's own allow list, applied to the files our ids name (C21). Vite refuses a request
|
|
44
|
+
* for a file outside `server.fs.allow` only when it reads the file itself, and `load` reads and
|
|
45
|
+
* compiles it here, by whichever id names it: a module id, a component id, a sheet id, or the
|
|
46
|
+
* `.uf.tsx` path itself. So `load` asks Vite's own `isFileLoadingAllowed`, which also honours
|
|
47
|
+
* `server.fs.strict` and `server.fs.deny`, about the file and about the id without its query,
|
|
48
|
+
* since Vite records a module a served module imports under the id it imported
|
|
49
|
+
* (`safeModulePaths`), suffix and all. A refusal is Vite's own `ERR_DENIED_ID` error, named after
|
|
50
|
+
* the file, which its middleware answers with a 403, as it answers a request for any other file.
|
|
51
|
+
*/
|
|
52
|
+
function accessCheck(config) {
|
|
53
|
+
const allowed = import("vite").then(({ isFileLoadingAllowed }) => isFileLoadingAllowed);
|
|
54
|
+
return async (file, id) => {
|
|
55
|
+
const isAllowed = await allowed;
|
|
56
|
+
if (isAllowed(config, file) || isAllowed(config, withoutQuery(id))) return;
|
|
57
|
+
throw Object.assign(/* @__PURE__ */ new Error(`The request id "${file}" is outside of Vite's serving allow list.`), {
|
|
58
|
+
code: "ERR_DENIED_ID",
|
|
59
|
+
id: file
|
|
60
|
+
});
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
const resolves = (context) => "resolve" in context;
|
|
64
|
+
const hasEnvironment = (context) => "environment" in context;
|
|
65
|
+
const isSource = (file) => file.endsWith(".uf.tsx") || file.endsWith(".uf.ts");
|
|
66
|
+
/** A resolved id without its query: Vite versions a file of `node_modules` with `?v=`. */
|
|
67
|
+
const withoutQuery = (id) => {
|
|
68
|
+
const index = id.indexOf("?");
|
|
69
|
+
return index === -1 ? id : id.slice(0, index);
|
|
70
|
+
};
|
|
71
|
+
/** A path with forward slashes, as ids and the project's root are written on every platform. */
|
|
72
|
+
const toPosix = (path) => path.split(sep).join("/");
|
|
73
|
+
/**
|
|
74
|
+
* The hooks are unplugin's generic ones, which it maps onto each bundler it offers (ADR-0075). Only
|
|
75
|
+
* what Vite alone has sits under `vite:`: its config, its dependency scan, its environments and
|
|
76
|
+
* `api`. Only Vite runs end to end: the rest are trusted to work by default.
|
|
77
|
+
*/
|
|
78
|
+
const factory = (options = {}, meta) => {
|
|
79
|
+
if (options.target === void 0 && meta.framework !== "vite") throw new TypeError(`The plugin has no \`target\` option. Only Vite detects a target, from its framework's plugin: pass \`target\` on ${meta.framework}.`);
|
|
80
|
+
if (options.passThrough) checkPassThrough(options.passThrough);
|
|
81
|
+
let target;
|
|
82
|
+
let suffix;
|
|
83
|
+
let composableSuffix;
|
|
84
|
+
let composableIds;
|
|
85
|
+
let extension;
|
|
86
|
+
let joins;
|
|
87
|
+
let subRequest;
|
|
88
|
+
const selectTarget = (selected) => {
|
|
89
|
+
target = resolveTarget(selected);
|
|
90
|
+
suffix = idSuffix(selected, options.extension);
|
|
91
|
+
composableSuffix = composableIdSuffix(selected);
|
|
92
|
+
composableIds = composableFilter(composableSuffix);
|
|
93
|
+
extension = extname(`${SOURCE_EXTENSION}${suffix}`);
|
|
94
|
+
joins = SCRIPT.test(extension);
|
|
95
|
+
subRequest = new RegExp(`[?&]${extension.slice(1)}(?:[&=]|$)`);
|
|
96
|
+
};
|
|
97
|
+
if (options.target !== void 0) selectTarget(options.target);
|
|
98
|
+
const composableRequest = (id) => parseComposableId(id, composableSuffix);
|
|
99
|
+
const suffixes = options.target !== void 0 ? [suffix] : options.extension !== void 0 ? [idSuffix("vue", options.extension)] : [...new Set(Object.values(ID_SUFFIXES))];
|
|
100
|
+
const composableFilters = (options.target !== void 0 ? [composableSuffix] : [...new Set(Object.values(COMPOSABLE_ID_SUFFIXES))]).map(composableFilter);
|
|
101
|
+
const bareSpecifiers = RESOLVES_BARE.includes(meta.framework);
|
|
102
|
+
const moduleIds = [
|
|
103
|
+
...suffixes.map(moduleIdFilter),
|
|
104
|
+
COMPONENT_ID_FILTER,
|
|
105
|
+
...composableFilters
|
|
106
|
+
];
|
|
107
|
+
const loadIds = [
|
|
108
|
+
...moduleIds,
|
|
109
|
+
moduleIdFilter(""),
|
|
110
|
+
SHEET_ID_FILTER
|
|
111
|
+
];
|
|
112
|
+
let root = toPosix(process.cwd());
|
|
113
|
+
const seen = /* @__PURE__ */ new Map();
|
|
114
|
+
const read = async (path) => {
|
|
115
|
+
const text = await readFile(path, "utf8").catch(() => void 0);
|
|
116
|
+
if (!seen.has(path)) seen.set(path, text);
|
|
117
|
+
return text;
|
|
118
|
+
};
|
|
119
|
+
const manifests = /* @__PURE__ */ new Map();
|
|
120
|
+
const packageAt = (directory) => {
|
|
121
|
+
if (manifests.has(directory)) return manifests.get(directory);
|
|
122
|
+
let found;
|
|
123
|
+
try {
|
|
124
|
+
const manifest = JSON.parse(readFileSync(`${directory}/package.json`, "utf8"));
|
|
125
|
+
if (manifest && typeof manifest === "object" && "name" in manifest && typeof manifest.name === "string") found = {
|
|
126
|
+
name: manifest.name,
|
|
127
|
+
directory
|
|
128
|
+
};
|
|
129
|
+
} catch {}
|
|
130
|
+
const parent = posix.dirname(directory);
|
|
131
|
+
if (found === void 0 && parent !== directory) found = packageAt(parent);
|
|
132
|
+
manifests.set(directory, found);
|
|
133
|
+
return found;
|
|
134
|
+
};
|
|
135
|
+
const packageOf = (directory) => packageAt(directory)?.name;
|
|
136
|
+
/** A file's path below the root, or the whole path for a file outside it. */
|
|
137
|
+
const belowRoot = (file) => file.startsWith(`${root}/`) ? file.slice(root.length) : file;
|
|
138
|
+
/**
|
|
139
|
+
* The root of the graph a file belongs to (ADR-0075). A file of the project belongs to the
|
|
140
|
+
* project's. A package's belongs to the package's, so its file names, and what hashes them, are
|
|
141
|
+
* the same wherever it is installed: by the path for a file of `node_modules`, whose package may
|
|
142
|
+
* be published under another name, or by its manifest for a linked one outside the root.
|
|
143
|
+
*/
|
|
144
|
+
const graphRootOf = (file) => {
|
|
145
|
+
const below = belowRoot(file);
|
|
146
|
+
const index = below.lastIndexOf(NODE_MODULES);
|
|
147
|
+
if (index !== -1) {
|
|
148
|
+
const start = index + 14;
|
|
149
|
+
const [first, second, third] = below.slice(start).split("/");
|
|
150
|
+
const depth = first?.startsWith("@") ? [
|
|
151
|
+
first,
|
|
152
|
+
second,
|
|
153
|
+
third
|
|
154
|
+
] : [first, second];
|
|
155
|
+
if (depth.every((part) => part !== void 0)) {
|
|
156
|
+
const name = depth.slice(0, -1).join("/");
|
|
157
|
+
return `${file.slice(0, file.length - below.length + start)}${name}`;
|
|
158
|
+
}
|
|
159
|
+
return root;
|
|
160
|
+
}
|
|
161
|
+
return below === file ? packageAt(posix.dirname(file))?.directory ?? root : root;
|
|
162
|
+
};
|
|
163
|
+
const graphs = /* @__PURE__ */ new Map();
|
|
164
|
+
const graphOf = (file) => {
|
|
165
|
+
const graphRoot = graphRootOf(file);
|
|
166
|
+
let graph = graphs.get(graphRoot);
|
|
167
|
+
if (!graph) {
|
|
168
|
+
graph = {
|
|
169
|
+
root: graphRoot,
|
|
170
|
+
project: createProject({
|
|
171
|
+
root: graphRoot,
|
|
172
|
+
readFile: read,
|
|
173
|
+
typeOracle: createSyntacticOracle({
|
|
174
|
+
root: graphRoot,
|
|
175
|
+
readFile: read
|
|
176
|
+
}),
|
|
177
|
+
targets: [target],
|
|
178
|
+
format: options.format !== false,
|
|
179
|
+
sourceMap: true,
|
|
180
|
+
plugins: options.plugins,
|
|
181
|
+
...options.passThrough ? { passThrough: options.passThrough } : {}
|
|
182
|
+
}),
|
|
183
|
+
reachedPackages: /* @__PURE__ */ new Set()
|
|
184
|
+
};
|
|
185
|
+
graphs.set(graphRoot, graph);
|
|
186
|
+
}
|
|
187
|
+
return graph;
|
|
188
|
+
};
|
|
189
|
+
/** A file's graph and its name there: relative to the graph's root, with forward slashes. */
|
|
190
|
+
const locate = (file) => {
|
|
191
|
+
const graph = graphOf(file);
|
|
192
|
+
return {
|
|
193
|
+
graph,
|
|
194
|
+
filename: toPosix(relative(graph.root, file))
|
|
195
|
+
};
|
|
196
|
+
};
|
|
197
|
+
let queue = Promise.resolve();
|
|
198
|
+
const exclusive = (task) => {
|
|
199
|
+
const run = queue.then(task);
|
|
200
|
+
queue = run.catch(() => void 0);
|
|
201
|
+
return run;
|
|
202
|
+
};
|
|
203
|
+
const compiled = /* @__PURE__ */ new Map();
|
|
204
|
+
const loadMaps = /* @__PURE__ */ new Map();
|
|
205
|
+
const sheets = /* @__PURE__ */ new Map();
|
|
206
|
+
const failures = /* @__PURE__ */ new WeakMap();
|
|
207
|
+
const failuresIn = (environment) => {
|
|
208
|
+
let failed = failures.get(environment);
|
|
209
|
+
if (!failed) {
|
|
210
|
+
failed = /* @__PURE__ */ new Map();
|
|
211
|
+
failures.set(environment, failed);
|
|
212
|
+
}
|
|
213
|
+
return failed;
|
|
214
|
+
};
|
|
215
|
+
const sharedFailures = /* @__PURE__ */ new Map();
|
|
216
|
+
/** A file's name relative to the project's root, with forward slashes, for a message. */
|
|
217
|
+
const filenameOf = (file) => toPosix(relative(root, file));
|
|
218
|
+
/** A compile's errors, which name a package's file by its own root, so they say where that is. */
|
|
219
|
+
const formatErrors = (graph, filename, errors, source) => {
|
|
220
|
+
const message = formatDiagnostics(errors, { [filename]: source });
|
|
221
|
+
return graph.root === root ? message : `${message}\n\nThe file is in the package at ${filenameOf(graph.root)}.`;
|
|
222
|
+
};
|
|
223
|
+
/**
|
|
224
|
+
* A component id taken apart, unless the id is a module id: a suffix of several extensions
|
|
225
|
+
* (`.client.vue`) reads as a component id too, and a module id wins.
|
|
226
|
+
*/
|
|
227
|
+
const componentRequest = (id) => parseModuleId(id, suffix) ? void 0 : parseComponentId(id);
|
|
228
|
+
/**
|
|
229
|
+
* The module of ours that a framework's plugin split `id` off, when it is such a module: Qwik's
|
|
230
|
+
* optimizer moves a component's body, with the imports it reads, into a segment named after the
|
|
231
|
+
* module, `<module id>_<symbol>.js` (`Form.uf.tsx_Form_component_<hash>.js`). Its imports are
|
|
232
|
+
* the module's, so they resolve as the module's do (ADR-0053). Only that shape counts: a file
|
|
233
|
+
* that merely starts with a module's name (`Form.uf.tsx_helpers.ts`) is no segment.
|
|
234
|
+
*/
|
|
235
|
+
const splitFrom = (id) => {
|
|
236
|
+
const index = id.indexOf("?");
|
|
237
|
+
const segment = SEGMENT.exec(index === -1 ? id : id.slice(0, index));
|
|
238
|
+
return segment ? parseModuleId(segment[1], suffix) ?? componentRequest(segment[1]) : void 0;
|
|
239
|
+
};
|
|
240
|
+
/**
|
|
241
|
+
* What a module's output depends on (ADR-0071): its own file and every file the project's edges
|
|
242
|
+
* reach from it, its children's and its composables' types included, named relative to the
|
|
243
|
+
* graph's root, and the packages they read types from, by name.
|
|
244
|
+
*/
|
|
245
|
+
const reachedFrom = ({ project, reachedPackages }, filename) => {
|
|
246
|
+
const files = /* @__PURE__ */ new Set([filename]);
|
|
247
|
+
const packages = /* @__PURE__ */ new Set();
|
|
248
|
+
for (const file of files) for (const dependency of project.dependencies(file)) if (dependency.startsWith(PACKAGE)) packages.add(dependency.slice(4));
|
|
249
|
+
else files.add(dependency);
|
|
250
|
+
for (const name of packages) reachedPackages.add(name);
|
|
251
|
+
return {
|
|
252
|
+
files: [...files],
|
|
253
|
+
packages
|
|
254
|
+
};
|
|
255
|
+
};
|
|
256
|
+
/** The paths of the files the oracle read for the packages, by their real paths. */
|
|
257
|
+
const packageFiles = (packages) => {
|
|
258
|
+
if (!packages.size) return [];
|
|
259
|
+
return [...seen.keys()].filter((path) => {
|
|
260
|
+
const name = packageOf(posix.dirname(path));
|
|
261
|
+
return name !== void 0 && packages.has(name);
|
|
262
|
+
});
|
|
263
|
+
};
|
|
264
|
+
/**
|
|
265
|
+
* The names a changed file has in a graph and its oracle: relative to the graph's root, for a
|
|
266
|
+
* file under it, and `pkg:<name>`, for a file of a package a module of the graph reached. One
|
|
267
|
+
* file may have both: a workspace package that one module imports by name and another by path,
|
|
268
|
+
* or a library's own files that its playground imports by the library's name.
|
|
269
|
+
*/
|
|
270
|
+
const namesOf = (graph, path) => {
|
|
271
|
+
const name = packageOf(posix.dirname(path));
|
|
272
|
+
return [...path.startsWith(`${graph.root}/`) ? [path.slice(graph.root.length + 1)] : [], ...name !== void 0 && graph.reachedPackages.has(name) ? [`${PACKAGE}${name}`] : []];
|
|
273
|
+
};
|
|
274
|
+
const recompiled = /* @__PURE__ */ new Map();
|
|
275
|
+
/**
|
|
276
|
+
* Hands each graph every change it has not seen among the files at these paths, under every
|
|
277
|
+
* name each file has there, so `update` compiles again the modules whose inputs changed, those
|
|
278
|
+
* compiled for other modules included. Gives whether anything changed.
|
|
279
|
+
*/
|
|
280
|
+
const handOver = async (paths) => {
|
|
281
|
+
const changed = /* @__PURE__ */ new Map();
|
|
282
|
+
for (const path of paths) {
|
|
283
|
+
if (!seen.has(path)) continue;
|
|
284
|
+
const text = await readFile(path, "utf8").catch(() => void 0);
|
|
285
|
+
if (text !== seen.get(path)) changed.set(path, text);
|
|
286
|
+
}
|
|
287
|
+
if (!changed.size) return false;
|
|
288
|
+
const before = new Map([...changed.keys()].map((path) => [path, seen.get(path)]));
|
|
289
|
+
for (const [path, text] of changed) seen.set(path, text);
|
|
290
|
+
try {
|
|
291
|
+
for (const graph of graphs.values()) {
|
|
292
|
+
const names = [...changed.keys()].flatMap((path) => namesOf(graph, path));
|
|
293
|
+
if (!names.length) continue;
|
|
294
|
+
const { compiled: modules } = await graph.project.update([...new Set(names)]);
|
|
295
|
+
if (meta.framework !== "vite") continue;
|
|
296
|
+
for (const path of changed.keys()) {
|
|
297
|
+
const files = recompiled.get(path) ?? /* @__PURE__ */ new Set();
|
|
298
|
+
for (const module of modules) files.add(posix.join(graph.root, module));
|
|
299
|
+
recompiled.set(path, files);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
} catch (error) {
|
|
303
|
+
for (const [path, text] of before) seen.set(path, text);
|
|
304
|
+
throw error;
|
|
305
|
+
}
|
|
306
|
+
return true;
|
|
307
|
+
};
|
|
308
|
+
/** Hands the project every change it has not seen among what a module reached. */
|
|
309
|
+
const refresh = (graph, filename) => {
|
|
310
|
+
const { files, packages } = reachedFrom(graph, filename);
|
|
311
|
+
return handOver(/* @__PURE__ */ new Set([...files.map((file) => `${graph.root}/${file}`), ...packageFiles(packages)]));
|
|
312
|
+
};
|
|
313
|
+
/**
|
|
314
|
+
* The current compile of a `.uf.tsx` file or a `.uf.ts` module, shared by every id that loads
|
|
315
|
+
* it, and the source it compiled. The changes the project has not seen go through its `update`
|
|
316
|
+
* first, which compiles again only the modules whose inputs changed (ADR-0071), so an edit is
|
|
317
|
+
* seen without a bundler's watcher too. A first compile may reach a file the project still holds
|
|
318
|
+
* as it was for another module: the changes it reached go through `update` after it, and the
|
|
319
|
+
* compile is taken again. The result's outputs hold the copies of the shared `.ts` modules it
|
|
320
|
+
* reaches, as `compile()`'s do (ADR-0070): the golden guard judges them.
|
|
321
|
+
*/
|
|
322
|
+
const compileFile = (file) => exclusive(async () => {
|
|
323
|
+
const { graph, filename } = locate(file);
|
|
324
|
+
const { project } = graph;
|
|
325
|
+
await refresh(graph, filename);
|
|
326
|
+
let result = await project.compile(filename);
|
|
327
|
+
if (await refresh(graph, filename)) result = await project.compile(filename);
|
|
328
|
+
const source = seen.get(`${graph.root}/${filename}`) ?? "";
|
|
329
|
+
const files = result.outputs[target.name] ?? [];
|
|
330
|
+
if (!files.length) return {
|
|
331
|
+
result,
|
|
332
|
+
source
|
|
333
|
+
};
|
|
334
|
+
const outputs = {
|
|
335
|
+
...result.outputs,
|
|
336
|
+
[target.name]: [...files, ...project.copies(filename)]
|
|
337
|
+
};
|
|
338
|
+
return {
|
|
339
|
+
result: {
|
|
340
|
+
...result,
|
|
341
|
+
outputs
|
|
342
|
+
},
|
|
343
|
+
source
|
|
344
|
+
};
|
|
345
|
+
});
|
|
346
|
+
/** The guard's verdict on a load: the message that fails the module, if any. */
|
|
347
|
+
const guard = async (event) => {
|
|
348
|
+
if (!options.onCompile) return void 0;
|
|
349
|
+
let verdict;
|
|
350
|
+
try {
|
|
351
|
+
verdict = await options.onCompile(event);
|
|
352
|
+
} catch (error) {
|
|
353
|
+
const reason = error instanceof Error ? error.stack ?? error.message : String(error);
|
|
354
|
+
return `onCompile threw while checking ${event.id}: ${reason}`;
|
|
355
|
+
}
|
|
356
|
+
if (typeof verdict !== "string") return void 0;
|
|
357
|
+
return verdict || `onCompile failed ${event.id} without a message.`;
|
|
358
|
+
};
|
|
359
|
+
/** A module id coming back, as the absolute id `load` receives, if its `.uf.tsx` file exists. */
|
|
360
|
+
const resolveModuleId = (path, query, importer) => {
|
|
361
|
+
const id = moduleIdCandidates(path, root, importer).find((candidate) => existsSync(parseModuleId(candidate, suffix).file));
|
|
362
|
+
return id === void 0 ? void 0 : `${id}${query}`;
|
|
363
|
+
};
|
|
364
|
+
/**
|
|
365
|
+
* Fails a module through `error`, which throws on Rollup's family, and fails webpack's build
|
|
366
|
+
* through `emitError`. esbuild only records the message, and drops it when `load` returns no code
|
|
367
|
+
* or a `transform` follows (ADR-0075), so there the hook throws, which esbuild does report.
|
|
368
|
+
*/
|
|
369
|
+
const failModule = (context, message) => {
|
|
370
|
+
context.error(message);
|
|
371
|
+
if (meta.framework === "esbuild") throw new Error(message);
|
|
372
|
+
};
|
|
373
|
+
const bundler = meta.framework === "vite" ? "Vite" : meta.framework;
|
|
374
|
+
/**
|
|
375
|
+
* Why an authored `.uf.tsx` file reached `load` as itself on a target whose module ids have a
|
|
376
|
+
* suffix: its import did not name the file, so `resolveId` never saw it. A bare specifier gets
|
|
377
|
+
* here only on a bundler whose context can't resolve one (ADR-0075).
|
|
378
|
+
*/
|
|
379
|
+
const bypassed = (file) => {
|
|
380
|
+
const composables = file.endsWith(COMPOSABLE_EXTENSION);
|
|
381
|
+
const extension = composables ? COMPOSABLE_EXTENSION : SOURCE_EXTENSION;
|
|
382
|
+
const what = composables ? `plain TypeScript rather than as its ${target.name} composables` : `plain TSX rather than as its ${target.name} component`;
|
|
383
|
+
return `${filenameOf(file)} reached ${bundler} as itself, which would load it as ${what}: the import that resolved to it does not name the file (a package's \`exports\` entry or a tsconfig path, say), so this plugin never saw it. Import it with a specifier that ends in ${extension}, such as "./${basename(file)}"${bareSpecifiers ? "" : `; a package that ships ${extension} sources must export them under subpaths that do`}.`;
|
|
384
|
+
};
|
|
385
|
+
/**
|
|
386
|
+
* The id an output's relative import of another output resolves to (ADR-0053), looked up in
|
|
387
|
+
* the importer's current compile: a component of its file, or of a file it imports. A file's
|
|
388
|
+
* main component loads under its module id, as does every component on a target that joins a
|
|
389
|
+
* file's outputs; any other under its component id. `undefined` for an import no compile names,
|
|
390
|
+
* or from an importer whose file is gone.
|
|
391
|
+
*/
|
|
392
|
+
const childId = async (file, specifier) => {
|
|
393
|
+
if (!existsSync(file)) return void 0;
|
|
394
|
+
const { result } = await compileFile(file);
|
|
395
|
+
const wanted = posix.join(posix.dirname(file), specifier);
|
|
396
|
+
const files = [file, ...(result.ir?.imports ?? []).map((entry) => posix.join(posix.dirname(file), entry.file))];
|
|
397
|
+
for (const candidate of files) {
|
|
398
|
+
const compiled = candidate === file ? result : (await compileFile(candidate)).result;
|
|
399
|
+
const owners = compiled.owners[target.name] ?? {};
|
|
400
|
+
for (const [path, component] of Object.entries(owners)) {
|
|
401
|
+
const output = posix.join(posix.dirname(candidate), path);
|
|
402
|
+
if (output !== wanted && output.replace(/\.[^./]+$/, "") !== wanted) continue;
|
|
403
|
+
if (candidate.endsWith(".uf.ts")) return composableId(candidate, composableSuffix);
|
|
404
|
+
if (joins || component === mainComponent(compiled.ir)) return moduleId(candidate, suffix);
|
|
405
|
+
return componentId(candidate, path);
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
};
|
|
409
|
+
/**
|
|
410
|
+
* The scoped sheet this target's compile of `file` emitted (ADR-0063), whatever the id's
|
|
411
|
+
* query: plugin-vue's style request and Qwik's `?inline` load the same text.
|
|
412
|
+
*/
|
|
413
|
+
const sheetOf = async (id, file) => {
|
|
414
|
+
const { graph, filename } = locate(file);
|
|
415
|
+
const { result, source } = await compileFile(file);
|
|
416
|
+
const errors = result.diagnostics.filter((diagnostic) => diagnostic.severity === "error");
|
|
417
|
+
if (errors.length > 0) return { error: formatErrors(graph, filename, errors, source) };
|
|
418
|
+
const path = `${basename(file)}${SHEET_SUFFIX}`;
|
|
419
|
+
const emitted = (result.outputs[target.name] ?? []).find((output) => output.path === path);
|
|
420
|
+
if (!emitted) return { error: `The ${target.name} target emitted no ${path} for ${filename}, which the id ${id} loads: ${basename(file)} imports no stylesheet.` };
|
|
421
|
+
return { code: emitted.contents };
|
|
422
|
+
};
|
|
423
|
+
/**
|
|
424
|
+
* A module's watch edges but its own file (ADR-0053, ADR-0071): every file its output depends
|
|
425
|
+
* on, by its absolute path, a package's files included unless they sit in `node_modules`, which
|
|
426
|
+
* Vite's watcher ignores. A file that does not exist is left out, since Vite's import analysis
|
|
427
|
+
* fails an import it cannot resolve: once created, it reaches its dependents on their next
|
|
428
|
+
* load, which the project checks.
|
|
429
|
+
*/
|
|
430
|
+
const watchedFiles = (file) => {
|
|
431
|
+
const { graph, filename } = locate(file);
|
|
432
|
+
const { files, packages } = reachedFrom(graph, filename);
|
|
433
|
+
const paths = files.filter((each) => each !== filename).map((each) => posix.join(graph.root, each));
|
|
434
|
+
paths.push(...packageFiles(packages));
|
|
435
|
+
return [...new Set(paths)].filter((path) => !belowRoot(path).includes(NODE_MODULES) && existsSync(path));
|
|
436
|
+
};
|
|
437
|
+
/**
|
|
438
|
+
* The code of a module of composables (ADR-0069): each composable's output, joined into one
|
|
439
|
+
* module, as a script target joins a file's components, so a caller's import of any of them
|
|
440
|
+
* resolves to this id.
|
|
441
|
+
*/
|
|
442
|
+
async function loadComposables(id, file, failed) {
|
|
443
|
+
const { graph, filename } = locate(file);
|
|
444
|
+
const { result, source } = await compileFile(file);
|
|
445
|
+
const files = result.outputs[target.name] ?? [];
|
|
446
|
+
for (const diagnostic of result.diagnostics) if (diagnostic.severity !== "error") this.warn(formatDiagnostic(diagnostic, source));
|
|
447
|
+
const verdict = await guard({
|
|
448
|
+
id,
|
|
449
|
+
file,
|
|
450
|
+
target: target.name,
|
|
451
|
+
files,
|
|
452
|
+
diagnostics: result.diagnostics,
|
|
453
|
+
...result.ir ? { ir: result.ir } : {}
|
|
454
|
+
});
|
|
455
|
+
const fail = (message) => {
|
|
456
|
+
compiled.delete(id);
|
|
457
|
+
failed.delete(id);
|
|
458
|
+
return failModule(this, verdict ? `${message}\n\n${verdict}` : message);
|
|
459
|
+
};
|
|
460
|
+
const errors = result.diagnostics.filter((diagnostic) => diagnostic.severity === "error");
|
|
461
|
+
if (errors.length > 0) return fail(formatErrors(graph, filename, errors, source));
|
|
462
|
+
const owners = result.owners[target.name] ?? {};
|
|
463
|
+
const owned = files.filter((output) => Object.hasOwn(owners, output.path));
|
|
464
|
+
const assembly = assembleModule({
|
|
465
|
+
target: target.name,
|
|
466
|
+
filename,
|
|
467
|
+
extension: ".ts",
|
|
468
|
+
files: owned,
|
|
469
|
+
exports: [],
|
|
470
|
+
siblings: owned.map((output) => output.path)
|
|
471
|
+
});
|
|
472
|
+
if ("error" in assembly) return fail(assembly.error);
|
|
473
|
+
compiled.set(id, assembly.code);
|
|
474
|
+
if (verdict === void 0) failed.delete(id);
|
|
475
|
+
else {
|
|
476
|
+
const names = (result.ir?.composables ?? []).map(({ name }) => name);
|
|
477
|
+
failed.set(id, {
|
|
478
|
+
message: verdict,
|
|
479
|
+
exports: names
|
|
480
|
+
});
|
|
481
|
+
}
|
|
482
|
+
return assembly;
|
|
483
|
+
}
|
|
484
|
+
/**
|
|
485
|
+
* The authored file a specifier names, by the file it resolves to. Rollup's family resolves it
|
|
486
|
+
* through the bundler, so aliases and other plugins' answers work. On Vite a bare specifier asks
|
|
487
|
+
* `resolveBare` first, because `this.resolve` would register a `.uf.ts` file of `node_modules` with the
|
|
488
|
+
* dependency optimiser, which bundles it as plain TypeScript before this plugin sees it, and a
|
|
489
|
+
* `.uf.tsx` file too in a project that adds `.tsx` to `optimizeDeps.extensions` (ADR-0075). The
|
|
490
|
+
* others' contexts have no resolver, so there the specifier must be a path: `./X.uf`, `./X.uf.js` and
|
|
491
|
+
* `./X.uf.jsx` find `X.uf.tsx` or `X.uf.ts` as the bundler's resolver would, and a package's
|
|
492
|
+
* specifier is left to the bundler.
|
|
493
|
+
*/
|
|
494
|
+
const fileOf = async (context, path, importer) => {
|
|
495
|
+
if (resolves(context)) {
|
|
496
|
+
if (resolveBare && BARE.test(path) && hasEnvironment(context)) {
|
|
497
|
+
const id = await (await resolveBare)(context.environment, path, importer).catch(() => void 0);
|
|
498
|
+
const found = id === void 0 ? void 0 : withoutQuery(id);
|
|
499
|
+
if (found?.includes(NODE_MODULES) && isSource(found)) return found;
|
|
500
|
+
if (!specifierFilter(suffix).test(path) && !composableIds.test(path)) return found;
|
|
501
|
+
}
|
|
502
|
+
const resolved = await context.resolve(path, importer, { skipSelf: true });
|
|
503
|
+
return resolved && !resolved.external ? withoutQuery(resolved.id) : void 0;
|
|
504
|
+
}
|
|
505
|
+
const [named] = moduleIdCandidates(path, root, importer);
|
|
506
|
+
if (named === void 0) return void 0;
|
|
507
|
+
const stem = /^(.*\.uf)(?:\.jsx?)?$/.exec(named)?.[1];
|
|
508
|
+
return (stem === void 0 ? [named] : [
|
|
509
|
+
named,
|
|
510
|
+
`${stem}.tsx`,
|
|
511
|
+
`${stem}.ts`
|
|
512
|
+
]).find((candidate) => existsSync(candidate));
|
|
513
|
+
};
|
|
514
|
+
let resolveBare;
|
|
515
|
+
let checkAccess;
|
|
516
|
+
/** What `resolveId` answers (see the README), on every bundler. Only Vite's scan sets `scan`. */
|
|
517
|
+
const resolveModule = async (context, source, importer, scan) => {
|
|
518
|
+
const index = source.indexOf("?");
|
|
519
|
+
const path = index === -1 ? source : source.slice(0, index);
|
|
520
|
+
const query = index === -1 ? "" : source.slice(index);
|
|
521
|
+
if (parseModuleId(path, SHEET_SUFFIX)) {
|
|
522
|
+
if (RELATIVE.test(path) && !(importer && (parseModuleId(importer, suffix) ?? componentRequest(importer) ?? splitFrom(importer)))) return null;
|
|
523
|
+
const id = moduleIdCandidates(path, root, importer).find((candidate) => existsSync(parseModuleId(candidate, SHEET_SUFFIX).file));
|
|
524
|
+
return id === void 0 ? null : `${id}${query}`;
|
|
525
|
+
}
|
|
526
|
+
if (componentRequest(path)) {
|
|
527
|
+
const id = moduleIdCandidates(path, root, importer).find((candidate) => existsSync(componentRequest(candidate).file));
|
|
528
|
+
return id === void 0 ? null : `${id}${query}`;
|
|
529
|
+
}
|
|
530
|
+
const from = importer && (parseModuleId(importer, suffix) ?? componentRequest(importer) ?? composableRequest(importer) ?? splitFrom(importer));
|
|
531
|
+
if (from && isAbsolute(from.file) && RELATIVE.test(path) && !specifierFilter(suffix).test(path)) {
|
|
532
|
+
const child = await childId(from.file, path);
|
|
533
|
+
if (child !== void 0) return `${child}${query}`;
|
|
534
|
+
}
|
|
535
|
+
if (!(bareSpecifiers && BARE.test(path)) && !specifierFilter(suffix).test(source) && !composableIds.test(source)) return null;
|
|
536
|
+
if (suffix && path.endsWith(`.uf.tsx${suffix}`)) return resolveModuleId(path, query, importer) ?? null;
|
|
537
|
+
if (composableSuffix && path.endsWith(`.uf.ts${composableSuffix}`)) {
|
|
538
|
+
const id = moduleIdCandidates(path, root, importer).find((candidate) => existsSync(composableRequest(candidate).file));
|
|
539
|
+
return id === void 0 ? null : `${id}${query}`;
|
|
540
|
+
}
|
|
541
|
+
if (VITE_FILE_QUERY.test(query)) return null;
|
|
542
|
+
const file = await fileOf(context, path, importer);
|
|
543
|
+
if (file === void 0) return null;
|
|
544
|
+
const composable = file.endsWith(COMPOSABLE_EXTENSION);
|
|
545
|
+
if (!composable && !file.endsWith(".uf.tsx")) return null;
|
|
546
|
+
if (scan) return `${SCAN_ID_PREFIX}${file}`;
|
|
547
|
+
if (importer !== void 0 && (parseModuleId(importer, suffix) ?? componentRequest(importer) ?? composableRequest(importer))) return file;
|
|
548
|
+
return composable ? composableId(file, composableSuffix, query) : moduleId(file, suffix, query);
|
|
549
|
+
};
|
|
550
|
+
/**
|
|
551
|
+
* The authored file a module of Vite's graph was loaded from, if it is one of ours: a sheet, a
|
|
552
|
+
* module of composables, a component, a main module or a Qwik segment, with any framework's
|
|
553
|
+
* query. Vite's own file queries (`?raw`, `?url`) read the authored file, so they stay Vite's.
|
|
554
|
+
*/
|
|
555
|
+
const ownerOf = (id) => {
|
|
556
|
+
if (id === null || VITE_FILE_QUERY.test(id)) return void 0;
|
|
557
|
+
return (parseModuleId(id, SHEET_SUFFIX) ?? composableRequest(id) ?? componentRequest(id) ?? parseModuleId(id, suffix) ?? splitFrom(id))?.file;
|
|
558
|
+
};
|
|
559
|
+
const served = /* @__PURE__ */ new Set();
|
|
560
|
+
const isOurs = (node) => {
|
|
561
|
+
if (ownerOf(node.id) !== void 0) return true;
|
|
562
|
+
return node.id !== null && !served.has(node.id) && node.importers.size > 0 && [...node.importers].every((importer) => ownerOf(importer.id) !== void 0);
|
|
563
|
+
};
|
|
564
|
+
const hotUpdates = /* @__PURE__ */ new Map();
|
|
565
|
+
const recompiledBy = (file, timestamp) => {
|
|
566
|
+
const known = hotUpdates.get(file);
|
|
567
|
+
if (known?.timestamp === timestamp) return known.files;
|
|
568
|
+
const files = exclusive(async () => {
|
|
569
|
+
await handOver([file]);
|
|
570
|
+
const taken = recompiled.get(file) ?? /* @__PURE__ */ new Set();
|
|
571
|
+
recompiled.delete(file);
|
|
572
|
+
return taken;
|
|
573
|
+
});
|
|
574
|
+
hotUpdates.set(file, {
|
|
575
|
+
timestamp,
|
|
576
|
+
files
|
|
577
|
+
});
|
|
578
|
+
return files;
|
|
579
|
+
};
|
|
580
|
+
/**
|
|
581
|
+
* The modules a change updates in one environment (ADR-0075). Vite's own list holds the file's
|
|
582
|
+
* nodes, and on a target with an id suffix those are only watch edges, which it never analysed,
|
|
583
|
+
* so it would send nothing. So the list becomes every module of ours whose file the project
|
|
584
|
+
* compiled again, its scoped sheet included, because a sheet's text and the scope id in its
|
|
585
|
+
* component's markup change together (ADR-0063). Modules not ours keep Vite's handling. A file
|
|
586
|
+
* created or deleted also reaches the modules of that file, which the project no longer holds,
|
|
587
|
+
* and Vite's own list stays, since it holds the modules that failed to resolve the new file.
|
|
588
|
+
*/
|
|
589
|
+
const hotModules = async ({ type, file, timestamp, modules }, idToModule) => {
|
|
590
|
+
const files = new Set(await recompiledBy(file, timestamp));
|
|
591
|
+
if (type !== "update") files.add(file);
|
|
592
|
+
const updated = [...idToModule.values()].filter((node) => {
|
|
593
|
+
const owner = ownerOf(node.id);
|
|
594
|
+
return owner !== void 0 && files.has(owner);
|
|
595
|
+
});
|
|
596
|
+
const kept = type === "update" ? modules.filter((node) => !isOurs(node)) : modules;
|
|
597
|
+
return [.../* @__PURE__ */ new Set([...kept, ...updated])];
|
|
598
|
+
};
|
|
599
|
+
/**
|
|
600
|
+
* Qwik's own plugin sends its hot update, or its full reload with its hot update off, from the
|
|
601
|
+
* `ssr` environment only. A client-rendered app loads nothing there, so it would get neither.
|
|
602
|
+
* In place, Qwik also keeps the sheet a component's `useStyles$` added first, while the scope id
|
|
603
|
+
* in its markup changes with the sheet (ADR-0063). So when the `ssr` environment holds none of
|
|
604
|
+
* the change's modules, the client environment reloads the page itself (ADR-0075). Vite
|
|
605
|
+
* invalidates nothing a hook leaves out of its list, so our modules are invalidated here first,
|
|
606
|
+
* and the reloaded page loads the new code.
|
|
607
|
+
*/
|
|
608
|
+
const reloadQwikClient = (environment, { server, timestamp }, modules) => {
|
|
609
|
+
const ours = modules.filter((node) => ownerOf(node.id) !== void 0);
|
|
610
|
+
if (ours.length === 0) return modules;
|
|
611
|
+
const files = new Set(ours.map((node) => ownerOf(node.id)));
|
|
612
|
+
if ([...server.environments.ssr?.moduleGraph.idToModuleMap.values() ?? []].some((node) => files.has(ownerOf(node.id)))) return modules;
|
|
613
|
+
const invalidated = /* @__PURE__ */ new Set();
|
|
614
|
+
for (const node of ours) environment.moduleGraph.invalidateModule(node, invalidated, timestamp, true);
|
|
615
|
+
environment.hot.send({ type: "full-reload" });
|
|
616
|
+
return modules.filter((node) => !ours.includes(node));
|
|
617
|
+
};
|
|
618
|
+
/**
|
|
619
|
+
* What `load` answers, keeping the guard's verdicts in `failed`. With `access`, a `.uf.tsx` file
|
|
620
|
+
* any of our ids names, queries included, goes through it before anything reads the file.
|
|
621
|
+
*/
|
|
622
|
+
async function loadModule(id, failed, access) {
|
|
623
|
+
const requested = parseModuleId(id, SHEET_SUFFIX) ?? componentRequest(id) ?? parseModuleId(id, suffix) ?? parseModuleId(id, "");
|
|
624
|
+
if (access && requested && isAbsolute(requested.file)) await access(requested.file, id);
|
|
625
|
+
const sheet = parseModuleId(id, SHEET_SUFFIX);
|
|
626
|
+
if (sheet) {
|
|
627
|
+
if (!isAbsolute(sheet.file)) return null;
|
|
628
|
+
const loaded = await sheetOf(id, sheet.file);
|
|
629
|
+
return "error" in loaded ? failModule(this, loaded.error) : loaded;
|
|
630
|
+
}
|
|
631
|
+
const composables = composableRequest(id);
|
|
632
|
+
if (composables) {
|
|
633
|
+
if (!isAbsolute(composables.file) || VITE_FILE_QUERY.test(composables.query)) return null;
|
|
634
|
+
return loadComposables.call(this, id, composables.file, failed);
|
|
635
|
+
}
|
|
636
|
+
const component = componentRequest(id);
|
|
637
|
+
const request = component ?? parseModuleId(id, suffix);
|
|
638
|
+
if (!request) {
|
|
639
|
+
const authored = parseModuleId(id, "") ?? parseComposableId(id, "");
|
|
640
|
+
if (!authored || VITE_FILE_QUERY.test(authored.query)) return null;
|
|
641
|
+
return failModule(this, bypassed(authored.file));
|
|
642
|
+
}
|
|
643
|
+
if (!isAbsolute(request.file)) return null;
|
|
644
|
+
if (VITE_FILE_QUERY.test(request.query) || subRequest.test(request.query)) return null;
|
|
645
|
+
const { graph, filename } = locate(request.file);
|
|
646
|
+
const { result, source } = await compileFile(request.file);
|
|
647
|
+
const files = result.outputs[target.name] ?? [];
|
|
648
|
+
for (const diagnostic of result.diagnostics) if (diagnostic.severity !== "error") this.warn(formatDiagnostic(diagnostic, source));
|
|
649
|
+
const verdict = await guard({
|
|
650
|
+
id,
|
|
651
|
+
file: request.file,
|
|
652
|
+
target: target.name,
|
|
653
|
+
files,
|
|
654
|
+
diagnostics: result.diagnostics,
|
|
655
|
+
...result.ir ? { ir: result.ir } : {}
|
|
656
|
+
});
|
|
657
|
+
const fail = (message) => {
|
|
658
|
+
compiled.delete(id);
|
|
659
|
+
failed.delete(id);
|
|
660
|
+
return failModule(this, verdict ? `${message}\n\n${verdict}` : message);
|
|
661
|
+
};
|
|
662
|
+
const errors = result.diagnostics.filter((diagnostic) => diagnostic.severity === "error");
|
|
663
|
+
if (errors.length > 0) return fail(formatErrors(graph, filename, errors, source));
|
|
664
|
+
const owners = result.owners[target.name] ?? {};
|
|
665
|
+
const main = mainComponent(result.ir);
|
|
666
|
+
const owned = files.filter((file) => Object.hasOwn(owners, file.path));
|
|
667
|
+
const loaded = component ? files.filter((file) => file.path === component.output) : joins ? owned : files.filter((file) => owners[file.path] === main);
|
|
668
|
+
if (component && !loaded.length) return fail(`The ${target.name} target emitted no ${component.output} for ${filename}, which the id ${id} loads.`);
|
|
669
|
+
const exports = (result.ir?.exports ?? []).filter((entry) => joins || component !== void 0 || entry.local === main);
|
|
670
|
+
const assembly = assembleModule({
|
|
671
|
+
target: target.name,
|
|
672
|
+
filename,
|
|
673
|
+
extension: component ? extname(component.output) : extension,
|
|
674
|
+
files: loaded,
|
|
675
|
+
exports: component ? [] : exports,
|
|
676
|
+
siblings: owned.map((file) => file.path)
|
|
677
|
+
});
|
|
678
|
+
if ("error" in assembly) return fail(assembly.error);
|
|
679
|
+
compiled.set(id, assembly.code);
|
|
680
|
+
if (assembly.map) loadMaps.set(id, assembly.map);
|
|
681
|
+
else loadMaps.delete(id);
|
|
682
|
+
const emitted = files.find((file) => file.path === `${basename(request.file)}${SHEET_SUFFIX}`);
|
|
683
|
+
if (emitted) sheets.set(request.file, emitted.contents);
|
|
684
|
+
else sheets.delete(request.file);
|
|
685
|
+
if (verdict === void 0) failed.delete(id);
|
|
686
|
+
else {
|
|
687
|
+
const named = exports.filter((entry) => entry.kind === "named");
|
|
688
|
+
failed.set(id, {
|
|
689
|
+
message: verdict,
|
|
690
|
+
exports: named.map((entry) => entry.name)
|
|
691
|
+
});
|
|
692
|
+
}
|
|
693
|
+
return assembly;
|
|
694
|
+
}
|
|
695
|
+
/**
|
|
696
|
+
* The watch edges (see resolveId): to the file, for every id but one that is the file itself,
|
|
697
|
+
* and to every file the project says the output depends on (ADR-0071): each child's `.uf.tsx`,
|
|
698
|
+
* each composable's `.uf.ts`, each shared `.ts` module and each file a type was read from, so an
|
|
699
|
+
* edit to one invalidates the module. They are added in `transform` rather than `load`: Vite
|
|
700
|
+
* keeps the files a load adds only when the module is already in its graph, which a cold
|
|
701
|
+
* request's is not, while a transform always runs on a module the graph holds.
|
|
702
|
+
*/
|
|
703
|
+
function addWatchEdges(id) {
|
|
704
|
+
const composables = composableRequest(id);
|
|
705
|
+
const component = composables ? void 0 : componentRequest(id);
|
|
706
|
+
const request = composables ?? component ?? parseModuleId(id, suffix);
|
|
707
|
+
if (!request || !isAbsolute(request.file)) return null;
|
|
708
|
+
if (composables ? composableSuffix : suffix || component) this.addWatchFile(request.file);
|
|
709
|
+
for (const path of watchedFiles(request.file)) this.addWatchFile(path);
|
|
710
|
+
return null;
|
|
711
|
+
}
|
|
712
|
+
const resolveFilter = { id: new RegExp([
|
|
713
|
+
...suffixes.map(specifierFilter),
|
|
714
|
+
COMPONENT_ID_FILTER,
|
|
715
|
+
RELATIVE,
|
|
716
|
+
SHEET_ID_FILTER,
|
|
717
|
+
...composableFilters,
|
|
718
|
+
...bareSpecifiers ? [BARE] : []
|
|
719
|
+
].map(({ source }) => source).join("|")) };
|
|
720
|
+
const pre = {
|
|
721
|
+
name: PLUGIN_NAME,
|
|
722
|
+
enforce: "pre",
|
|
723
|
+
resolveId: {
|
|
724
|
+
filter: resolveFilter,
|
|
725
|
+
handler(source, importer) {
|
|
726
|
+
return resolveModule(this, source, importer, false);
|
|
727
|
+
}
|
|
728
|
+
},
|
|
729
|
+
load: {
|
|
730
|
+
filter: { id: loadIds },
|
|
731
|
+
handler(id) {
|
|
732
|
+
return loadModule.call(this, id, sharedFailures);
|
|
733
|
+
}
|
|
734
|
+
},
|
|
735
|
+
transform: {
|
|
736
|
+
filter: { id: moduleIds },
|
|
737
|
+
handler(_code, id) {
|
|
738
|
+
return addWatchEdges.call(this, id);
|
|
739
|
+
}
|
|
740
|
+
},
|
|
741
|
+
watchChange(id) {
|
|
742
|
+
exclusive(() => handOver([toPosix(id)]));
|
|
743
|
+
},
|
|
744
|
+
webpack(compiler) {
|
|
745
|
+
root = toPosix(compiler.context);
|
|
746
|
+
},
|
|
747
|
+
rspack(compiler) {
|
|
748
|
+
root = toPosix(compiler.context);
|
|
749
|
+
},
|
|
750
|
+
esbuild: { config(buildOptions) {
|
|
751
|
+
if (buildOptions.absWorkingDir) root = toPosix(buildOptions.absWorkingDir);
|
|
752
|
+
} },
|
|
753
|
+
vite: {
|
|
754
|
+
configResolved(config) {
|
|
755
|
+
root = config.root;
|
|
756
|
+
checkAccess = config.command === "serve" ? accessCheck(config) : void 0;
|
|
757
|
+
resolveBare = import("vite").then(({ createIdResolver }) => createIdResolver(config));
|
|
758
|
+
const file = config.configFile ? filenameOf(config.configFile) : "inline Vite configuration";
|
|
759
|
+
if (options.target === void 0) {
|
|
760
|
+
const detected = detectTarget(config.plugins, file);
|
|
761
|
+
if ("error" in detected) this.error(detected.error);
|
|
762
|
+
else selectTarget(detected.target);
|
|
763
|
+
}
|
|
764
|
+
if (target.name === "vue") {
|
|
765
|
+
const warning = hostOptionWarning(config.plugins, file);
|
|
766
|
+
if (warning) this.warn(warning);
|
|
767
|
+
fillVueScriptFs(config.plugins);
|
|
768
|
+
}
|
|
769
|
+
chainSvelteOnce(config.plugins, (id) => compiled.has(id));
|
|
770
|
+
chainQwikSegments(config.plugins, (id) => {
|
|
771
|
+
const parent = SEGMENT.exec(id)?.[1];
|
|
772
|
+
return parent === void 0 ? void 0 : loadMaps.get(parent);
|
|
773
|
+
});
|
|
774
|
+
},
|
|
775
|
+
resolveId: {
|
|
776
|
+
filter: resolveFilter,
|
|
777
|
+
handler(source, importer, resolveOptions) {
|
|
778
|
+
const scan = "scan" in resolveOptions && resolveOptions.scan === true;
|
|
779
|
+
return resolveModule(this, source, importer, scan);
|
|
780
|
+
}
|
|
781
|
+
},
|
|
782
|
+
load: {
|
|
783
|
+
filter: { id: loadIds },
|
|
784
|
+
handler(id) {
|
|
785
|
+
const client = this.environment.config.consumer === "client";
|
|
786
|
+
return loadModule.call(this, id, failuresIn(this.environment), client ? checkAccess : void 0);
|
|
787
|
+
}
|
|
788
|
+
},
|
|
789
|
+
transform(_code, id) {
|
|
790
|
+
served.add(id);
|
|
791
|
+
return addWatchEdges.call(this, id);
|
|
792
|
+
},
|
|
793
|
+
async hotUpdate(update) {
|
|
794
|
+
const modules = await hotModules(update, this.environment.moduleGraph.idToModuleMap);
|
|
795
|
+
return target.name === "qwik" && this.environment.name === "client" && update.type === "update" ? reloadQwikClient(this.environment, update, modules) : modules;
|
|
796
|
+
},
|
|
797
|
+
api: {
|
|
798
|
+
getCompiled: (id) => compiled.get(id),
|
|
799
|
+
getSheet: (file) => sheets.get(file)
|
|
800
|
+
}
|
|
801
|
+
}
|
|
802
|
+
};
|
|
803
|
+
/** A module the guard failed, as the code that replaces it. */
|
|
804
|
+
const failing = (failure) => failure ? {
|
|
805
|
+
code: failingModule(failure),
|
|
806
|
+
map: { mappings: "" }
|
|
807
|
+
} : null;
|
|
808
|
+
return [pre, {
|
|
809
|
+
name: `${PLUGIN_NAME}:guard`,
|
|
810
|
+
enforce: "post",
|
|
811
|
+
transform: {
|
|
812
|
+
filter: { id: moduleIds },
|
|
813
|
+
handler: (_code, id) => failing(sharedFailures.get(id))
|
|
814
|
+
},
|
|
815
|
+
vite: { transform: {
|
|
816
|
+
filter: { id: moduleIds },
|
|
817
|
+
handler(_code, id) {
|
|
818
|
+
return failing(failures.get(this.environment)?.get(id));
|
|
819
|
+
}
|
|
820
|
+
} }
|
|
821
|
+
}];
|
|
822
|
+
};
|
|
823
|
+
/**
|
|
824
|
+
* A file's main component (ADR-0053): its default export, or its only exported component;
|
|
825
|
+
* `undefined` for a file with neither, whose module id loads nothing.
|
|
826
|
+
*/
|
|
827
|
+
function mainComponent(ir) {
|
|
828
|
+
const exports = ir?.exports ?? [];
|
|
829
|
+
const main = exports.find((entry) => entry.kind === "default");
|
|
830
|
+
if (main) return main.local;
|
|
831
|
+
const locals = new Set(exports.map((entry) => entry.local));
|
|
832
|
+
return locals.size === 1 ? [...locals][0] : void 0;
|
|
833
|
+
}
|
|
834
|
+
/**
|
|
835
|
+
* A module that throws the message when it runs, and still provides every export the compiled
|
|
836
|
+
* module had, so its importers link and the message reaches the report.
|
|
837
|
+
*/
|
|
838
|
+
function failingModule({ message, exports }) {
|
|
839
|
+
const lines = [`throw new Error(${JSON.stringify(message)});`, "export default undefined;"];
|
|
840
|
+
if (exports.length > 0) {
|
|
841
|
+
const names = exports.map((name) => `failed as ${JSON.stringify(name)}`).join(", ");
|
|
842
|
+
lines.push("const failed = undefined;", `export { ${names} };`);
|
|
843
|
+
}
|
|
844
|
+
return `${lines.join("\n")}\n`;
|
|
845
|
+
}
|
|
846
|
+
/** The unframework plugin for every bundler unplugin offers. Only Vite runs end to end. */
|
|
847
|
+
const unframeworkUnplugin = createUnplugin(factory);
|
|
848
|
+
const bun = unframeworkUnplugin.bun;
|
|
849
|
+
const esbuild = unframeworkUnplugin.esbuild;
|
|
850
|
+
const farm = unframeworkUnplugin.farm;
|
|
851
|
+
const rolldown = unframeworkUnplugin.rolldown;
|
|
852
|
+
const rollup = unframeworkUnplugin.rollup;
|
|
853
|
+
const rsbuild = unframeworkUnplugin.rsbuild;
|
|
854
|
+
const rspack = unframeworkUnplugin.rspack;
|
|
855
|
+
const unloader = unframeworkUnplugin.unloader;
|
|
856
|
+
const webpack = unframeworkUnplugin.webpack;
|
|
857
|
+
//#endregion
|
|
858
|
+
export { PLUGIN_NAME, bun, esbuild, farm, rolldown, rollup, rsbuild, rspack, unframeworkUnplugin, unloader, webpack };
|