| 1 | import type { OutputAsset, OutputChunk, RolldownOutput } from "rolldown"; |
| 2 | |
| 3 | const CLOUDFLARE_MODULE_REFERENCE_REGEX = |
| 4 | /__CLOUDFLARE_MODULE__(CompiledWasm|Data|Text)__(.*?)__CLOUDFLARE_MODULE__/; |
| 5 | |
| 6 | export function getEntryChunk(output: RolldownOutput): OutputChunk { |
| 7 | const entry = output.output.find( |
| 8 | (item): item is OutputChunk => item.type === "chunk" && item.isEntry, |
| 9 | ); |
| 10 | if (!entry) { |
| 11 | throw new Error("Expected generated output to contain an entry chunk"); |
| 12 | } |
| 13 | return entry; |
| 14 | } |
| 15 | |
| 16 | export function getChunk( |
| 17 | output: RolldownOutput, |
| 18 | fileName: string, |
| 19 | ): OutputChunk { |
| 20 | const chunk = output.output.find( |
| 21 | (item): item is OutputChunk => |
| 22 | item.type === "chunk" && item.fileName === fileName, |
| 23 | ); |
| 24 | if (!chunk) { |
| 25 | throw new Error(`Expected generated output to contain chunk "${fileName}"`); |
| 26 | } |
| 27 | return chunk; |
| 28 | } |
| 29 | |
| 30 | export function findAsset( |
| 31 | output: RolldownOutput, |
| 32 | matcher: RegExp, |
| 33 | ): OutputAsset | undefined { |
| 34 | return output.output.find( |
| 35 | (item): item is OutputAsset => |
| 36 | item.type === "asset" && matcher.test(item.fileName), |
| 37 | ); |
| 38 | } |
| 39 | |
| 40 | export function getAsset(output: RolldownOutput, matcher: RegExp): OutputAsset { |
| 41 | const asset = findAsset(output, matcher); |
| 42 | if (!asset) { |
| 43 | throw new Error( |
| 44 | `Expected generated output to contain asset matching ${matcher}`, |
| 45 | ); |
| 46 | } |
| 47 | return asset; |
| 48 | } |
| 49 | |
| 50 | export function getFileNames(output: RolldownOutput): Array<string> { |
| 51 | return output.output.map((item) => item.fileName); |
| 52 | } |
| 53 | |
| 54 | export function hasCloudflareModuleReferences(code: string): boolean { |
| 55 | return CLOUDFLARE_MODULE_REFERENCE_REGEX.test(code); |
| 56 | } |