@sigil-dev/grimoire 0.8.6 → 0.9.1
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 +4 -6
- package/server.ts +5 -1
- package/src/client/deps.ts +13 -0
- package/src/client/head.ts +1 -0
- package/src/client/router.ts +308 -228
- package/src/dev/compile-module.ts +1 -1
- package/src/dev/hmr-client.ts +30 -3
- package/src/dev/hmr-server.ts +187 -187
- package/src/dev/watcher.ts +4 -2
- package/src/env/dynamic-private.ts +19 -0
- package/src/env/dynamic-public.ts +22 -0
- package/src/env/plugin.ts +6 -0
- package/src/env/typegen.ts +10 -0
- package/src/logger/index.ts +0 -2
- package/src/rendering/index.ts +137 -63
- package/src/server/build.ts +199 -219
- package/src/server/coordinator.ts +316 -232
- package/src/server/index.ts +623 -622
- package/src/server/interactive.ts +56 -54
- package/src/server/plugins.ts +2 -1
- package/src/types.ts +282 -272
- package/test/client-router.test.ts +38 -0
- package/test/env.test.ts +90 -0
package/src/server/build.ts
CHANGED
|
@@ -11,229 +11,209 @@ import { generateTypes } from "../typegen";
|
|
|
11
11
|
import type { BuildResult, GrimoireConfig, GrimoirePlugin } from "../types";
|
|
12
12
|
import { runHook } from "./plugins";
|
|
13
13
|
|
|
14
|
+
function makeAliases(config: GrimoireConfig): Record<string, string> {
|
|
15
|
+
return {
|
|
16
|
+
...Object.fromEntries(
|
|
17
|
+
Object.entries(config.alias ?? {}).map(([k, v]) => [k, join(cwd(), v)]),
|
|
18
|
+
),
|
|
19
|
+
"$env/static/public": join(import.meta.dir, "../env/public.ts"),
|
|
20
|
+
"$env/static/private": join(import.meta.dir, "../env/private.ts"),
|
|
21
|
+
"$env/dynamic/public": join(import.meta.dir, "../env/dynamic-public.ts"),
|
|
22
|
+
"$env/dynamic/private": join(import.meta.dir, "../env/dynamic-private.ts"),
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const makeRoutesPlugin = (manifestPath: string) => ({
|
|
27
|
+
name: "grimoire-routes",
|
|
28
|
+
setup(build: any) {
|
|
29
|
+
build.onResolve({ filter: /^#grimoire-routes$/ }, () => ({
|
|
30
|
+
path: manifestPath,
|
|
31
|
+
}));
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
|
|
14
35
|
export async function rebuildForChange(
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
36
|
+
changedFilePath: string,
|
|
37
|
+
config: GrimoireConfig,
|
|
38
|
+
plugins: GrimoirePlugin[] = [],
|
|
18
39
|
): Promise<{ result: BuildResult }> {
|
|
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
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
plugins: [sigil({ mode: "hydrate" }), makeRoutesPlugin(hydrateManifest)],
|
|
82
|
-
// @ts-expect-error
|
|
83
|
-
alias: resolvedAlias,
|
|
84
|
-
}),
|
|
85
|
-
Bun.build({
|
|
86
|
-
entrypoints: [join(import.meta.dir, "../client/index.ts")],
|
|
87
|
-
outdir: publicDir,
|
|
88
|
-
plugins: [sigil({ mode: "dom" }), makeRoutesPlugin(domManifest)],
|
|
89
|
-
// @ts-expect-error
|
|
90
|
-
alias: resolvedAlias,
|
|
91
|
-
}),
|
|
92
|
-
]);
|
|
93
|
-
|
|
94
|
-
return {
|
|
95
|
-
result: {
|
|
96
|
-
success: hydrateResult.success && domResult.success,
|
|
97
|
-
outputs: [...hydrateResult.outputs, ...domResult.outputs].map(
|
|
98
|
-
(o) => o.path,
|
|
99
|
-
),
|
|
100
|
-
errors: [...hydrateResult.logs, ...domResult.logs].map(String),
|
|
101
|
-
},
|
|
102
|
-
};
|
|
40
|
+
const { routes = "src/routes" } = config;
|
|
41
|
+
const isDev = config.dev ?? false;
|
|
42
|
+
const routesDir = isAbsolute(routes) ? routes : join(process.cwd(), routes);
|
|
43
|
+
|
|
44
|
+
const tree = await scanRoutes(routesDir, process.cwd());
|
|
45
|
+
const changedRoute = [
|
|
46
|
+
...tree.routes.filter((r) => r.type === "page" || r.type === "simple"),
|
|
47
|
+
...tree.layouts.filter((r) => r.type === "layout"),
|
|
48
|
+
].find((r) => normalizePath(r.filePath) === normalizePath(changedFilePath));
|
|
49
|
+
|
|
50
|
+
const compiledDir = join(process.cwd(), ".grimoire/compiled");
|
|
51
|
+
|
|
52
|
+
if (changedRoute) {
|
|
53
|
+
await Promise.all([
|
|
54
|
+
transformRoutes([changedRoute], compiledDir, "hydrate", plugins),
|
|
55
|
+
transformRoutes([changedRoute], compiledDir, "dom", plugins),
|
|
56
|
+
]);
|
|
57
|
+
const allClientRoutes = [
|
|
58
|
+
...tree.routes.filter((r) => r.type === "page" || r.type === "simple"),
|
|
59
|
+
...tree.layouts.filter((r) => r.type === "layout"),
|
|
60
|
+
];
|
|
61
|
+
const hydrateManifest = join(process.cwd(), ".grimoire/_routes.hydrate.js");
|
|
62
|
+
const domManifest = join(process.cwd(), ".grimoire/_routes.dom.js");
|
|
63
|
+
const [allHFiles, allDFiles] = await Promise.all([
|
|
64
|
+
transformRoutes(allClientRoutes, compiledDir, "hydrate", plugins),
|
|
65
|
+
transformRoutes(allClientRoutes, compiledDir, "dom", plugins),
|
|
66
|
+
]);
|
|
67
|
+
await Promise.all([
|
|
68
|
+
Bun.write(hydrateManifest, generateManifest(allClientRoutes, allHFiles)),
|
|
69
|
+
Bun.write(domManifest, generateManifest(allClientRoutes, allDFiles)),
|
|
70
|
+
]);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const publicDir = join(process.cwd(), "public/__grimoire__");
|
|
74
|
+
const hydrateManifest = join(process.cwd(), ".grimoire/_routes.hydrate.js");
|
|
75
|
+
const domManifest = join(process.cwd(), ".grimoire/_routes.dom.js");
|
|
76
|
+
const resolvedAlias = makeAliases(config);
|
|
77
|
+
|
|
78
|
+
const [hydrateResult, domResult] = await Promise.all([
|
|
79
|
+
Bun.build({
|
|
80
|
+
entrypoints: [join(import.meta.dir, "../rendering/hydrate.ts")],
|
|
81
|
+
outdir: publicDir,
|
|
82
|
+
plugins: [sigil({ mode: "hydrate", dev: isDev }), makeRoutesPlugin(hydrateManifest)],
|
|
83
|
+
// @ts-expect-error
|
|
84
|
+
alias: resolvedAlias,
|
|
85
|
+
}),
|
|
86
|
+
Bun.build({
|
|
87
|
+
entrypoints: [join(import.meta.dir, "../client/index.ts")],
|
|
88
|
+
outdir: publicDir,
|
|
89
|
+
plugins: [sigil({ mode: "dom", dev: isDev }), makeRoutesPlugin(domManifest)],
|
|
90
|
+
// @ts-expect-error
|
|
91
|
+
alias: resolvedAlias,
|
|
92
|
+
}),
|
|
93
|
+
]);
|
|
94
|
+
|
|
95
|
+
return {
|
|
96
|
+
result: {
|
|
97
|
+
success: hydrateResult.success && domResult.success,
|
|
98
|
+
outputs: [...hydrateResult.outputs, ...domResult.outputs].map((o) => o.path),
|
|
99
|
+
errors: [...hydrateResult.logs, ...domResult.logs].map(String),
|
|
100
|
+
},
|
|
101
|
+
};
|
|
103
102
|
}
|
|
104
103
|
|
|
105
104
|
export async function buildProject(
|
|
106
|
-
|
|
107
|
-
|
|
105
|
+
config: GrimoireConfig,
|
|
106
|
+
plugins: GrimoirePlugin[] = [],
|
|
108
107
|
): Promise<{ result: BuildResult; tree: RouteTree }> {
|
|
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
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
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
|
-
if (!hydrateResult.success) {
|
|
223
|
-
for (const log of hydrateResult.logs) console.error(log);
|
|
224
|
-
}
|
|
225
|
-
if (!domResult.success) {
|
|
226
|
-
for (const log of domResult.logs) console.error(log);
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
const result: BuildResult = {
|
|
230
|
-
success: hydrateResult.success && domResult.success,
|
|
231
|
-
outputs: [...hydrateResult.outputs, ...domResult.outputs].map(
|
|
232
|
-
(o) => o.path,
|
|
233
|
-
),
|
|
234
|
-
errors: [...hydrateResult.logs, ...domResult.logs].map(String),
|
|
235
|
-
};
|
|
236
|
-
|
|
237
|
-
await runHook(plugins, "onBuildEnd", result);
|
|
238
|
-
return { result, tree };
|
|
108
|
+
await runHook(plugins, "onBuildStart");
|
|
109
|
+
|
|
110
|
+
const { routes = "src/routes" } = config;
|
|
111
|
+
const isDev = config.dev ?? false;
|
|
112
|
+
|
|
113
|
+
const routesDir = isAbsolute(routes)
|
|
114
|
+
? routes.replace(/\0/g, "")
|
|
115
|
+
: join(process.cwd(), routes).replace(/\0/g, "");
|
|
116
|
+
const tree = await scanRoutes(routesDir, process.cwd());
|
|
117
|
+
|
|
118
|
+
const outDir = join(process.cwd(), ".grimoire");
|
|
119
|
+
await mkdir(outDir, { recursive: true });
|
|
120
|
+
|
|
121
|
+
await generateTypes(tree, {
|
|
122
|
+
projectRoot: process.cwd(),
|
|
123
|
+
routesDir,
|
|
124
|
+
outDir: join(outDir, "types"),
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
const { generateEnvTypes } = await import("../env/typegen");
|
|
128
|
+
const envTypeDir = join(outDir, "types");
|
|
129
|
+
await mkdir(envTypeDir, { recursive: true });
|
|
130
|
+
await generateEnvTypes(process.cwd(), envTypeDir);
|
|
131
|
+
|
|
132
|
+
const compiledDir = join(outDir, "compiled");
|
|
133
|
+
await mkdir(compiledDir, { recursive: true });
|
|
134
|
+
|
|
135
|
+
const clientRoutes = [
|
|
136
|
+
...tree.routes.filter((r) => r.type === "page" || r.type === "simple"),
|
|
137
|
+
...tree.layouts.filter((r) => r.type === "layout"),
|
|
138
|
+
];
|
|
139
|
+
|
|
140
|
+
const [hydrateFiles, domFiles] = await Promise.all([
|
|
141
|
+
transformRoutes(clientRoutes, compiledDir, "hydrate", plugins),
|
|
142
|
+
transformRoutes(clientRoutes, compiledDir, "dom", plugins),
|
|
143
|
+
]);
|
|
144
|
+
|
|
145
|
+
const hydrateManifest = join(process.cwd(), ".grimoire/_routes.hydrate.js");
|
|
146
|
+
const domManifest = join(process.cwd(), ".grimoire/_routes.dom.js");
|
|
147
|
+
await Promise.all([
|
|
148
|
+
Bun.write(hydrateManifest, generateManifest(clientRoutes, hydrateFiles)),
|
|
149
|
+
Bun.write(domManifest, generateManifest(clientRoutes, domFiles)),
|
|
150
|
+
]);
|
|
151
|
+
|
|
152
|
+
const resolvedAlias = makeAliases(config);
|
|
153
|
+
const bundleStrategy = config.bundleStrategy ?? "split";
|
|
154
|
+
const publicDir = join(process.cwd(), "public/__grimoire__");
|
|
155
|
+
|
|
156
|
+
if (bundleStrategy === "single") {
|
|
157
|
+
const singleResult = await Bun.build({
|
|
158
|
+
entrypoints: [
|
|
159
|
+
join(import.meta.dir, "../rendering/hydrate.ts"),
|
|
160
|
+
join(import.meta.dir, "../client/index.ts"),
|
|
161
|
+
],
|
|
162
|
+
outdir: publicDir,
|
|
163
|
+
plugins: [
|
|
164
|
+
sigil({ mode: "hydrate", dev: isDev }),
|
|
165
|
+
sigil({ mode: "dom", dev: isDev }),
|
|
166
|
+
makeRoutesPlugin(hydrateManifest),
|
|
167
|
+
makeRoutesPlugin(domManifest),
|
|
168
|
+
],
|
|
169
|
+
// @ts-expect-error
|
|
170
|
+
alias: resolvedAlias,
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
if (!singleResult.success) {
|
|
174
|
+
for (const log of singleResult.logs) console.error(log);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const result: BuildResult = {
|
|
178
|
+
success: singleResult.success,
|
|
179
|
+
outputs: singleResult.outputs.map((o) => o.path),
|
|
180
|
+
errors: singleResult.logs.map(String),
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
await runHook(plugins, "onBuildEnd", result);
|
|
184
|
+
return { result, tree };
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const [hydrateResult, domResult] = await Promise.all([
|
|
188
|
+
Bun.build({
|
|
189
|
+
entrypoints: [join(import.meta.dir, "../rendering/hydrate.ts")],
|
|
190
|
+
outdir: publicDir,
|
|
191
|
+
plugins: [sigil({ mode: "hydrate", dev: isDev }), makeRoutesPlugin(hydrateManifest)],
|
|
192
|
+
// @ts-expect-error
|
|
193
|
+
alias: resolvedAlias,
|
|
194
|
+
}),
|
|
195
|
+
Bun.build({
|
|
196
|
+
entrypoints: [join(import.meta.dir, "../client/index.ts")],
|
|
197
|
+
outdir: publicDir,
|
|
198
|
+
plugins: [sigil({ mode: "dom", dev: isDev }), makeRoutesPlugin(domManifest)],
|
|
199
|
+
// @ts-expect-error
|
|
200
|
+
alias: resolvedAlias,
|
|
201
|
+
}),
|
|
202
|
+
]);
|
|
203
|
+
|
|
204
|
+
if (!hydrateResult.success) {
|
|
205
|
+
for (const log of hydrateResult.logs) console.error(log);
|
|
206
|
+
}
|
|
207
|
+
if (!domResult.success) {
|
|
208
|
+
for (const log of domResult.logs) console.error(log);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
const result: BuildResult = {
|
|
212
|
+
success: hydrateResult.success && domResult.success,
|
|
213
|
+
outputs: [...hydrateResult.outputs, ...domResult.outputs].map((o) => o.path),
|
|
214
|
+
errors: [...hydrateResult.logs, ...domResult.logs].map(String),
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
await runHook(plugins, "onBuildEnd", result);
|
|
218
|
+
return { result, tree };
|
|
239
219
|
}
|