@sigil-dev/grimoire 0.9.26 → 0.9.28

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.
@@ -6,7 +6,7 @@ import {
6
6
  serverStyleSnippet,
7
7
  } from "@sigil-dev/compiler/css";
8
8
  import { log } from "../logger/instance";
9
- import { rewriteAliases } from "../server/resolver";
9
+ import { pinRuntime, rewriteAliases } from "../server/resolver";
10
10
  import type { GrimoirePlugin } from "../types";
11
11
  import type { ParserPlugin } from "@babel/parser";
12
12
 
@@ -84,13 +84,12 @@ export function registerSSRPlugin(
84
84
  ? (["typescript", "jsx"] as ParserPlugin[])
85
85
  : (["typescript"] as ParserPlugin[]),
86
86
  },
87
- plugins: [[sigilPlugin, { mode: "ssr", hash }]],
87
+ plugins: [[sigilPlugin, { mode: "ssr", hash, aliases }]],
88
88
  filename: path,
89
89
  });
90
90
 
91
- let contents = rewriteAliases(
92
- transpiler.transformSync(result?.code ?? ""),
93
- aliases,
91
+ let contents = pinRuntime(
92
+ rewriteAliases(transpiler.transformSync(result?.code ?? ""), aliases),
94
93
  );
95
94
  if (styles.css && hash) {
96
95
  contents = serverStyleSnippet(hash, styles.css) + contents;
@@ -1,262 +1,295 @@
1
- import { mkdir } from "node:fs/promises";
2
- import { isAbsolute, join } from "node:path";
3
- import { sigil } from "@sigil-dev/compiler/bun";
4
- import { generateManifest } from "../routing/manifest-gen";
5
- import type { RouteTree } from "../routing/scanner";
6
- import { scanRoutes } from "../routing/scanner";
7
- import { transformRoutes } from "../routing/transform-routes";
8
- import { generateTypes } from "../typegen";
9
- import type { BuildResult, GrimoireConfig, GrimoirePlugin } from "../types";
10
- import { clientDir, pruneClientDir, resetClientDir } from "./client-dir";
11
- import { recordClientEntry } from "./client-entry";
12
- import { runHook } from "./plugins";
13
- import { projectAliases, resolverPlugin } from "./resolver";
14
-
15
- const makeRoutesPlugin = (manifestPath: string) => ({
16
- name: "grimoire-routes",
17
- setup(build: any) {
18
- build.onResolve({ filter: /^#grimoire-routes$/ }, () => ({
19
- path: manifestPath,
20
- }));
21
- },
22
- });
23
-
24
- /**
25
- * Rebuild the client bundles in dev.
26
- *
27
- * `hydrate.js` and the dom bundle are produced once at startup, so any full
28
- * reload after an edit would otherwise hand the browser pre-edit code. Route
29
- * files are re-transformed (Bun.build only ever sees plain JS) and the
30
- * manifests regenerated, which also picks up routes added since startup.
31
- * Returns the freshly scanned tree so the server can adopt it.
32
- */
33
- export async function rebuildClient(
34
- config: GrimoireConfig,
35
- plugins: GrimoirePlugin[] = [],
36
- ): Promise<{ result: BuildResult; tree: RouteTree }> {
37
- const { routes = "src/routes" } = config;
38
- const isDev = config.dev ?? false;
39
- const routesDir = isAbsolute(routes) ? routes : join(process.cwd(), routes);
40
-
41
- const tree = await scanRoutes(routesDir, process.cwd());
42
- const compiledDir = join(process.cwd(), ".grimoire/compiled");
43
- const clientRoutes = [
44
- ...tree.routes.filter((r) => r.type === "page" || r.type === "simple"),
45
- ...tree.layouts.filter((r) => r.type === "layout"),
46
- ];
47
-
48
- const hydrateManifest = join(process.cwd(), ".grimoire/_routes.hydrate.js");
49
- const domManifest = join(process.cwd(), ".grimoire/_routes.dom.js");
50
- const [hydrateFiles, domFiles] = await Promise.all([
51
- transformRoutes(clientRoutes, compiledDir, "hydrate", plugins),
52
- transformRoutes(clientRoutes, compiledDir, "dom", plugins),
53
- ]);
54
- await Promise.all([
55
- Bun.write(
56
- hydrateManifest,
57
- generateManifest(clientRoutes, hydrateFiles, tree.paramMatcherFiles),
58
- ),
59
- Bun.write(
60
- domManifest,
61
- generateManifest(clientRoutes, domFiles, tree.paramMatcherFiles),
62
- ),
63
- ]);
64
-
65
- const bundleDir = clientDir();
66
-
67
- const [hydrateResult, domResult] = await Promise.all([
68
- Bun.build({
69
- entrypoints: [join(import.meta.dir, "../rendering/hydrate.ts")],
70
- outdir: bundleDir,
71
- naming: { entry: "[name]-[hash].[ext]" },
72
- // report failures in result.logs instead of throwing past them
73
- throw: false,
74
- // lazy route entries in the manifest use dynamic import()
75
- splitting: true,
76
- plugins: [
77
- sigil({ mode: "hydrate", dev: isDev }),
78
- makeRoutesPlugin(hydrateManifest),
79
- resolverPlugin(config, "browser"),
80
- ],
81
- }),
82
- Bun.build({
83
- entrypoints: [join(import.meta.dir, "../client/index.ts")],
84
- outdir: bundleDir,
85
- naming: { entry: "[name]-[hash].[ext]" },
86
- // report failures in result.logs instead of throwing past them
87
- throw: false,
88
- splitting: true,
89
- plugins: [
90
- sigil({ mode: "dom", dev: isDev }),
91
- makeRoutesPlugin(domManifest),
92
- resolverPlugin(config, "browser"),
93
- ],
94
- }),
95
- ]);
96
-
97
- const outputs = [...hydrateResult.outputs, ...domResult.outputs].map(
98
- (o) => o.path,
99
- );
100
- const success = hydrateResult.success && domResult.success;
101
- // a failed build keeps the last good bundle in place
102
- if (success) {
103
- recordClientEntry([...hydrateResult.outputs, ...domResult.outputs]);
104
- await pruneClientDir(outputs);
105
- }
106
-
107
- return {
108
- tree,
109
- result: {
110
- success,
111
- outputs,
112
- errors: [...hydrateResult.logs, ...domResult.logs].map(String),
113
- },
114
- };
115
- }
116
-
117
- export async function buildProject(
118
- config: GrimoireConfig,
119
- plugins: GrimoirePlugin[] = [],
120
- ): Promise<{ result: BuildResult; tree: RouteTree }> {
121
- await runHook(plugins, "onBuildStart");
122
-
123
- const { routes = "src/routes" } = config;
124
- const isDev = config.dev ?? false;
125
-
126
- const routesDir = isAbsolute(routes)
127
- ? routes.replace(/\0/g, "")
128
- : join(process.cwd(), routes).replace(/\0/g, "");
129
- const tree = await scanRoutes(routesDir, process.cwd());
130
-
131
- const outDir = join(process.cwd(), ".grimoire");
132
- await mkdir(outDir, { recursive: true });
133
-
134
- await generateTypes(tree, {
135
- projectRoot: process.cwd(),
136
- routesDir,
137
- outDir: join(outDir, "types"),
138
- aliases: projectAliases(config),
139
- });
140
-
141
- const { generateEnvTypes } = await import("../env/typegen");
142
- const envTypeDir = join(outDir, "types");
143
- await mkdir(envTypeDir, { recursive: true });
144
- await generateEnvTypes(process.cwd(), envTypeDir);
145
-
146
- const compiledDir = join(outDir, "compiled");
147
- await mkdir(compiledDir, { recursive: true });
148
-
149
- const clientRoutes = [
150
- ...tree.routes.filter((r) => r.type === "page" || r.type === "simple"),
151
- ...tree.layouts.filter((r) => r.type === "layout"),
152
- ];
153
-
154
- const [hydrateFiles, domFiles] = await Promise.all([
155
- transformRoutes(clientRoutes, compiledDir, "hydrate", plugins),
156
- transformRoutes(clientRoutes, compiledDir, "dom", plugins),
157
- ]);
158
-
159
- const hydrateManifest = join(process.cwd(), ".grimoire/_routes.hydrate.js");
160
- const domManifest = join(process.cwd(), ".grimoire/_routes.dom.js");
161
- await Promise.all([
162
- Bun.write(
163
- hydrateManifest,
164
- generateManifest(clientRoutes, hydrateFiles, tree.paramMatcherFiles),
165
- ),
166
- Bun.write(
167
- domManifest,
168
- generateManifest(clientRoutes, domFiles, tree.paramMatcherFiles),
169
- ),
170
- ]);
171
-
172
- const bundleStrategy = config.bundleStrategy ?? "split";
173
- const bundleDir = clientDir();
174
- await resetClientDir();
175
-
176
- if (bundleStrategy === "single") {
177
- const singleResult = await Bun.build({
178
- entrypoints: [
179
- join(import.meta.dir, "../rendering/hydrate.ts"),
180
- join(import.meta.dir, "../client/index.ts"),
181
- ],
182
- outdir: bundleDir,
183
- naming: { entry: "[name]-[hash].[ext]" },
184
- // report failures in result.logs instead of throwing past them
185
- throw: false,
186
- // lazy route entries in the manifest use dynamic import()
187
- splitting: true,
188
- plugins: [
189
- sigil({ mode: "hydrate", dev: isDev }),
190
- sigil({ mode: "dom", dev: isDev }),
191
- makeRoutesPlugin(hydrateManifest),
192
- makeRoutesPlugin(domManifest),
193
- resolverPlugin(config, "browser"),
194
- ],
195
- });
196
-
197
- if (!singleResult.success) {
198
- for (const log of singleResult.logs) console.error(log);
199
- } else {
200
- recordClientEntry(singleResult.outputs);
201
- }
202
-
203
- const result: BuildResult = {
204
- success: singleResult.success,
205
- outputs: singleResult.outputs.map((o) => o.path),
206
- errors: singleResult.logs.map(String),
207
- };
208
-
209
- await runHook(plugins, "onBuildEnd", result);
210
- return { result, tree };
211
- }
212
-
213
- const [hydrateResult, domResult] = await Promise.all([
214
- Bun.build({
215
- entrypoints: [join(import.meta.dir, "../rendering/hydrate.ts")],
216
- outdir: bundleDir,
217
- naming: { entry: "[name]-[hash].[ext]" },
218
- // report failures in result.logs instead of throwing past them
219
- throw: false,
220
- // lazy route entries in the manifest use dynamic import()
221
- splitting: true,
222
- plugins: [
223
- sigil({ mode: "hydrate", dev: isDev }),
224
- makeRoutesPlugin(hydrateManifest),
225
- resolverPlugin(config, "browser"),
226
- ],
227
- }),
228
- Bun.build({
229
- entrypoints: [join(import.meta.dir, "../client/index.ts")],
230
- outdir: bundleDir,
231
- naming: { entry: "[name]-[hash].[ext]" },
232
- // report failures in result.logs instead of throwing past them
233
- throw: false,
234
- splitting: true,
235
- plugins: [
236
- sigil({ mode: "dom", dev: isDev }),
237
- makeRoutesPlugin(domManifest),
238
- resolverPlugin(config, "browser"),
239
- ],
240
- }),
241
- ]);
242
-
243
- if (!hydrateResult.success) {
244
- for (const log of hydrateResult.logs) console.error(log);
245
- } else {
246
- recordClientEntry(hydrateResult.outputs);
247
- }
248
- if (!domResult.success) {
249
- for (const log of domResult.logs) console.error(log);
250
- }
251
-
252
- const result: BuildResult = {
253
- success: hydrateResult.success && domResult.success,
254
- outputs: [...hydrateResult.outputs, ...domResult.outputs].map(
255
- (o) => o.path,
256
- ),
257
- errors: [...hydrateResult.logs, ...domResult.logs].map(String),
258
- };
259
-
260
- await runHook(plugins, "onBuildEnd", result);
261
- return { result, tree };
262
- }
1
+ import { mkdir } from "node:fs/promises";
2
+ import { isAbsolute, join } from "node:path";
3
+ import { sigil } from "@sigil-dev/compiler/bun";
4
+ import { generateManifest } from "../routing/manifest-gen";
5
+ import type { RouteTree } from "../routing/scanner";
6
+ import { scanRoutes } from "../routing/scanner";
7
+ import { transformRoutes } from "../routing/transform-routes";
8
+ import { generateTypes } from "../typegen";
9
+ import type { BuildResult, GrimoireConfig, GrimoirePlugin } from "../types";
10
+ import {
11
+ clientDir,
12
+ pruneClientDir,
13
+ RUNTIME_URL,
14
+ resetClientDir,
15
+ } from "./client-dir";
16
+ import { recordClientEntry } from "./client-entry";
17
+ import { runHook } from "./plugins";
18
+ import { projectAliases, resolverPlugin } from "./resolver";
19
+
20
+ const makeRoutesPlugin = (manifestPath: string) => ({
21
+ name: "grimoire-routes",
22
+ setup(build: any) {
23
+ build.onResolve({ filter: /^#grimoire-routes$/ }, () => ({
24
+ path: manifestPath,
25
+ }));
26
+ },
27
+ });
28
+
29
+ /**
30
+ * Dev only: the client bundles import the runtime from RUNTIME_URL, the file
31
+ * HMR-swapped modules import, instead of each carrying a copy.
32
+ *
33
+ * Two copies are two sets of module state. The one that broke: the router
34
+ * collects a re-render's effects with `collectEffects`, which sets a flag on
35
+ * globalThis that every copy reads, but queues into its own module. A swapped
36
+ * layout's effects (`{children}`, any prop in text) went into the other copy's
37
+ * queue, which nothing flushes, so the page vanished from the layout. Signals,
38
+ * context and effect scopes don't cross copies either.
39
+ *
40
+ * Production has no swapped modules, so it keeps bundling the runtime.
41
+ */
42
+ const sharedDevRuntime = {
43
+ name: "grimoire-shared-dev-runtime",
44
+ setup(build: any) {
45
+ build.onResolve({ filter: /^@sigil-dev\/runtime$/ }, () => ({
46
+ path: RUNTIME_URL,
47
+ external: true,
48
+ }));
49
+ },
50
+ };
51
+
52
+ /**
53
+ * Rebuild the client bundles in dev.
54
+ *
55
+ * `hydrate.js` and the dom bundle are produced once at startup, so any full
56
+ * reload after an edit would otherwise hand the browser pre-edit code. Route
57
+ * files are re-transformed (Bun.build only ever sees plain JS) and the
58
+ * manifests regenerated, which also picks up routes added since startup.
59
+ * Returns the freshly scanned tree so the server can adopt it.
60
+ */
61
+ export async function rebuildClient(
62
+ config: GrimoireConfig,
63
+ plugins: GrimoirePlugin[] = [],
64
+ ): Promise<{ result: BuildResult; tree: RouteTree }> {
65
+ const { routes = "src/routes" } = config;
66
+ const isDev = config.dev ?? false;
67
+ const routesDir = isAbsolute(routes) ? routes : join(process.cwd(), routes);
68
+
69
+ const tree = await scanRoutes(routesDir, process.cwd());
70
+ const compiledDir = join(process.cwd(), ".grimoire/compiled");
71
+ const clientRoutes = [
72
+ ...tree.routes.filter((r) => r.type === "page" || r.type === "simple"),
73
+ ...tree.layouts.filter((r) => r.type === "layout"),
74
+ ];
75
+
76
+ const hydrateManifest = join(process.cwd(), ".grimoire/_routes.hydrate.js");
77
+ const domManifest = join(process.cwd(), ".grimoire/_routes.dom.js");
78
+ const [hydrateFiles, domFiles] = await Promise.all([
79
+ transformRoutes(clientRoutes, compiledDir, "hydrate", plugins),
80
+ transformRoutes(clientRoutes, compiledDir, "dom", plugins),
81
+ ]);
82
+ await Promise.all([
83
+ Bun.write(
84
+ hydrateManifest,
85
+ generateManifest(clientRoutes, hydrateFiles, tree.paramMatcherFiles),
86
+ ),
87
+ Bun.write(
88
+ domManifest,
89
+ generateManifest(clientRoutes, domFiles, tree.paramMatcherFiles),
90
+ ),
91
+ ]);
92
+
93
+ const bundleDir = clientDir();
94
+
95
+ const [hydrateResult, domResult] = await Promise.all([
96
+ Bun.build({
97
+ entrypoints: [join(import.meta.dir, "../rendering/hydrate.ts")],
98
+ outdir: bundleDir,
99
+ naming: { entry: "[name]-[hash].[ext]" },
100
+ // report failures in result.logs instead of throwing past them
101
+ throw: false,
102
+ // lazy route entries in the manifest use dynamic import()
103
+ splitting: true,
104
+ plugins: [
105
+ ...(isDev ? [sharedDevRuntime] : []),
106
+ sigil({ mode: "hydrate", dev: isDev, aliases: projectAliases(config) }),
107
+ makeRoutesPlugin(hydrateManifest),
108
+ resolverPlugin(config, "browser"),
109
+ ],
110
+ }),
111
+ Bun.build({
112
+ entrypoints: [join(import.meta.dir, "../client/index.ts")],
113
+ outdir: bundleDir,
114
+ naming: { entry: "[name]-[hash].[ext]" },
115
+ // report failures in result.logs instead of throwing past them
116
+ throw: false,
117
+ splitting: true,
118
+ plugins: [
119
+ ...(isDev ? [sharedDevRuntime] : []),
120
+ sigil({ mode: "dom", dev: isDev, aliases: projectAliases(config) }),
121
+ makeRoutesPlugin(domManifest),
122
+ resolverPlugin(config, "browser"),
123
+ ],
124
+ }),
125
+ ]);
126
+
127
+ const outputs = [...hydrateResult.outputs, ...domResult.outputs].map(
128
+ (o) => o.path,
129
+ );
130
+ const success = hydrateResult.success && domResult.success;
131
+ // a failed build keeps the last good bundle in place
132
+ if (success) {
133
+ recordClientEntry([...hydrateResult.outputs, ...domResult.outputs]);
134
+ await pruneClientDir(outputs);
135
+ }
136
+
137
+ return {
138
+ tree,
139
+ result: {
140
+ success,
141
+ outputs,
142
+ errors: [...hydrateResult.logs, ...domResult.logs].map(String),
143
+ },
144
+ };
145
+ }
146
+
147
+ export async function buildProject(
148
+ config: GrimoireConfig,
149
+ plugins: GrimoirePlugin[] = [],
150
+ ): Promise<{ result: BuildResult; tree: RouteTree }> {
151
+ await runHook(plugins, "onBuildStart");
152
+
153
+ const { routes = "src/routes" } = config;
154
+ const isDev = config.dev ?? false;
155
+
156
+ const routesDir = isAbsolute(routes)
157
+ ? routes.replace(/\0/g, "")
158
+ : join(process.cwd(), routes).replace(/\0/g, "");
159
+ const tree = await scanRoutes(routesDir, process.cwd());
160
+
161
+ const outDir = join(process.cwd(), ".grimoire");
162
+ await mkdir(outDir, { recursive: true });
163
+
164
+ await generateTypes(tree, {
165
+ projectRoot: process.cwd(),
166
+ routesDir,
167
+ outDir: join(outDir, "types"),
168
+ aliases: projectAliases(config),
169
+ });
170
+
171
+ const { generateEnvTypes } = await import("../env/typegen");
172
+ const envTypeDir = join(outDir, "types");
173
+ await mkdir(envTypeDir, { recursive: true });
174
+ await generateEnvTypes(process.cwd(), envTypeDir);
175
+
176
+ const compiledDir = join(outDir, "compiled");
177
+ await mkdir(compiledDir, { recursive: true });
178
+
179
+ const clientRoutes = [
180
+ ...tree.routes.filter((r) => r.type === "page" || r.type === "simple"),
181
+ ...tree.layouts.filter((r) => r.type === "layout"),
182
+ ];
183
+
184
+ const [hydrateFiles, domFiles] = await Promise.all([
185
+ transformRoutes(clientRoutes, compiledDir, "hydrate", plugins),
186
+ transformRoutes(clientRoutes, compiledDir, "dom", plugins),
187
+ ]);
188
+
189
+ const hydrateManifest = join(process.cwd(), ".grimoire/_routes.hydrate.js");
190
+ const domManifest = join(process.cwd(), ".grimoire/_routes.dom.js");
191
+ await Promise.all([
192
+ Bun.write(
193
+ hydrateManifest,
194
+ generateManifest(clientRoutes, hydrateFiles, tree.paramMatcherFiles),
195
+ ),
196
+ Bun.write(
197
+ domManifest,
198
+ generateManifest(clientRoutes, domFiles, tree.paramMatcherFiles),
199
+ ),
200
+ ]);
201
+
202
+ const bundleStrategy = config.bundleStrategy ?? "split";
203
+ const bundleDir = clientDir();
204
+ await resetClientDir();
205
+
206
+ if (bundleStrategy === "single") {
207
+ const singleResult = await Bun.build({
208
+ entrypoints: [
209
+ join(import.meta.dir, "../rendering/hydrate.ts"),
210
+ join(import.meta.dir, "../client/index.ts"),
211
+ ],
212
+ outdir: bundleDir,
213
+ naming: { entry: "[name]-[hash].[ext]" },
214
+ // report failures in result.logs instead of throwing past them
215
+ throw: false,
216
+ // lazy route entries in the manifest use dynamic import()
217
+ splitting: true,
218
+ plugins: [
219
+ ...(isDev ? [sharedDevRuntime] : []),
220
+ sigil({ mode: "hydrate", dev: isDev, aliases: projectAliases(config) }),
221
+ sigil({ mode: "dom", dev: isDev, aliases: projectAliases(config) }),
222
+ makeRoutesPlugin(hydrateManifest),
223
+ makeRoutesPlugin(domManifest),
224
+ resolverPlugin(config, "browser"),
225
+ ],
226
+ });
227
+
228
+ if (!singleResult.success) {
229
+ for (const log of singleResult.logs) console.error(log);
230
+ } else {
231
+ recordClientEntry(singleResult.outputs);
232
+ }
233
+
234
+ const result: BuildResult = {
235
+ success: singleResult.success,
236
+ outputs: singleResult.outputs.map((o) => o.path),
237
+ errors: singleResult.logs.map(String),
238
+ };
239
+
240
+ await runHook(plugins, "onBuildEnd", result);
241
+ return { result, tree };
242
+ }
243
+
244
+ const [hydrateResult, domResult] = await Promise.all([
245
+ Bun.build({
246
+ entrypoints: [join(import.meta.dir, "../rendering/hydrate.ts")],
247
+ outdir: bundleDir,
248
+ naming: { entry: "[name]-[hash].[ext]" },
249
+ // report failures in result.logs instead of throwing past them
250
+ throw: false,
251
+ // lazy route entries in the manifest use dynamic import()
252
+ splitting: true,
253
+ plugins: [
254
+ ...(isDev ? [sharedDevRuntime] : []),
255
+ sigil({ mode: "hydrate", dev: isDev, aliases: projectAliases(config) }),
256
+ makeRoutesPlugin(hydrateManifest),
257
+ resolverPlugin(config, "browser"),
258
+ ],
259
+ }),
260
+ Bun.build({
261
+ entrypoints: [join(import.meta.dir, "../client/index.ts")],
262
+ outdir: bundleDir,
263
+ naming: { entry: "[name]-[hash].[ext]" },
264
+ // report failures in result.logs instead of throwing past them
265
+ throw: false,
266
+ splitting: true,
267
+ plugins: [
268
+ ...(isDev ? [sharedDevRuntime] : []),
269
+ sigil({ mode: "dom", dev: isDev, aliases: projectAliases(config) }),
270
+ makeRoutesPlugin(domManifest),
271
+ resolverPlugin(config, "browser"),
272
+ ],
273
+ }),
274
+ ]);
275
+
276
+ if (!hydrateResult.success) {
277
+ for (const log of hydrateResult.logs) console.error(log);
278
+ } else {
279
+ recordClientEntry(hydrateResult.outputs);
280
+ }
281
+ if (!domResult.success) {
282
+ for (const log of domResult.logs) console.error(log);
283
+ }
284
+
285
+ const result: BuildResult = {
286
+ success: hydrateResult.success && domResult.success,
287
+ outputs: [...hydrateResult.outputs, ...domResult.outputs].map(
288
+ (o) => o.path,
289
+ ),
290
+ errors: [...hydrateResult.logs, ...domResult.logs].map(String),
291
+ };
292
+
293
+ await runHook(plugins, "onBuildEnd", result);
294
+ return { result, tree };
295
+ }
@@ -8,6 +8,12 @@ const logger = log.scope("build");
8
8
  /** The URL the browser bundle is served under. */
9
9
  export const CLIENT_URL = "/__grimoire__/";
10
10
 
11
+ /**
12
+ * In dev, the one copy of @sigil-dev/runtime every browser module shares: the
13
+ * client bundles and HMR-swapped modules alike. See sharedDevRuntime.
14
+ */
15
+ export const RUNTIME_URL = `${CLIENT_URL}runtime.js`;
16
+
11
17
  /**
12
18
  * Where the browser bundle is written: hydrate.js, the client router, their
13
19
  * chunks, and in dev the runtime. Generated output, so it lives in .grimoire/
@@ -17,7 +23,7 @@ export const clientDir = (root = process.cwd()) =>
17
23
  join(root, ".grimoire", "client");
18
24
 
19
25
  /** Files dev writes beside the bundle, which a dev rebuild must keep. */
20
- const DEV_FILES = new Set(["runtime.js", "grimoire-client.js"]);
26
+ export const DEV_FILES = new Set(["runtime.js", "grimoire-client.js"]);
21
27
 
22
28
  /**
23
29
  * Empties the bundle directory before a build, so the chunks of the last one
@@ -12,11 +12,20 @@
12
12
  */
13
13
 
14
14
  import { copyFile, cp, mkdir, rm, writeFile } from "node:fs/promises";
15
- import { dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
15
+ import {
16
+ basename,
17
+ dirname,
18
+ isAbsolute,
19
+ join,
20
+ relative,
21
+ resolve,
22
+ sep,
23
+ } from "node:path";
16
24
  import type { AdapterContext } from "../adapters/types";
17
25
  import { normalizeBase, stripBase, withBase } from "../base";
18
26
  import { matchRoute } from "../routing/router";
19
27
  import type { RouteFile, RouteTree } from "../routing/scanner";
28
+ import { CLIENT_URL, clientDir, DEV_FILES } from "./client-dir";
20
29
  import { publicCssMap } from "./public-assets";
21
30
  import { RESERVED_EXPORTS } from "./reserved";
22
31
 
@@ -416,6 +425,14 @@ export async function prerenderSite(
416
425
  if (e.code !== "ENOENT") throw e;
417
426
  },
418
427
  );
428
+ // The browser bundle is built into .grimoire/client/, not public/, and
429
+ // every page loads it from CLIENT_URL; a static host has only what is here.
430
+ await cp(clientDir(root), join(outDir, CLIENT_URL.replaceAll("/", "")), {
431
+ recursive: true,
432
+ filter: (src) => !DEV_FILES.has(basename(src)),
433
+ }).catch((e: NodeJS.ErrnoException) => {
434
+ if (e.code !== "ENOENT") throw e;
435
+ });
419
436
  // The pages link /style.<hash>.css; a static host has only the plain name.
420
437
  // Both are written, so a page that still asks for the plain one works too.
421
438
  for (const [plain, hashed] of Object.entries(publicCssMap(false, root))) {