@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.
- package/package.json +3 -3
- package/src/client/router.ts +926 -914
- package/src/dev/compile-module.ts +193 -211
- package/src/dev/graph.ts +6 -5
- package/src/dev/hmr-server.ts +7 -2
- package/src/dev/loader.ts +144 -141
- package/src/dev/resolve.ts +85 -70
- package/src/rendering/hydrate.ts +162 -161
- package/src/rendering/index.ts +779 -779
- package/src/rendering/ssrPlugin.ts +4 -5
- package/src/server/build.ts +295 -262
- package/src/server/client-dir.ts +7 -1
- package/src/server/prerender.ts +18 -1
- package/src/server/resolver.ts +123 -102
- package/src/typegen/index.ts +61 -9
- package/test/client-router.test.ts +33 -0
- package/test/hmr.test.ts +24 -0
- package/test/prerender.test.ts +19 -0
- package/test/typegen.test.ts +109 -0
|
@@ -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 =
|
|
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;
|
package/src/server/build.ts
CHANGED
|
@@ -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 {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
]
|
|
64
|
-
|
|
65
|
-
const
|
|
66
|
-
|
|
67
|
-
const
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
]
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
const
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
const
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
),
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
const
|
|
173
|
-
|
|
174
|
-
await
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
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
|
+
}
|
package/src/server/client-dir.ts
CHANGED
|
@@ -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
|
package/src/server/prerender.ts
CHANGED
|
@@ -12,11 +12,20 @@
|
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
14
|
import { copyFile, cp, mkdir, rm, writeFile } from "node:fs/promises";
|
|
15
|
-
import {
|
|
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))) {
|