@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.
@@ -0,0 +1,165 @@
1
+ // src/graph-channel.ts
2
+ import { createRequire } from "node:module";
3
+ import { existsSync } from "node:fs";
4
+ import { join } from "node:path";
5
+ import { fileURLToPath } from "node:url";
6
+ var VIRTUAL = "virtual:fluixi/devtools-agent";
7
+ var RESOLVED = `\0${VIRTUAL}`;
8
+ var ENDPOINT = "/__fluixi/graph";
9
+ var EVENT = "fluixi:graph";
10
+ var MISSING = "the plugin could not load @fluixi/devtools from its own dependencies";
11
+ var WAITING = "no page has reported yet — open the application in a browser";
12
+ var INTERVAL = 100;
13
+ var HISTORY = 500;
14
+ var agentSource = (devtools, plugins, signal, reactive) => `
15
+ import { installDevtoolsHook, observeGraph } from ${JSON.stringify(devtools)};
16
+ import ${JSON.stringify(plugins)};
17
+ import { observeReactiveNodes } from ${JSON.stringify(signal)};
18
+ import { VERSION } from ${JSON.stringify(reactive)};
19
+
20
+ // The runtime this module imports is the application's own, same module graph, same nodes.
21
+ // Nothing waits for the runtime to offer itself; that path is for a tool loaded from outside.
22
+ //
23
+ // \`watch\` is this copy of devtools, handed over with the runtime. A browser extension
24
+ // installs a hook at document_start and so owns it; without this the graph would be built
25
+ // by that extension's build rather than the one shipped alongside this application.
26
+ const hook = installDevtoolsHook();
27
+ hook.inject({ observeReactiveNodes, version: VERSION, watch: observeGraph });
28
+
29
+ const send = (kind, snapshot) => import.meta.hot?.send(${JSON.stringify(EVENT)}, { kind, snapshot });
30
+
31
+ const first = hook.snapshot();
32
+ // Kept and handed back, so a browser panel polling the same hook cannot consume what this
33
+ // one has not been told about yet.
34
+ let cursor = first?.cursor;
35
+ send('full', first);
36
+ const timer = setInterval(() => {
37
+ const delta = hook.snapshot({ partial: true, since: cursor });
38
+ if (!delta) return;
39
+ cursor = delta.cursor;
40
+ // Events count on their own: an effect that ran without changing a value moves no node.
41
+ if (delta.nodes.length || delta.removed?.length || delta.events?.length) send('delta', delta);
42
+ }, ${INTERVAL});
43
+
44
+ import.meta.hot?.dispose(() => clearInterval(timer));
45
+ `;
46
+ function ownDevtools() {
47
+ return resolveForBrowser("@fluixi/devtools");
48
+ }
49
+ function ownPlugins() {
50
+ return resolveForBrowser("@fluixi/devtools/plugins");
51
+ }
52
+ function resolveForBrowser(spec) {
53
+ try {
54
+ const resolved = import.meta.resolve?.(spec);
55
+ if (resolved) return fileURLToPath(resolved);
56
+ } catch {
57
+ }
58
+ try {
59
+ const required = createRequire(import.meta.url).resolve(spec);
60
+ if (!required.endsWith(".cjs")) return required;
61
+ for (const ext of [".mjs", ".js"]) {
62
+ const sibling = required.slice(0, -".cjs".length) + ext;
63
+ if (existsSync(sibling)) return sibling;
64
+ }
65
+ return required;
66
+ } catch {
67
+ return null;
68
+ }
69
+ }
70
+ function withGraphChannel(base, enabled, start) {
71
+ let wire = null;
72
+ let available = false;
73
+ let reason = MISSING;
74
+ let root = process.cwd();
75
+ let log;
76
+ let graph = /* @__PURE__ */ new Map();
77
+ let stamp = 0;
78
+ let events = [];
79
+ const baseResolveId = base.resolveId;
80
+ const baseTransformIndexHtml = base.transformIndexHtml;
81
+ const baseLoad = base.load;
82
+ const baseConfigureServer = base.configureServer;
83
+ return {
84
+ ...base,
85
+ resolveId(id, importer) {
86
+ if (id === VIRTUAL) return RESOLVED;
87
+ return baseResolveId?.call(this, id, importer) ?? null;
88
+ },
89
+ async load(id) {
90
+ if (id !== RESOLVED) return await baseLoad?.call(this, id) ?? null;
91
+ const from = join(root, "fluixi-devtools-agent.js");
92
+ const specs = ["@fluixi/reactive/signal", "@fluixi/reactive"];
93
+ const ids = await Promise.all(specs.map((spec) => this.resolve?.(spec, from, { skipSelf: true })));
94
+ const missing = specs.filter((_, i) => !ids[i]?.id);
95
+ const devtools = ownDevtools();
96
+ const plugins = ownPlugins();
97
+ if (missing.length || !devtools || !plugins) {
98
+ reason = missing.length ? `${missing.join(", ")} did not resolve from ${root}` : MISSING;
99
+ log?.(`[fluixi] reactive graph off — ${reason}`);
100
+ return "export {};";
101
+ }
102
+ return agentSource(`/@fs/${devtools}`, `/@fs/${plugins}`, ids[0].id, ids[1].id);
103
+ },
104
+ async configureServer(server) {
105
+ await baseConfigureServer?.call(this, server);
106
+ if (!enabled()) return;
107
+ root = server.config?.root ?? root;
108
+ log = server.config?.logger?.info.bind(server.config.logger);
109
+ wire = await import("@fluixi/devtools/wire").catch(() => null);
110
+ available = wire !== null;
111
+ if (wire) start();
112
+ if (wire) reason = WAITING;
113
+ else {
114
+ log?.(`[fluixi] reactive graph off — ${MISSING}`);
115
+ }
116
+ server.middlewares.use(ENDPOINT, (_req, res) => {
117
+ const snapshot = wire ? { ...wire.serializeGraph(graph, stamp || Date.now()), events } : { t: Date.now(), nodes: [] };
118
+ const body = JSON.stringify(reason ? { ...snapshot, reason } : snapshot);
119
+ res.setHeader("Content-Type", "application/json");
120
+ res.setHeader("Access-Control-Allow-Origin", "*");
121
+ res.setHeader("Cache-Control", "no-store");
122
+ res.end(body);
123
+ });
124
+ if (!wire) return;
125
+ server.ws.on(EVENT, (payload) => {
126
+ const message = payload;
127
+ if (!message?.snapshot || !wire) return;
128
+ const full = message.kind === "full";
129
+ graph = wire.applySnapshot(full ? /* @__PURE__ */ new Map() : graph, message.snapshot);
130
+ if (full) events = [];
131
+ const arriving = message.snapshot.events;
132
+ if (arriving?.length) events = [...events, ...arriving].slice(-HISTORY);
133
+ stamp = Date.now();
134
+ reason = null;
135
+ });
136
+ },
137
+ transformIndexHtml: {
138
+ order: "pre",
139
+ handler(html, ctx) {
140
+ const inherited = callInheritedHtml(baseTransformIndexHtml, html, ctx);
141
+ if (!enabled() || !available || !wire) return inherited;
142
+ return {
143
+ html: typeof inherited === "string" ? inherited : html,
144
+ tags: [
145
+ {
146
+ tag: "script",
147
+ // First in the head, so the entry cannot create a node before the agent runs.
148
+ injectTo: "head-prepend",
149
+ attrs: { type: "module", src: `/@id/${VIRTUAL}` }
150
+ }
151
+ ]
152
+ };
153
+ }
154
+ }
155
+ };
156
+ }
157
+ function callInheritedHtml(hook, html, ctx) {
158
+ if (typeof hook === "function") return hook(html, ctx);
159
+ const handler = hook?.handler;
160
+ return handler?.(html, ctx);
161
+ }
162
+
163
+ export {
164
+ withGraphChannel
165
+ };
@@ -0,0 +1,82 @@
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
+ }
79
+
80
+ export {
81
+ share
82
+ };
@@ -1,164 +1,6 @@
1
- // src/graph-channel.ts
2
- import { createRequire } from "node:module";
3
- import { existsSync } from "node:fs";
4
- import { join } from "node:path";
5
- import { fileURLToPath } from "node:url";
6
- var VIRTUAL = "virtual:fluixi/devtools-agent";
7
- var RESOLVED = `\0${VIRTUAL}`;
8
- var ENDPOINT = "/__fluixi/graph";
9
- var EVENT = "fluixi:graph";
10
- var MISSING = "the plugin could not load @fluixi/devtools from its own dependencies";
11
- var WAITING = "no page has reported yet — open the application in a browser";
12
- var INTERVAL = 100;
13
- var HISTORY = 500;
14
- var agentSource = (devtools, plugins, signal, reactive) => `
15
- import { installDevtoolsHook, observeGraph } from ${JSON.stringify(devtools)};
16
- import ${JSON.stringify(plugins)};
17
- import { observeReactiveNodes } from ${JSON.stringify(signal)};
18
- import { VERSION } from ${JSON.stringify(reactive)};
19
-
20
- // The runtime this module imports is the application's own, same module graph, same nodes.
21
- // Nothing waits for the runtime to offer itself; that path is for a tool loaded from outside.
22
- //
23
- // \`watch\` is this copy of devtools, handed over with the runtime. A browser extension
24
- // installs a hook at document_start and so owns it; without this the graph would be built
25
- // by that extension's build rather than the one shipped alongside this application.
26
- const hook = installDevtoolsHook();
27
- hook.inject({ observeReactiveNodes, version: VERSION, watch: observeGraph });
28
-
29
- const send = (kind, snapshot) => import.meta.hot?.send(${JSON.stringify(EVENT)}, { kind, snapshot });
30
-
31
- const first = hook.snapshot();
32
- // Kept and handed back, so a browser panel polling the same hook cannot consume what this
33
- // one has not been told about yet.
34
- let cursor = first?.cursor;
35
- send('full', first);
36
- const timer = setInterval(() => {
37
- const delta = hook.snapshot({ partial: true, since: cursor });
38
- if (!delta) return;
39
- cursor = delta.cursor;
40
- // Events count on their own: an effect that ran without changing a value moves no node.
41
- if (delta.nodes.length || delta.removed?.length || delta.events?.length) send('delta', delta);
42
- }, ${INTERVAL});
43
-
44
- import.meta.hot?.dispose(() => clearInterval(timer));
45
- `;
46
- function ownDevtools() {
47
- return resolveForBrowser("@fluixi/devtools");
48
- }
49
- function ownPlugins() {
50
- return resolveForBrowser("@fluixi/devtools/plugins");
51
- }
52
- function resolveForBrowser(spec) {
53
- try {
54
- const resolved = import.meta.resolve?.(spec);
55
- if (resolved) return fileURLToPath(resolved);
56
- } catch {
57
- }
58
- try {
59
- const required = createRequire(import.meta.url).resolve(spec);
60
- if (!required.endsWith(".cjs")) return required;
61
- for (const ext of [".mjs", ".js"]) {
62
- const sibling = required.slice(0, -".cjs".length) + ext;
63
- if (existsSync(sibling)) return sibling;
64
- }
65
- return required;
66
- } catch {
67
- return null;
68
- }
69
- }
70
- function withGraphChannel(base, enabled, start) {
71
- let wire = null;
72
- let available = false;
73
- let reason = MISSING;
74
- let root = process.cwd();
75
- let log;
76
- let graph = /* @__PURE__ */ new Map();
77
- let stamp = 0;
78
- let events = [];
79
- const baseResolveId = base.resolveId;
80
- const baseTransformIndexHtml = base.transformIndexHtml;
81
- const baseLoad = base.load;
82
- const baseConfigureServer = base.configureServer;
83
- return {
84
- ...base,
85
- resolveId(id, importer) {
86
- if (id === VIRTUAL) return RESOLVED;
87
- return baseResolveId?.call(this, id, importer) ?? null;
88
- },
89
- async load(id) {
90
- if (id !== RESOLVED) return await baseLoad?.call(this, id) ?? null;
91
- const from = join(root, "fluixi-devtools-agent.js");
92
- const specs = ["@fluixi/reactive/signal", "@fluixi/reactive"];
93
- const ids = await Promise.all(specs.map((spec) => this.resolve?.(spec, from, { skipSelf: true })));
94
- const missing = specs.filter((_, i) => !ids[i]?.id);
95
- const devtools = ownDevtools();
96
- const plugins = ownPlugins();
97
- if (missing.length || !devtools || !plugins) {
98
- reason = missing.length ? `${missing.join(", ")} did not resolve from ${root}` : MISSING;
99
- log?.(`[fluixi] reactive graph off — ${reason}`);
100
- return "export {};";
101
- }
102
- return agentSource(`/@fs/${devtools}`, `/@fs/${plugins}`, ids[0].id, ids[1].id);
103
- },
104
- async configureServer(server) {
105
- await baseConfigureServer?.call(this, server);
106
- if (!enabled()) return;
107
- root = server.config?.root ?? root;
108
- log = server.config?.logger?.info.bind(server.config.logger);
109
- wire = await import("@fluixi/devtools/wire").catch(() => null);
110
- available = wire !== null;
111
- if (wire) start();
112
- if (wire) reason = WAITING;
113
- else {
114
- log?.(`[fluixi] reactive graph off — ${MISSING}`);
115
- }
116
- server.middlewares.use(ENDPOINT, (_req, res) => {
117
- const snapshot = wire ? { ...wire.serializeGraph(graph, stamp || Date.now()), events } : { t: Date.now(), nodes: [] };
118
- const body = JSON.stringify(reason ? { ...snapshot, reason } : snapshot);
119
- res.setHeader("Content-Type", "application/json");
120
- res.setHeader("Access-Control-Allow-Origin", "*");
121
- res.setHeader("Cache-Control", "no-store");
122
- res.end(body);
123
- });
124
- if (!wire) return;
125
- server.ws.on(EVENT, (payload) => {
126
- const message = payload;
127
- if (!message?.snapshot || !wire) return;
128
- const full = message.kind === "full";
129
- graph = wire.applySnapshot(full ? /* @__PURE__ */ new Map() : graph, message.snapshot);
130
- if (full) events = [];
131
- const arriving = message.snapshot.events;
132
- if (arriving?.length) events = [...events, ...arriving].slice(-HISTORY);
133
- stamp = Date.now();
134
- reason = null;
135
- });
136
- },
137
- transformIndexHtml: {
138
- order: "pre",
139
- handler(html, ctx) {
140
- const inherited = callInheritedHtml(baseTransformIndexHtml, html, ctx);
141
- if (!enabled() || !available || !wire) return inherited;
142
- return {
143
- html: typeof inherited === "string" ? inherited : html,
144
- tags: [
145
- {
146
- tag: "script",
147
- // First in the head, so the entry cannot create a node before the agent runs.
148
- injectTo: "head-prepend",
149
- attrs: { type: "module", src: `/@id/${VIRTUAL}` }
150
- }
151
- ]
152
- };
153
- }
154
- }
155
- };
156
- }
157
- function callInheritedHtml(hook, html, ctx) {
158
- if (typeof hook === "function") return hook(html, ctx);
159
- const handler = hook?.handler;
160
- return handler?.(html, ctx);
161
- }
1
+ import {
2
+ withGraphChannel
3
+ } from "./chunk-BYJCWN3C.mjs";
162
4
  export {
163
5
  withGraphChannel
164
6
  };
package/dist/index.cjs CHANGED
@@ -1,4 +1,4 @@
1
- /*! @fluixi/vite-plugin v1.0.0-alpha.108 | (c) 2026 Ibrahima Touré and Fluixi contributors | MIT */
1
+ /*! @fluixi/vite-plugin v1.0.0-alpha.110 | (c) 2026 Ibrahima Touré and Fluixi contributors | MIT */
2
2
  "use strict";
3
3
  var __create = Object.create;
4
4
  var __defProp = Object.defineProperty;
@@ -288,6 +288,7 @@ function fluixi(options = {}) {
288
288
  const enabled = () => serving && settings.devtools !== false;
289
289
  const base = (0, import_integrations.createVitePlugin)(settings);
290
290
  const inherited = base.configResolved;
291
+ const compile = base.transform;
291
292
  const start = () => {
292
293
  settings.sourceLocations = true;
293
294
  settings.sourceLocationsExternal = typeof settings.devtools === "object" && settings.devtools.external === true;
@@ -301,6 +302,19 @@ function fluixi(options = {}) {
301
302
  await inherited?.(config);
302
303
  serving = config?.command === "serve";
303
304
  settings.sourceRoot = config.root;
305
+ },
306
+ // After the compiler, on the app's own files, in dev: the server render goes through it too,
307
+ // so both sides have the same structure to hydrate.
308
+ async transform(code, id, opts) {
309
+ const result = await compile.call(this, code, id, opts);
310
+ const own = settings.sourceRoot !== void 0 && id.startsWith(settings.sourceRoot.replace(/\/$/, "") + "/");
311
+ if (!serving || options.hot === false || !own || !/\.[cm]?[jt]sx?$/.test(id) || id.includes("/node_modules/") || id.startsWith("\0") || id.includes("?")) {
312
+ return result;
313
+ }
314
+ const compiled = !result ? { code, map: null } : typeof result === "string" ? { code: result, map: null } : result;
315
+ const file = id.slice(settings.sourceRoot.replace(/\/$/, "").length + 1);
316
+ const hot = (0, import_integrations.hotRewrite)(compiled.code, file, { keepState: typeof options.hot === "object" && !!options.hot.keepState });
317
+ return hot ? { ...compiled, code: hot } : result ?? null;
304
318
  }
305
319
  },
306
320
  enabled,
package/dist/index.d.ts CHANGED
@@ -1,6 +1,18 @@
1
1
  import type { Plugin } from 'vite';
2
2
  import { createVitePlugin, type CompilerOptions } from '@fluixi/compiler/integrations';
3
- export type FluixiViteOptions = CompilerOptions;
3
+ export type FluixiViteOptions = CompilerOptions & {
4
+ /**
5
+ * Swap an edited component in place in dev, keeping the rest of the page's state, instead of
6
+ * reloading the page. Default true. `// @refresh reload` in a file opts that file out.
7
+ *
8
+ * `{ keepState: true }` also keeps the edited component's signals and stores, matched by the
9
+ * order its body creates them, and its children's, matched by position. Off by default;
10
+ * `// @refresh reset` in a file opts that file out.
11
+ */
12
+ hot?: boolean | {
13
+ keepState?: boolean;
14
+ };
15
+ };
4
16
  /**
5
17
  * Fluixi Vite plugin.
6
18
  *
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AACnC,OAAO,EAAE,gBAAgB,EAAE,KAAK,eAAe,EAAE,MAAM,+BAA+B,CAAC;AAGvF,MAAM,MAAM,iBAAiB,GAAG,eAAe,CAAC;AAEhD;;;;;;;;;;;;GAYG;AACH,wBAAgB,MAAM,CAAC,OAAO,GAAE,iBAAsB,GAAG,MAAM,CAuC9D;AAED,eAAe,MAAM,CAAC;AAEtB,OAAO,EAAE,KAAK,EAAE,KAAK,YAAY,EAAE,KAAK,aAAa,EAAE,MAAM,YAAY,CAAC;AAG1E,OAAO,EAAE,gBAAgB,EAAE,CAAC;AAC5B,YAAY,EAAE,eAAe,EAAE,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AACnC,OAAO,EAAE,gBAAgB,EAAc,KAAK,eAAe,EAAE,MAAM,+BAA+B,CAAC;AAGnG,MAAM,MAAM,iBAAiB,GAAG,eAAe,GAAG;IAChD;;;;;;;OAOG;IACH,GAAG,CAAC,EAAE,OAAO,GAAG;QAAE,SAAS,CAAC,EAAE,OAAO,CAAA;KAAE,CAAC;CACzC,CAAC;AAEF;;;;;;;;;;;;GAYG;AACH,wBAAgB,MAAM,CAAC,OAAO,GAAE,iBAAsB,GAAG,MAAM,CAyD9D;AAED,eAAe,MAAM,CAAC;AAEtB,OAAO,EAAE,KAAK,EAAE,KAAK,YAAY,EAAE,KAAK,aAAa,EAAE,MAAM,YAAY,CAAC;AAG1E,OAAO,EAAE,gBAAgB,EAAE,CAAC;AAC5B,YAAY,EAAE,eAAe,EAAE,CAAC"}
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { createVitePlugin } from '@fluixi/compiler/integrations';
1
+ import { createVitePlugin, hotRewrite } from '@fluixi/compiler/integrations';
2
2
  import { withGraphChannel } from './graph-channel.js';
3
3
  /**
4
4
  * Fluixi Vite plugin.
@@ -22,6 +22,7 @@ export function fluixi(options = {}) {
22
22
  const enabled = () => serving && settings.devtools !== false;
23
23
  const base = createVitePlugin(settings);
24
24
  const inherited = base.configResolved;
25
+ const compile = base.transform;
25
26
  const start = () => {
26
27
  settings.sourceLocations = true;
27
28
  // Marking a dependency is for someone working on the framework, so it is asked for.
@@ -40,6 +41,23 @@ export function fluixi(options = {}) {
40
41
  serving = config?.command === 'serve';
41
42
  settings.sourceRoot = config.root;
42
43
  },
44
+ // After the compiler, on the app's own files, in dev: the server render goes through it too,
45
+ // so both sides have the same structure to hydrate.
46
+ async transform(code, id, opts) {
47
+ const result = (await compile.call(this, code, id, opts));
48
+ // The app's own files only: a package linked from outside the root (a workspace copy of
49
+ // the framework or a component library) is left as it is.
50
+ const own = settings.sourceRoot !== undefined && id.startsWith(settings.sourceRoot.replace(/\/$/, '') + '/');
51
+ if (!serving || options.hot === false || !own || !/\.[cm]?[jt]sx?$/.test(id) || id.includes('/node_modules/') || id.startsWith('\0') || id.includes('?')) {
52
+ return result;
53
+ }
54
+ // A module with nothing to compile (no JSX, no html``) comes back null: a custom element
55
+ // in plain TypeScript still needs its swap.
56
+ const compiled = !result ? { code, map: null } : typeof result === 'string' ? { code: result, map: null } : result;
57
+ const file = id.slice(settings.sourceRoot.replace(/\/$/, '').length + 1);
58
+ const hot = hotRewrite(compiled.code, file, { keepState: typeof options.hot === 'object' && !!options.hot.keepState });
59
+ return hot ? { ...compiled, code: hot } : result ?? null;
60
+ },
43
61
  }, enabled, start);
44
62
  }
45
63
  export default fluixi;