@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 95 lines 3.08 KB
Raw
1 import path from "node:path";
2 import type * as rolldown from "rolldown";
3 import { RolldownMagicString } from "rolldown";
4
5 export const InternalWorkerExportPlugin = (): rolldown.Plugin => {
6 const getChunks = (bundle: rolldown.OutputBundle) => {
7 const chunks = new Map<string, rolldown.OutputChunk>();
8 for (const [fileName, output] of Object.entries(bundle)) {
9 if (output.type === "chunk") {
10 chunks.set(fileName, output);
11 }
12 }
13 return chunks;
14 };
15
16 const getImportedChunks = (
17 chunk: rolldown.OutputChunk,
18 chunks: Map<string, rolldown.OutputChunk>,
19 ) => {
20 const modules: Array<rolldown.OutputChunk> = [];
21 const seen = new Set<string>();
22
23 const visit = (module: rolldown.OutputChunk) => {
24 for (const fileName of module.imports) {
25 const imported = chunks.get(fileName);
26 if (!imported || seen.has(fileName)) {
27 continue;
28 }
29 seen.add(fileName);
30 visit(imported);
31 modules.push(imported);
32 }
33 };
34
35 visit(chunk);
36 return modules;
37 };
38
39 const getRelativeModulePath = (importer: string, imported: string) => {
40 const relativePath = path.posix.relative(path.posix.dirname(importer), imported);
41 return relativePath.startsWith(".") ? relativePath : `./${relativePath}`;
42 };
43
44 return {
45 name: "distilled:worker-exports",
46 generateBundle(_options, bundle) {
47 const chunks = getChunks(bundle);
48
49 for (const chunk of chunks.values()) {
50 const magicString = new RolldownMagicString(chunk.code);
51 const code = chunk.code;
52
53 if (chunk.isEntry) {
54 const modules = getImportedChunks(chunk, chunks);
55 const imports: Array<string> = [];
56 const moduleEntries: Array<string> = [];
57 for (let index = 0; index < modules.length; index++) {
58 const module = modules[index];
59 const relativePath = getRelativeModulePath(chunk.fileName, module.fileName);
60 if (module.isEntry) {
61 imports.push(
62 `import { modules as worker${index}Modules } from ${JSON.stringify(relativePath)};`,
63 );
64 moduleEntries.push(` ...worker${index}Modules,`);
65 } else {
66 imports.push(`import worker${index} from ${JSON.stringify(relativePath)};`);
67 moduleEntries.push(` ${JSON.stringify(module.fileName)}: worker${index},`);
68 }
69 }
70
71 magicString.update(
72 0,
73 code.length,
74 [
75 ...imports,
76 imports.length > 0 ? "" : undefined,
77 `export const main = ${JSON.stringify(chunk.fileName)};`,
78 "export const modules = {",
79 ` ${JSON.stringify(chunk.fileName)}: ${JSON.stringify(code)},`,
80 ...moduleEntries,
81 "};",
82 "",
83 ]
84 .filter((line) => line !== undefined)
85 .join("\n"),
86 );
87 } else {
88 magicString.update(0, code.length, `export default ${JSON.stringify(code)};\n`);
89 }
90
91 chunk.code = magicString.toString();
92 }
93 },
94 };
95 };