这是indexloc提供的服务,不要输入任何密码
Skip to content

fix: preserve top-level when running terser for IIFE #900

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;')
})
})