@sigil-dev/grimoire 0.9.29 → 0.9.30
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/bin/sigil.ts +4 -0
- package/index.ts +16 -10
- package/package.json +14 -6
- package/server.ts +2 -2
- package/src/adapters/bun.ts +17 -17
- package/src/adapters/static.ts +41 -41
- package/src/adapters/types.ts +36 -36
- package/src/base.ts +4 -1
- package/src/client/csrf.ts +47 -47
- package/src/client/deferred.ts +1 -4
- package/src/client/enhance.ts +160 -160
- package/src/client/router.ts +927 -926
- package/src/config-file.ts +84 -0
- package/src/dev/compile-module.ts +199 -193
- package/src/dev/hmr-client.ts +229 -229
- package/src/dev/hmr-server.ts +274 -266
- package/src/dev/loader.ts +144 -144
- package/src/dev/runtime-bundle.ts +5 -3
- package/src/duration.ts +46 -0
- package/src/env/modules.ts +62 -62
- package/src/env/required.ts +42 -42
- package/src/env/typegen.ts +61 -61
- package/src/headers.ts +105 -105
- package/src/logger/transports.ts +117 -117
- package/src/plugin-order.ts +88 -0
- package/src/rendering/head.ts +49 -49
- package/src/rendering/hydrate.ts +162 -162
- package/src/rendering/images.ts +43 -43
- package/src/rendering/preload.ts +22 -8
- package/src/rendering/ssrPlugin.ts +107 -107
- package/src/rendering/timing.ts +40 -40
- package/src/routing/route-ids.ts +65 -0
- package/src/routing/route.ts +65 -0
- package/src/routing/router.ts +84 -32
- package/src/routing/scanner.ts +18 -5
- package/src/routing/transform-routes.ts +102 -102
- package/src/server/after-response.ts +206 -206
- package/src/server/asset-cache.ts +57 -57
- package/src/server/build.ts +304 -295
- package/src/server/cached.ts +114 -114
- package/src/server/client-address.ts +82 -82
- package/src/server/client-dir.ts +63 -63
- package/src/server/client-entry.ts +44 -44
- package/src/server/context.ts +18 -10
- package/src/server/cookie-utils.ts +80 -80
- package/src/server/coordinator.ts +476 -476
- package/src/server/hooks.ts +122 -122
- package/src/server/index.ts +8 -0
- package/src/server/plugins.ts +172 -172
- package/src/server/prerender.ts +473 -473
- package/src/server/reserved.ts +17 -17
- package/src/server/resolver.ts +149 -123
- package/src/server/rewrite.ts +28 -28
- package/src/server/warm.ts +9 -3
- package/src/server/with-headers.ts +27 -27
- package/src/server/worker.ts +2 -6
- package/src/server-only.ts +78 -0
- package/src/testing/dom.ts +73 -73
- package/src/testing/index.ts +169 -169
- package/src/testing/server.ts +1 -0
- package/src/typegen/index.ts +97 -2
- package/src/types.ts +140 -19
- package/test/after-response.test.ts +132 -132
- package/test/asset-cache-policy.test.ts +6 -2
- package/test/base.test.ts +20 -6
- package/test/cached.test.ts +126 -126
- package/test/client-address.test.ts +78 -78
- package/test/client-csrf.test.ts +61 -61
- package/test/client-dir.test.ts +101 -101
- package/test/client-router.test.ts +37 -5
- package/test/config-file.test.ts +84 -0
- package/test/coordinator.test.ts +9 -2
- package/test/deferred.test.ts +4 -2
- package/test/dev-loader.test.ts +30 -30
- package/test/env.test.ts +171 -171
- package/test/force-production.test.ts +7 -2
- package/test/images.test.ts +20 -8
- package/test/locals-header.test.ts +61 -61
- package/test/middleware.test.ts +243 -243
- package/test/parser-plugins.test.ts +98 -92
- package/test/plugin-handle.test.ts +62 -62
- package/test/plugin-order.test.ts +98 -0
- package/test/plugin-routes.test.ts +47 -42
- package/test/plugin-services-context.test.ts +7 -2
- package/test/preload.test.ts +4 -1
- package/test/prerender.test.ts +3 -1
- package/test/rendering.test.ts +3 -2
- package/test/required-env.test.ts +32 -32
- package/test/resolver-windows.test.ts +22 -23
- package/test/rewrite-html.test.ts +90 -90
- package/test/route-group-layouts.test.ts +38 -36
- package/test/route.test.ts +103 -0
- package/test/routing.test.ts +225 -197
- package/test/scanning.test.ts +53 -33
- package/test/server-context.test.ts +22 -0
- package/test/server-only.test.ts +110 -0
- package/test/server.test.ts +640 -640
- package/test/services.test.ts +3 -3
- package/test/streaming.test.ts +11 -6
- package/test/testing-helpers.test.ts +166 -166
- package/test/typegen.test.ts +209 -112
- package/test/warm.test.ts +96 -96
- package/test/worker-context.test.ts +59 -59
- package/test/workspace-files.test.ts +26 -10
- package/test/workspace-versions.test.ts +146 -131
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { orderPlugins } from "./plugin-order";
|
|
4
|
+
import type { GrimoireConfig } from "./types";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Environment variables that sigil.config.ts reads at the top level.
|
|
8
|
+
*
|
|
9
|
+
* The config is imported by `sigil build`, the coordinator and every CLI
|
|
10
|
+
* command, not only where requests are served, so a variable read while it
|
|
11
|
+
* loads is read in all of those places, and is usually unset in some of
|
|
12
|
+
* them. `adapter(Bun.env.DATABASE_URL!)` failing `sigil build` is the case
|
|
13
|
+
* this exists for.
|
|
14
|
+
*
|
|
15
|
+
* A read with a fallback (`?? x`, `|| x`) is left out: it has an answer
|
|
16
|
+
* when the variable is unset.
|
|
17
|
+
*
|
|
18
|
+
* Found in the source rather than by trapping the reads: `Bun.env` is a
|
|
19
|
+
* non-configurable property and cannot be swapped for a proxy. So this sees
|
|
20
|
+
* the config file itself, not modules it imports.
|
|
21
|
+
*/
|
|
22
|
+
export function envReadsIn(source: string): string[] {
|
|
23
|
+
const names = new Set<string>();
|
|
24
|
+
const re =
|
|
25
|
+
/\b(?:process\.env|Bun\.env|import\.meta\.env)(?:\.([A-Za-z_][A-Za-z0-9_]*)|\[\s*["'`]([A-Za-z_][A-Za-z0-9_]*)["'`]\s*\])(\s*(?:\?\?|\|\|))?/g;
|
|
26
|
+
for (const m of source.matchAll(re)) {
|
|
27
|
+
// `env.PORT ?? 3000` has an answer when the variable is unset.
|
|
28
|
+
if (m[3]) continue;
|
|
29
|
+
names.add((m[1] ?? m[2])!);
|
|
30
|
+
}
|
|
31
|
+
return [...names];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** The names among `reads` that are unset or empty in `env`. */
|
|
35
|
+
export function unsetConfigEnv(
|
|
36
|
+
reads: string[],
|
|
37
|
+
env: Record<string, string | undefined> = process.env,
|
|
38
|
+
): string[] {
|
|
39
|
+
return reads.filter((name) => !env[name]);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function configEnvWarning(file: string, unset: string[]): string {
|
|
43
|
+
const list = unset.join(", ");
|
|
44
|
+
return (
|
|
45
|
+
`${file} reads ${list} while it loads, and ${unset.length === 1 ? "it is" : "they are"} unset here. ` +
|
|
46
|
+
"The config is also loaded by `sigil build` and the CLI, where secrets usually are not set. " +
|
|
47
|
+
"Let the plugin read it when the server starts instead, e.g. `adapter()` reads DATABASE_URL itself."
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Import the project's sigil.config.ts, or `{}` when there is none.
|
|
53
|
+
*
|
|
54
|
+
* Every place that loads the config goes through here, so the warning about
|
|
55
|
+
* environment reads is said wherever the footgun would fire. A config that
|
|
56
|
+
* throws while loading is rethrown with the unset variables named, since an
|
|
57
|
+
* unset variable is the usual reason.
|
|
58
|
+
*/
|
|
59
|
+
export async function importConfig(
|
|
60
|
+
cwd: string,
|
|
61
|
+
options: { warn?: (message: string) => void } = {},
|
|
62
|
+
): Promise<GrimoireConfig> {
|
|
63
|
+
const file = join(cwd, "sigil.config.ts");
|
|
64
|
+
if (!existsSync(file)) return {};
|
|
65
|
+
const warn = options.warn ?? ((m: string) => console.warn(`[sigil] ${m}`));
|
|
66
|
+
let unset: string[] = [];
|
|
67
|
+
try {
|
|
68
|
+
unset = unsetConfigEnv(envReadsIn(readFileSync(file, "utf-8")));
|
|
69
|
+
} catch {}
|
|
70
|
+
let mod: { default?: GrimoireConfig };
|
|
71
|
+
try {
|
|
72
|
+
mod = await import(file);
|
|
73
|
+
} catch (e) {
|
|
74
|
+
if (unset.length && e instanceof Error) {
|
|
75
|
+
e.message += `\n\n${configEnvWarning("sigil.config.ts", unset)}`;
|
|
76
|
+
}
|
|
77
|
+
throw e;
|
|
78
|
+
}
|
|
79
|
+
if (unset.length) warn(configEnvWarning("sigil.config.ts", unset));
|
|
80
|
+
const config = mod.default ?? {};
|
|
81
|
+
return config.plugins
|
|
82
|
+
? { ...config, plugins: orderPlugins(config.plugins) }
|
|
83
|
+
: config;
|
|
84
|
+
}
|
|
@@ -1,193 +1,199 @@
|
|
|
1
|
-
import { createHash } from "node:crypto";
|
|
2
|
-
import { mkdirSync, writeFileSync } from "node:fs";
|
|
3
|
-
import { join, relative } from "node:path";
|
|
4
|
-
import { transformSync } from "@babel/core";
|
|
5
|
-
import sigilPlugin from "@sigil-dev/compiler/babel";
|
|
6
|
-
import {
|
|
7
|
-
clientStyleSnippet,
|
|
8
|
-
computeHash,
|
|
9
|
-
extractStyles,
|
|
10
|
-
} from "@sigil-dev/compiler/css";
|
|
11
|
-
import
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
14
|
-
import {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
if (specifier
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
ctx.
|
|
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
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
(
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
export
|
|
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
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
const
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { mkdirSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { join, relative } from "node:path";
|
|
4
|
+
import { transformSync } from "@babel/core";
|
|
5
|
+
import sigilPlugin from "@sigil-dev/compiler/babel";
|
|
6
|
+
import {
|
|
7
|
+
clientStyleSnippet,
|
|
8
|
+
computeHash,
|
|
9
|
+
extractStyles,
|
|
10
|
+
} from "@sigil-dev/compiler/css";
|
|
11
|
+
import { RUNTIME_URL } from "../server/client-dir";
|
|
12
|
+
import { serverOnlyError, serverOnlyReason } from "../server-only";
|
|
13
|
+
import type { DevGraph } from "./graph";
|
|
14
|
+
import { normalizePath } from "./paths";
|
|
15
|
+
import { absoluteAliases, resolveProjectImport } from "./resolve";
|
|
16
|
+
|
|
17
|
+
const MODULES_BASE = "/__grimoire__/m/";
|
|
18
|
+
const DEPS_BASE = "/__grimoire__/dep/";
|
|
19
|
+
|
|
20
|
+
export interface BrowserCompileContext {
|
|
21
|
+
projectRoot: string;
|
|
22
|
+
/** GrimoireConfig.alias — prefix → project-relative directory. */
|
|
23
|
+
aliases: Record<string, string>;
|
|
24
|
+
/** Supplies the version each dependency URL is stamped with. */
|
|
25
|
+
graph?: DevGraph;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** `/__grimoire__/m/src/lib/Card.tsx.js?v=3` for a project module. */
|
|
29
|
+
function moduleUrl(absPath: string, ctx: BrowserCompileContext): string {
|
|
30
|
+
const rel = relative(ctx.projectRoot, absPath).replace(/\\/g, "/");
|
|
31
|
+
const version = ctx.graph?.get(absPath)?.version ?? 0;
|
|
32
|
+
return `${MODULES_BASE}${rel}.js?v=${version}`;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Rewrite one import specifier to a browser-servable URL.
|
|
37
|
+
*
|
|
38
|
+
* Project modules carry their graph version in the query string: the browser
|
|
39
|
+
* caches ES modules by URL forever, so an edited component is only re-fetched
|
|
40
|
+
* when the importing module's own text points at a new URL.
|
|
41
|
+
*/
|
|
42
|
+
function rewriteSpecifier(
|
|
43
|
+
specifier: string,
|
|
44
|
+
fromFile: string,
|
|
45
|
+
ctx: BrowserCompileContext,
|
|
46
|
+
): string {
|
|
47
|
+
if (specifier === "@sigil-dev/runtime") return RUNTIME_URL;
|
|
48
|
+
if (specifier.startsWith("$env/"))
|
|
49
|
+
return `/__grimoire__/env/${specifier.slice("$env/".length)}.js`;
|
|
50
|
+
|
|
51
|
+
const resolved = resolveProjectImport(
|
|
52
|
+
specifier,
|
|
53
|
+
fromFile,
|
|
54
|
+
ctx.projectRoot,
|
|
55
|
+
ctx.aliases,
|
|
56
|
+
);
|
|
57
|
+
if (resolved) {
|
|
58
|
+
const reason = serverOnlyReason(resolved, ctx.projectRoot);
|
|
59
|
+
if (reason)
|
|
60
|
+
throw serverOnlyError(resolved, ctx.projectRoot, reason, fromFile);
|
|
61
|
+
return moduleUrl(resolved, ctx);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// unresolvable relative import (missing file) — keep a module URL so the
|
|
65
|
+
// browser reports the 404 against a path the developer recognises
|
|
66
|
+
if (specifier.startsWith(".") || specifier.startsWith("$lib/")) {
|
|
67
|
+
const rel = specifier.startsWith("$lib/")
|
|
68
|
+
? specifier.replace("$lib/", "src/lib/")
|
|
69
|
+
: relative(ctx.projectRoot, join(fromFile, "..", specifier)).replace(
|
|
70
|
+
/\\/g,
|
|
71
|
+
"/",
|
|
72
|
+
);
|
|
73
|
+
return `${MODULES_BASE}${rel}.js`;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const pkgName = specifier.startsWith("@")
|
|
77
|
+
? specifier.split("/").slice(0, 2).join("/")
|
|
78
|
+
: specifier.split("/")[0];
|
|
79
|
+
return `${DEPS_BASE}${pkgName!.replace("/", "__")}.js`;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Rewrite static `from "…"` / `import "…"` and dynamic `import("…")`. */
|
|
83
|
+
function rewriteImports(
|
|
84
|
+
code: string,
|
|
85
|
+
filePath: string,
|
|
86
|
+
ctx: BrowserCompileContext,
|
|
87
|
+
): string {
|
|
88
|
+
return code
|
|
89
|
+
.replace(
|
|
90
|
+
/(from\s+|import\s+)(["'])([^"']+)\2/g,
|
|
91
|
+
(_m, keyword: string, quote: string, specifier: string) =>
|
|
92
|
+
`${keyword}${quote}${rewriteSpecifier(specifier, filePath, ctx)}${quote}`,
|
|
93
|
+
)
|
|
94
|
+
.replace(
|
|
95
|
+
/(import\s*\(\s*)(["'])([^"']+)\2/g,
|
|
96
|
+
(_m, keyword: string, quote: string, specifier: string) =>
|
|
97
|
+
`${keyword}${quote}${rewriteSpecifier(specifier, filePath, ctx)}${quote}`,
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export interface CompileResult {
|
|
102
|
+
js: string;
|
|
103
|
+
css: string | null;
|
|
104
|
+
cssHash: string;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export async function compileForBrowser(
|
|
108
|
+
filePath: string,
|
|
109
|
+
ctx: BrowserCompileContext,
|
|
110
|
+
): Promise<CompileResult> {
|
|
111
|
+
const source = await Bun.file(filePath).text();
|
|
112
|
+
|
|
113
|
+
// the scope hash must be the compiler's own: a swapped module whose
|
|
114
|
+
// elements carry a different class than the emitted CSS loses its styles
|
|
115
|
+
const hash = computeHash(filePath);
|
|
116
|
+
const { code: sourceNoStyle, css } = extractStyles(source, hash, filePath);
|
|
117
|
+
|
|
118
|
+
// dom mode: swapped modules always re-render from scratch, they never
|
|
119
|
+
// claim server-rendered nodes
|
|
120
|
+
const babelResult = transformSync(sourceNoStyle, {
|
|
121
|
+
configFile: false,
|
|
122
|
+
babelrc: false,
|
|
123
|
+
parserOpts: { plugins: ["typescript", "jsx"] },
|
|
124
|
+
plugins: [
|
|
125
|
+
[
|
|
126
|
+
sigilPlugin,
|
|
127
|
+
{
|
|
128
|
+
mode: "dom",
|
|
129
|
+
hash: css ? hash : undefined,
|
|
130
|
+
dev: true,
|
|
131
|
+
aliases: absoluteAliases(ctx.projectRoot, ctx.aliases),
|
|
132
|
+
},
|
|
133
|
+
],
|
|
134
|
+
],
|
|
135
|
+
filename: filePath,
|
|
136
|
+
});
|
|
137
|
+
// strip TypeScript types
|
|
138
|
+
const transpiler = new Bun.Transpiler({ loader: "ts", target: "browser" });
|
|
139
|
+
let js = transpiler.transformSync(babelResult?.code ?? "");
|
|
140
|
+
|
|
141
|
+
// inject scoped CSS at runtime
|
|
142
|
+
if (css) js = clientStyleSnippet(hash, css) + js;
|
|
143
|
+
|
|
144
|
+
js = rewriteImports(js, filePath, ctx);
|
|
145
|
+
|
|
146
|
+
return { js, css, cssHash: hash };
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const cacheName = (absPath: string, version: number): string => {
|
|
150
|
+
// hashed so one flat cache dir holds every module, path separators and all
|
|
151
|
+
const hash = createHash("md5")
|
|
152
|
+
.update(normalizePath(absPath))
|
|
153
|
+
.digest("hex")
|
|
154
|
+
.slice(0, 8);
|
|
155
|
+
return `${hash}_v${version}.js`;
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Compile a module for the browser at its current graph version, memoised on
|
|
160
|
+
* disk. The version comes from the graph, not the request URL: a client may
|
|
161
|
+
* ask for a stale `?v=`, and serving anything but current code would
|
|
162
|
+
* reintroduce the mismatch HMR exists to avoid.
|
|
163
|
+
*/
|
|
164
|
+
export async function getDevModule(
|
|
165
|
+
absPath: string,
|
|
166
|
+
ctx: BrowserCompileContext,
|
|
167
|
+
): Promise<CompileResult> {
|
|
168
|
+
const version = ctx.graph?.get(absPath)?.version ?? 0;
|
|
169
|
+
const cachePath = join(
|
|
170
|
+
ctx.projectRoot,
|
|
171
|
+
".grimoire/dev-modules",
|
|
172
|
+
cacheName(absPath, version),
|
|
173
|
+
);
|
|
174
|
+
|
|
175
|
+
const cached = Bun.file(cachePath);
|
|
176
|
+
if (await cached.exists()) {
|
|
177
|
+
return { js: await cached.text(), css: null, cssHash: "" };
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const result = await compileForBrowser(absPath, ctx);
|
|
181
|
+
cacheDevModule(absPath, version, ctx.projectRoot, result.js);
|
|
182
|
+
return result;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Store already-compiled output for a version. Used by the HMR pipeline, which
|
|
187
|
+
* must compile the edited source *before* bumping the version — a failed
|
|
188
|
+
* compile has to leave the previous version live.
|
|
189
|
+
*/
|
|
190
|
+
export function cacheDevModule(
|
|
191
|
+
absPath: string,
|
|
192
|
+
version: number,
|
|
193
|
+
projectRoot: string,
|
|
194
|
+
js: string,
|
|
195
|
+
): void {
|
|
196
|
+
const dir = join(projectRoot, ".grimoire/dev-modules");
|
|
197
|
+
mkdirSync(dir, { recursive: true });
|
|
198
|
+
writeFileSync(join(dir, cacheName(absPath, version)), js);
|
|
199
|
+
}
|