@chee/patchwork-bundles 0.1.0 → 0.2.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 (53) hide show
  1. package/dist/esbuild.d.ts +3 -0
  2. package/dist/esbuild.d.ts.map +1 -0
  3. package/{src/esbuild.ts → dist/esbuild.js} +1 -0
  4. package/dist/esbuild.js.map +1 -0
  5. package/dist/farm.d.ts +3 -0
  6. package/dist/farm.d.ts.map +1 -0
  7. package/{src/farm.ts → dist/farm.js} +1 -0
  8. package/dist/farm.js.map +1 -0
  9. package/dist/index.d.ts +5 -0
  10. package/dist/index.d.ts.map +1 -0
  11. package/dist/index.js +98 -0
  12. package/dist/index.js.map +1 -0
  13. package/dist/rollup.d.ts +3 -0
  14. package/dist/rollup.d.ts.map +1 -0
  15. package/{src/rollup.ts → dist/rollup.js} +1 -0
  16. package/dist/rollup.js.map +1 -0
  17. package/dist/rspack.d.ts +3 -0
  18. package/dist/rspack.d.ts.map +1 -0
  19. package/{src/rspack.ts → dist/rspack.js} +1 -0
  20. package/dist/rspack.js.map +1 -0
  21. package/dist/test/integration.test.d.ts +2 -0
  22. package/dist/test/integration.test.d.ts.map +1 -0
  23. package/dist/test/integration.test.js +172 -0
  24. package/dist/test/integration.test.js.map +1 -0
  25. package/dist/types.d.ts +41 -0
  26. package/dist/types.d.ts.map +1 -0
  27. package/dist/types.js +10 -0
  28. package/dist/types.js.map +1 -0
  29. package/dist/utils.d.ts +42 -0
  30. package/dist/utils.d.ts.map +1 -0
  31. package/dist/utils.js +141 -0
  32. package/dist/utils.js.map +1 -0
  33. package/dist/vite.d.ts +3 -0
  34. package/dist/vite.d.ts.map +1 -0
  35. package/{src/vite.ts → dist/vite.js} +1 -0
  36. package/dist/vite.js.map +1 -0
  37. package/dist/webpack.d.ts +3 -0
  38. package/dist/webpack.d.ts.map +1 -0
  39. package/{src/webpack.ts → dist/webpack.js} +1 -0
  40. package/dist/webpack.js.map +1 -0
  41. package/package.json +4 -1
  42. package/.claude/settings.local.json +0 -27
  43. package/pnpm-workspace.yaml +0 -41
  44. package/src/index.ts +0 -133
  45. package/src/types.ts +0 -52
  46. package/src/utils.ts +0 -166
  47. package/test/integration.test.ts +0 -305
  48. package/tsconfig.json +0 -40
  49. package/vitest.config.d.ts +0 -3
  50. package/vitest.config.d.ts.map +0 -1
  51. package/vitest.config.js +0 -10
  52. package/vitest.config.js.map +0 -1
  53. package/vitest.config.ts +0 -10
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACnD,OAAO,EAAE,OAAO,IAAI,WAAW,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAC5D,OAAO,EAAE,OAAO,IAAI,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAC5D,OAAO,EAAE,IAAI,IAAI,SAAS,EAAE,KAAK,IAAI,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAE1E;;GAEG;AACH,MAAM,UAAU,eAAe,CAAC,IAAY;IAC1C,OAAO,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC,CAAC;AACjD,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,kBAAkB,CAAC,EAAU;IAI3C,MAAM,UAAU,GAAG,EAAE,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;IACnC,MAAM,QAAQ,GAAG,EAAE,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;IAEpC,IAAI,QAAQ,EAAE,CAAC;QACb,MAAM,WAAW,GAAG,EAAE,CAAC,OAAO,CAAC,GAAG,EAAE,UAAU,GAAG,CAAC,CAAC,CAAC;QACpD,IAAI,WAAW,KAAK,CAAC,CAAC,EAAE,CAAC;YACvB,OAAO,EAAE,OAAO,EAAE,EAAE,EAAE,OAAO,EAAE,GAAG,EAAE,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,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,EAAE,GAAG,CAAC,EAAE,CAAC,CAAC;QAC1C,IAAI,CAAC,cAAc,CAAC,IAAI,CAAC,IAAI,CAAC;YAAE,SAAS;QACzC,MAAM,MAAM,GAAG,qBAAqB,CAAC,OAAO,CAAC,SAAS,CAAC,EAAE,IAAI,CAAC,CAAC;QAC/D,IAAI,CAAC,MAAM;YAAE,SAAS;QACtB,KAAK,MAAM,CAAC,IAAI,oBAAoB,CAAC,MAAM,EAAE,IAAI,CAAC,EAAE,CAAC;YACnD,IAAI,CAAC,KAAK,SAAS;gBAAE,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,sCAAsC;QAC3E,CAAC;IACH,CAAC;IAED,OAAO,KAAK,CAAC;AACf,CAAC;AAED;;;GAGG;AACH,SAAS,qBAAqB,CAAC,OAAe,EAAE,IAAY;IAC1D,MAAM,IAAI,GAAG,WAAW,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;IACxC,MAAM,UAAU,GAAG;QACjB,IAAI;QACJ,GAAG,IAAI,KAAK;QACZ,GAAG,IAAI,MAAM;QACb,WAAW,CAAC,IAAI,EAAE,UAAU,CAAC;QAC7B,WAAW,CAAC,IAAI,EAAE,WAAW,CAAC;KAC/B,CAAC;IACF,OAAO,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC;AACvD,CAAC;AAED,IAAI,UAAqC,CAAC;AAC1C,wEAAwE;AACxE,MAAM,UAAU,WAAW;IACzB,OAAO,CAAC,UAAU,KAAK,SAAS,CAAC,CAAC;AACpC,CAAC"}
package/dist/vite.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ declare const _default: (options?: import("./types.js").PatchworkBundleOptions | undefined) => import("vite").Plugin<any> | import("vite").Plugin<any>[];
2
+ export default _default;
3
+ //# sourceMappingURL=vite.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vite.d.ts","sourceRoot":"","sources":["../src/vite.ts"],"names":[],"mappings":";AACA,wBAAqC"}
@@ -1,2 +1,3 @@
1
1
  import { patchworkBundles } from "./index.js";
2
2
  export default patchworkBundles.vite;
3
+ //# sourceMappingURL=vite.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vite.js","sourceRoot":"","sources":["../src/vite.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAC9C,eAAe,gBAAgB,CAAC,IAAI,CAAC"}
@@ -0,0 +1,3 @@
1
+ declare const _default: (options?: import("./types.js").PatchworkBundleOptions | undefined) => WebpackPluginInstance;
2
+ export default _default;
3
+ //# sourceMappingURL=webpack.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"webpack.d.ts","sourceRoot":"","sources":["../src/webpack.ts"],"names":[],"mappings":";AACA,wBAAwC"}
@@ -1,2 +1,3 @@
1
1
  import { patchworkBundles } from "./index.js";
2
2
  export default patchworkBundles.webpack;
3
+ //# sourceMappingURL=webpack.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"webpack.js","sourceRoot":"","sources":["../src/webpack.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAC9C,eAAe,gBAAgB,CAAC,OAAO,CAAC"}
package/package.json CHANGED
@@ -1,9 +1,12 @@
1
1
  {
2
2
  "name": "@chee/patchwork-bundles",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "type": "module",
5
5
  "author": "Ink & Switch",
6
6
  "license": "MIT",
7
+ "files": [
8
+ "dist"
9
+ ],
7
10
  "exports": {
8
11
  ".": {
9
12
  "import": "./dist/index.js",
@@ -1,27 +0,0 @@
1
- {
2
- "permissions": {
3
- "allow": [
4
- "WebFetch(domain:www.npmjs.com)",
5
- "WebSearch",
6
- "Bash(find:*)",
7
- "WebFetch(domain:registry.npmjs.org)",
8
- "Bash(pnpm --version:*)",
9
- "Bash(npx pnpm@next-11:*)",
10
- "Bash(npm view:*)",
11
- "Bash(ls:*)",
12
- "Bash(echo:*)",
13
- "Bash(pnpm test:*)",
14
- "Bash(node -e:*)",
15
- "Bash(pnpm build *)",
16
- "Bash(PATCHWORK_SYNC_SERVER=wss://subduction.sync.inkandswitch.com npx pnpm@11 install --no-frozen-lockfile)",
17
- "Bash(rm -rf node_modules pnpm-lock.yaml)",
18
- "Bash(time PATCHWORK_SUB=false npx pnpm@11 install --no-frozen-lockfile)",
19
- "Bash(sed -i '' 's/kFcrzeDmr5zXE1jShvxUPsoToAN/6iXwddwF9cwrjmM5yqp2xUENxUY/' package.json)",
20
- "Bash(cat)",
21
- "Read(//private/tmp/**)",
22
- "Read(//tmp/**)",
23
- "Bash(PATCHWORK_SYNC_SERVER=wss://subduction.sync.inkandswitch.com npx pnpm@11 install --no-frozen-lockfile --config.strictDepBuilds=false)",
24
- "Bash(PATCHWORK_SYNC_SERVER=wss://subduction.sync.inkandswitch.com npx pnpm@11 install --no-frozen-lockfile --config.dangerouslyAllowAllBuilds=true)"
25
- ]
26
- }
27
- }
@@ -1,41 +0,0 @@
1
- overrides:
2
- "@automerge/automerge": "catalog:"
3
- "@automerge/automerge-repo": "catalog:"
4
- "@automerge/automerge-repo-keyhive": "catalog:"
5
- "@automerge/automerge-repo-network-messagechannel": "catalog:"
6
- "@automerge/automerge-repo-network-websocket": "catalog:"
7
- "@automerge/automerge-repo-react-hooks": "catalog:"
8
- "@automerge/automerge-repo-solid-primitives": "catalog:"
9
- "@automerge/automerge-repo-storage-indexeddb": "catalog:"
10
- "@automerge/automerge-subduction": "catalog:"
11
- "@automerge/react": "catalog:"
12
- "@automerge/vanillajs": "catalog:"
13
- "@keyhive/keyhive": "catalog:"
14
-
15
- minimumReleaseAgeExclude:
16
- - '@automerge/*'
17
- - '@inkandswitch/*'
18
- - '@chee/*'
19
-
20
- catalog:
21
- "@automerge/automerge": 3.3.0-fragments.2
22
- "@automerge/automerge-repo": 2.6.0-subduction.40
23
- "@automerge/automerge-repo-keyhive": 0.3.0-alpha.sub.8b
24
- "@automerge/automerge-repo-network-messagechannel": 2.6.0-subduction.40
25
- "@automerge/automerge-repo-network-websocket": 2.6.0-subduction.40
26
- "@automerge/automerge-repo-react-hooks": 2.6.0-subduction.40
27
- "@automerge/automerge-repo-solid-primitives": 2.6.0-subduction.40
28
- "@automerge/automerge-repo-storage-indexeddb": 2.6.0-subduction.40
29
- "@automerge/automerge-repo-storage-nodefs": 2.6.0-subduction.40
30
- "@automerge/automerge-subduction": 0.16.0
31
- "@automerge/react": 2.6.0-subduction.40
32
- "@automerge/vanillajs": 2.6.0-subduction.40
33
- "@keyhive/keyhive": 0.1.0-alpha.5
34
- "@types/react": 18.3.1
35
- "@types/react-dom": 18.3.1
36
- react: 18.3.1
37
- react-dom: 18.3.1
38
-
39
- allowBuilds:
40
- cbor-extract: true
41
- esbuild: true
package/src/index.ts DELETED
@@ -1,133 +0,0 @@
1
- import { createUnplugin } from "unplugin";
2
- import { resolve } from "node:path";
3
- import {
4
- readPackageJson,
5
- parseBareSpecifier,
6
- isBareSpecifier,
7
- resolveDepEntryPoint,
8
- collectModuleExports,
9
- ensureLexer,
10
- } from "./utils.js";
11
- import { getImportableUrlFromAutomergeUrl } from "@inkandswitch/patchwork-filesystem";
12
- import externals from "@inkandswitch/patchwork-bootloader/externals";
13
- import type { AutomergeUrl } from "@automerge/automerge-repo";
14
- import { resolveOptions, type PatchworkBundleOptions } from "./types.js";
15
-
16
- // Prefix marking a virtual module we generate for
17
- // `rewrite.automerge: "patchwork:cross-origin"`. The rest of the id is the
18
- // original bare specifier. The leading NUL keeps other tooling from treating it
19
- // as a real file path.
20
- const CROSS_ORIGIN_PREFIX = "\0patchwork-bundle-cross-origin:";
21
-
22
- const VALID_IDENT = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
23
-
24
- export const patchworkBundles = createUnplugin(
25
- (rawOptions?: PatchworkBundleOptions) => {
26
- const options = resolveOptions(rawOptions);
27
- let deps: Record<string, string>;
28
-
29
- return {
30
- name: "patchwork-bundles",
31
- enforce: "pre",
32
-
33
- async buildStart() {
34
- const pkgPath = resolve(process.cwd(), options.packageJsonPath);
35
- const pkgJson = readPackageJson(pkgPath);
36
- deps = pkgJson.dependencies ?? {};
37
- if (options.rewrite.automerge === "patchwork:cross-origin") {
38
- await ensureLexer();
39
- }
40
- },
41
-
42
- resolveId(id: string) {
43
- // Virtual modules we emitted below are handled in load().
44
- if (id.startsWith(CROSS_ORIGIN_PREFIX)) return id;
45
-
46
- if (!isBareSpecifier(id)) return undefined;
47
-
48
- // Bootloader externals — always external (served via importmap)
49
- if (externals.includes(id)) {
50
- return { id, external: true };
51
- }
52
-
53
- const { pkgName, subpath } = parseBareSpecifier(id);
54
-
55
- if (pkgName !== id && externals.includes(pkgName)) {
56
- return { id, external: true };
57
- }
58
-
59
- const version = deps[pkgName];
60
- if (!version) return undefined;
61
-
62
- // Automerge deps — rewrite to service worker URLs
63
- if (version.startsWith("automerge:")) {
64
- if (options.rewrite.automerge === "patchwork") {
65
- const entryPoint = resolveDepEntryPoint(pkgName, subpath);
66
- const url = getImportableUrlFromAutomergeUrl(
67
- version as AutomergeUrl,
68
- entryPoint
69
- );
70
- return { id: url, external: true };
71
- }
72
- // Cross-origin mode: defer URL construction to the browser so the
73
- // service-worker path resolves against the *running document's*
74
- // origin, not the (possibly cross-origin) module's. Emit a virtual
75
- // re-export module; the code is generated in load().
76
- if (options.rewrite.automerge === "patchwork:cross-origin") {
77
- return CROSS_ORIGIN_PREFIX + id;
78
- }
79
- return undefined;
80
- }
81
-
82
- // npm fallback — optionally rewrite to esm.sh
83
- if (options.rewrite.npm === "esm.sh") {
84
- const cleanVersion = version.replace(/^[\^~>=<\s]+/, "");
85
- const esmId = subpath === "." ? pkgName : id;
86
- return {
87
- id: `https://esm.sh/${esmId}@${cleanVersion}`,
88
- external: true,
89
- };
90
- }
91
-
92
- return undefined;
93
- },
94
-
95
- load(id: string) {
96
- if (!id.startsWith(CROSS_ORIGIN_PREFIX)) return undefined;
97
-
98
- const spec = id.slice(CROSS_ORIGIN_PREFIX.length);
99
- const { pkgName, subpath } = parseBareSpecifier(spec);
100
- const version = deps[pkgName] as AutomergeUrl;
101
- const entryPoint = resolveDepEntryPoint(pkgName, subpath);
102
-
103
- // Enumerate the dep's named exports so the virtual module can re-export
104
- // the same shape. Read the installed copy in node_modules — assumed to
105
- // match the automerge doc the service worker serves.
106
- const entryFile = resolve(
107
- process.cwd(),
108
- "node_modules",
109
- pkgName,
110
- entryPoint
111
- );
112
- const exportNames = collectModuleExports(entryFile);
113
- const hasDefault = exportNames.delete("default");
114
- const named = [...exportNames].filter((n) => VALID_IDENT.test(n));
115
-
116
- const lines = [
117
- `import { getImportableUrlFromAutomergeUrl as __pwResolve } from "@inkandswitch/patchwork-filesystem";`,
118
- `const __pwUrl = __pwResolve(${JSON.stringify(version)}, ${JSON.stringify(entryPoint)});`,
119
- `const __pwMod = await import(/* @vite-ignore */ __pwUrl);`,
120
- ...named.map(
121
- (n) => `export const ${n} = __pwMod[${JSON.stringify(n)}];`
122
- ),
123
- ];
124
- if (hasDefault) lines.push(`export default __pwMod.default;`);
125
-
126
- return { code: lines.join("\n"), moduleSideEffects: true };
127
- },
128
- };
129
- }
130
- );
131
-
132
- export default patchworkBundles;
133
- export type { PatchworkBundleOptions } from "./types.js";
package/src/types.ts DELETED
@@ -1,52 +0,0 @@
1
- export interface PatchworkBundleOptions {
2
- /** Path to the project's package.json. Default: "./package.json" relative to cwd */
3
- packageJsonPath?: string;
4
-
5
- rewrite?: {
6
- /**
7
- * How to handle dependencies with `automerge:` version specifiers.
8
- * - `"patchwork"` (default): rewrite bare imports to *root-relative*
9
- * service-worker URLs (`/{encodeURIComponent("automerge:docid")}/resolved/entry.js`),
10
- * resolving through the dep's package.json exports with the `"patchwork"` condition.
11
- * The leading `/` resolves against the *importing module's* origin, so this
12
- * only works when the tool is served same-origin as the patchwork service
13
- * worker. A tool served from a different origin (e.g. a separate static
14
- * host) will 404 on these imports.
15
- * - `"patchwork:cross-origin"`: rewrite bare imports to a virtual module
16
- * that resolves the URL *at runtime* via `getImportableUrlFromAutomergeUrl`
17
- * and dynamically imports it, re-exporting the dep's named exports
18
- * (top-level await). This resolves against the running document's origin
19
- * instead of the module's, so it works even when the tool is loaded
20
- * cross-origin from where the service worker lives. Requires a build
21
- * target that supports top-level await (e.g. `es2022`/`esnext`).
22
- * - `"bundle"`: include the dependency code in the bundle normally.
23
- */
24
- automerge?: "patchwork" | "patchwork:cross-origin" | "bundle";
25
-
26
- /**
27
- * How to handle npm dependencies (deps that aren't `automerge:` URLs
28
- * and aren't in the bootloader externals list).
29
- * - `"bundle"` (default): bundle them normally.
30
- * - `"esm.sh"`: rewrite to `https://esm.sh/<pkg>@<version>` external URLs.
31
- */
32
- npm?: "esm.sh" | "bundle";
33
- };
34
- }
35
-
36
- export interface ResolvedOptions {
37
- packageJsonPath: string;
38
- rewrite: {
39
- automerge: "patchwork" | "patchwork:cross-origin" | "bundle";
40
- npm: "esm.sh" | "bundle";
41
- };
42
- }
43
-
44
- export function resolveOptions(raw?: PatchworkBundleOptions): ResolvedOptions {
45
- return {
46
- packageJsonPath: raw?.packageJsonPath ?? "./package.json",
47
- rewrite: {
48
- automerge: raw?.rewrite?.automerge ?? "patchwork",
49
- npm: raw?.rewrite?.npm ?? "bundle",
50
- },
51
- };
52
- }
package/src/utils.ts DELETED
@@ -1,166 +0,0 @@
1
- import { readFileSync, existsSync } from "node:fs";
2
- import { resolve as resolvePath, dirname } from "node:path";
3
- import { resolve as resolveExports } from "resolve.exports";
4
- import { init as initLexer, parse as parseModule } from "es-module-lexer";
5
-
6
- /**
7
- * Read and parse a package.json file.
8
- */
9
- export function readPackageJson(path: string): Record<string, any> {
10
- return JSON.parse(readFileSync(path, "utf-8"));
11
- }
12
-
13
- /**
14
- * Parse a bare import specifier into package name and subpath.
15
- *
16
- * - `"abc"` → `{ pkgName: "abc", subpath: "." }`
17
- * - `"abc/utils"` → `{ pkgName: "abc", subpath: "./utils" }`
18
- * - `"@scope/pkg"` → `{ pkgName: "@scope/pkg", subpath: "." }`
19
- * - `"@scope/pkg/utils"` → `{ pkgName: "@scope/pkg", subpath: "./utils" }`
20
- */
21
- export function parseBareSpecifier(id: string): {
22
- pkgName: string;
23
- subpath: string;
24
- } {
25
- const firstSlash = id.indexOf("/");
26
- const isScoped = id.startsWith("@");
27
-
28
- if (isScoped) {
29
- const secondSlash = id.indexOf("/", firstSlash + 1);
30
- if (secondSlash === -1) {
31
- return { pkgName: id, subpath: "." };
32
- }
33
- return {
34
- pkgName: id.slice(0, secondSlash),
35
- subpath: "./" + id.slice(secondSlash + 1),
36
- };
37
- }
38
-
39
- if (firstSlash === -1) {
40
- return { pkgName: id, subpath: "." };
41
- }
42
-
43
- return {
44
- pkgName: id.slice(0, firstSlash),
45
- subpath: "./" + id.slice(firstSlash + 1),
46
- };
47
- }
48
-
49
- /**
50
- * Returns true if the specifier is a bare import (not relative, absolute,
51
- * or a protocol URL).
52
- */
53
- export function isBareSpecifier(id: string): boolean {
54
- return (
55
- id.length > 0 &&
56
- !id.startsWith(".") &&
57
- !id.startsWith("/") &&
58
- !id.includes(":")
59
- );
60
- }
61
-
62
- /**
63
- * Resolve a dependency's entry point by reading its package.json from
64
- * `node_modules/` and resolving through its `exports` field.
65
- *
66
- * Returns the resolved path relative to the package root (e.g. `"./dist/index.js"`),
67
- * or the subpath itself if resolution fails.
68
- */
69
- export function resolveDepEntryPoint(
70
- pkgName: string,
71
- subpath: string = ".",
72
- conditions: string[] = ["patchwork", "browser", "import"]
73
- ): string {
74
- try {
75
- const depPkgJsonPath = resolvePath(
76
- process.cwd(),
77
- "node_modules",
78
- pkgName,
79
- "package.json"
80
- );
81
- const depPkgJson = readPackageJson(depPkgJsonPath);
82
-
83
- const resolved = resolveExports(depPkgJson, subpath, { conditions });
84
- if (resolved && resolved[0]) {
85
- return resolved[0];
86
- }
87
-
88
- // Fallback to "main" for root export
89
- if (subpath === "." && typeof depPkgJson.main === "string") {
90
- return depPkgJson.main;
91
- }
92
- } catch {
93
- // If we can't read the dep's package.json, just pass through the subpath
94
- }
95
-
96
- return subpath;
97
- }
98
-
99
- /**
100
- * Enumerate the *named* exports of an ES module file, following relative
101
- * `export * from "./x"` re-exports one dependency at a time. Used to generate a
102
- * runtime-resolved virtual module that re-exports the same names as the real
103
- * module. `"default"` is included in the returned set if the module has a
104
- * default export.
105
- *
106
- * es-module-lexer must be initialised first (see {@link ensureLexer}).
107
- */
108
- export function collectModuleExports(
109
- entryPath: string,
110
- seen: Set<string> = new Set()
111
- ): Set<string> {
112
- const names = new Set<string>();
113
- if (seen.has(entryPath) || !existsSync(entryPath)) return names;
114
- seen.add(entryPath);
115
-
116
- let source: string;
117
- try {
118
- source = readFileSync(entryPath, "utf-8");
119
- } catch {
120
- return names;
121
- }
122
-
123
- const [imports, exports] = parseModule(source);
124
- for (const e of exports) {
125
- if (e.n) names.add(e.n);
126
- }
127
-
128
- // `export * from "./rel"` contributes names that aren't listed inline. Those
129
- // re-exports show up in `imports`; detect them by the statement text and
130
- // recurse into relative targets.
131
- for (const imp of imports) {
132
- const spec = imp.n;
133
- if (!spec || !spec.startsWith(".")) continue;
134
- const stmt = source.slice(imp.ss, imp.se);
135
- if (!/^export\s*\*/.test(stmt)) continue;
136
- const target = resolveRelativeModule(dirname(entryPath), spec);
137
- if (!target) continue;
138
- for (const n of collectModuleExports(target, seen)) {
139
- if (n !== "default") names.add(n); // `export *` never re-exports default
140
- }
141
- }
142
-
143
- return names;
144
- }
145
-
146
- /**
147
- * Resolve a relative module specifier to an existing file, trying the literal
148
- * path and common JS extensions / index files.
149
- */
150
- function resolveRelativeModule(fromDir: string, spec: string): string | null {
151
- const base = resolvePath(fromDir, spec);
152
- const candidates = [
153
- base,
154
- `${base}.js`,
155
- `${base}.mjs`,
156
- resolvePath(base, "index.js"),
157
- resolvePath(base, "index.mjs"),
158
- ];
159
- return candidates.find((c) => existsSync(c)) ?? null;
160
- }
161
-
162
- let lexerReady: Promise<void> | undefined;
163
- /** Initialise es-module-lexer's WASM once; safe to await repeatedly. */
164
- export function ensureLexer(): Promise<void> {
165
- return (lexerReady ??= initLexer);
166
- }