@ogygia/compiler 0.0.0 → 0.14.0

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 (156) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +62 -1
  3. package/dist/_virtual/_@oxc-project_runtime@0.156.0/helpers/esm/taggedTemplateLiteral.js +5 -0
  4. package/dist/app-spec.d.ts +18 -0
  5. package/dist/app-spec.js +22 -0
  6. package/dist/blank-styles.d.ts +1 -0
  7. package/dist/blank-styles.js +24 -0
  8. package/dist/bom.d.ts +2 -0
  9. package/dist/bom.js +7 -0
  10. package/dist/build-cache.d.ts +13 -0
  11. package/dist/build-cache.js +65 -0
  12. package/dist/ctx.d.ts +238 -0
  13. package/dist/ctx.js +221 -0
  14. package/dist/dev/css-scope.d.ts +16 -0
  15. package/dist/dev/css-scope.js +32 -0
  16. package/dist/dev/dev-hmr.d.ts +24 -0
  17. package/dist/dev/dev-hmr.js +51 -0
  18. package/dist/dev/hmr.d.ts +34 -0
  19. package/dist/dev/hmr.js +52 -0
  20. package/dist/driver.d.ts +349 -0
  21. package/dist/driver.js +1401 -0
  22. package/dist/exports/index.d.ts +77 -0
  23. package/dist/exports/index.js +349 -0
  24. package/dist/exports/parse.d.ts +65 -0
  25. package/dist/exports/parse.js +251 -0
  26. package/dist/fouc-css.d.ts +102 -0
  27. package/dist/fouc-css.js +304 -0
  28. package/dist/free-vars.d.ts +32 -0
  29. package/dist/free-vars.js +454 -0
  30. package/dist/fs.d.ts +31 -0
  31. package/dist/fs.js +82 -0
  32. package/dist/ids.d.ts +49 -0
  33. package/dist/ids.js +52 -0
  34. package/dist/index.d.ts +23 -0
  35. package/dist/index.js +23 -0
  36. package/dist/kit.d.ts +204 -0
  37. package/dist/kit.js +554 -0
  38. package/dist/leg.d.ts +34 -0
  39. package/dist/leg.js +26 -0
  40. package/dist/link/analyses.d.ts +26 -0
  41. package/dist/link/analyses.js +237 -0
  42. package/dist/link/build-output.d.ts +47 -0
  43. package/dist/link/build-output.js +168 -0
  44. package/dist/link/caps.d.ts +32 -0
  45. package/dist/link/caps.js +39 -0
  46. package/dist/link/context-detect.d.ts +28 -0
  47. package/dist/link/context-detect.js +143 -0
  48. package/dist/link/emit-gate.d.ts +43 -0
  49. package/dist/link/emit-gate.js +55 -0
  50. package/dist/link/file-token.d.ts +11 -0
  51. package/dist/link/file-token.js +15 -0
  52. package/dist/link/island-deps.d.ts +183 -0
  53. package/dist/link/island-deps.js +802 -0
  54. package/dist/link/location-map.d.ts +24 -0
  55. package/dist/link/location-map.js +51 -0
  56. package/dist/link/own-reach.d.ts +15 -0
  57. package/dist/link/own-reach.js +188 -0
  58. package/dist/link/registry-stub.d.ts +50 -0
  59. package/dist/link/registry-stub.js +163 -0
  60. package/dist/link/relocations.d.ts +79 -0
  61. package/dist/link/relocations.js +198 -0
  62. package/dist/link/router-css.d.ts +38 -0
  63. package/dist/link/router-css.js +124 -0
  64. package/dist/link/runtime-entry.d.ts +103 -0
  65. package/dist/link/runtime-entry.js +183 -0
  66. package/dist/link/server-manifest.d.ts +25 -0
  67. package/dist/link/server-manifest.js +102 -0
  68. package/dist/link/transport.d.ts +42 -0
  69. package/dist/link/transport.js +91 -0
  70. package/dist/macros/dollar.d.ts +24 -0
  71. package/dist/macros/dollar.js +397 -0
  72. package/dist/macros/pipeline.d.ts +50 -0
  73. package/dist/macros/pipeline.js +133 -0
  74. package/dist/macros/source.d.ts +9 -0
  75. package/dist/macros/source.js +115 -0
  76. package/dist/macros/store.d.ts +22 -0
  77. package/dist/macros/store.js +217 -0
  78. package/dist/macros/wire.d.ts +17 -0
  79. package/dist/macros/wire.js +116 -0
  80. package/dist/map-chain.d.ts +18 -0
  81. package/dist/map-chain.js +42 -0
  82. package/dist/messages.d.ts +672 -0
  83. package/dist/messages.js +938 -0
  84. package/dist/ownership-stamps.d.ts +9 -0
  85. package/dist/ownership-stamps.js +101 -0
  86. package/dist/parse/oxc.d.ts +36 -0
  87. package/dist/parse/oxc.js +35 -0
  88. package/dist/parse/roots.d.ts +4 -0
  89. package/dist/parse/roots.js +27 -0
  90. package/dist/parse/scan.d.ts +65 -0
  91. package/dist/parse/scan.js +297 -0
  92. package/dist/parse/svelte-ast.d.ts +7 -0
  93. package/dist/parse/svelte-ast.js +99 -0
  94. package/dist/program.d.ts +140 -0
  95. package/dist/program.js +304 -0
  96. package/dist/queries.d.ts +105 -0
  97. package/dist/queries.js +326 -0
  98. package/dist/region/emit.d.ts +168 -0
  99. package/dist/region/emit.js +292 -0
  100. package/dist/region/identity.d.ts +32 -0
  101. package/dist/region/identity.js +46 -0
  102. package/dist/region/ir.d.ts +56 -0
  103. package/dist/region/ir.js +0 -0
  104. package/dist/region/marked-imports.d.ts +27 -0
  105. package/dist/region/marked-imports.js +265 -0
  106. package/dist/region/transform.d.ts +189 -0
  107. package/dist/region/transform.js +1565 -0
  108. package/dist/scan-facts.d.ts +36 -0
  109. package/dist/scan-facts.js +54 -0
  110. package/dist/script-request.d.ts +1 -0
  111. package/dist/script-request.js +33 -0
  112. package/dist/splice.d.ts +11 -0
  113. package/dist/splice.js +33 -0
  114. package/dist/transportables.d.ts +23 -0
  115. package/dist/transportables.js +144 -0
  116. package/dist/virtual/core.d.ts +158 -0
  117. package/dist/virtual/core.js +588 -0
  118. package/dist/virtual/resolver.d.ts +56 -0
  119. package/dist/virtual/resolver.js +249 -0
  120. package/dist/virtual/specifier.d.ts +10 -0
  121. package/dist/virtual/specifier.js +6 -0
  122. package/dist/virtual/table.d.ts +118 -0
  123. package/dist/virtual/table.js +54 -0
  124. package/dist/virtual/types.d.ts +17 -0
  125. package/dist/virtual/types.js +100 -0
  126. package/dist/virtual/value-names.d.ts +2 -0
  127. package/dist/virtual/value-names.js +18 -0
  128. package/dist/vite/definers.d.ts +16 -0
  129. package/dist/vite/definers.js +69 -0
  130. package/dist/vite/dev-maps.d.ts +34 -0
  131. package/dist/vite/dev-maps.js +91 -0
  132. package/dist/vite/index.d.ts +21 -0
  133. package/dist/vite/index.js +874 -0
  134. package/dist/vite/kit-dirs.d.ts +6 -0
  135. package/dist/vite/kit-dirs.js +53 -0
  136. package/dist/vite/late-island.d.ts +11 -0
  137. package/dist/vite/late-island.js +19 -0
  138. package/dist/vite/lazy-files.d.ts +2 -0
  139. package/dist/vite/lazy-files.js +72 -0
  140. package/dist/vite/options.d.ts +195 -0
  141. package/dist/vite/options.js +72 -0
  142. package/dist/vite/package-files.d.ts +3 -0
  143. package/dist/vite/package-files.js +88 -0
  144. package/dist/vite/paths.d.ts +14 -0
  145. package/dist/vite/paths.js +37 -0
  146. package/dist/vite/plugin-api.d.ts +316 -0
  147. package/dist/vite/plugin-api.js +328 -0
  148. package/dist/vite/session.d.ts +28 -0
  149. package/dist/vite/session.js +43 -0
  150. package/dist/vite/sourcemaps.d.ts +19 -0
  151. package/dist/vite/sourcemaps.js +75 -0
  152. package/dist/vite/style-preprocess.d.ts +2 -0
  153. package/dist/vite/style-preprocess.js +84 -0
  154. package/dist/vite/svelte-entries.d.ts +4 -0
  155. package/dist/vite/svelte-entries.js +10 -0
  156. package/package.json +87 -12
@@ -0,0 +1,24 @@
1
+ /** The fixed-name map's file, in the app dir (beside Kit's `version.json`). */
2
+ export declare const CURRENT_LOCATIONS_FILE = "ogygia.json";
3
+ /** Orders `[key, value]` pairs by key: what the build writes from the bundler's entries, which it
4
+ * emits in no stable order, is written in one order every build (#490). */
5
+ export declare function by_key(a: readonly [string, unknown], b: readonly [string, unknown]): number;
6
+ /** Orders strings by UTF-16 code unit, the order a plain `.sort()` gives, but usable as a comparator
7
+ * on a field. */
8
+ export declare function by_code_unit(a: string, b: string): number;
9
+ /**
10
+ * Both maps, as output files: the content-hashed one first. `pairs`: identity file → hashed file,
11
+ * output-relative. Empty when there is nothing to map.
12
+ *
13
+ * An island's identity (`<appDir>/ogygia/og-region.<iid>.js`) is a name, not a file; its location
14
+ * is a content-hashed file under `immutable/`. The same map is written twice:
15
+ * `<appDir>/immutable/og-locations.<hash>.json`, named by its content, which the runtime's script
16
+ * names (`data-og-locations`) for this build's page; and `<appDir>/ogygia.json` at the one fixed
17
+ * name, beside Kit's `version.json`, which a page that outlived its build reads to wake on the
18
+ * current code. Both sides of each pair are relative to the file holding them, so the browser
19
+ * resolves them against wherever the build is served.
20
+ */
21
+ export declare function location_map_files(app_dir: string, pairs: Iterable<readonly [string, string]>, hash: (source: string) => string): {
22
+ fileName: string;
23
+ source: string;
24
+ }[];
@@ -0,0 +1,51 @@
1
+ import path from "node:path";
2
+ // src/link/location-map.ts
3
+ /** The fixed-name map's file, in the app dir (beside Kit's `version.json`). */
4
+ const CURRENT_LOCATIONS_FILE = "ogygia.json";
5
+ function relative_specifier(from_file, to_file) {
6
+ const rel = path.posix.relative(path.posix.dirname(from_file), to_file);
7
+ return rel.startsWith(".") ? rel : `./${rel}`;
8
+ }
9
+ /** Orders `[key, value]` pairs by key: what the build writes from the bundler's entries, which it
10
+ * emits in no stable order, is written in one order every build (#490). */
11
+ function by_key(a, b) {
12
+ return by_code_unit(a[0], b[0]);
13
+ }
14
+ /** Orders strings by UTF-16 code unit, the order a plain `.sort()` gives, but usable as a comparator
15
+ * on a field. */
16
+ function by_code_unit(a, b) {
17
+ if (a < b) return -1;
18
+ return a > b ? 1 : 0;
19
+ }
20
+ function map_source(file, pairs) {
21
+ const map = {};
22
+ for (const [identity, location] of pairs) map[relative_specifier(file, identity)] = relative_specifier(file, location);
23
+ return JSON.stringify(map);
24
+ }
25
+ /**
26
+ * Both maps, as output files: the content-hashed one first. `pairs`: identity file → hashed file,
27
+ * output-relative. Empty when there is nothing to map.
28
+ *
29
+ * An island's identity (`<appDir>/ogygia/og-region.<iid>.js`) is a name, not a file; its location
30
+ * is a content-hashed file under `immutable/`. The same map is written twice:
31
+ * `<appDir>/immutable/og-locations.<hash>.json`, named by its content, which the runtime's script
32
+ * names (`data-og-locations`) for this build's page; and `<appDir>/ogygia.json` at the one fixed
33
+ * name, beside Kit's `version.json`, which a page that outlived its build reads to wake on the
34
+ * current code. Both sides of each pair are relative to the file holding them, so the browser
35
+ * resolves them against wherever the build is served.
36
+ */
37
+ function location_map_files(app_dir, pairs, hash) {
38
+ const list = [...pairs].sort(by_key);
39
+ if (!list.length) return [];
40
+ const hashed_dir = `${app_dir}/immutable`;
41
+ const hashed_source = map_source(`${hashed_dir}/og-locations.json`, list);
42
+ const current = `${app_dir}/${CURRENT_LOCATIONS_FILE}`;
43
+ return [{
44
+ fileName: `${hashed_dir}/og-locations.${hash(hashed_source)}.json`,
45
+ source: hashed_source
46
+ }, {
47
+ fileName: current,
48
+ source: map_source(current, list)
49
+ }];
50
+ }
51
+ export { CURRENT_LOCATIONS_FILE, by_code_unit, by_key, location_map_files };
@@ -0,0 +1,15 @@
1
+ /** `all`: the module can reach any of ogygia's exports. */
2
+ export type OwnReach = {
3
+ names: Set<string>;
4
+ all: boolean;
5
+ content: boolean;
6
+ };
7
+ /** The module provides cross-island context (the `context` mark). */
8
+ export declare function reaches_context(r: OwnReach): boolean;
9
+ /** The module produces a value that crosses Kit's transport (`crosses_wire`). */
10
+ export declare function reaches_wire(r: OwnReach): boolean;
11
+ /**
12
+ * The ogygia exports `src` can reach. `kind` picks the parser (`'svelte'` for a component, `'script'`
13
+ * for `.ts`/`.js`). A file that names neither package answers nothing without a parse.
14
+ */
15
+ export declare function own_reach(src: string, id: string, kind: "svelte" | "script"): OwnReach;
@@ -0,0 +1,188 @@
1
+ import { parse_roots } from "../parse/roots.js";
2
+ import { OWN_PACKAGE_NAMES } from "@ogygia/core/contract";
3
+ import { walk } from "estree-walker";
4
+ // src/link/own-reach.ts
5
+ const NAMES_A_PACKAGE = /['"`](?:ogygia|@ogygia\/core|@ogygia\/content)\b/;
6
+ const CONTEXT_PROVIDERS = [
7
+ "Provide",
8
+ "setContext",
9
+ "createContext"
10
+ ];
11
+ const WIRE_PRODUCERS = [
12
+ "region",
13
+ "Region",
14
+ "site"
15
+ ];
16
+ /** The module provides cross-island context (the `context` mark). */
17
+ function reaches_context(r) {
18
+ return r.all || CONTEXT_PROVIDERS.some((n) => r.names.has(n));
19
+ }
20
+ /** The module produces a value that crosses Kit's transport (`crosses_wire`). */
21
+ function reaches_wire(r) {
22
+ return r.content || r.all || WIRE_PRODUCERS.some((n) => r.names.has(n));
23
+ }
24
+ function is_own(spec) {
25
+ return typeof spec === "string" && OWN_PACKAGE_NAMES.includes(spec);
26
+ }
27
+ function is_content(spec) {
28
+ return typeof spec === "string" && (spec === "@ogygia/content" || spec.startsWith("@ogygia/content/"));
29
+ }
30
+ function static_spec(n) {
31
+ if (!n) return null;
32
+ if (n.type === "Literal" && typeof n.value === "string") return n.value;
33
+ if (n.type === "TemplateLiteral" && n.expressions?.length === 0) return n.quasis?.[0]?.value?.cooked ?? null;
34
+ return null;
35
+ }
36
+ function name_of(n) {
37
+ if (!n) return null;
38
+ if (n.type === "Identifier") return n.name ?? null;
39
+ if (n.type === "Literal" && typeof n.value === "string") return n.value;
40
+ return null;
41
+ }
42
+ function member_name(m) {
43
+ return m.computed ? static_spec(m.property) : name_of(m.property);
44
+ }
45
+ function pattern_keys(p) {
46
+ const keys = [];
47
+ for (const prop of p.properties ?? []) {
48
+ if (prop.type !== "Property" || prop.computed) return null;
49
+ const k = name_of(prop.key);
50
+ if (k == null) return null;
51
+ keys.push(k);
52
+ }
53
+ return keys;
54
+ }
55
+ function dynamic_reads(path) {
56
+ let i = path.length - 1;
57
+ let value = path[i];
58
+ const unwrap = () => {
59
+ while (value.parent?.type === "ParenthesizedExpression") value = path[--i];
60
+ };
61
+ unwrap();
62
+ if (value.parent?.type !== "AwaitExpression") return null;
63
+ value = path[--i];
64
+ unwrap();
65
+ const parent = value.parent;
66
+ if (!parent) return null;
67
+ if (parent.type === "MemberExpression" && value.key === "object") {
68
+ const n = member_name(parent);
69
+ return n == null ? null : [n];
70
+ }
71
+ if (parent.type === "VariableDeclarator" && value.key === "init" && parent.id?.type === "ObjectPattern") return pattern_keys(parent.id);
72
+ return null;
73
+ }
74
+ /**
75
+ * The ogygia exports `src` can reach. `kind` picks the parser (`'svelte'` for a component, `'script'`
76
+ * for `.ts`/`.js`). A file that names neither package answers nothing without a parse.
77
+ */
78
+ function own_reach(src, id, kind) {
79
+ if (!NAMES_A_PACKAGE.test(src)) return {
80
+ names: /* @__PURE__ */ new Set(),
81
+ all: false,
82
+ content: false
83
+ };
84
+ const roots = parse_roots(src, id, kind);
85
+ if (!roots) return {
86
+ names: /* @__PURE__ */ new Set(),
87
+ all: true,
88
+ content: false
89
+ };
90
+ const names = /* @__PURE__ */ new Set();
91
+ let all = false;
92
+ let content = false;
93
+ const namespaces = /* @__PURE__ */ new Set();
94
+ const declarations = /* @__PURE__ */ new Set();
95
+ const references = [];
96
+ const path = [];
97
+ const take_import = (node) => {
98
+ const spec = node.source?.value;
99
+ if (node.importKind === "type") return;
100
+ if (is_content(spec)) content = true;
101
+ if (!is_own(spec)) return;
102
+ for (const s of node.specifiers ?? []) {
103
+ if (s.importKind === "type") continue;
104
+ if (s.type === "ImportSpecifier") {
105
+ const n = name_of(s.imported);
106
+ if (n != null) names.add(n);
107
+ } else if (s.type === "ImportNamespaceSpecifier") {
108
+ namespaces.add(s.local.name);
109
+ declarations.add(s.local);
110
+ } else all = true;
111
+ }
112
+ };
113
+ for (const root of roots) walk(root, {
114
+ enter(n, parent, key) {
115
+ const node = n;
116
+ path.push({
117
+ node,
118
+ parent,
119
+ key: key == null ? null : String(key)
120
+ });
121
+ switch (node.type) {
122
+ case "ImportDeclaration":
123
+ take_import(node);
124
+ break;
125
+ case "ExportNamedDeclaration": {
126
+ const spec = node.source?.value;
127
+ if (!spec || node.exportKind === "type") break;
128
+ if (is_content(spec)) content = true;
129
+ if (!is_own(spec)) break;
130
+ for (const s of node.specifiers ?? []) {
131
+ if (s.exportKind === "type") continue;
132
+ const local = name_of(s.local);
133
+ if (local != null) names.add(local);
134
+ }
135
+ break;
136
+ }
137
+ case "ExportAllDeclaration": {
138
+ if (node.exportKind === "type") break;
139
+ const spec = node.source?.value;
140
+ if (is_content(spec)) content = true;
141
+ if (is_own(spec)) all = true;
142
+ break;
143
+ }
144
+ case "ImportExpression": {
145
+ const spec = static_spec(node.source);
146
+ if (is_content(spec)) content = true;
147
+ if (!is_own(spec)) break;
148
+ const reads = dynamic_reads(path);
149
+ if (reads == null) all = true;
150
+ else for (const r of reads) names.add(r);
151
+ break;
152
+ }
153
+ case "Identifier":
154
+ references.push(path[path.length - 1]);
155
+ break;
156
+ case "Component": {
157
+ const [head, member] = String(node.name ?? "").split(".");
158
+ if (member && namespaces.has(head)) names.add(member);
159
+ break;
160
+ }
161
+ }
162
+ },
163
+ leave() {
164
+ path.pop();
165
+ }
166
+ });
167
+ if (namespaces.size > 0 && !all) for (const { node, parent, key } of references) {
168
+ if (!namespaces.has(node.name) || declarations.has(node)) continue;
169
+ if (parent?.type === "MemberExpression") {
170
+ if (key === "property" && !parent.computed) continue;
171
+ const n = key === "object" ? member_name(parent) : null;
172
+ if (n != null) {
173
+ names.add(n);
174
+ continue;
175
+ }
176
+ }
177
+ if (parent?.type === "Property" && key === "key" && !parent.shorthand && !parent.computed) continue;
178
+ if ((parent?.type === "ImportSpecifier" || parent?.type === "ExportSpecifier") && key !== "local") continue;
179
+ all = true;
180
+ break;
181
+ }
182
+ return {
183
+ names,
184
+ all,
185
+ content
186
+ };
187
+ }
188
+ export { own_reach, reaches_context, reaches_wire };
@@ -0,0 +1,50 @@
1
+ /**
2
+ * `ogygia:region/registry-stub/…`: keeps a region registry (a `.ts` module of marked imports) out
3
+ * of a csr=false page's client graph. Kit links the CSS of every module that graph reaches, but
4
+ * what a csr=false page needs is decided at SSR, where Region links the CSS of each region that
5
+ * renders. So on the csr=false client leg of a build, a route host's import of a registry reads the
6
+ * registry with only its marks blanked ({@link registry_client_source}): plain imports keep their
7
+ * edges (they render on the server, and the graph is the only thing that links their CSS). A
8
+ * csr=true page, the island world and dev keep the real registry.
9
+ */
10
+ export declare const REGISTRY_STUB_PREFIX: string;
11
+ /** The query that names a registry's client-leg variant (its marks blanked, everything else kept). */
12
+ export declare const REGISTRY_CLIENT_QUERY = "?og-registry-client";
13
+ /** The client-leg variant id of a registry: its real absolute path (relative imports resolve from
14
+ * the registry's own directory) plus {@link REGISTRY_CLIENT_QUERY}. */
15
+ export declare function registry_client_id(abs_path: string): string;
16
+ /** The registry path a client-leg variant id names, or `null` for any other id. */
17
+ export declare function registry_client_path(id: string): string | null;
18
+ /**
19
+ * The static VALUE import specifiers of a script module — `import … from`, side-effect `import '…'`,
20
+ * `export … from`, `export * from` (type-only forms skipped; dynamic `import()` skipped: Kit links
21
+ * no CSS for a node's dynamic chunks). `null` when the module does not parse.
22
+ */
23
+ export declare function static_script_specs(source: string, file: string): string[] | null;
24
+ /**
25
+ * The registry's source with its MARKS blanked and everything else kept, or `null` when it does not
26
+ * parse (the caller falls back to the names-only stub). A mark is an import whose `with { … }`
27
+ * carries a region key (`region_keys`: the app's wake / render / region / preset names), default
28
+ * or named. Each blanked binding becomes
29
+ * `const X = undefined` so the module still links; the client leg never runs it anyway.
30
+ */
31
+ export declare function registry_client_source(source: string, file: string, region_keys: ReadonlySet<string>): string | null;
32
+ /** Root-relative posix path → the stub id. `names` are the exports the stub must declare. */
33
+ export declare function registry_stub_id(rel_posix: string, names: Iterable<string>): string;
34
+ /** True for a (resolved or bare) stub id. */
35
+ export declare function is_registry_stub_id(id: string): boolean;
36
+ /** The export names a stub id carries (`default` included when the registry has one). */
37
+ export declare function registry_stub_names(id: string): string[];
38
+ /** The stub module's source: every name as `undefined`, so the host links and nothing is shipped. */
39
+ export declare function registry_stub_source(names: readonly string[]): string;
40
+ /**
41
+ * The names a module exports, scanned from its source (declarations, `export { a, b as c }`,
42
+ * `export default`). A textual scan — registries are plain TypeScript lists, and a name the scan
43
+ * misses is covered by {@link imported_names} (what the host actually asks for).
44
+ */
45
+ export declare function export_names(source: string): Set<string>;
46
+ /**
47
+ * The names `importer_source` imports from `spec` (default → `default`, named → their exported
48
+ * names, `* as ns` → nothing extra: a namespace import links against whatever the stub declares).
49
+ */
50
+ export declare function imported_names(importer_source: string, spec: string): Set<string>;
@@ -0,0 +1,163 @@
1
+ import { parse_module } from "../parse/oxc.js";
2
+ import { region_family } from "../ids.js";
3
+ import { walk } from "estree-walker";
4
+ // src/link/registry-stub.ts
5
+ /**
6
+ * `ogygia:region/registry-stub/…`: keeps a region registry (a `.ts` module of marked imports) out
7
+ * of a csr=false page's client graph. Kit links the CSS of every module that graph reaches, but
8
+ * what a csr=false page needs is decided at SSR, where Region links the CSS of each region that
9
+ * renders. So on the csr=false client leg of a build, a route host's import of a registry reads the
10
+ * registry with only its marks blanked ({@link registry_client_source}): plain imports keep their
11
+ * edges (they render on the server, and the graph is the only thing that links their CSS). A
12
+ * csr=true page, the island world and dev keep the real registry.
13
+ */
14
+ const REGISTRY_STUB_PREFIX = region_family("registry_stub");
15
+ /** The query that names a registry's client-leg variant (its marks blanked, everything else kept). */
16
+ const REGISTRY_CLIENT_QUERY = "?og-registry-client";
17
+ /** The client-leg variant id of a registry: its real absolute path (relative imports resolve from
18
+ * the registry's own directory) plus {@link REGISTRY_CLIENT_QUERY}. */
19
+ function registry_client_id(abs_path) {
20
+ return abs_path + REGISTRY_CLIENT_QUERY;
21
+ }
22
+ /** The registry path a client-leg variant id names, or `null` for any other id. */
23
+ function registry_client_path(id) {
24
+ return id.endsWith("?og-registry-client") ? id.slice(0, -19) : null;
25
+ }
26
+ function is_og_macro(node, prop) {
27
+ return node?.type === "MemberExpression" && node.property?.name === prop && node.object?.type === "MemberExpression" && node.object.property?.name === "og" && node.object.object?.type === "MetaProperty" && node.object.object.meta?.name === "import" && node.object.object.property?.name === "meta";
28
+ }
29
+ /**
30
+ * The static VALUE import specifiers of a script module — `import … from`, side-effect `import '…'`,
31
+ * `export … from`, `export * from` (type-only forms skipped; dynamic `import()` skipped: Kit links
32
+ * no CSS for a node's dynamic chunks). `null` when the module does not parse.
33
+ */
34
+ function static_script_specs(source, file) {
35
+ const parsed = parse_module(source, file);
36
+ if (!parsed.ok) return null;
37
+ const specs = [];
38
+ for (const node of parsed.program.body) {
39
+ if (!(node.type === "ImportDeclaration" || node.type === "ExportAllDeclaration" || node.type === "ExportNamedDeclaration" && node.source) || node.importKind === "type" || node.exportKind === "type") continue;
40
+ const value = node.source?.value;
41
+ if (typeof value === "string") specs.push(value);
42
+ }
43
+ return specs;
44
+ }
45
+ function undefined_consts(locals) {
46
+ return locals.length ? `const ${locals.map((l) => `${l} = undefined`).join(", ")};` : "";
47
+ }
48
+ /**
49
+ * The registry's source with its MARKS blanked and everything else kept, or `null` when it does not
50
+ * parse (the caller falls back to the names-only stub). A mark is an import whose `with { … }`
51
+ * carries a region key (`region_keys`: the app's wake / render / region / preset names), default
52
+ * or named. Each blanked binding becomes
53
+ * `const X = undefined` so the module still links; the client leg never runs it anyway.
54
+ */
55
+ function registry_client_source(source, file, region_keys) {
56
+ const parsed = parse_module(source, file);
57
+ if (!parsed.ok) return null;
58
+ const body = parsed.program.body;
59
+ const edits = [];
60
+ walk(parsed.program, { enter(n) {
61
+ const node = n;
62
+ if (node.type !== "CallExpression") return;
63
+ if (!is_og_macro(node.callee, "regions")) return;
64
+ edits.push({
65
+ start: node.start,
66
+ end: node.end,
67
+ text: "({})"
68
+ });
69
+ this.skip();
70
+ } });
71
+ for (const node of body) {
72
+ if (node.type !== "ImportDeclaration" || node.importKind === "type") continue;
73
+ if (!(node.attributes ?? []).some((a) => region_keys.has(a.key?.name ?? a.key?.value))) continue;
74
+ const locals = (node.specifiers ?? []).map((s) => s.local.name);
75
+ edits.push({
76
+ start: node.start,
77
+ end: node.end,
78
+ text: undefined_consts(locals)
79
+ });
80
+ }
81
+ if (!edits.length) return source;
82
+ edits.sort((a, b) => b.start - a.start);
83
+ let out = source;
84
+ for (const e of edits) out = out.slice(0, e.start) + e.text + out.slice(e.end);
85
+ return out;
86
+ }
87
+ const NAMES_PARAM = "?names=";
88
+ const EXPORT_DECL_G = /^\s*export\s+(?:async\s+)?(?:const|let|var|function\*?|class|enum|abstract\s+class)\s+([A-Za-z_$][\w$]*)/gm;
89
+ const EXPORT_LIST_G = /^\s*export\s*\{([^}]*)\}/gm;
90
+ const EXPORT_DEFAULT_RE = /^\s*export\s+default\b/m;
91
+ const IMPORT_FROM_G = /import\s+(?:type\s+)?([^'";]*?)\s*from\s*(['"])([^'"]+)\2/g;
92
+ const IMPORT_SIDE_EFFECT_RE = /^\s*$/;
93
+ const AS_RE = /\s+as\s+/;
94
+ const WS_G = /\s+/g;
95
+ /** Root-relative posix path → the stub id. `names` are the exports the stub must declare. */
96
+ function registry_stub_id(rel_posix, names) {
97
+ const sorted = [...new Set(names)].sort();
98
+ return `${REGISTRY_STUB_PREFIX}${encodeURIComponent(rel_posix)}.js${NAMES_PARAM}${sorted.map(encodeURIComponent).join(",")}`;
99
+ }
100
+ /** True for a (resolved or bare) stub id. */
101
+ function is_registry_stub_id(id) {
102
+ return (id.startsWith("\0") ? id.slice(1) : id).startsWith(REGISTRY_STUB_PREFIX);
103
+ }
104
+ /** The export names a stub id carries (`default` included when the registry has one). */
105
+ function registry_stub_names(id) {
106
+ const at = id.indexOf(NAMES_PARAM);
107
+ if (at < 0) return [];
108
+ return id.slice(at + 7).split(",").filter(Boolean).map(decodeURIComponent);
109
+ }
110
+ /** The stub module's source: every name as `undefined`, so the host links and nothing is shipped. */
111
+ function registry_stub_source(names) {
112
+ const out = [];
113
+ for (const n of names) if (n === "default") out.push("export default undefined;\n");
114
+ else if (/^[A-Za-z_$][\w$]*$/.test(n)) out.push(`export const ${n} = undefined;\n`);
115
+ return out.length ? out.join("") : "export {};\n";
116
+ }
117
+ /**
118
+ * The names a module exports, scanned from its source (declarations, `export { a, b as c }`,
119
+ * `export default`). A textual scan — registries are plain TypeScript lists, and a name the scan
120
+ * misses is covered by {@link imported_names} (what the host actually asks for).
121
+ */
122
+ function export_names(source) {
123
+ const names = /* @__PURE__ */ new Set();
124
+ EXPORT_DECL_G.lastIndex = 0;
125
+ let m;
126
+ while (m = EXPORT_DECL_G.exec(source)) names.add(m[1]);
127
+ EXPORT_LIST_G.lastIndex = 0;
128
+ while (m = EXPORT_LIST_G.exec(source)) for (const part of m[1].split(",")) {
129
+ const p = part.trim();
130
+ if (!p) continue;
131
+ const [, alias] = p.split(AS_RE);
132
+ const name = (alias ?? p).trim();
133
+ if (name === "default" || /^[A-Za-z_$][\w$]*$/.test(name)) names.add(name);
134
+ }
135
+ if (EXPORT_DEFAULT_RE.test(source)) names.add("default");
136
+ return names;
137
+ }
138
+ /**
139
+ * The names `importer_source` imports from `spec` (default → `default`, named → their exported
140
+ * names, `* as ns` → nothing extra: a namespace import links against whatever the stub declares).
141
+ */
142
+ function imported_names(importer_source, spec) {
143
+ const names = /* @__PURE__ */ new Set();
144
+ IMPORT_FROM_G.lastIndex = 0;
145
+ let m;
146
+ while (m = IMPORT_FROM_G.exec(importer_source)) {
147
+ if (m[3] !== spec) continue;
148
+ const clause = m[1].replace(WS_G, " ").trim();
149
+ if (IMPORT_SIDE_EFFECT_RE.test(clause)) continue;
150
+ const brace = clause.indexOf("{");
151
+ const head = (brace >= 0 ? clause.slice(0, brace) : clause).replace(",", "").trim();
152
+ if (head && !head.startsWith("*")) names.add("default");
153
+ if (brace >= 0) for (const part of clause.slice(brace + 1, clause.indexOf("}")).split(",")) {
154
+ const p = part.trim().replace(/^type\s+/, "");
155
+ if (!p) continue;
156
+ const [exported] = p.split(AS_RE);
157
+ const name = exported.trim();
158
+ if (name) names.add(name);
159
+ }
160
+ }
161
+ return names;
162
+ }
163
+ export { REGISTRY_CLIENT_QUERY, REGISTRY_STUB_PREFIX, export_names, imported_names, is_registry_stub_id, registry_client_id, registry_client_path, registry_client_source, registry_stub_id, registry_stub_names, registry_stub_source, static_script_specs };
@@ -0,0 +1,79 @@
1
+ import { Program } from "../program.js";
2
+ import { PageReadsSink } from "../ctx.js";
3
+ import MagicString from "magic-string";
4
+ /** A module as the bundler's graph knows it. */
5
+ export type GraphInfo = {
6
+ importedIds: readonly string[];
7
+ dynamicallyImportedIds: readonly string[];
8
+ } | null;
9
+ /** What a relocation's value is made from: the final records and the finished graph. */
10
+ export interface RelocateCx {
11
+ readonly program: Program;
12
+ /** A region's public URL, the key its CSS is handed off under. */
13
+ public_url(iid: string): string;
14
+ /** The leg's late server regions' chunk refs, by island id. */
15
+ readonly late_server_regions: ReadonlyMap<string, string>;
16
+ /** The chunk being finished, and an emitted chunk's file name by its ref. */
17
+ readonly chunk_file: string;
18
+ file_name(ref: string): string;
19
+ /** The file of the chunk a module is the facade of (its own chunk), if it has one. */
20
+ facade_file(id: string): string | undefined;
21
+ graph(id: string): GraphInfo;
22
+ module_ids(): Iterable<string>;
23
+ /** The seed-shaping plugin's answers (`dataShake()`); null when none is listed, and then every
24
+ * hole looks the page up. */
25
+ readonly page_reads: Pick<PageReadsSink, "keys"> | null;
26
+ }
27
+ export interface Relocation<Cx = RelocateCx> {
28
+ readonly name: string;
29
+ /** The text in a chunk that stands for the value (or begins it, with `pattern`). */
30
+ readonly token: string;
31
+ /** A token that carries its own part (a module id): each match is replaced whole; group 1 is the
32
+ * quote around it, group 2 its part. Without one the token is the whole match. */
33
+ readonly pattern?: RegExp;
34
+ /** Only a server leg's chunks carry it. */
35
+ readonly server_only: boolean;
36
+ /** A chunk it patched hands on a map of the patch. Left out: the chunk's map passes as it was
37
+ * given, as these patches always did (one map through every pass is report step 13). */
38
+ readonly mapped?: true;
39
+ /** The value, once per chunk, then per token: `quote` is the character before the token (a token
40
+ * inside a string literal is escaped for that literal's quote), or the pattern's quote; `part` is
41
+ * the pattern's part (`''` without one). */
42
+ value(cx: Cx): (quote: string, part: string) => string;
43
+ }
44
+ /**
45
+ * Each lazy loader's own file (vite/lazy-files.ts): the path from the chunk being finished to the
46
+ * module's own chunk, or `null` when it has none (it shares a chunk with others: the bundler reads
47
+ * it out of that chunk's exports, which a plain import of the file would not).
48
+ */
49
+ export declare const FILE: Relocation;
50
+ /**
51
+ * Values known only at the end of a leg. A virtual module loads before the bundler has transformed
52
+ * every module, so a value that needs every transform (which holes read `page.data`) is written as
53
+ * a token, and each finished chunk has its tokens replaced at renderChunk from the final records.
54
+ * A token is a string or an expression the bundler keeps, never a comment, which it drops before
55
+ * renderChunk. This table is every such token. A leg whose output still holds a token is said
56
+ * (`relocation_unpatched`). One value comes from a later leg: {@link WRITTEN_RELOCATIONS}.
57
+ */
58
+ export declare const RELOCATIONS: readonly Relocation[];
59
+ /** What the client leg's handoff makes: the island deps JSON. */
60
+ export interface WrittenCx {
61
+ readonly island_deps: string;
62
+ }
63
+ /** The values a later leg writes into this leg's files after they were written. */
64
+ export declare const WRITTEN_RELOCATIONS: readonly Relocation<WrittenCx>[];
65
+ /** The relocations a leg's chunks carry. */
66
+ export declare const relocations_of: (server: boolean) => readonly Relocation[];
67
+ /**
68
+ * Replace every token of `table` in a finished chunk (each found in the chunk as it was given),
69
+ * counting each patch into `counts` (by name). `null` when the chunk carries none. `mapped`: a
70
+ * `mapped` relocation patched it.
71
+ */
72
+ export declare function relocate<Cx>(code: string, table: readonly Relocation<Cx>[], counts: Map<string, number>, cx: Cx): {
73
+ s: MagicString;
74
+ mapped: boolean;
75
+ } | null;
76
+ /** A relocation a leg's output left unpatched: a chunk still holds its token. */
77
+ export declare function unpatched(server: boolean, chunks: Iterable<{
78
+ code: string;
79
+ }>): Relocation[];