Skip to content
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

fix(typescript): emit declaration files for type-only source files that are not explicitly included #1555

Conversation

ianyong
Copy link
Contributor

@ianyong ianyong commented Aug 5, 2023

Rollup Plugin Name: @rollup/plugin-typescript

This PR contains:

  • bugfix
  • feature
  • refactor
  • documentation
  • other

Are tests included?

  • yes (bugfixes and features will not be merged without tests)
  • no

Breaking Changes?

  • yes (breaking changes will not be merged unless absolutely necessary)
  • no

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

Please read the linked issue first for context.

The core issue is that TypeScript source files which contain only type definitions transpile into nothing. This means that Rollup has no equivalent JavaScript files to operate on (i.e., we cannot handle these files if they are not already included in tsconfig.json because within the Rollup build hooks and output generation hooks, these files do not exist). Specifically in the test case added, the export statement in main.ts is removed when transpiling into JavaScript because MyNumber is a type. This means that by the time Rollup operates on main.js, there is no longer any logical link between main.ts & should-be-emitted-types.ts.

However, the observation here is that we don't have to manually keep track of which files to emit because the TypeScript compiler already does it for us. Specifically, the TypeScript compiler only emits the files which are necessary (this is verified in the test case I added). This means that we can simply emit all declaration & source map files in emittedFiles to solve the linked issue.

Comment on lines -26 to +28
function isMapOutputFile(name: string): boolean {
return name.endsWith('.map');
export function isMapOutputFile(name: string): boolean {
return name.endsWith('ts.map');
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason for this change is that we only need to worry about emitting .d.ts.map (and equivalent .d.cts.map & .d.mts.map) files. The .js.map files are already being emitted by the TypeScript compiler (unless the noEmit option is set to true).

Copy link
Collaborator

@shellscape shellscape left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Will this alter any existing behavior, cause a difference in output (aside from additional files)

@ianyong
Copy link
Contributor Author

ianyong commented Aug 17, 2023

Will this alter any existing behavior, cause a difference in output (aside from additional files)

No, the only change in behaviour is that in the generateBundle output generation hook, we output the set of files emitted by the TypeScript compiler instead of computing this set of files ourselves. Every other step in the build process is unchanged.

@shellscape shellscape merged commit 027bca6 into rollup:master Aug 26, 2023
28 checks passed
@shellscape
Copy link
Collaborator

thanks!

@shellscape
Copy link
Collaborator

don't report bugs on closed pull requests. open a new issue and follow the issue template

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants