| 1 | import type { Plugin } from "rolldown"; |
| 2 | |
| 3 | export interface BundleAnalyzerPluginOptions { |
| 4 | /** |
| 5 | * The filename of the bundle analysis data. |
| 6 | * @default "analyze-data.md" |
| 7 | */ |
| 8 | readonly fileName?: string; |
| 9 | /** |
| 10 | * The format of the bundle analysis data. |
| 11 | * @default "md" |
| 12 | */ |
| 13 | readonly format?: "json" | "md"; |
| 14 | } |
| 15 | |
| 16 | /** |
| 17 | * Wraps rolldown's experimental bundle analyzer plugin, which emits a report |
| 18 | * describing the composition of the bundle. |
| 19 | * |
| 20 | * The report includes: |
| 21 | * - all chunks and their relationships |
| 22 | * - the modules bundled into each chunk |
| 23 | * - import dependencies between chunks |
| 24 | * - the modules reachable from each entry point |
| 25 | */ |
| 26 | export const bundleAnalyzerPlugin = async ( |
| 27 | options: BundleAnalyzerPluginOptions = {}, |
| 28 | ): Promise<Plugin> => { |
| 29 | // `rolldown/experimental` loads `@rolldown/binding-*` at module scope, |
| 30 | // so it is imported lazily — importing this module (e.g. via the |
| 31 | // `alchemy/Bundle` barrel) must never load the native binding (#562). |
| 32 | const { bundleAnalyzerPlugin: rolldownBundleAnalyzerPlugin } = |
| 33 | await import("rolldown/experimental"); |
| 34 | return rolldownBundleAnalyzerPlugin({ |
| 35 | fileName: options.fileName, |
| 36 | format: options.format ?? "md", |
| 37 | }); |
| 38 | }; |