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

Support importHelpers with module:preserve #59852

Merged
merged 1 commit into from
Sep 5, 2024

Conversation

rbuckton
Copy link
Member

@rbuckton rbuckton commented Sep 4, 2024

When we shipped module: preserve we did not include full support for importHelpers, which injects an import for the tslib helper library. This changes our emit in the following ways when these two features are combined:

  • When the source file is an .mts/.mjs file, we will emit import { __helper } from "tslib"; to the output file and reference the helper using the imported name (e.g., __helper).
  • When the source file is a .cts/.cjs file, we will emit const tslib_1 = require("tslib"); to the output file and reference the helper using a qualified name (e.g., tslib_1.__helper).
  • When the source file is a .ts/.js file, we will fall back to the .mts/.mjs behavior, above.

It's important to note that module: preserve does not imply a module format for .ts/.js input files regardless as to the value of "type" in package.json, so we will assume ESM for that case.

Fixes #59789

@typescript-bot typescript-bot added Author: Team For Milestone Bug PRs that fix a bug with a specific milestone labels Sep 4, 2024
@rbuckton
Copy link
Member Author

rbuckton commented Sep 4, 2024

As an alternative for .ts/.js emit, we could choose to ignore importHelpers and emit the helpers inline, just as we do for non-module Scripts.

@rbuckton rbuckton merged commit f39fe7d into main Sep 5, 2024
32 checks passed
@rbuckton rbuckton deleted the fixImportHelpersModulePreserve branch September 5, 2024 21:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Author: Team For Milestone Bug PRs that fix a bug with a specific milestone
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

"importHelpers": true combined with "module": "preserve" results in undefined helper usage
3 participants