Skip to content

Commit e5d28ac

Browse files
committed
fix: treat script-style ambient declarations as modules
closes rolldown/tsdown#988
1 parent 21685c8 commit e5d28ac

5 files changed

Lines changed: 48 additions & 10 deletions

File tree

‎README.md‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -192,15 +192,21 @@ export default {
192192
}
193193
```
194194

195-
## CommonJS
195+
## Limitations
196196

197-
Declaration bundling requires an ESM Rolldown output. For CommonJS packages,
198-
build the JavaScript output separately and use `emitDtsOnly` for a second
199-
declaration-only build.
197+
Declaration bundling requires an ESM Rolldown output. The `cjs` output format is
198+
not supported. For CommonJS packages, build the JavaScript output separately
199+
and use `emitDtsOnly` for a second declaration-only build.
200200

201-
The plugin expects ESM-style declaration input. Syntax such as `export =` or
202-
`import x = require('x')` may not bundle correctly. If it comes from a
203-
dependency, mark that dependency as external.
201+
The following declaration input cannot be bundled:
202+
203+
- CommonJS export assignments such as `export = value`.
204+
- CommonJS import aliases such as `import value = require('package')`.
205+
- Script-style ambient declarations, such as `declare module 'package' { ... }`
206+
or unexported top-level `declare` statements. Bundled declaration chunks are
207+
always modules, which would change the meaning of these declarations.
208+
209+
Mark dependencies that use unsupported declaration syntax as external.
204210

205211
## Credits
206212

‎src/fake-js/index.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,17 @@ export function createFakeJsPlugin({
571571
return { code: EMPTY_STUB, map: null }
572572
}
573573

574+
if (!program.body.some(isModuleStatement)) {
575+
program.body.push(
576+
b.ExportNamedDeclaration({
577+
declaration: null,
578+
specifiers: [],
579+
source: null,
580+
attributes: [],
581+
}),
582+
)
583+
}
584+
574585
// recover comments
575586
const comments = new Set<t.Comment>()
576587
const commentsValue = new Set<string>() // deduplicate
@@ -629,3 +640,17 @@ export function createFakeJsPlugin({
629640
return declarationMap.get(declarationId)!
630641
}
631642
}
643+
644+
function isModuleStatement(node: t.ProgramStatement): boolean {
645+
return (
646+
is.oneOf(node, [
647+
'ImportDeclaration',
648+
'ExportAllDeclaration',
649+
'ExportDefaultDeclaration',
650+
'ExportNamedDeclaration',
651+
'TSExportAssignment',
652+
]) ||
653+
(node.type === 'TSImportEqualsDeclaration' &&
654+
node.moduleReference.type === 'TSExternalModuleReference')
655+
)
656+
}

‎tests/__snapshots__/custom-language.test.ts.snap‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ exports[`volar > astro 1`] = `
66
interface Props {
77
name: string;
88
}
9-
declare function Foo(_props: Props): any;"
9+
declare function Foo(_props: Props): any;
10+
export {};"
1011
`;
1112

1213
exports[`volar > custom without volar (oxc) 1`] = `

‎tests/__snapshots__/index.test.ts.snap‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,8 @@ import "./mod.js";
473473
//#region tests/fixtures/side-effects/mod.d.ts
474474
declare global {
475475
let sideEffectExecuted: boolean;
476-
}"
476+
}
477+
export {};"
477478
`;
478479
479480
exports[`sub namespace 1`] = `

‎tests/index.test.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ test('cyclic import', async () => {
611611
})
612612

613613
test('side effects', async () => {
614-
const { snapshot } = await rolldownBuild(
614+
const { snapshot, chunks } = await rolldownBuild(
615615
path.resolve(dirname, 'fixtures/side-effects/index.ts'),
616616
[
617617
dts({
@@ -622,6 +622,11 @@ test('side effects', async () => {
622622
{},
623623
{ preserveModules: true },
624624
)
625+
const modChunk = chunks.find((chunk) => chunk.fileName === 'mod.d.ts')
626+
if (modChunk?.type !== 'chunk') {
627+
throw new Error('Expected mod.d.ts to be an output chunk')
628+
}
629+
expect(modChunk.code).toContain('export {}')
625630
expect(snapshot).toMatchSnapshot()
626631
})
627632

0 commit comments

Comments
 (0)