Skip to content

Commit 517c5f3

Browse files
authored
fix: evaluate banner/footer function addon per chunk (#1011)
1 parent 93cf8cc commit 517c5f3

3 files changed

Lines changed: 55 additions & 11 deletions

File tree

‎src/features/output.ts‎

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -107,24 +107,25 @@ export function resolveChunkAddon(
107107
if (!chunkAddon) return
108108

109109
return (chunk: RenderedChunk) => {
110-
if (typeof chunkAddon === 'function') {
111-
chunkAddon = chunkAddon({
112-
format,
113-
fileName: chunk.fileName,
114-
})
115-
}
110+
const resolved =
111+
typeof chunkAddon === 'function'
112+
? chunkAddon({
113+
format,
114+
fileName: chunk.fileName,
115+
})
116+
: chunkAddon
116117

117-
if (typeof chunkAddon === 'string') {
118-
return chunkAddon
118+
if (typeof resolved === 'string') {
119+
return resolved
119120
}
120121

121122
switch (true) {
122123
case RE_JS.test(chunk.fileName):
123-
return chunkAddon?.js || ''
124+
return resolved?.js || ''
124125
case RE_CSS.test(chunk.fileName):
125-
return chunkAddon?.css || ''
126+
return resolved?.css || ''
126127
case RE_DTS.test(chunk.fileName):
127-
return chunkAddon?.dts || ''
128+
return resolved?.dts || ''
128129
default:
129130
return ''
130131
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
## a.mjs
2+
3+
```mjs
4+
// banner:a.mjs
5+
//#region a.ts
6+
const a = 1;
7+
//#endregion
8+
export { a };
9+
10+
// footer:a.mjs
11+
```
12+
13+
## b.mjs
14+
15+
```mjs
16+
// banner:b.mjs
17+
//#region b.ts
18+
const b = 2;
19+
//#endregion
20+
export { b };
21+
22+
// footer:b.mjs
23+
```

‎tests/e2e.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,26 @@ test('banner and footer option', async (context) => {
805805
expect(fileMap['index.d.mts']).toContain('// dts footer')
806806
})
807807

808+
test('banner and footer function run per chunk', async (context) => {
809+
const { fileMap } = await testBuild({
810+
context,
811+
files: {
812+
'a.ts': `export const a = 1`,
813+
'b.ts': `export const b = 2`,
814+
},
815+
options: {
816+
entry: ['a.ts', 'b.ts'],
817+
banner: ({ fileName }) => `// banner:${fileName}`,
818+
footer: ({ fileName }) => `// footer:${fileName}`,
819+
},
820+
})
821+
822+
expect(fileMap['a.mjs']).toContain('// banner:a.mjs')
823+
expect(fileMap['a.mjs']).toContain('// footer:a.mjs')
824+
expect(fileMap['b.mjs']).toContain('// banner:b.mjs')
825+
expect(fileMap['b.mjs']).toContain('// footer:b.mjs')
826+
})
827+
808828
test('dts enabled when exports.types exists', async (context) => {
809829
const files = {
810830
'index.ts': `export const hello = "world"`,

0 commit comments

Comments
 (0)