| 1 | import path from "node:path"; |
| 2 | import type { OutputAsset, OutputChunk } from "rolldown"; |
| 3 | |
| 4 | export interface MiniflareModule { |
| 5 | path: string; |
| 6 | type: |
| 7 | | "ESModule" |
| 8 | | "CommonJS" |
| 9 | | "Text" |
| 10 | | "Data" |
| 11 | | "CompiledWasm" |
| 12 | | "PythonModule"; |
| 13 | contents?: string | Uint8Array<ArrayBuffer> | undefined; |
| 14 | } |
| 15 | |
| 16 | export function miniflareModulesFromRolldownOutput( |
| 17 | output: Array<OutputChunk | OutputAsset>, |
| 18 | ): Array<MiniflareModule> { |
| 19 | return output.flatMap((item) => { |
| 20 | const type = moduleTypeFromExtension(path.extname(item.fileName)); |
| 21 | const contents = |
| 22 | item.type === "chunk" |
| 23 | ? item.code |
| 24 | : (item.source as string | Uint8Array<ArrayBuffer>); |
| 25 | if (type === "SourceMap") { |
| 26 | return []; |
| 27 | } |
| 28 | return { |
| 29 | path: item.fileName, |
| 30 | type, |
| 31 | contents, |
| 32 | }; |
| 33 | }); |
| 34 | } |
| 35 | |
| 36 | export function moduleTypeFromExtension( |
| 37 | ext: string, |
| 38 | ): MiniflareModule["type"] | "SourceMap" { |
| 39 | switch (ext) { |
| 40 | case ".wasm": |
| 41 | return "CompiledWasm"; |
| 42 | case ".txt": |
| 43 | case ".html": |
| 44 | case ".sql": |
| 45 | case ".css": |
| 46 | case ".json": |
| 47 | case ".custom": |
| 48 | return "Text"; |
| 49 | case ".bin": |
| 50 | return "Data"; |
| 51 | case ".mjs": |
| 52 | case ".js": |
| 53 | return "ESModule"; |
| 54 | case ".cjs": |
| 55 | return "CommonJS"; |
| 56 | case ".map": |
| 57 | return "SourceMap"; |
| 58 | default: |
| 59 | return "Text"; |
| 60 | } |
| 61 | } |