Skip to content

Commit

Permalink
fix: preserve top-level when running terser for IIFE (#900)
Browse files Browse the repository at this point in the history
Co-authored-by: EGOIST <hi@egoist.dev>
  • Loading branch information
sapphi-red and egoist committed Jun 16, 2023
1 parent 2f41663 commit 2c0435a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ export async function build(_options: Options) {
minifyOptions: options.minify,
format,
terserOptions: options.terserOptions,
globalName: options.globalName,
logger,
}),
])
Expand Down
8 changes: 5 additions & 3 deletions src/plugins/terser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ export const terserPlugin = ({
minifyOptions,
format,
terserOptions = {},
logger
globalName,
logger,
}: {
minifyOptions: Options['minify']
format: Format
terserOptions?: MinifyOptions,
terserOptions?: MinifyOptions
globalName?: string
logger: Logger
}): Plugin => {
return {
Expand All @@ -37,7 +39,7 @@ export const terserPlugin = ({

if (format === 'esm') {
defaultOptions.module = true
} else {
} else if (!(format === 'iife' && globalName !== undefined)) {
defaultOptions.toplevel = true
}

Expand Down
37 changes: 26 additions & 11 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1255,12 +1255,10 @@ test(`should generate export {} when there are no exports in source file`, async
})

test('custom inject style function', async () => {
const { outFiles, getFileContent } = await run(
getTestName(),
{
'input.ts': `import './style.css'`,
'style.css': `.hello { color: red }`,
'tsup.config.ts': `
const { outFiles, getFileContent } = await run(getTestName(), {
'input.ts': `import './style.css'`,
'style.css': `.hello { color: red }`,
'tsup.config.ts': `
export default {
entry: ['src/input.ts'],
minify: true,
Expand All @@ -1269,13 +1267,30 @@ test('custom inject style function', async () => {
return "__custom_inject_style__(" + css +")";
}
}`,
},
)
})
expect(outFiles).toEqual(['input.js', 'input.mjs'])
expect(await getFileContent('dist/input.mjs')).toContain('__custom_inject_style__(`.hello{color:red}\n`)')
expect(await getFileContent('dist/input.js')).toContain('__custom_inject_style__(`.hello{color:red}\n`)')
expect(await getFileContent('dist/input.mjs')).toContain(
'__custom_inject_style__(`.hello{color:red}\n`)'
)
expect(await getFileContent('dist/input.js')).toContain(
'__custom_inject_style__(`.hello{color:red}\n`)'
)
})

test('preserve top-level variable for IIFE format', async () => {
const { outFiles, getFileContent } = await run(getTestName(), {
'input.ts': `export default 'foo'`,
'tsup.config.ts': `
export default {
entry: ['src/input.ts'],
globalName: 'globalFoo',
minify: 'terser',
format: ['iife']
}`,
})
expect(outFiles).toEqual(['input.global.js'])
expect(await getFileContent('dist/input.global.js')).toMatch(/globalFoo\s*=/)
})

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

expect(outFiles).toEqual(['input.cjs', 'input.css'])
expect(await getFileContent('dist/input.css')).toContain('color: blue;')
})
})

0 comments on commit 2c0435a

Please sign in to comment.