@sigil-dev/grimoire 0.9.2 → 0.9.4
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/index.ts +37 -35
- package/package.json +9 -12
- package/preload.js +3 -3
- package/server.ts +19 -19
- package/src/client/deps.ts +31 -8
- package/src/client/enhance.ts +102 -98
- package/src/client/head.ts +30 -30
- package/src/client/index.ts +5 -5
- package/src/client/router.ts +529 -371
- package/src/client/scope.ts +22 -22
- package/src/dev/compile-module.ts +163 -163
- package/src/dev/effect-registry.ts +23 -23
- package/src/dev/graph.ts +114 -114
- package/src/dev/hmr-client.ts +185 -185
- package/src/dev/loader.ts +47 -47
- package/src/dev/paths.ts +14 -14
- package/src/dev/runtime-bundle.ts +52 -52
- package/src/dev/watcher.ts +47 -46
- package/src/env/dynamic-private.ts +19 -19
- package/src/env/dynamic-public.ts +22 -22
- package/src/env/index.ts +25 -25
- package/src/env/plugin.ts +19 -19
- package/src/env/private.ts +5 -5
- package/src/env/public.ts +7 -7
- package/src/env/typegen.ts +61 -61
- package/src/headers.ts +114 -114
- package/src/integrations/vite.ts +73 -73
- package/src/logger/index.ts +4 -4
- package/src/logger/instance.ts +6 -6
- package/src/logger/logger.ts +113 -113
- package/src/logger/request-middleware.ts +24 -24
- package/src/logger/transports.ts +117 -117
- package/src/logger/types.ts +39 -39
- package/src/rendering/head.ts +47 -47
- package/src/rendering/hydrate.ts +146 -119
- package/src/rendering/index.ts +573 -372
- package/src/rendering/ssrPlugin.ts +69 -69
- package/src/routing/manifest-gen.ts +55 -42
- package/src/routing/router.ts +135 -109
- package/src/routing/scanner.ts +187 -141
- package/src/routing/transform-routes.ts +102 -102
- package/src/sentinels/error.ts +52 -52
- package/src/sentinels/fail.ts +41 -41
- package/src/sentinels/redirect.ts +35 -35
- package/src/server/build.ts +255 -219
- package/src/server/context.ts +10 -10
- package/src/server/cookie-utils.ts +66 -66
- package/src/server/coordinator.ts +364 -356
- package/src/server/hooks.ts +114 -114
- package/src/server/index.ts +677 -658
- package/src/server/interactive.ts +56 -56
- package/src/server/plugins.ts +121 -120
- package/src/server/worker.ts +58 -58
- package/src/typegen/index.ts +389 -356
- package/src/types.ts +8 -4
- package/test/client-router.test.ts +295 -35
- package/test/context.test.ts +52 -52
- package/test/env.test.ts +70 -70
- package/test/exports.test.ts +37 -37
- package/test/fail.test.ts +46 -46
- package/test/headers.test.ts +100 -100
- package/test/hydration.test.ts +119 -119
- package/test/middleware.test.ts +223 -223
- package/test/preload.ts +5 -5
- package/test/redirect-error.test.ts +112 -112
- package/test/rendering.test.ts +728 -579
- package/test/routing.test.ts +194 -81
- package/test/scanning.test.ts +254 -200
- package/test/scope.test.ts +195 -195
- package/test/server.test.ts +354 -249
- package/test/streaming.test.ts +242 -125
- package/test/transform-routes.test.ts +87 -84
- package/test/typegen.test.ts +697 -682
- package/tsconfig.json +10 -10
- package/.grimoire/_routes.dom.js +0 -8
- package/.grimoire/_routes.hydrate.js +0 -8
- package/.grimoire/tsconfig.generated.json +0 -11
- package/.grimoire/types/ambient.d.ts +0 -59
- package/.grimoire/types/api/hello/$types.d.ts +0 -50
- package/.grimoire/types/api/items/$types.d.ts +0 -50
- package/.grimoire/types/echo/$types.d.ts +0 -50
- package/.grimoire/types/env-private.d.ts +0 -5
- package/.grimoire/types/env-public.d.ts +0 -5
- package/.grimoire/types/mixed/$types.d.ts +0 -50
- package/.grimoire/types/params/[docId]/$types.d.ts +0 -52
- package/.grimoire/types/reject/$types.d.ts +0 -50
- package/public/__grimoire__/hydrate.js +0 -585
- package/public/__grimoire__/index.js +0 -490
package/src/client/scope.ts
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Open an effect scope: run fn() and return a dispose function that tears down
|
|
3
|
-
* every createEffect registered during that call.
|
|
4
|
-
*
|
|
5
|
-
* Communicates with @sigil-dev/runtime's createEffect via globalThis.__sigilEffectScope
|
|
6
|
-
* so this file does not need to import from @sigil-dev/runtime (which would pull the
|
|
7
|
-
* runtime source into Bun.build and break the test sandbox).
|
|
8
|
-
*/
|
|
9
|
-
export function withEffectScope(fn: () => void): () => void {
|
|
10
|
-
const prev = (globalThis as any).__sigilEffectScope;
|
|
11
|
-
const disposers: (() => void)[] = [];
|
|
12
|
-
(globalThis as any).__sigilEffectScope = disposers;
|
|
13
|
-
try {
|
|
14
|
-
fn();
|
|
15
|
-
} finally {
|
|
16
|
-
(globalThis as any).__sigilEffectScope = prev;
|
|
17
|
-
}
|
|
18
|
-
return () => {
|
|
19
|
-
for (const d of disposers) d();
|
|
20
|
-
disposers.length = 0;
|
|
21
|
-
};
|
|
22
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Open an effect scope: run fn() and return a dispose function that tears down
|
|
3
|
+
* every createEffect registered during that call.
|
|
4
|
+
*
|
|
5
|
+
* Communicates with @sigil-dev/runtime's createEffect via globalThis.__sigilEffectScope
|
|
6
|
+
* so this file does not need to import from @sigil-dev/runtime (which would pull the
|
|
7
|
+
* runtime source into Bun.build and break the test sandbox).
|
|
8
|
+
*/
|
|
9
|
+
export function withEffectScope(fn: () => void): () => void {
|
|
10
|
+
const prev = (globalThis as any).__sigilEffectScope;
|
|
11
|
+
const disposers: (() => void)[] = [];
|
|
12
|
+
(globalThis as any).__sigilEffectScope = disposers;
|
|
13
|
+
try {
|
|
14
|
+
fn();
|
|
15
|
+
} finally {
|
|
16
|
+
(globalThis as any).__sigilEffectScope = prev;
|
|
17
|
+
}
|
|
18
|
+
return () => {
|
|
19
|
+
for (const d of disposers) d();
|
|
20
|
+
disposers.length = 0;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
@@ -1,163 +1,163 @@
|
|
|
1
|
-
import { existsSync } from "node:fs";
|
|
2
|
-
import { dirname, join, relative } from "node:path";
|
|
3
|
-
import { pathToFileURL } from "node:url";
|
|
4
|
-
|
|
5
|
-
const RUNTIME_URL = "/__grimoire__/runtime.js";
|
|
6
|
-
const MODULES_BASE = "/__grimoire__/m/";
|
|
7
|
-
const DEPS_BASE = "/__grimoire__/dep/";
|
|
8
|
-
/**
|
|
9
|
-
* Rewrite a single import specifier to a browser-servable URL.
|
|
10
|
-
* project-relative → /__grimoire__/m/...
|
|
11
|
-
* @sigil-dev/runtime → /__grimoire__/runtime.js
|
|
12
|
-
* bare npm → /__grimoire__/dep/...
|
|
13
|
-
* relative → /__grimoire__/m/... (resolved)
|
|
14
|
-
*/
|
|
15
|
-
function rewriteSpecifier(
|
|
16
|
-
specifier: string,
|
|
17
|
-
fromFile: string,
|
|
18
|
-
projectRoot: string,
|
|
19
|
-
): string {
|
|
20
|
-
if (specifier === "@sigil-dev/runtime") return RUNTIME_URL;
|
|
21
|
-
// if (specifier.startsWith("@sigil-dev/")) return RUNTIME_URL; // all sigil packages
|
|
22
|
-
|
|
23
|
-
// relative import — resolve to absolute, then make a module URL
|
|
24
|
-
if (specifier.startsWith(".")) {
|
|
25
|
-
const resolved = join(dirname(fromFile), specifier);
|
|
26
|
-
for (const ext of [".ts", ".tsx", ".js", ".jsx"]) {
|
|
27
|
-
const candidate = resolved.endsWith(ext) ? resolved : resolved + ext;
|
|
28
|
-
if (existsSync(candidate)) {
|
|
29
|
-
const rel = relative(projectRoot, candidate).replace(/\\/g, "/");
|
|
30
|
-
return `${MODULES_BASE}${rel}.js`;
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
// already has extension
|
|
34
|
-
const rel = relative(projectRoot, resolved).replace(/\\/g, "/");
|
|
35
|
-
return `${MODULES_BASE}${rel}.js`;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
// $lib alias
|
|
39
|
-
if (specifier.startsWith("$lib/")) {
|
|
40
|
-
const rel = specifier.replace("$lib/", "src/lib/");
|
|
41
|
-
// try common extensions
|
|
42
|
-
for (const ext of [".ts", ".tsx", ".js", ".jsx"]) {
|
|
43
|
-
const candidate = join(projectRoot, rel + ext);
|
|
44
|
-
if (existsSync(candidate)) {
|
|
45
|
-
return `${MODULES_BASE}${rel}${ext}.js`;
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
return `${MODULES_BASE}${rel}.ts.js`; // fallback
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
// $env — keep as-is, these are virtual modules handled elsewhere
|
|
52
|
-
if (specifier.startsWith("$env/")) return specifier;
|
|
53
|
-
|
|
54
|
-
// bare npm specifier → dep bundle
|
|
55
|
-
const pkgName = specifier.startsWith("@")
|
|
56
|
-
? specifier.split("/").slice(0, 2).join("/")
|
|
57
|
-
: specifier.split("/")[0];
|
|
58
|
-
return `${DEPS_BASE}${pkgName!.replace("/", "__")}.js`;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Rewrite all import/export from specifiers in compiled JS output.
|
|
63
|
-
* Works on the static pattern `from "..."` — sufficient for Babel output.
|
|
64
|
-
*/
|
|
65
|
-
function rewriteImports(
|
|
66
|
-
code: string,
|
|
67
|
-
filePath: string,
|
|
68
|
-
projectRoot: string,
|
|
69
|
-
): string {
|
|
70
|
-
return code.replace(
|
|
71
|
-
/(from\s+|import\s+)(["'])([^"']+)\2/g,
|
|
72
|
-
(_match, keyword, quote, specifier) => {
|
|
73
|
-
const rewritten = rewriteSpecifier(specifier, filePath, projectRoot);
|
|
74
|
-
return `${keyword}${quote}${rewritten}${quote}`;
|
|
75
|
-
},
|
|
76
|
-
);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
export interface CompileResult {
|
|
80
|
-
js: string;
|
|
81
|
-
css: string | null;
|
|
82
|
-
cssHash: string;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
export async function compileForBrowser(
|
|
86
|
-
filePath: string,
|
|
87
|
-
projectRoot: string,
|
|
88
|
-
): Promise<CompileResult> {
|
|
89
|
-
const source = await Bun.file(filePath).text();
|
|
90
|
-
|
|
91
|
-
// extract scoped CSS
|
|
92
|
-
const STYLE_RE = /<style[^>]*>([\s\S]*?)<\/style>/i;
|
|
93
|
-
const { computeHash, scopeCSS } = await import(
|
|
94
|
-
pathToFileURL(
|
|
95
|
-
join(
|
|
96
|
-
projectRoot,
|
|
97
|
-
"node_modules/@sigil-dev/compiler/src/babel/util/css.ts",
|
|
98
|
-
),
|
|
99
|
-
).href
|
|
100
|
-
);
|
|
101
|
-
const cssHash = computeHash(filePath);
|
|
102
|
-
const styleMatch = source.match(STYLE_RE);
|
|
103
|
-
const css = styleMatch ? scopeCSS(styleMatch[1].trim(), cssHash) : null;
|
|
104
|
-
const sourceNoStyle = source.replace(/<style[^>]*>[\s\S]*?<\/style>/gi, "");
|
|
105
|
-
|
|
106
|
-
// Babel transform — dom mode
|
|
107
|
-
const { transformSync } = await import("@babel/core");
|
|
108
|
-
const sigilPlugin = (await import("@sigil-dev/compiler/babel")).default;
|
|
109
|
-
const { createHash } = await import("node:crypto");
|
|
110
|
-
const hash = createHash("md5").update(filePath).digest("hex").slice(0, 8);
|
|
111
|
-
|
|
112
|
-
const babelResult = transformSync(sourceNoStyle, {
|
|
113
|
-
configFile: false,
|
|
114
|
-
babelrc: false,
|
|
115
|
-
parserOpts: { plugins: ["typescript", "jsx"] },
|
|
116
|
-
plugins: [[sigilPlugin, { mode: "dom", hash, dev: true }]],
|
|
117
|
-
filename: filePath,
|
|
118
|
-
});
|
|
119
|
-
// strip TypeScript types
|
|
120
|
-
const transpiler = new Bun.Transpiler({ loader: "ts", target: "browser" });
|
|
121
|
-
let js = transpiler.transformSync(babelResult?.code ?? "");
|
|
122
|
-
|
|
123
|
-
const runtimeSpecifiers = [
|
|
124
|
-
"createSignal",
|
|
125
|
-
"createRawSignal",
|
|
126
|
-
"createEffect",
|
|
127
|
-
"createMemo",
|
|
128
|
-
"reconcile",
|
|
129
|
-
"claim",
|
|
130
|
-
"claimText",
|
|
131
|
-
"claimComment",
|
|
132
|
-
"hydrateKeyedList",
|
|
133
|
-
"insert",
|
|
134
|
-
"getHydrationNodes",
|
|
135
|
-
"pushHydrationNodes",
|
|
136
|
-
"popHydrationNodes",
|
|
137
|
-
"snapshot",
|
|
138
|
-
"tracking",
|
|
139
|
-
"createEffectPre",
|
|
140
|
-
"createInspectEffect",
|
|
141
|
-
"withEffectScope",
|
|
142
|
-
].join(", ");
|
|
143
|
-
|
|
144
|
-
js = js.replace(
|
|
145
|
-
/import\s*\{[^}]+\}\s*from\s*["']@sigil-dev\/runtime["'];?/,
|
|
146
|
-
`import { ${runtimeSpecifiers} } from "@sigil-dev/runtime";`,
|
|
147
|
-
);
|
|
148
|
-
|
|
149
|
-
// inject scoped CSS at runtime
|
|
150
|
-
if (css) {
|
|
151
|
-
js = `if (typeof document !== 'undefined' && !document.getElementById('sigil-${hash}')) {
|
|
152
|
-
const __s = document.createElement('style');
|
|
153
|
-
__s.id = 'sigil-${hash}';
|
|
154
|
-
__s.textContent = ${JSON.stringify(css)};
|
|
155
|
-
document.head.appendChild(__s);
|
|
156
|
-
}\n${js}`;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
// rewrite imports to browser-servable URLs
|
|
160
|
-
js = rewriteImports(js, filePath, projectRoot);
|
|
161
|
-
|
|
162
|
-
return { js, css, cssHash };
|
|
163
|
-
}
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { dirname, join, relative } from "node:path";
|
|
3
|
+
import { pathToFileURL } from "node:url";
|
|
4
|
+
|
|
5
|
+
const RUNTIME_URL = "/__grimoire__/runtime.js";
|
|
6
|
+
const MODULES_BASE = "/__grimoire__/m/";
|
|
7
|
+
const DEPS_BASE = "/__grimoire__/dep/";
|
|
8
|
+
/**
|
|
9
|
+
* Rewrite a single import specifier to a browser-servable URL.
|
|
10
|
+
* project-relative → /__grimoire__/m/...
|
|
11
|
+
* @sigil-dev/runtime → /__grimoire__/runtime.js
|
|
12
|
+
* bare npm → /__grimoire__/dep/...
|
|
13
|
+
* relative → /__grimoire__/m/... (resolved)
|
|
14
|
+
*/
|
|
15
|
+
function rewriteSpecifier(
|
|
16
|
+
specifier: string,
|
|
17
|
+
fromFile: string,
|
|
18
|
+
projectRoot: string,
|
|
19
|
+
): string {
|
|
20
|
+
if (specifier === "@sigil-dev/runtime") return RUNTIME_URL;
|
|
21
|
+
// if (specifier.startsWith("@sigil-dev/")) return RUNTIME_URL; // all sigil packages
|
|
22
|
+
|
|
23
|
+
// relative import — resolve to absolute, then make a module URL
|
|
24
|
+
if (specifier.startsWith(".")) {
|
|
25
|
+
const resolved = join(dirname(fromFile), specifier);
|
|
26
|
+
for (const ext of [".ts", ".tsx", ".js", ".jsx"]) {
|
|
27
|
+
const candidate = resolved.endsWith(ext) ? resolved : resolved + ext;
|
|
28
|
+
if (existsSync(candidate)) {
|
|
29
|
+
const rel = relative(projectRoot, candidate).replace(/\\/g, "/");
|
|
30
|
+
return `${MODULES_BASE}${rel}.js`;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
// already has extension
|
|
34
|
+
const rel = relative(projectRoot, resolved).replace(/\\/g, "/");
|
|
35
|
+
return `${MODULES_BASE}${rel}.js`;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// $lib alias
|
|
39
|
+
if (specifier.startsWith("$lib/")) {
|
|
40
|
+
const rel = specifier.replace("$lib/", "src/lib/");
|
|
41
|
+
// try common extensions
|
|
42
|
+
for (const ext of [".ts", ".tsx", ".js", ".jsx"]) {
|
|
43
|
+
const candidate = join(projectRoot, rel + ext);
|
|
44
|
+
if (existsSync(candidate)) {
|
|
45
|
+
return `${MODULES_BASE}${rel}${ext}.js`;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return `${MODULES_BASE}${rel}.ts.js`; // fallback
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// $env — keep as-is, these are virtual modules handled elsewhere
|
|
52
|
+
if (specifier.startsWith("$env/")) return specifier;
|
|
53
|
+
|
|
54
|
+
// bare npm specifier → dep bundle
|
|
55
|
+
const pkgName = specifier.startsWith("@")
|
|
56
|
+
? specifier.split("/").slice(0, 2).join("/")
|
|
57
|
+
: specifier.split("/")[0];
|
|
58
|
+
return `${DEPS_BASE}${pkgName!.replace("/", "__")}.js`;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Rewrite all import/export from specifiers in compiled JS output.
|
|
63
|
+
* Works on the static pattern `from "..."` — sufficient for Babel output.
|
|
64
|
+
*/
|
|
65
|
+
function rewriteImports(
|
|
66
|
+
code: string,
|
|
67
|
+
filePath: string,
|
|
68
|
+
projectRoot: string,
|
|
69
|
+
): string {
|
|
70
|
+
return code.replace(
|
|
71
|
+
/(from\s+|import\s+)(["'])([^"']+)\2/g,
|
|
72
|
+
(_match, keyword, quote, specifier) => {
|
|
73
|
+
const rewritten = rewriteSpecifier(specifier, filePath, projectRoot);
|
|
74
|
+
return `${keyword}${quote}${rewritten}${quote}`;
|
|
75
|
+
},
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export interface CompileResult {
|
|
80
|
+
js: string;
|
|
81
|
+
css: string | null;
|
|
82
|
+
cssHash: string;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export async function compileForBrowser(
|
|
86
|
+
filePath: string,
|
|
87
|
+
projectRoot: string,
|
|
88
|
+
): Promise<CompileResult> {
|
|
89
|
+
const source = await Bun.file(filePath).text();
|
|
90
|
+
|
|
91
|
+
// extract scoped CSS
|
|
92
|
+
const STYLE_RE = /<style[^>]*>([\s\S]*?)<\/style>/i;
|
|
93
|
+
const { computeHash, scopeCSS } = await import(
|
|
94
|
+
pathToFileURL(
|
|
95
|
+
join(
|
|
96
|
+
projectRoot,
|
|
97
|
+
"node_modules/@sigil-dev/compiler/src/babel/util/css.ts",
|
|
98
|
+
),
|
|
99
|
+
).href
|
|
100
|
+
);
|
|
101
|
+
const cssHash = computeHash(filePath);
|
|
102
|
+
const styleMatch = source.match(STYLE_RE);
|
|
103
|
+
const css = styleMatch ? scopeCSS(styleMatch[1].trim(), cssHash) : null;
|
|
104
|
+
const sourceNoStyle = source.replace(/<style[^>]*>[\s\S]*?<\/style>/gi, "");
|
|
105
|
+
|
|
106
|
+
// Babel transform — dom mode
|
|
107
|
+
const { transformSync } = await import("@babel/core");
|
|
108
|
+
const sigilPlugin = (await import("@sigil-dev/compiler/babel")).default;
|
|
109
|
+
const { createHash } = await import("node:crypto");
|
|
110
|
+
const hash = createHash("md5").update(filePath).digest("hex").slice(0, 8);
|
|
111
|
+
|
|
112
|
+
const babelResult = transformSync(sourceNoStyle, {
|
|
113
|
+
configFile: false,
|
|
114
|
+
babelrc: false,
|
|
115
|
+
parserOpts: { plugins: ["typescript", "jsx"] },
|
|
116
|
+
plugins: [[sigilPlugin, { mode: "dom", hash, dev: true }]],
|
|
117
|
+
filename: filePath,
|
|
118
|
+
});
|
|
119
|
+
// strip TypeScript types
|
|
120
|
+
const transpiler = new Bun.Transpiler({ loader: "ts", target: "browser" });
|
|
121
|
+
let js = transpiler.transformSync(babelResult?.code ?? "");
|
|
122
|
+
|
|
123
|
+
const runtimeSpecifiers = [
|
|
124
|
+
"createSignal",
|
|
125
|
+
"createRawSignal",
|
|
126
|
+
"createEffect",
|
|
127
|
+
"createMemo",
|
|
128
|
+
"reconcile",
|
|
129
|
+
"claim",
|
|
130
|
+
"claimText",
|
|
131
|
+
"claimComment",
|
|
132
|
+
"hydrateKeyedList",
|
|
133
|
+
"insert",
|
|
134
|
+
"getHydrationNodes",
|
|
135
|
+
"pushHydrationNodes",
|
|
136
|
+
"popHydrationNodes",
|
|
137
|
+
"snapshot",
|
|
138
|
+
"tracking",
|
|
139
|
+
"createEffectPre",
|
|
140
|
+
"createInspectEffect",
|
|
141
|
+
"withEffectScope",
|
|
142
|
+
].join(", ");
|
|
143
|
+
|
|
144
|
+
js = js.replace(
|
|
145
|
+
/import\s*\{[^}]+\}\s*from\s*["']@sigil-dev\/runtime["'];?/,
|
|
146
|
+
`import { ${runtimeSpecifiers} } from "@sigil-dev/runtime";`,
|
|
147
|
+
);
|
|
148
|
+
|
|
149
|
+
// inject scoped CSS at runtime
|
|
150
|
+
if (css) {
|
|
151
|
+
js = `if (typeof document !== 'undefined' && !document.getElementById('sigil-${hash}')) {
|
|
152
|
+
const __s = document.createElement('style');
|
|
153
|
+
__s.id = 'sigil-${hash}';
|
|
154
|
+
__s.textContent = ${JSON.stringify(css)};
|
|
155
|
+
document.head.appendChild(__s);
|
|
156
|
+
}\n${js}`;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// rewrite imports to browser-servable URLs
|
|
160
|
+
js = rewriteImports(js, filePath, projectRoot);
|
|
161
|
+
|
|
162
|
+
return { js, css, cssHash };
|
|
163
|
+
}
|
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
import { normalizePath } from "./paths";
|
|
2
|
-
|
|
3
|
-
const registry = new Map<string, (() => void)[]>();
|
|
4
|
-
|
|
5
|
-
export function registerModuleEffect(
|
|
6
|
-
filePath: string,
|
|
7
|
-
dispose: () => void,
|
|
8
|
-
): void {
|
|
9
|
-
const key = normalizePath(filePath);
|
|
10
|
-
let list = registry.get(key);
|
|
11
|
-
if (!list) registry.set(key, (list = []));
|
|
12
|
-
list.push(dispose);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export function disposeModuleEffects(filePath: string): void {
|
|
16
|
-
const key = normalizePath(filePath);
|
|
17
|
-
for (const d of registry.get(key) ?? []) {
|
|
18
|
-
try {
|
|
19
|
-
d();
|
|
20
|
-
} catch {}
|
|
21
|
-
}
|
|
22
|
-
registry.delete(key);
|
|
23
|
-
}
|
|
1
|
+
import { normalizePath } from "./paths";
|
|
2
|
+
|
|
3
|
+
const registry = new Map<string, (() => void)[]>();
|
|
4
|
+
|
|
5
|
+
export function registerModuleEffect(
|
|
6
|
+
filePath: string,
|
|
7
|
+
dispose: () => void,
|
|
8
|
+
): void {
|
|
9
|
+
const key = normalizePath(filePath);
|
|
10
|
+
let list = registry.get(key);
|
|
11
|
+
if (!list) registry.set(key, (list = []));
|
|
12
|
+
list.push(dispose);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function disposeModuleEffects(filePath: string): void {
|
|
16
|
+
const key = normalizePath(filePath);
|
|
17
|
+
for (const d of registry.get(key) ?? []) {
|
|
18
|
+
try {
|
|
19
|
+
d();
|
|
20
|
+
} catch {}
|
|
21
|
+
}
|
|
22
|
+
registry.delete(key);
|
|
23
|
+
}
|
package/src/dev/graph.ts
CHANGED
|
@@ -1,114 +1,114 @@
|
|
|
1
|
-
import type { RouteFile } from "../routing/scanner";
|
|
2
|
-
import { normalizePath } from "./paths";
|
|
3
|
-
|
|
4
|
-
export interface SpecRef {
|
|
5
|
-
start: number;
|
|
6
|
-
end: number;
|
|
7
|
-
target: string; // normalizePath key or dep name
|
|
8
|
-
kind: "project" | "dep" | "runtime";
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export interface ModNode {
|
|
12
|
-
key: string;
|
|
13
|
-
displayPath: string;
|
|
14
|
-
importers: Set<string>;
|
|
15
|
-
imports: Set<string>;
|
|
16
|
-
version: number;
|
|
17
|
-
compiled: {
|
|
18
|
-
js: string;
|
|
19
|
-
css: string | null;
|
|
20
|
-
cssHash: string;
|
|
21
|
-
specifiers: SpecRef[];
|
|
22
|
-
} | null;
|
|
23
|
-
isRoute: RouteFile | null;
|
|
24
|
-
lastSubstituted: Map<string, number>;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export class DevGraph {
|
|
28
|
-
private nodes = new Map<string, ModNode>();
|
|
29
|
-
|
|
30
|
-
get(key: string): ModNode | undefined {
|
|
31
|
-
return this.nodes.get(normalizePath(key));
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
getOrCreate(filePath: string, displayPath?: string): ModNode {
|
|
35
|
-
const key = normalizePath(filePath);
|
|
36
|
-
if (!this.nodes.has(key)) {
|
|
37
|
-
this.nodes.set(key, {
|
|
38
|
-
key,
|
|
39
|
-
displayPath: displayPath ?? filePath,
|
|
40
|
-
importers: new Set(),
|
|
41
|
-
imports: new Set(),
|
|
42
|
-
version: 0,
|
|
43
|
-
compiled: null,
|
|
44
|
-
isRoute: null,
|
|
45
|
-
lastSubstituted: new Map(),
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
return this.nodes.get(key)!;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
addEdge(importerPath: string, importedPath: string): void {
|
|
52
|
-
const a = normalizePath(importerPath);
|
|
53
|
-
const b = normalizePath(importedPath);
|
|
54
|
-
this.getOrCreate(a).imports.add(b);
|
|
55
|
-
this.getOrCreate(b).importers.add(a);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/** DFS for cycles in the subgraph reachable from key via forward edges */
|
|
59
|
-
hasCycle(key: string): boolean {
|
|
60
|
-
const k = normalizePath(key);
|
|
61
|
-
const visited = new Set<string>();
|
|
62
|
-
const stack = new Set<string>();
|
|
63
|
-
const dfs = (node: string): boolean => {
|
|
64
|
-
if (stack.has(node)) return true;
|
|
65
|
-
if (visited.has(node)) return false;
|
|
66
|
-
visited.add(node);
|
|
67
|
-
stack.add(node);
|
|
68
|
-
for (const imp of this.nodes.get(node)?.imports ?? []) {
|
|
69
|
-
if (dfs(imp)) return true;
|
|
70
|
-
}
|
|
71
|
-
stack.delete(node);
|
|
72
|
-
return false;
|
|
73
|
-
};
|
|
74
|
-
return dfs(k);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/** Walk importers up to routes. Returns affected RouteFiles. */
|
|
78
|
-
affectedRoutes(changedKey: string): RouteFile[] {
|
|
79
|
-
const key = normalizePath(changedKey);
|
|
80
|
-
const seen = new Set<string>([key]);
|
|
81
|
-
const queue = [key];
|
|
82
|
-
const hits: RouteFile[] = [];
|
|
83
|
-
while (queue.length) {
|
|
84
|
-
const f = queue.pop()!;
|
|
85
|
-
const node = this.nodes.get(f);
|
|
86
|
-
if (!node) continue;
|
|
87
|
-
if (node.isRoute) {
|
|
88
|
-
hits.push(node.isRoute);
|
|
89
|
-
continue;
|
|
90
|
-
}
|
|
91
|
-
for (const importer of node.importers) {
|
|
92
|
-
if (!seen.has(importer)) {
|
|
93
|
-
seen.add(importer);
|
|
94
|
-
queue.push(importer);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
return hits;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
markRoute(filePath: string, route: RouteFile): void {
|
|
102
|
-
this.getOrCreate(filePath).isRoute = route;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
bump(filePath: string): number {
|
|
106
|
-
const node = this.getOrCreate(filePath);
|
|
107
|
-
node.version++;
|
|
108
|
-
return node.version;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
all(): IterableIterator<ModNode> {
|
|
112
|
-
return this.nodes.values();
|
|
113
|
-
}
|
|
114
|
-
}
|
|
1
|
+
import type { RouteFile } from "../routing/scanner";
|
|
2
|
+
import { normalizePath } from "./paths";
|
|
3
|
+
|
|
4
|
+
export interface SpecRef {
|
|
5
|
+
start: number;
|
|
6
|
+
end: number;
|
|
7
|
+
target: string; // normalizePath key or dep name
|
|
8
|
+
kind: "project" | "dep" | "runtime";
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface ModNode {
|
|
12
|
+
key: string;
|
|
13
|
+
displayPath: string;
|
|
14
|
+
importers: Set<string>;
|
|
15
|
+
imports: Set<string>;
|
|
16
|
+
version: number;
|
|
17
|
+
compiled: {
|
|
18
|
+
js: string;
|
|
19
|
+
css: string | null;
|
|
20
|
+
cssHash: string;
|
|
21
|
+
specifiers: SpecRef[];
|
|
22
|
+
} | null;
|
|
23
|
+
isRoute: RouteFile | null;
|
|
24
|
+
lastSubstituted: Map<string, number>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export class DevGraph {
|
|
28
|
+
private nodes = new Map<string, ModNode>();
|
|
29
|
+
|
|
30
|
+
get(key: string): ModNode | undefined {
|
|
31
|
+
return this.nodes.get(normalizePath(key));
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
getOrCreate(filePath: string, displayPath?: string): ModNode {
|
|
35
|
+
const key = normalizePath(filePath);
|
|
36
|
+
if (!this.nodes.has(key)) {
|
|
37
|
+
this.nodes.set(key, {
|
|
38
|
+
key,
|
|
39
|
+
displayPath: displayPath ?? filePath,
|
|
40
|
+
importers: new Set(),
|
|
41
|
+
imports: new Set(),
|
|
42
|
+
version: 0,
|
|
43
|
+
compiled: null,
|
|
44
|
+
isRoute: null,
|
|
45
|
+
lastSubstituted: new Map(),
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
return this.nodes.get(key)!;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
addEdge(importerPath: string, importedPath: string): void {
|
|
52
|
+
const a = normalizePath(importerPath);
|
|
53
|
+
const b = normalizePath(importedPath);
|
|
54
|
+
this.getOrCreate(a).imports.add(b);
|
|
55
|
+
this.getOrCreate(b).importers.add(a);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** DFS for cycles in the subgraph reachable from key via forward edges */
|
|
59
|
+
hasCycle(key: string): boolean {
|
|
60
|
+
const k = normalizePath(key);
|
|
61
|
+
const visited = new Set<string>();
|
|
62
|
+
const stack = new Set<string>();
|
|
63
|
+
const dfs = (node: string): boolean => {
|
|
64
|
+
if (stack.has(node)) return true;
|
|
65
|
+
if (visited.has(node)) return false;
|
|
66
|
+
visited.add(node);
|
|
67
|
+
stack.add(node);
|
|
68
|
+
for (const imp of this.nodes.get(node)?.imports ?? []) {
|
|
69
|
+
if (dfs(imp)) return true;
|
|
70
|
+
}
|
|
71
|
+
stack.delete(node);
|
|
72
|
+
return false;
|
|
73
|
+
};
|
|
74
|
+
return dfs(k);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Walk importers up to routes. Returns affected RouteFiles. */
|
|
78
|
+
affectedRoutes(changedKey: string): RouteFile[] {
|
|
79
|
+
const key = normalizePath(changedKey);
|
|
80
|
+
const seen = new Set<string>([key]);
|
|
81
|
+
const queue = [key];
|
|
82
|
+
const hits: RouteFile[] = [];
|
|
83
|
+
while (queue.length) {
|
|
84
|
+
const f = queue.pop()!;
|
|
85
|
+
const node = this.nodes.get(f);
|
|
86
|
+
if (!node) continue;
|
|
87
|
+
if (node.isRoute) {
|
|
88
|
+
hits.push(node.isRoute);
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
for (const importer of node.importers) {
|
|
92
|
+
if (!seen.has(importer)) {
|
|
93
|
+
seen.add(importer);
|
|
94
|
+
queue.push(importer);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return hits;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
markRoute(filePath: string, route: RouteFile): void {
|
|
102
|
+
this.getOrCreate(filePath).isRoute = route;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
bump(filePath: string): number {
|
|
106
|
+
const node = this.getOrCreate(filePath);
|
|
107
|
+
node.version++;
|
|
108
|
+
return node.version;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
all(): IterableIterator<ModNode> {
|
|
112
|
+
return this.nodes.values();
|
|
113
|
+
}
|
|
114
|
+
}
|