@fluixi/vite-plugin 1.0.0-alpha.108 → 1.0.0-alpha.110

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/dist/index.mjs CHANGED
@@ -1,256 +1,20 @@
1
- /*! @fluixi/vite-plugin v1.0.0-alpha.108 | (c) 2026 Ibrahima Touré and Fluixi contributors | MIT */
2
-
3
- // src/index.ts
4
- import { createVitePlugin } from "@fluixi/compiler/integrations";
5
-
6
- // src/graph-channel.ts
7
- import { createRequire } from "node:module";
8
- import { existsSync } from "node:fs";
9
- import { join } from "node:path";
10
- import { fileURLToPath } from "node:url";
11
- var VIRTUAL = "virtual:fluixi/devtools-agent";
12
- var RESOLVED = `\0${VIRTUAL}`;
13
- var ENDPOINT = "/__fluixi/graph";
14
- var EVENT = "fluixi:graph";
15
- var MISSING = "the plugin could not load @fluixi/devtools from its own dependencies";
16
- var WAITING = "no page has reported yet — open the application in a browser";
17
- var INTERVAL = 100;
18
- var HISTORY = 500;
19
- var agentSource = (devtools, plugins, signal, reactive) => `
20
- import { installDevtoolsHook, observeGraph } from ${JSON.stringify(devtools)};
21
- import ${JSON.stringify(plugins)};
22
- import { observeReactiveNodes } from ${JSON.stringify(signal)};
23
- import { VERSION } from ${JSON.stringify(reactive)};
24
-
25
- // The runtime this module imports is the application's own, same module graph, same nodes.
26
- // Nothing waits for the runtime to offer itself; that path is for a tool loaded from outside.
27
- //
28
- // \`watch\` is this copy of devtools, handed over with the runtime. A browser extension
29
- // installs a hook at document_start and so owns it; without this the graph would be built
30
- // by that extension's build rather than the one shipped alongside this application.
31
- const hook = installDevtoolsHook();
32
- hook.inject({ observeReactiveNodes, version: VERSION, watch: observeGraph });
33
-
34
- const send = (kind, snapshot) => import.meta.hot?.send(${JSON.stringify(EVENT)}, { kind, snapshot });
35
-
36
- const first = hook.snapshot();
37
- // Kept and handed back, so a browser panel polling the same hook cannot consume what this
38
- // one has not been told about yet.
39
- let cursor = first?.cursor;
40
- send('full', first);
41
- const timer = setInterval(() => {
42
- const delta = hook.snapshot({ partial: true, since: cursor });
43
- if (!delta) return;
44
- cursor = delta.cursor;
45
- // Events count on their own: an effect that ran without changing a value moves no node.
46
- if (delta.nodes.length || delta.removed?.length || delta.events?.length) send('delta', delta);
47
- }, ${INTERVAL});
48
-
49
- import.meta.hot?.dispose(() => clearInterval(timer));
50
- `;
51
- function ownDevtools() {
52
- return resolveForBrowser("@fluixi/devtools");
53
- }
54
- function ownPlugins() {
55
- return resolveForBrowser("@fluixi/devtools/plugins");
56
- }
57
- function resolveForBrowser(spec) {
58
- try {
59
- const resolved = import.meta.resolve?.(spec);
60
- if (resolved) return fileURLToPath(resolved);
61
- } catch {
62
- }
63
- try {
64
- const required = createRequire(import.meta.url).resolve(spec);
65
- if (!required.endsWith(".cjs")) return required;
66
- for (const ext of [".mjs", ".js"]) {
67
- const sibling = required.slice(0, -".cjs".length) + ext;
68
- if (existsSync(sibling)) return sibling;
69
- }
70
- return required;
71
- } catch {
72
- return null;
73
- }
74
- }
75
- function withGraphChannel(base, enabled, start) {
76
- let wire = null;
77
- let available = false;
78
- let reason = MISSING;
79
- let root = process.cwd();
80
- let log;
81
- let graph = /* @__PURE__ */ new Map();
82
- let stamp = 0;
83
- let events = [];
84
- const baseResolveId = base.resolveId;
85
- const baseTransformIndexHtml = base.transformIndexHtml;
86
- const baseLoad = base.load;
87
- const baseConfigureServer = base.configureServer;
88
- return {
89
- ...base,
90
- resolveId(id, importer) {
91
- if (id === VIRTUAL) return RESOLVED;
92
- return baseResolveId?.call(this, id, importer) ?? null;
93
- },
94
- async load(id) {
95
- if (id !== RESOLVED) return await baseLoad?.call(this, id) ?? null;
96
- const from = join(root, "fluixi-devtools-agent.js");
97
- const specs = ["@fluixi/reactive/signal", "@fluixi/reactive"];
98
- const ids = await Promise.all(specs.map((spec) => this.resolve?.(spec, from, { skipSelf: true })));
99
- const missing = specs.filter((_, i) => !ids[i]?.id);
100
- const devtools = ownDevtools();
101
- const plugins = ownPlugins();
102
- if (missing.length || !devtools || !plugins) {
103
- reason = missing.length ? `${missing.join(", ")} did not resolve from ${root}` : MISSING;
104
- log?.(`[fluixi] reactive graph off — ${reason}`);
105
- return "export {};";
106
- }
107
- return agentSource(`/@fs/${devtools}`, `/@fs/${plugins}`, ids[0].id, ids[1].id);
108
- },
109
- async configureServer(server) {
110
- await baseConfigureServer?.call(this, server);
111
- if (!enabled()) return;
112
- root = server.config?.root ?? root;
113
- log = server.config?.logger?.info.bind(server.config.logger);
114
- wire = await import("@fluixi/devtools/wire").catch(() => null);
115
- available = wire !== null;
116
- if (wire) start();
117
- if (wire) reason = WAITING;
118
- else {
119
- log?.(`[fluixi] reactive graph off — ${MISSING}`);
120
- }
121
- server.middlewares.use(ENDPOINT, (_req, res) => {
122
- const snapshot = wire ? { ...wire.serializeGraph(graph, stamp || Date.now()), events } : { t: Date.now(), nodes: [] };
123
- const body = JSON.stringify(reason ? { ...snapshot, reason } : snapshot);
124
- res.setHeader("Content-Type", "application/json");
125
- res.setHeader("Access-Control-Allow-Origin", "*");
126
- res.setHeader("Cache-Control", "no-store");
127
- res.end(body);
128
- });
129
- if (!wire) return;
130
- server.ws.on(EVENT, (payload) => {
131
- const message = payload;
132
- if (!message?.snapshot || !wire) return;
133
- const full = message.kind === "full";
134
- graph = wire.applySnapshot(full ? /* @__PURE__ */ new Map() : graph, message.snapshot);
135
- if (full) events = [];
136
- const arriving = message.snapshot.events;
137
- if (arriving?.length) events = [...events, ...arriving].slice(-HISTORY);
138
- stamp = Date.now();
139
- reason = null;
140
- });
141
- },
142
- transformIndexHtml: {
143
- order: "pre",
144
- handler(html, ctx) {
145
- const inherited = callInheritedHtml(baseTransformIndexHtml, html, ctx);
146
- if (!enabled() || !available || !wire) return inherited;
147
- return {
148
- html: typeof inherited === "string" ? inherited : html,
149
- tags: [
150
- {
151
- tag: "script",
152
- // First in the head, so the entry cannot create a node before the agent runs.
153
- injectTo: "head-prepend",
154
- attrs: { type: "module", src: `/@id/${VIRTUAL}` }
155
- }
156
- ]
157
- };
158
- }
159
- }
160
- };
161
- }
162
- function callInheritedHtml(hook, html, ctx) {
163
- if (typeof hook === "function") return hook(html, ctx);
164
- const handler = hook?.handler;
165
- return handler?.(html, ctx);
166
- }
167
-
168
- // src/share.ts
169
- import { isAbsolute, relative } from "node:path";
170
- var PREFIX = "share/";
171
- var chunkName = (id) => PREFIX + id.replace(/^@/, "").replace(/[^a-zA-Z0-9]+/g, "-");
172
- function share(options) {
173
- const modules = [...new Set(options.modules)];
174
- const names = new Map(modules.map((id) => [chunkName(id), id]));
175
- if (names.size !== modules.length) {
176
- throw new Error("[fluixi] share: two modules map to the same chunk name; list each id once.");
177
- }
178
- let base = "/";
179
- let browser = true;
180
- let serving = false;
181
- let root = process.cwd();
182
- let server;
183
- const devUrl = async (id) => {
184
- const client = server.environments.client;
185
- await client.depsOptimizer?.scanProcessing;
186
- const resolved = await client.pluginContainer.resolveId(id, `${root}/index.html`);
187
- if (!resolved) throw new Error(`[fluixi] share: the dev server cannot resolve ${id}.`);
188
- const file = resolved.id;
189
- const inside = !relative(root, file).startsWith("..") && !isAbsolute(relative(root, file));
190
- return base + (inside ? relative(root, file) : `@fs${file}`);
191
- };
192
- const importMap = (bundle) => {
193
- const imports = {};
194
- for (const file of Object.values(bundle)) {
195
- if (file.type !== "chunk" || !file.name) continue;
196
- const id = names.get(file.name);
197
- if (id) imports[id] = base + file.fileName;
198
- }
199
- const missing = modules.filter((id) => !imports[id]);
200
- if (missing.length) throw new Error(`[fluixi] share: no chunk was written for ${missing.join(", ")}.`);
201
- return { imports };
202
- };
203
- return {
204
- name: "fluixi:share",
205
- config(_, env) {
206
- return env.command === "serve" ? { optimizeDeps: { include: [...modules] } } : void 0;
207
- },
208
- configResolved(config) {
209
- serving = config.command === "serve";
210
- root = config.root;
211
- browser = !config.build.ssr;
212
- base = config.base.startsWith("/") ? config.base : "/";
213
- },
214
- configureServer(devServer) {
215
- server = devServer;
216
- },
217
- buildStart() {
218
- if (serving || !browser) return;
219
- for (const id of modules) {
220
- this.emitFile({ type: "chunk", id, name: chunkName(id), preserveSignature: "strict" });
221
- }
222
- },
223
- generateBundle(_, bundle) {
224
- if (serving || !browser) return;
225
- this.emitFile({
226
- type: "asset",
227
- fileName: options.manifest ?? "share.json",
228
- source: `${JSON.stringify(importMap(bundle), null, 2)}
229
- `
230
- });
231
- },
232
- transformIndexHtml: {
233
- order: "post",
234
- async handler(_html, ctx) {
235
- if (serving && server) {
236
- const imports = {};
237
- for (const id of modules) imports[id] = await devUrl(id);
238
- return [{ tag: "script", attrs: { type: "importmap" }, children: JSON.stringify({ imports }), injectTo: "head-prepend" }];
239
- }
240
- if (!browser || !ctx.bundle) return;
241
- return [{ tag: "script", attrs: { type: "importmap" }, children: JSON.stringify(importMap(ctx.bundle)), injectTo: "head-prepend" }];
242
- }
243
- }
244
- };
245
- }
1
+ /*! @fluixi/vite-plugin v1.0.0-alpha.110 | (c) 2026 Ibrahima Touré and Fluixi contributors | MIT */
2
+ import {
3
+ withGraphChannel
4
+ } from "./chunk-BYJCWN3C.mjs";
5
+ import {
6
+ share
7
+ } from "./chunk-YOXAUGOY.mjs";
246
8
 
247
9
  // src/index.ts
10
+ import { createVitePlugin, hotRewrite } from "@fluixi/compiler/integrations";
248
11
  function fluixi(options = {}) {
249
12
  const settings = options;
250
13
  let serving = false;
251
14
  const enabled = () => serving && settings.devtools !== false;
252
15
  const base = createVitePlugin(settings);
253
16
  const inherited = base.configResolved;
17
+ const compile = base.transform;
254
18
  const start = () => {
255
19
  settings.sourceLocations = true;
256
20
  settings.sourceLocationsExternal = typeof settings.devtools === "object" && settings.devtools.external === true;
@@ -264,16 +28,29 @@ function fluixi(options = {}) {
264
28
  await inherited?.(config);
265
29
  serving = config?.command === "serve";
266
30
  settings.sourceRoot = config.root;
31
+ },
32
+ // After the compiler, on the app's own files, in dev: the server render goes through it too,
33
+ // so both sides have the same structure to hydrate.
34
+ async transform(code, id, opts) {
35
+ const result = await compile.call(this, code, id, opts);
36
+ const own = settings.sourceRoot !== void 0 && id.startsWith(settings.sourceRoot.replace(/\/$/, "") + "/");
37
+ if (!serving || options.hot === false || !own || !/\.[cm]?[jt]sx?$/.test(id) || id.includes("/node_modules/") || id.startsWith("\0") || id.includes("?")) {
38
+ return result;
39
+ }
40
+ const compiled = !result ? { code, map: null } : typeof result === "string" ? { code: result, map: null } : result;
41
+ const file = id.slice(settings.sourceRoot.replace(/\/$/, "").length + 1);
42
+ const hot = hotRewrite(compiled.code, file, { keepState: typeof options.hot === "object" && !!options.hot.keepState });
43
+ return hot ? { ...compiled, code: hot } : result ?? null;
267
44
  }
268
45
  },
269
46
  enabled,
270
47
  start
271
48
  );
272
49
  }
273
- var index_default = fluixi;
50
+ var src_default = fluixi;
274
51
  export {
275
52
  createVitePlugin,
276
- index_default as default,
53
+ src_default as default,
277
54
  fluixi,
278
55
  share
279
56
  };
package/dist/share.mjs CHANGED
@@ -1,81 +1,6 @@
1
- // src/share.ts
2
- import { isAbsolute, relative } from "node:path";
3
- var PREFIX = "share/";
4
- var chunkName = (id) => PREFIX + id.replace(/^@/, "").replace(/[^a-zA-Z0-9]+/g, "-");
5
- function share(options) {
6
- const modules = [...new Set(options.modules)];
7
- const names = new Map(modules.map((id) => [chunkName(id), id]));
8
- if (names.size !== modules.length) {
9
- throw new Error("[fluixi] share: two modules map to the same chunk name; list each id once.");
10
- }
11
- let base = "/";
12
- let browser = true;
13
- let serving = false;
14
- let root = process.cwd();
15
- let server;
16
- const devUrl = async (id) => {
17
- const client = server.environments.client;
18
- await client.depsOptimizer?.scanProcessing;
19
- const resolved = await client.pluginContainer.resolveId(id, `${root}/index.html`);
20
- if (!resolved) throw new Error(`[fluixi] share: the dev server cannot resolve ${id}.`);
21
- const file = resolved.id;
22
- const inside = !relative(root, file).startsWith("..") && !isAbsolute(relative(root, file));
23
- return base + (inside ? relative(root, file) : `@fs${file}`);
24
- };
25
- const importMap = (bundle) => {
26
- const imports = {};
27
- for (const file of Object.values(bundle)) {
28
- if (file.type !== "chunk" || !file.name) continue;
29
- const id = names.get(file.name);
30
- if (id) imports[id] = base + file.fileName;
31
- }
32
- const missing = modules.filter((id) => !imports[id]);
33
- if (missing.length) throw new Error(`[fluixi] share: no chunk was written for ${missing.join(", ")}.`);
34
- return { imports };
35
- };
36
- return {
37
- name: "fluixi:share",
38
- config(_, env) {
39
- return env.command === "serve" ? { optimizeDeps: { include: [...modules] } } : void 0;
40
- },
41
- configResolved(config) {
42
- serving = config.command === "serve";
43
- root = config.root;
44
- browser = !config.build.ssr;
45
- base = config.base.startsWith("/") ? config.base : "/";
46
- },
47
- configureServer(devServer) {
48
- server = devServer;
49
- },
50
- buildStart() {
51
- if (serving || !browser) return;
52
- for (const id of modules) {
53
- this.emitFile({ type: "chunk", id, name: chunkName(id), preserveSignature: "strict" });
54
- }
55
- },
56
- generateBundle(_, bundle) {
57
- if (serving || !browser) return;
58
- this.emitFile({
59
- type: "asset",
60
- fileName: options.manifest ?? "share.json",
61
- source: `${JSON.stringify(importMap(bundle), null, 2)}
62
- `
63
- });
64
- },
65
- transformIndexHtml: {
66
- order: "post",
67
- async handler(_html, ctx) {
68
- if (serving && server) {
69
- const imports = {};
70
- for (const id of modules) imports[id] = await devUrl(id);
71
- return [{ tag: "script", attrs: { type: "importmap" }, children: JSON.stringify({ imports }), injectTo: "head-prepend" }];
72
- }
73
- if (!browser || !ctx.bundle) return;
74
- return [{ tag: "script", attrs: { type: "importmap" }, children: JSON.stringify(importMap(ctx.bundle)), injectTo: "head-prepend" }];
75
- }
76
- }
77
- };
78
- }
1
+ import {
2
+ share
3
+ } from "./chunk-YOXAUGOY.mjs";
79
4
  export {
80
5
  share
81
6
  };