@sigil-dev/grimoire 0.9.29 → 0.9.31

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.
Files changed (105) hide show
  1. package/bin/sigil.ts +4 -0
  2. package/index.ts +16 -10
  3. package/package.json +14 -6
  4. package/server.ts +2 -2
  5. package/src/adapters/bun.ts +17 -17
  6. package/src/adapters/static.ts +41 -41
  7. package/src/adapters/types.ts +36 -36
  8. package/src/base.ts +4 -1
  9. package/src/client/csrf.ts +47 -47
  10. package/src/client/deferred.ts +1 -4
  11. package/src/client/enhance.ts +160 -160
  12. package/src/client/router.ts +927 -926
  13. package/src/config-file.ts +84 -0
  14. package/src/dev/compile-module.ts +199 -193
  15. package/src/dev/hmr-client.ts +229 -229
  16. package/src/dev/hmr-server.ts +274 -266
  17. package/src/dev/loader.ts +144 -144
  18. package/src/dev/runtime-bundle.ts +5 -3
  19. package/src/duration.ts +46 -0
  20. package/src/env/modules.ts +62 -62
  21. package/src/env/required.ts +42 -42
  22. package/src/env/typegen.ts +61 -61
  23. package/src/headers.ts +105 -105
  24. package/src/logger/transports.ts +117 -117
  25. package/src/plugin-order.ts +88 -0
  26. package/src/rendering/head.ts +49 -49
  27. package/src/rendering/hydrate.ts +162 -162
  28. package/src/rendering/images.ts +43 -43
  29. package/src/rendering/preload.ts +22 -8
  30. package/src/rendering/ssrPlugin.ts +107 -107
  31. package/src/rendering/timing.ts +40 -40
  32. package/src/routing/route-ids.ts +65 -0
  33. package/src/routing/route.ts +65 -0
  34. package/src/routing/router.ts +84 -32
  35. package/src/routing/scanner.ts +18 -5
  36. package/src/routing/transform-routes.ts +102 -102
  37. package/src/server/after-response.ts +206 -206
  38. package/src/server/asset-cache.ts +57 -57
  39. package/src/server/build.ts +304 -295
  40. package/src/server/cached.ts +114 -114
  41. package/src/server/client-address.ts +82 -82
  42. package/src/server/client-dir.ts +63 -63
  43. package/src/server/client-entry.ts +44 -44
  44. package/src/server/context.ts +18 -10
  45. package/src/server/cookie-utils.ts +80 -80
  46. package/src/server/coordinator.ts +479 -476
  47. package/src/server/hooks.ts +122 -122
  48. package/src/server/index.ts +1181 -1171
  49. package/src/server/plugins.ts +172 -172
  50. package/src/server/prerender.ts +473 -473
  51. package/src/server/reserved.ts +17 -17
  52. package/src/server/resolver.ts +149 -123
  53. package/src/server/rewrite.ts +28 -28
  54. package/src/server/warm.ts +9 -3
  55. package/src/server/with-headers.ts +27 -27
  56. package/src/server/worker.ts +2 -6
  57. package/src/server-only.ts +78 -0
  58. package/src/testing/dom.ts +73 -73
  59. package/src/testing/index.ts +169 -169
  60. package/src/testing/server.ts +1 -0
  61. package/src/typegen/index.ts +97 -2
  62. package/src/types.ts +642 -508
  63. package/test/after-response.test.ts +132 -132
  64. package/test/asset-cache-policy.test.ts +6 -2
  65. package/test/base.test.ts +20 -6
  66. package/test/cached.test.ts +126 -126
  67. package/test/client-address.test.ts +78 -78
  68. package/test/client-csrf.test.ts +61 -61
  69. package/test/client-dir.test.ts +101 -101
  70. package/test/client-router.test.ts +37 -5
  71. package/test/config-file.test.ts +84 -0
  72. package/test/coordinator.test.ts +9 -2
  73. package/test/deferred.test.ts +4 -2
  74. package/test/dev-loader.test.ts +30 -30
  75. package/test/env.test.ts +171 -171
  76. package/test/force-production.test.ts +7 -2
  77. package/test/images.test.ts +20 -8
  78. package/test/locals-header.test.ts +61 -61
  79. package/test/middleware.test.ts +243 -243
  80. package/test/parser-plugins.test.ts +98 -92
  81. package/test/plugin-handle.test.ts +62 -62
  82. package/test/plugin-order.test.ts +98 -0
  83. package/test/plugin-routes.test.ts +47 -42
  84. package/test/plugin-services-context.test.ts +7 -2
  85. package/test/preload.test.ts +4 -1
  86. package/test/prerender.test.ts +3 -1
  87. package/test/rendering.test.ts +3 -2
  88. package/test/required-env.test.ts +32 -32
  89. package/test/resolver-windows.test.ts +22 -23
  90. package/test/rewrite-html.test.ts +90 -90
  91. package/test/route-group-layouts.test.ts +38 -36
  92. package/test/route.test.ts +103 -0
  93. package/test/routing.test.ts +225 -197
  94. package/test/scanning.test.ts +53 -33
  95. package/test/server-context.test.ts +22 -0
  96. package/test/server-only.test.ts +110 -0
  97. package/test/server.test.ts +659 -640
  98. package/test/services.test.ts +3 -3
  99. package/test/streaming.test.ts +11 -6
  100. package/test/testing-helpers.test.ts +166 -166
  101. package/test/typegen.test.ts +209 -112
  102. package/test/warm.test.ts +96 -96
  103. package/test/worker-context.test.ts +59 -59
  104. package/test/workspace-files.test.ts +26 -10
  105. 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 type { DevGraph } from "./graph";
12
- import { normalizePath } from "./paths";
13
- import { RUNTIME_URL } from "../server/client-dir";
14
- import { absoluteAliases, resolveProjectImport } from "./resolve";
15
-
16
- const MODULES_BASE = "/__grimoire__/m/";
17
- const DEPS_BASE = "/__grimoire__/dep/";
18
-
19
- export interface BrowserCompileContext {
20
- projectRoot: string;
21
- /** GrimoireConfig.alias — prefix → project-relative directory. */
22
- aliases: Record<string, string>;
23
- /** Supplies the version each dependency URL is stamped with. */
24
- graph?: DevGraph;
25
- }
26
-
27
- /** `/__grimoire__/m/src/lib/Card.tsx.js?v=3` for a project module. */
28
- function moduleUrl(absPath: string, ctx: BrowserCompileContext): string {
29
- const rel = relative(ctx.projectRoot, absPath).replace(/\\/g, "/");
30
- const version = ctx.graph?.get(absPath)?.version ?? 0;
31
- return `${MODULES_BASE}${rel}.js?v=${version}`;
32
- }
33
-
34
- /**
35
- * Rewrite one import specifier to a browser-servable URL.
36
- *
37
- * Project modules carry their graph version in the query string: the browser
38
- * caches ES modules by URL forever, so an edited component is only re-fetched
39
- * when the importing module's own text points at a new URL.
40
- */
41
- function rewriteSpecifier(
42
- specifier: string,
43
- fromFile: string,
44
- ctx: BrowserCompileContext,
45
- ): string {
46
- if (specifier === "@sigil-dev/runtime") return RUNTIME_URL;
47
- if (specifier.startsWith("$env/"))
48
- return `/__grimoire__/env/${specifier.slice("$env/".length)}.js`;
49
-
50
- const resolved = resolveProjectImport(
51
- specifier,
52
- fromFile,
53
- ctx.projectRoot,
54
- ctx.aliases,
55
- );
56
- if (resolved) return moduleUrl(resolved, ctx);
57
-
58
- // unresolvable relative import (missing file) — keep a module URL so the
59
- // browser reports the 404 against a path the developer recognises
60
- if (specifier.startsWith(".") || specifier.startsWith("$lib/")) {
61
- const rel = specifier.startsWith("$lib/")
62
- ? specifier.replace("$lib/", "src/lib/")
63
- : relative(ctx.projectRoot, join(fromFile, "..", specifier)).replace(
64
- /\\/g,
65
- "/",
66
- );
67
- return `${MODULES_BASE}${rel}.js`;
68
- }
69
-
70
- const pkgName = specifier.startsWith("@")
71
- ? specifier.split("/").slice(0, 2).join("/")
72
- : specifier.split("/")[0];
73
- return `${DEPS_BASE}${pkgName!.replace("/", "__")}.js`;
74
- }
75
-
76
- /** Rewrite static `from "…"` / `import "…"` and dynamic `import("…")`. */
77
- function rewriteImports(
78
- code: string,
79
- filePath: string,
80
- ctx: BrowserCompileContext,
81
- ): string {
82
- return code
83
- .replace(
84
- /(from\s+|import\s+)(["'])([^"']+)\2/g,
85
- (_m, keyword: string, quote: string, specifier: string) =>
86
- `${keyword}${quote}${rewriteSpecifier(specifier, filePath, ctx)}${quote}`,
87
- )
88
- .replace(
89
- /(import\s*\(\s*)(["'])([^"']+)\2/g,
90
- (_m, keyword: string, quote: string, specifier: string) =>
91
- `${keyword}${quote}${rewriteSpecifier(specifier, filePath, ctx)}${quote}`,
92
- );
93
- }
94
-
95
- export interface CompileResult {
96
- js: string;
97
- css: string | null;
98
- cssHash: string;
99
- }
100
-
101
- export async function compileForBrowser(
102
- filePath: string,
103
- ctx: BrowserCompileContext,
104
- ): Promise<CompileResult> {
105
- const source = await Bun.file(filePath).text();
106
-
107
- // the scope hash must be the compiler's own: a swapped module whose
108
- // elements carry a different class than the emitted CSS loses its styles
109
- const hash = computeHash(filePath);
110
- const { code: sourceNoStyle, css } = extractStyles(source, hash, filePath);
111
-
112
- // dom mode: swapped modules always re-render from scratch, they never
113
- // claim server-rendered nodes
114
- const babelResult = transformSync(sourceNoStyle, {
115
- configFile: false,
116
- babelrc: false,
117
- parserOpts: { plugins: ["typescript", "jsx"] },
118
- plugins: [
119
- [
120
- sigilPlugin,
121
- {
122
- mode: "dom",
123
- hash: css ? hash : undefined,
124
- dev: true,
125
- aliases: absoluteAliases(ctx.projectRoot, ctx.aliases),
126
- },
127
- ],
128
- ],
129
- filename: filePath,
130
- });
131
- // strip TypeScript types
132
- const transpiler = new Bun.Transpiler({ loader: "ts", target: "browser" });
133
- let js = transpiler.transformSync(babelResult?.code ?? "");
134
-
135
- // inject scoped CSS at runtime
136
- if (css) js = clientStyleSnippet(hash, css) + js;
137
-
138
- js = rewriteImports(js, filePath, ctx);
139
-
140
- return { js, css, cssHash: hash };
141
- }
142
-
143
- const cacheName = (absPath: string, version: number): string => {
144
- // hashed so one flat cache dir holds every module, path separators and all
145
- const hash = createHash("md5")
146
- .update(normalizePath(absPath))
147
- .digest("hex")
148
- .slice(0, 8);
149
- return `${hash}_v${version}.js`;
150
- };
151
-
152
- /**
153
- * Compile a module for the browser at its current graph version, memoised on
154
- * disk. The version comes from the graph, not the request URL: a client may
155
- * ask for a stale `?v=`, and serving anything but current code would
156
- * reintroduce the mismatch HMR exists to avoid.
157
- */
158
- export async function getDevModule(
159
- absPath: string,
160
- ctx: BrowserCompileContext,
161
- ): Promise<CompileResult> {
162
- const version = ctx.graph?.get(absPath)?.version ?? 0;
163
- const cachePath = join(
164
- ctx.projectRoot,
165
- ".grimoire/dev-modules",
166
- cacheName(absPath, version),
167
- );
168
-
169
- const cached = Bun.file(cachePath);
170
- if (await cached.exists()) {
171
- return { js: await cached.text(), css: null, cssHash: "" };
172
- }
173
-
174
- const result = await compileForBrowser(absPath, ctx);
175
- cacheDevModule(absPath, version, ctx.projectRoot, result.js);
176
- return result;
177
- }
178
-
179
- /**
180
- * Store already-compiled output for a version. Used by the HMR pipeline, which
181
- * must compile the edited source *before* bumping the version — a failed
182
- * compile has to leave the previous version live.
183
- */
184
- export function cacheDevModule(
185
- absPath: string,
186
- version: number,
187
- projectRoot: string,
188
- js: string,
189
- ): void {
190
- const dir = join(projectRoot, ".grimoire/dev-modules");
191
- mkdirSync(dir, { recursive: true });
192
- writeFileSync(join(dir, cacheName(absPath, version)), js);
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
+ }