@ecopages/react 0.2.0-beta.13 → 0.2.0-beta.15

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.
@@ -1,461 +1,15 @@
1
1
  import { existsSync, readFileSync } from "node:fs";
2
2
  import { dirname, extname, resolve } from "node:path";
3
- import { cachedParseSync } from "@ecopages/core/cache";
4
3
  import { ClientGraphBoundaryCache } from "./client-graph-boundary-cache.js";
5
- import { analyzeReachability } from "./reachability-analyzer.js";
4
+ import {
5
+ parseDeclaredModules,
6
+ toModuleBaseSpecifier,
7
+ mergeRequestedExportRules,
8
+ snapshotRegistry,
9
+ diffRequestedExportRules
10
+ } from "./client-graph-reachability.js";
11
+ import { transformModuleImports } from "./client-graph-ast-transform.js";
6
12
  const SOURCE_FILE_FILTER = /\.(tsx?|jsx?)$/;
7
- const SERVER_ONLY_ECO_PAGE_OPTION_KEYS = /* @__PURE__ */ new Set([
8
- "cache",
9
- "middleware",
10
- "requires",
11
- "metadata",
12
- "staticProps",
13
- "staticPaths"
14
- ]);
15
- function isBareSpecifier(specifier) {
16
- if (specifier.startsWith(".")) return false;
17
- if (specifier.startsWith("/")) return false;
18
- if (specifier.includes("://")) return false;
19
- return true;
20
- }
21
- function isProjectAliasSpecifier(specifier) {
22
- return specifier.startsWith("@/") || specifier.startsWith("~/") || specifier.startsWith("ecopages:");
23
- }
24
- function isServerOnlySpecifier(specifier) {
25
- if (specifier.startsWith("node:")) return true;
26
- return /(?:^|[/])[^/]+\.server(?:$|\.)/.test(specifier);
27
- }
28
- function toModuleBaseSpecifier(specifier) {
29
- if (!isBareSpecifier(specifier) || specifier.startsWith("node:")) {
30
- return specifier;
31
- }
32
- if (specifier.startsWith("@")) {
33
- const [scope, name2] = specifier.split("/");
34
- if (!scope || !name2) return specifier;
35
- return `${scope}/${name2}`;
36
- }
37
- const [name] = specifier.split("/");
38
- return name ?? specifier;
39
- }
40
- function parseDeclaredModules(moduleDeclarations) {
41
- const map = /* @__PURE__ */ new Map();
42
- for (const declaration of moduleDeclarations ?? []) {
43
- const source = declaration.trim();
44
- if (source.length === 0) continue;
45
- const openBraceIndex = source.indexOf("{");
46
- if (openBraceIndex < 0) {
47
- map.set(toModuleBaseSpecifier(source), "*");
48
- continue;
49
- }
50
- const closeBraceIndex = source.indexOf("}", openBraceIndex);
51
- const rawPkg = source.slice(0, openBraceIndex).trim();
52
- if (rawPkg.length === 0) continue;
53
- const pkg = toModuleBaseSpecifier(rawPkg);
54
- const namedImportsStr = closeBraceIndex > openBraceIndex ? source.slice(openBraceIndex + 1, closeBraceIndex) : source.slice(openBraceIndex + 1);
55
- const namedImports = namedImportsStr.split(",").map((s) => s.trim()).filter(Boolean);
56
- const existing = map.get(pkg);
57
- if (existing === "*") continue;
58
- if (!existing) {
59
- if (namedImports.length === 0) {
60
- map.set(pkg, "*");
61
- } else {
62
- map.set(pkg, new Set(namedImports));
63
- }
64
- } else {
65
- for (const name of namedImports) {
66
- existing.add(name);
67
- }
68
- }
69
- }
70
- return map;
71
- }
72
- function mergeDeclaredModulesMap(a, b) {
73
- const result = new Map(a);
74
- for (const [pkg, imports] of b.entries()) {
75
- const existing = result.get(pkg);
76
- if (existing === "*") continue;
77
- if (imports === "*") {
78
- result.set(pkg, "*");
79
- continue;
80
- }
81
- if (!existing) {
82
- result.set(pkg, imports);
83
- } else {
84
- for (const name of imports) {
85
- existing.add(name);
86
- }
87
- }
88
- }
89
- return result;
90
- }
91
- function parserLanguageForFile(filename) {
92
- const extension = extname(filename).toLowerCase();
93
- if (extension === ".tsx") return "tsx";
94
- if (extension === ".ts") return "ts";
95
- if (extension === ".jsx") return "jsx";
96
- return "js";
97
- }
98
- function getObjectPropertyKeyName(node) {
99
- if (!node) return void 0;
100
- if (node.type === "Identifier") return node.name;
101
- if (node.type === "StringLiteral" || node.type === "Literal") {
102
- return typeof node.value === "string" ? node.value : void 0;
103
- }
104
- return void 0;
105
- }
106
- function stripServerOnlyEcoPageOptions(source, program) {
107
- const edits = [];
108
- function walk(node) {
109
- if (!node || typeof node !== "object") return;
110
- if (Array.isArray(node)) {
111
- for (const child of node) walk(child);
112
- return;
113
- }
114
- if (node.type === "CallExpression" && node.callee?.type === "MemberExpression" && node.callee.object?.type === "Identifier" && node.callee.object.name === "eco" && node.callee.property?.type === "Identifier" && node.callee.property.name === "page" && node.arguments?.[0]?.type === "ObjectExpression") {
115
- const objectExpression = node.arguments[0];
116
- const keptProperties = [];
117
- let removedProperty = false;
118
- for (const property of objectExpression.properties ?? []) {
119
- if (property?.type === "Property") {
120
- const keyName = getObjectPropertyKeyName(property.key);
121
- if (keyName && SERVER_ONLY_ECO_PAGE_OPTION_KEYS.has(keyName)) {
122
- removedProperty = true;
123
- continue;
124
- }
125
- }
126
- keptProperties.push(source.slice(property.start, property.end));
127
- }
128
- if (removedProperty) {
129
- const replacement = keptProperties.length > 0 ? `{ ${keptProperties.join(", ")} }` : "{}";
130
- edits.push({
131
- start: objectExpression.start,
132
- end: objectExpression.end,
133
- replacement
134
- });
135
- }
136
- }
137
- for (const key in node) {
138
- if (key !== "type" && key !== "start" && key !== "end") {
139
- walk(node[key]);
140
- }
141
- }
142
- }
143
- walk(program);
144
- if (edits.length === 0) {
145
- return { transformed: source, modified: false };
146
- }
147
- edits.sort((a, b) => b.start - a.start);
148
- let transformed = source;
149
- for (const edit of edits) {
150
- transformed = transformed.slice(0, edit.start) + edit.replacement + transformed.slice(edit.end);
151
- }
152
- return { transformed, modified: true };
153
- }
154
- function normalizeRequestedExportsKey(pathname) {
155
- let normalized = pathname.replace(/\\/g, "/");
156
- normalized = normalized.replace(/\.(tsx?|jsx?)$/i, "");
157
- if (normalized.endsWith("/index")) {
158
- normalized = normalized.slice(0, -"/index".length);
159
- }
160
- return normalized;
161
- }
162
- function resolveRequestedExportsKey(importer, specifier) {
163
- if (isBareSpecifier(specifier) || isProjectAliasSpecifier(specifier)) {
164
- return void 0;
165
- }
166
- const resolved = specifier.startsWith("/") ? specifier : resolve(dirname(importer), specifier);
167
- return normalizeRequestedExportsKey(resolved);
168
- }
169
- function mergeRequestedExportRules(registry, moduleKey, rules) {
170
- const existing = registry.get(moduleKey);
171
- if (existing === "*") return;
172
- if (rules === "*") {
173
- registry.set(moduleKey, "*");
174
- return;
175
- }
176
- if (!existing) {
177
- registry.set(moduleKey, new Set(rules));
178
- return;
179
- }
180
- for (const rule of rules) {
181
- existing.add(rule);
182
- }
183
- }
184
- function cloneRequestedExportRules(rules) {
185
- return rules === "*" ? rules : new Set(rules);
186
- }
187
- function snapshotRegistry(registry) {
188
- const out = /* @__PURE__ */ new Map();
189
- for (const [key, rules] of registry) {
190
- out.set(key, cloneRequestedExportRules(rules));
191
- }
192
- return out;
193
- }
194
- function diffRequestedExportRules(before, after) {
195
- if (!before) {
196
- return cloneRequestedExportRules(after);
197
- }
198
- if (before === "*") {
199
- return void 0;
200
- }
201
- if (after === "*") {
202
- return "*";
203
- }
204
- const addedRules = /* @__PURE__ */ new Set();
205
- for (const rule of after) {
206
- if (!before.has(rule)) {
207
- addedRules.add(rule);
208
- }
209
- }
210
- return addedRules.size > 0 ? addedRules : void 0;
211
- }
212
- function transformModuleImports(source, filename, globallyAllowed, requestedExports) {
213
- let result;
214
- try {
215
- result = cachedParseSync(filename, source, {
216
- sourceType: "module",
217
- lang: parserLanguageForFile(filename)
218
- });
219
- } catch {
220
- return { transformed: source, modified: false };
221
- }
222
- const { program } = result;
223
- const localDeclared = [];
224
- function walk(node) {
225
- if (!node || typeof node !== "object") return;
226
- if (Array.isArray(node)) {
227
- for (const child of node) walk(child);
228
- return;
229
- }
230
- if (node.type === "Property" && node.key?.name === "modules" && node.value?.type === "ArrayExpression") {
231
- for (const el of node.value.elements) {
232
- if ((el.type === "StringLiteral" || el.type === "Literal") && typeof el.value === "string") {
233
- localDeclared.push(el.value);
234
- }
235
- }
236
- }
237
- for (const key in node) {
238
- if (key !== "type" && key !== "start" && key !== "end") {
239
- walk(node[key]);
240
- }
241
- }
242
- }
243
- walk(program);
244
- const locallyAllowed = parseDeclaredModules(localDeclared);
245
- const allowedMap = mergeDeclaredModulesMap(globallyAllowed, locallyAllowed);
246
- const explicitRequestedExports = requestedExports.get(normalizeRequestedExportsKey(filename));
247
- const reachability = analyzeReachability(source, filename, program, explicitRequestedExports);
248
- for (const statement of program.body) {
249
- if (statement.type === "ImportDeclaration") {
250
- const reachableRules = reachability.reachableImports.get(statement.source.value);
251
- const requestedModuleKey = resolveRequestedExportsKey(filename, statement.source.value);
252
- if (!requestedModuleKey || !reachableRules) continue;
253
- mergeRequestedExportRules(requestedExports, requestedModuleKey, reachableRules);
254
- continue;
255
- }
256
- if (statement.type === "ExportNamedDeclaration" && statement.source) {
257
- const reachableRules = reachability.reachableImports.get(statement.source.value);
258
- const requestedModuleKey = resolveRequestedExportsKey(filename, statement.source.value);
259
- if (!requestedModuleKey || !reachableRules) continue;
260
- mergeRequestedExportRules(requestedExports, requestedModuleKey, reachableRules);
261
- continue;
262
- }
263
- if (statement.type === "ExportAllDeclaration" && statement.source) {
264
- const reachableRules = reachability.reachableImports.get(statement.source.value);
265
- const requestedModuleKey = resolveRequestedExportsKey(filename, statement.source.value);
266
- if (!requestedModuleKey || !reachableRules) continue;
267
- mergeRequestedExportRules(requestedExports, requestedModuleKey, reachableRules);
268
- }
269
- }
270
- const edits = [];
271
- function processSpecifier(specifier) {
272
- const moduleBase = toModuleBaseSpecifier(specifier);
273
- const explicitRules = allowedMap.get(moduleBase);
274
- if (isServerOnlySpecifier(specifier)) {
275
- if (explicitRules) {
276
- return { allowed: true, rules: explicitRules };
277
- }
278
- return { allowed: false };
279
- }
280
- if (isProjectAliasSpecifier(specifier)) return { allowed: true, rules: explicitRules ?? "*" };
281
- if (!isBareSpecifier(specifier)) return { allowed: true, rules: explicitRules ?? "*" };
282
- return { allowed: true, rules: explicitRules ?? "*" };
283
- }
284
- function walkImports(node) {
285
- if (!node || typeof node !== "object") return;
286
- if (Array.isArray(node)) {
287
- for (const child of node) walkImports(child);
288
- return;
289
- }
290
- if (node.type === "ImportDeclaration") {
291
- const specifier = node.source.value;
292
- const reachableRules = reachability.reachableImports.get(specifier);
293
- const { allowed, rules } = processSpecifier(specifier);
294
- if (!allowed) {
295
- if (reachableRules && !reachability.isFallbackRoots) {
296
- throw new Error(
297
- `[Ecopages Client Reachability] Forbidden client import '${specifier}' at ${filename}:${node.start}. This import is explicitly reachable from the React render function.`
298
- );
299
- } else {
300
- edits.push({ start: node.start, end: node.end, replacement: "" });
301
- }
302
- return;
303
- }
304
- if (rules instanceof Set && node.specifiers && node.specifiers.length > 0) {
305
- let keptSpecifierCount = 0;
306
- let defaultImportLocal;
307
- let namespaceImportLocal;
308
- const namedImportNodes = [];
309
- for (const spec of node.specifiers) {
310
- if (spec.type === "ImportSpecifier") {
311
- const importedName = spec.imported.type === "Identifier" ? spec.imported.name : spec.imported.value;
312
- if (rules.has(importedName)) {
313
- keptSpecifierCount += 1;
314
- const localName = spec.local?.name;
315
- if (localName && localName !== importedName) {
316
- namedImportNodes.push(`${importedName} as ${localName}`);
317
- } else {
318
- namedImportNodes.push(importedName);
319
- }
320
- }
321
- } else if (spec.type === "ImportDefaultSpecifier") {
322
- if (rules.has("default")) {
323
- keptSpecifierCount += 1;
324
- defaultImportLocal = spec.local.name;
325
- }
326
- } else if (spec.type === "ImportNamespaceSpecifier") {
327
- if (rules.has("*")) {
328
- keptSpecifierCount += 1;
329
- namespaceImportLocal = spec.local.name;
330
- }
331
- }
332
- }
333
- if (keptSpecifierCount === 0) {
334
- edits.push({ start: node.start, end: node.end, replacement: "" });
335
- } else if (keptSpecifierCount < node.specifiers.length) {
336
- let newDeclaration = "";
337
- if (defaultImportLocal && namespaceImportLocal) {
338
- newDeclaration = `import ${defaultImportLocal}, * as ${namespaceImportLocal} from '${specifier}';`;
339
- } else if (namespaceImportLocal) {
340
- newDeclaration = `import * as ${namespaceImportLocal} from '${specifier}';`;
341
- } else if (defaultImportLocal && namedImportNodes.length > 0) {
342
- newDeclaration = `import ${defaultImportLocal}, { ${namedImportNodes.join(", ")} } from '${specifier}';`;
343
- } else if (defaultImportLocal) {
344
- newDeclaration = `import ${defaultImportLocal} from '${specifier}';`;
345
- } else {
346
- newDeclaration = `import { ${namedImportNodes.join(", ")} } from '${specifier}';`;
347
- }
348
- edits.push({ start: node.start, end: node.end, replacement: newDeclaration });
349
- }
350
- return;
351
- }
352
- if (!reachableRules && (!node.specifiers || node.specifiers.length === 0)) {
353
- edits.push({ start: node.start, end: node.end, replacement: "" });
354
- }
355
- return;
356
- }
357
- if (node.type === "ExportNamedDeclaration" && node.source) {
358
- const specifier = node.source.value;
359
- const { allowed } = processSpecifier(specifier);
360
- if (!allowed) {
361
- const reachableRules = reachability.reachableImports.get(specifier);
362
- if (reachableRules && !reachability.isFallbackRoots) {
363
- throw new Error(
364
- `[Ecopages Client Reachability] Forbidden client export from '${specifier}' at ${filename}:${node.start}. This export is explicitly reachable from the React render function.`
365
- );
366
- } else {
367
- edits.push({ start: node.start, end: node.end, replacement: "" });
368
- }
369
- }
370
- return;
371
- }
372
- if (node.type === "ExportAllDeclaration" && node.source) {
373
- const specifier = node.source.value;
374
- const { allowed } = processSpecifier(specifier);
375
- if (!allowed) {
376
- const reachableRules = reachability.reachableImports.get(specifier);
377
- if (reachableRules && !reachability.isFallbackRoots) {
378
- throw new Error(
379
- `[Ecopages Client Reachability] Forbidden client export * from '${specifier}' at ${filename}:${node.start}. This export is explicitly reachable from the React render function.`
380
- );
381
- } else {
382
- edits.push({ start: node.start, end: node.end, replacement: "" });
383
- }
384
- }
385
- return;
386
- }
387
- if (node.type === "ImportExpression" && node.source?.value) {
388
- const specifier = node.source.value;
389
- const { allowed } = processSpecifier(specifier);
390
- const reachableRules = reachability.reachableImports.get(specifier);
391
- if (!reachableRules) {
392
- if (!allowed) {
393
- edits.push({ start: node.start, end: node.end, replacement: "Promise.resolve({})" });
394
- }
395
- return;
396
- }
397
- if (!allowed) {
398
- if (!reachability.isFallbackRoots) {
399
- throw new Error(
400
- `[Ecopages Client Reachability] Forbidden dynamic import('${specifier}') at ${filename}:${node.start}. This import is explicitly reachable from the React render function.`
401
- );
402
- } else {
403
- edits.push({ start: node.start, end: node.end, replacement: "Promise.resolve({})" });
404
- }
405
- }
406
- return;
407
- }
408
- if (node.type === "CallExpression" && node.callee?.type === "Identifier" && node.callee.name === "require") {
409
- const arg = node.arguments?.[0];
410
- if (arg && (arg.type === "StringLiteral" || arg.type === "Literal") && typeof arg.value === "string") {
411
- const specifier = arg.value;
412
- const { allowed } = processSpecifier(specifier);
413
- const reachableRules = reachability.reachableImports.get(specifier);
414
- if (!reachableRules) {
415
- if (!allowed) {
416
- edits.push({ start: node.start, end: node.end, replacement: "({})" });
417
- }
418
- } else if (!allowed) {
419
- if (!reachability.isFallbackRoots) {
420
- throw new Error(
421
- `[Ecopages Client Reachability] Forbidden require('${specifier}') at ${filename}:${node.start}. This import is explicitly reachable from the React render function.`
422
- );
423
- } else {
424
- edits.push({ start: node.start, end: node.end, replacement: "({})" });
425
- }
426
- }
427
- }
428
- }
429
- for (const key in node) {
430
- if (key !== "type" && key !== "start" && key !== "end") {
431
- walkImports(node[key]);
432
- }
433
- }
434
- }
435
- walkImports(program);
436
- if (edits.length === 0) {
437
- return stripServerOnlyEcoPageOptions(source, program);
438
- }
439
- edits.sort((a, b) => b.start - a.start);
440
- let transformed = source;
441
- for (const edit of edits) {
442
- transformed = transformed.slice(0, edit.start) + edit.replacement + transformed.slice(edit.end);
443
- }
444
- let reparsedResult;
445
- try {
446
- reparsedResult = cachedParseSync(filename, transformed, {
447
- sourceType: "module",
448
- lang: parserLanguageForFile(filename)
449
- });
450
- } catch {
451
- return { transformed, modified: true };
452
- }
453
- const strippedPageOptions = stripServerOnlyEcoPageOptions(transformed, reparsedResult.program);
454
- if (strippedPageOptions.modified) {
455
- return strippedPageOptions;
456
- }
457
- return { transformed, modified: true };
458
- }
459
13
  function createClientGraphBoundaryPlugin(options) {
460
14
  return {
461
15
  name: "ecopages-client-graph-boundary",
@@ -0,0 +1,75 @@
1
+ /**
2
+ * Specifier classification and requested-export registry helpers for the client graph boundary.
3
+ */
4
+ import type { RequestedExportRules } from './client-graph-boundary-cache.js';
5
+ export declare function isBareSpecifier(specifier: string): boolean;
6
+ export declare function isProjectAliasSpecifier(specifier: string): boolean;
7
+ /**
8
+ * Determines whether a specifier should be treated as server-only.
9
+ *
10
+ * This covers Node built-ins as well as local module conventions such as
11
+ * `.server.ts` and extensionless imports that resolve to `.server.*` files.
12
+ *
13
+ * @param specifier - Raw import specifier from the module source.
14
+ * @returns True when the import must never become client-reachable.
15
+ */
16
+ export declare function isServerOnlySpecifier(specifier: string): boolean;
17
+ /**
18
+ * Strips down a deep path module specifier to its foundational root package name.
19
+ *
20
+ * When checking against the "allowed modules" whitelist, a component might import something deeply
21
+ * nested like `lodash/fp/map` or `@myorg/ui/button`. However, the user configuration only whitelists
22
+ * the root `lodash` or `@myorg/ui` package. This normalizer ensures we are comparing apples to apples
23
+ * by extracting the base package scope before checking the authorization list.
24
+ *
25
+ * @example
26
+ * toModuleBaseSpecifier('@scope/package/deep/file') -> '@scope/package'
27
+ * toModuleBaseSpecifier('lodash/cloneDeep') -> 'lodash'
28
+ * toModuleBaseSpecifier('node:fs') -> 'node:fs'
29
+ *
30
+ * @param specifier - The raw import specifier from the code.
31
+ * @returns The root package name, preserving scoped npm organizations.
32
+ */
33
+ export declare function toModuleBaseSpecifier(specifier: string): string;
34
+ /**
35
+ * Parses the grammar syntax of declared modules.
36
+ * Handles patterns like `@pkg/name` and `@pkg/name{namedImport,anotherImport}`
37
+ * returning a map of base packages to their explicitly allowed specifiers.
38
+ *
39
+ * @param moduleDeclarations - A list of module declaration strings.
40
+ * @returns A structured map of allowed packages and their named exports.
41
+ */
42
+ export declare function parseDeclaredModules(moduleDeclarations: readonly string[] | undefined): Map<string, Set<string> | '*'>;
43
+ /**
44
+ * Merges two declared module maps, combining their allowable specific scopes.
45
+ *
46
+ * @param a - First map of declared modules.
47
+ * @param b - Second map of declared modules to merge into the first.
48
+ * @returns A unified map of both declarations.
49
+ */
50
+ export declare function mergeDeclaredModulesMap(a: Map<string, Set<string> | '*'>, b: Map<string, Set<string> | '*'>): Map<string, Set<string> | '*'>;
51
+ export declare function normalizeRequestedExportsKey(pathname: string): string;
52
+ /**
53
+ * Resolves a local import specifier into a requested-export registry key.
54
+ *
55
+ * Bare package specifiers and project aliases are intentionally ignored because
56
+ * requested-export propagation is only used for cross-file local reachability.
57
+ *
58
+ * @param importer - Absolute path of the importing module.
59
+ * @param specifier - Raw import or re-export specifier.
60
+ * @returns Registry key for a local dependency, or `undefined` when not applicable.
61
+ */
62
+ export declare function resolveRequestedExportsKey(importer: string, specifier: string): string | undefined;
63
+ /**
64
+ * Merges newly discovered requested-export rules into the local propagation registry.
65
+ *
66
+ * Once a module is promoted to `'*'`, it stays fully reachable for the remainder
67
+ * of the transform pass.
68
+ *
69
+ * @param registry - Cross-module requested-export registry.
70
+ * @param moduleKey - Normalized local module key.
71
+ * @param rules - Newly observed reachable export rules for the module.
72
+ */
73
+ export declare function mergeRequestedExportRules(registry: Map<string, RequestedExportRules>, moduleKey: string, rules: Set<string> | '*'): void;
74
+ export declare function snapshotRegistry(registry: Map<string, RequestedExportRules>): Map<string, RequestedExportRules>;
75
+ export declare function diffRequestedExportRules(before: RequestedExportRules | undefined, after: RequestedExportRules): RequestedExportRules | undefined;
@@ -0,0 +1,148 @@
1
+ import { dirname, resolve } from "node:path";
2
+ function isBareSpecifier(specifier) {
3
+ if (specifier.startsWith(".")) return false;
4
+ if (specifier.startsWith("/")) return false;
5
+ if (specifier.includes("://")) return false;
6
+ return true;
7
+ }
8
+ function isProjectAliasSpecifier(specifier) {
9
+ return specifier.startsWith("@/") || specifier.startsWith("~/") || specifier.startsWith("ecopages:");
10
+ }
11
+ function isServerOnlySpecifier(specifier) {
12
+ if (specifier.startsWith("node:")) return true;
13
+ return /(?:^|[/])[^/]+\.server(?:$|\.)/.test(specifier);
14
+ }
15
+ function toModuleBaseSpecifier(specifier) {
16
+ if (!isBareSpecifier(specifier) || specifier.startsWith("node:")) {
17
+ return specifier;
18
+ }
19
+ if (specifier.startsWith("@")) {
20
+ const [scope, name2] = specifier.split("/");
21
+ if (!scope || !name2) return specifier;
22
+ return `${scope}/${name2}`;
23
+ }
24
+ const [name] = specifier.split("/");
25
+ return name ?? specifier;
26
+ }
27
+ function parseDeclaredModules(moduleDeclarations) {
28
+ const map = /* @__PURE__ */ new Map();
29
+ for (const declaration of moduleDeclarations ?? []) {
30
+ const source = declaration.trim();
31
+ if (source.length === 0) continue;
32
+ const openBraceIndex = source.indexOf("{");
33
+ if (openBraceIndex < 0) {
34
+ map.set(toModuleBaseSpecifier(source), "*");
35
+ continue;
36
+ }
37
+ const closeBraceIndex = source.indexOf("}", openBraceIndex);
38
+ const rawPkg = source.slice(0, openBraceIndex).trim();
39
+ if (rawPkg.length === 0) continue;
40
+ const pkg = toModuleBaseSpecifier(rawPkg);
41
+ const namedImportsStr = closeBraceIndex > openBraceIndex ? source.slice(openBraceIndex + 1, closeBraceIndex) : source.slice(openBraceIndex + 1);
42
+ const namedImports = namedImportsStr.split(",").map((s) => s.trim()).filter(Boolean);
43
+ const existing = map.get(pkg);
44
+ if (existing === "*") continue;
45
+ if (!existing) {
46
+ if (namedImports.length === 0) {
47
+ map.set(pkg, "*");
48
+ } else {
49
+ map.set(pkg, new Set(namedImports));
50
+ }
51
+ } else {
52
+ for (const name of namedImports) {
53
+ existing.add(name);
54
+ }
55
+ }
56
+ }
57
+ return map;
58
+ }
59
+ function mergeDeclaredModulesMap(a, b) {
60
+ const result = new Map(a);
61
+ for (const [pkg, imports] of b.entries()) {
62
+ const existing = result.get(pkg);
63
+ if (existing === "*") continue;
64
+ if (imports === "*") {
65
+ result.set(pkg, "*");
66
+ continue;
67
+ }
68
+ if (!existing) {
69
+ result.set(pkg, imports);
70
+ } else {
71
+ for (const name of imports) {
72
+ existing.add(name);
73
+ }
74
+ }
75
+ }
76
+ return result;
77
+ }
78
+ function normalizeRequestedExportsKey(pathname) {
79
+ let normalized = pathname.replace(/\\/g, "/");
80
+ normalized = normalized.replace(/\.(tsx?|jsx?)$/i, "");
81
+ if (normalized.endsWith("/index")) {
82
+ normalized = normalized.slice(0, -"/index".length);
83
+ }
84
+ return normalized;
85
+ }
86
+ function resolveRequestedExportsKey(importer, specifier) {
87
+ if (isBareSpecifier(specifier) || isProjectAliasSpecifier(specifier)) {
88
+ return void 0;
89
+ }
90
+ const resolved = specifier.startsWith("/") ? specifier : resolve(dirname(importer), specifier);
91
+ return normalizeRequestedExportsKey(resolved);
92
+ }
93
+ function mergeRequestedExportRules(registry, moduleKey, rules) {
94
+ const existing = registry.get(moduleKey);
95
+ if (existing === "*") return;
96
+ if (rules === "*") {
97
+ registry.set(moduleKey, "*");
98
+ return;
99
+ }
100
+ if (!existing) {
101
+ registry.set(moduleKey, new Set(rules));
102
+ return;
103
+ }
104
+ for (const rule of rules) {
105
+ existing.add(rule);
106
+ }
107
+ }
108
+ function cloneRequestedExportRules(rules) {
109
+ return rules === "*" ? rules : new Set(rules);
110
+ }
111
+ function snapshotRegistry(registry) {
112
+ const out = /* @__PURE__ */ new Map();
113
+ for (const [key, rules] of registry) {
114
+ out.set(key, cloneRequestedExportRules(rules));
115
+ }
116
+ return out;
117
+ }
118
+ function diffRequestedExportRules(before, after) {
119
+ if (!before) {
120
+ return cloneRequestedExportRules(after);
121
+ }
122
+ if (before === "*") {
123
+ return void 0;
124
+ }
125
+ if (after === "*") {
126
+ return "*";
127
+ }
128
+ const addedRules = /* @__PURE__ */ new Set();
129
+ for (const rule of after) {
130
+ if (!before.has(rule)) {
131
+ addedRules.add(rule);
132
+ }
133
+ }
134
+ return addedRules.size > 0 ? addedRules : void 0;
135
+ }
136
+ export {
137
+ diffRequestedExportRules,
138
+ isBareSpecifier,
139
+ isProjectAliasSpecifier,
140
+ isServerOnlySpecifier,
141
+ mergeDeclaredModulesMap,
142
+ mergeRequestedExportRules,
143
+ normalizeRequestedExportsKey,
144
+ parseDeclaredModules,
145
+ resolveRequestedExportsKey,
146
+ snapshotRegistry,
147
+ toModuleBaseSpecifier
148
+ };