rollup / plugins

🍣 The one-stop shop for official Rollup plugins
MIT License
3.57k stars 568 forks source link

fix(typescript): correctly resolve filenames of declaration files for `output.file` #1728

Open lameuler opened 1 month ago

lameuler commented 1 month ago

Rollup Plugin Name: @rollup/plugin-typescript

This PR contains:

Are tests included?

Breaking Changes?

If yes, then include "BREAKING CHANGES:" in the first commit message body, followed by a description of what is breaking.

List any relevant issue numbers:

Description

Currently, when output.file is specified instead of output.dir, the filename for emitting declaration files with this.emitFile is resolved incorrectly. This means that given a declarationDir that is outside of the bundle output directory (which is output.dir || dirname(output.file!)), the declarations will still be generated inside the bundle, albeit in a location not actually matching declarationDir that does not respect the absolute file path generated by typescript.

Whereas when using output.dir, declarationDir is actually validated and filenames for declarations are resolved correctly.

For example, with output.file = 'some-path/index.js' and declarationDir = 'types', the declaration files will be generated at some-path/types/index.d.ts, instead of types/index.d.ts which is the correct location (but invalid as it would be output the bundle directory). When output.dir = 'some-path' is used instead, validation fails as expected.

This can be fixed by just using dirname(output.file) in place of output.dir when output.file is specified, and including a validation check that declarationDir is valid when using output.file.

shellscape commented 4 weeks ago

Thanks for the PR. Since this will change some expectations around outputs, we'll have to mark this as a breaking change.