Skip to content

Commit 5329962

Browse files
committed
fix(directives): export var tracing and module invalidation
1 parent d080046 commit 5329962

2 files changed

Lines changed: 86 additions & 7 deletions

File tree

packages/start/src/directives/index.ts

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import { fileURLToPath } from "node:url";
2-
import { createFilter, type FilterPattern, normalizePath, type Plugin } from "vite";
2+
import {
3+
createFilter,
4+
EnvironmentModuleGraph,
5+
type FilterPattern,
6+
normalizePath,
7+
type Plugin,
8+
ViteDevServer,
9+
} from "vite";
310
import { compile, type CompileOptions } from "./compile.ts";
411

512
export interface ServerFunctionsFilter {
@@ -108,6 +115,40 @@ class Debouncer<T> {
108115
}
109116
}
110117

118+
function mergeManifestRecord(
119+
source: Set<string>,
120+
target: Set<string>,
121+
): { invalidPreload: boolean; invalidated: string[] } {
122+
const current = source.size;
123+
for (const entry of target) {
124+
source.add(entry);
125+
}
126+
return {
127+
invalidPreload: current !== source.size,
128+
invalidated: [...source],
129+
};
130+
}
131+
132+
function invalidateModule(moduleGraph: EnvironmentModuleGraph, path: string) {
133+
const target = moduleGraph.getModuleById(path);
134+
if (target) {
135+
moduleGraph.invalidateModule(target);
136+
}
137+
}
138+
139+
function invalidateModules(
140+
server: ViteDevServer | undefined,
141+
result: ReturnType<typeof mergeManifestRecord>,
142+
manifest: string,
143+
): void {
144+
if (server) {
145+
if (result.invalidPreload) {
146+
invalidateModule(server.environments.client.moduleGraph, manifest);
147+
invalidateModule(server.environments.ssr.moduleGraph, manifest);
148+
}
149+
}
150+
}
151+
111152
export function serverFunctionsPlugin(options: ServerFunctionsOptions): Plugin[] {
112153
const filter = createFilter(
113154
options.filter?.include || DEFAULT_INCLUDE,
@@ -122,6 +163,7 @@ export function serverFunctionsPlugin(options: ServerFunctionsOptions): Plugin[]
122163
server: undefined,
123164
client: undefined,
124165
};
166+
let currentServer: ViteDevServer;
125167

126168
return [
127169
{
@@ -130,6 +172,9 @@ export function serverFunctionsPlugin(options: ServerFunctionsOptions): Plugin[]
130172
configResolved(config) {
131173
env = config.mode !== "production" ? "development" : "production";
132174
},
175+
configureServer(server) {
176+
currentServer = server;
177+
},
133178
},
134179
{
135180
name: "solid-start:server-functions/preload",
@@ -161,19 +206,23 @@ export function serverFunctionsPlugin(options: ServerFunctionsOptions): Plugin[]
161206
if (!filter(id)) {
162207
return null;
163208
}
164-
const preloader = preload[mode];
165-
if (preloader) {
166-
preloader.defer();
167-
}
168209

169210
const result = await compile(id!, code, {
170-
...(mode === 'server' ? SERVER_OPTIONS : CLIENT_OPTIONS),
211+
...(mode === "server" ? SERVER_OPTIONS : CLIENT_OPTIONS),
171212
mode,
172213
env,
173214
});
174215

175216
if (result.valid) {
176-
manifest[mode].add(id!);
217+
const preloader = preload[mode];
218+
if (preloader) {
219+
preloader.defer();
220+
}
221+
invalidateModules(
222+
currentServer,
223+
mergeManifestRecord(manifest.server, new Set([id!])),
224+
options.manifest,
225+
);
177226

178227
return {
179228
code: result.code || "",

packages/start/src/directives/plugin.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,20 @@ function transformModuleLevelDirective(ctx: StateContext, program: babel.NodePat
225225
}
226226
}
227227
}
228+
const declarations = path.get("declaration");
229+
230+
if (isPathValid(declarations, t.isVariableDeclaration)) {
231+
for (const declaration of declarations.get("declarations")) {
232+
// Check if left is identifier
233+
const left = unwrapPath(declaration.get("id"), t.isIdentifier);
234+
if (left) {
235+
const binding = traceBinding(left, left.node.name);
236+
if (binding) {
237+
bindings.add(binding);
238+
}
239+
}
240+
}
241+
}
228242
},
229243
});
230244

@@ -264,6 +278,22 @@ function transformModuleLevelDirective(ctx: StateContext, program: babel.NodePat
264278
}
265279
}
266280
}
281+
282+
const declarations = path.get("declaration");
283+
284+
if (isPathValid(declarations, t.isVariableDeclaration)) {
285+
for (const declaration of declarations.get("declarations")) {
286+
// Check if left is identifier
287+
const left = unwrapPath(declaration.get("id"), t.isIdentifier);
288+
if (left) {
289+
const binding = traceBinding(left, left.node.name);
290+
if (binding) {
291+
uniqueBindings.add(binding);
292+
exportedBindings.set(left.node.name, binding);
293+
}
294+
}
295+
}
296+
}
267297
},
268298
});
269299

0 commit comments

Comments
 (0)