Skip to content

Commit 2c0435a

Browse files
sapphi-redegoist
andauthored
fix: preserve top-level when running terser for IIFE (#900)
Co-authored-by: EGOIST <hi@egoist.dev>
1 parent 2f41663 commit 2c0435a

File tree

3 files changed

+32
-14
lines changed

3 files changed

+32
-14
lines changed

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ export async function build(_options: Options) {
262262
minifyOptions: options.minify,
263263
format,
264264
terserOptions: options.terserOptions,
265+
globalName: options.globalName,
265266
logger,
266267
}),
267268
])

src/plugins/terser.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ export const terserPlugin = ({
99
minifyOptions,
1010
format,
1111
terserOptions = {},
12-
logger
12+
globalName,
13+
logger,
1314
}: {
1415
minifyOptions: Options['minify']
1516
format: Format
16-
terserOptions?: MinifyOptions,
17+
terserOptions?: MinifyOptions
18+
globalName?: string
1719
logger: Logger
1820
}): Plugin => {
1921
return {
@@ -37,7 +39,7 @@ export const terserPlugin = ({
3739

3840
if (format === 'esm') {
3941
defaultOptions.module = true
40-
} else {
42+
} else if (!(format === 'iife' && globalName !== undefined)) {
4143
defaultOptions.toplevel = true
4244
}
4345

test/index.test.ts

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,12 +1255,10 @@ test(`should generate export {} when there are no exports in source file`, async
12551255
})
12561256

12571257
test('custom inject style function', async () => {
1258-
const { outFiles, getFileContent } = await run(
1259-
getTestName(),
1260-
{
1261-
'input.ts': `import './style.css'`,
1262-
'style.css': `.hello { color: red }`,
1263-
'tsup.config.ts': `
1258+
const { outFiles, getFileContent } = await run(getTestName(), {
1259+
'input.ts': `import './style.css'`,
1260+
'style.css': `.hello { color: red }`,
1261+
'tsup.config.ts': `
12641262
export default {
12651263
entry: ['src/input.ts'],
12661264
minify: true,
@@ -1269,13 +1267,30 @@ test('custom inject style function', async () => {
12691267
return "__custom_inject_style__(" + css +")";
12701268
}
12711269
}`,
1272-
},
1273-
)
1270+
})
12741271
expect(outFiles).toEqual(['input.js', 'input.mjs'])
1275-
expect(await getFileContent('dist/input.mjs')).toContain('__custom_inject_style__(`.hello{color:red}\n`)')
1276-
expect(await getFileContent('dist/input.js')).toContain('__custom_inject_style__(`.hello{color:red}\n`)')
1272+
expect(await getFileContent('dist/input.mjs')).toContain(
1273+
'__custom_inject_style__(`.hello{color:red}\n`)'
1274+
)
1275+
expect(await getFileContent('dist/input.js')).toContain(
1276+
'__custom_inject_style__(`.hello{color:red}\n`)'
1277+
)
12771278
})
12781279

1280+
test('preserve top-level variable for IIFE format', async () => {
1281+
const { outFiles, getFileContent } = await run(getTestName(), {
1282+
'input.ts': `export default 'foo'`,
1283+
'tsup.config.ts': `
1284+
export default {
1285+
entry: ['src/input.ts'],
1286+
globalName: 'globalFoo',
1287+
minify: 'terser',
1288+
format: ['iife']
1289+
}`,
1290+
})
1291+
expect(outFiles).toEqual(['input.global.js'])
1292+
expect(await getFileContent('dist/input.global.js')).toMatch(/globalFoo\s*=/)
1293+
})
12791294

12801295
test('should load postcss esm config', async () => {
12811296
const { outFiles, getFileContent } = await run(getTestName(), {
@@ -1301,4 +1316,4 @@ test('should load postcss esm config', async () => {
13011316

13021317
expect(outFiles).toEqual(['input.cjs', 'input.css'])
13031318
expect(await getFileContent('dist/input.css')).toContain('color: blue;')
1304-
})
1319+
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy