The best way to control Claude Code, Codex and more from any computer or phone

main
ts 41 lines 1.53 KB
Raw
1 import path from "node:path";
2 import type * as rolldown from "rolldown";
3
4 const PACKAGE_NAME = "@alchemy.run/cloudflare-runtime";
5
6 /**
7 * Keeps the Vite build's references to sibling runtime components external.
8 * Source code uses ordinary relative TypeScript imports, while published
9 * output must cross component boundaries through the package export map so
10 * consumers receive one shared copy of the runtime types.
11 */
12 export const RuntimeSubpathExportPlugin = (): rolldown.Plugin => ({
13 name: "alchemy:runtime-subpath-exports",
14 resolveId(source, importer) {
15 if (importer === undefined || !source.startsWith(".")) {
16 return;
17 }
18
19 const normalizedImporter = importer.replaceAll("\\", "/");
20 const normalizedSource = source.replaceAll("\\", "/");
21 const absoluteImporter = /^[A-Za-z]:\//.test(normalizedImporter)
22 ? `/${normalizedImporter}`
23 : normalizedImporter;
24 const resolved = path.posix.resolve(path.posix.dirname(absoluteImporter), normalizedSource);
25 const match = resolved.match(/\/src\/(core|rolldown)\/(.+)\.ts$/);
26 if (match === null) {
27 return;
28 }
29
30 const [, component, modulePath] = match;
31 // Wire constants are internal and safe to inline in the Vite bundle.
32 if (component === "core" && modulePath === "globals/ProxyHeaders.shared") {
33 return;
34 }
35 const subpath =
36 modulePath === "index"
37 ? component
38 : `${component}/${modulePath.replace(/\.shared$/, "").replace(/\/index$/, "")}`;
39 return { id: `${PACKAGE_NAME}/${subpath}`, external: true };
40 },
41 });