|
| 1 | +import { RE_CSS, RE_DTS, RE_JS } from 'rolldown-plugin-dts/filename' |
1 | 2 | import { getPackageType, type PackageType } from '../utils/package' |
2 | | -import type { NormalizedFormat, ResolvedOptions } from '../options' |
3 | | -import type { InputOptions, PreRenderedChunk } from 'rolldown' |
| 3 | +import type { Format, NormalizedFormat, ResolvedOptions } from '../options' |
| 4 | +import type { |
| 5 | + AddonFunction, |
| 6 | + InputOptions, |
| 7 | + PreRenderedChunk, |
| 8 | + RenderedChunk, |
| 9 | +} from 'rolldown' |
4 | 10 |
|
5 | 11 | export interface OutExtensionContext { |
6 | 12 | options: InputOptions |
@@ -76,3 +82,37 @@ function createChunkFilename( |
76 | 82 | return `${basename}${chunk.name.endsWith('.d') ? dtsExtension : jsExtension}` |
77 | 83 | } |
78 | 84 | } |
| 85 | + |
| 86 | +export interface ChunkAddonObject { |
| 87 | + js?: string |
| 88 | + css?: string |
| 89 | + dts?: string |
| 90 | +} |
| 91 | +export type ChunkAddonFunction = (ctx: { |
| 92 | + format: Format |
| 93 | +}) => ChunkAddonObject | undefined |
| 94 | +export type ChunkAddon = ChunkAddonObject | ChunkAddonFunction |
| 95 | + |
| 96 | +export function resolveChunkAddon( |
| 97 | + chunkAddon: ChunkAddon | undefined, |
| 98 | + format: NormalizedFormat, |
| 99 | +): AddonFunction | undefined { |
| 100 | + if (!chunkAddon) return |
| 101 | + |
| 102 | + return (chunk: RenderedChunk) => { |
| 103 | + if (typeof chunkAddon === 'function') { |
| 104 | + chunkAddon = chunkAddon({ format }) |
| 105 | + } |
| 106 | + |
| 107 | + switch (true) { |
| 108 | + case RE_JS.test(chunk.fileName): |
| 109 | + return chunkAddon?.js || '' |
| 110 | + case RE_CSS.test(chunk.fileName): |
| 111 | + return chunkAddon?.css || '' |
| 112 | + case RE_DTS.test(chunk.fileName): |
| 113 | + return chunkAddon?.dts || '' |
| 114 | + default: |
| 115 | + return '' |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments