GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Fix __importDefault when used on typescript libraries by jakebailey · Pull Request #51474 · microsoft/TypeScript

Conversation

@jakebailey

@jakebailey Jake Bailey (jakebailey) commented Nov 10, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Fixes #51473

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@jakebailey
Add an export default to our libraries
d25da73
@typescript-bot TypeScript Bot (typescript-bot) assigned Jake Bailey (jakebailey) Nov 10, 2022
@typescript-bot TypeScript Bot (typescript-bot) added the Author: Team label Nov 10, 2022
@jakebailey

Jake Bailey (jakebailey) commented Nov 10, 2022

Copy link
Copy Markdown
Member Author

TypeScript Bot (@typescript-bot) pack this

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@typescript-bot TypeScript Bot (typescript-bot) added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Nov 10, 2022
@typescript-bot

TypeScript Bot (typescript-bot) commented Nov 10, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Heya Jake Bailey (@jakebailey), I've started to run the tarball bundle task on this PR at d25da73. You can monitor the build here.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@typescript-bot

TypeScript Bot (typescript-bot) commented Nov 10, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Hey Jake Bailey (@jakebailey), I've packed this into an installable tgz. You can install it for testing by referencing it in your package.json like so:

{
    "devDependencies": {
        "typescript": "https://typescript.visualstudio.com/cf7ac146-d525-443c-b23c-0d58337efebc/_apis/build/builds/138295/artifacts?artifactName=tgz&fileId=10618ECD016CC886706061B6603554907DAB9B79BFC5CC3AF2DCD78B6A8BBAEA02&fileName=/typescript-5.0.0-insiders.20221110.tgz"
    }
}

and then running npm install.


There is also a playground for this build and an npm module you can use via "typescript": "npm:@typescript-deploys/[email protected]".;

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jakebailey Jake Bailey (jakebailey) mentioned this pull request Nov 10, 2022
__esModule in ts cause breaking change #51473
Closed
@jakebailey
Jake Bailey (jakebailey) marked this pull request as ready for review November 10, 2022 05:37
@jakebailey

Jake Bailey (jakebailey) commented Nov 10, 2022

Copy link
Copy Markdown
Member Author

This does fix the issue, but, I'm not 100% sure this is good or not. default and __esModule really are a mess.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@AlCalzone

AlCalzone commented Nov 10, 2022

Copy link
Copy Markdown
Contributor

Not sure if this is any help, but esbuild mention this in their docs
https://esbuild.github.io/content-types/#default-interop

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jakebailey

Jake Bailey (jakebailey) commented Nov 10, 2022

Copy link
Copy Markdown
Member Author

Yeah, I've seen that, and https://github.com/evanw/bundler-esm-cjs-tests. Unfortunately it seems like in addition to this other stuff, tslib / TS's helpers are another aspect that need to be considered, so I don't know if I can extract a decision out of that. I'll just have to test a bunch of cases, I guess.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jakebailey
Merge branch 'main' into export-default
d062768
@jakebailey

Jake Bailey (jakebailey) commented Nov 10, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

A table of behaviors, where ts is require("typescript"):

test 4.8.4 main this PR
ts.version ✅ ✅ ✅
ts.default.version ❌ ❌ ✅
__importDefault(ts).version ❌ ✅ ✅
__importDefault(ts).default.version ✅ ❌ ✅
__importStar(ts).version ✅ ✅ ✅
__importStar(ts).default.version ✅ ❌ ✅

Without hacking things to drop __esModule, I don't know if I can do better than this.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jakebailey

Jake Bailey (jakebailey) commented Nov 10, 2022

Copy link
Copy Markdown
Member Author

Maybe Wesley Wigham (@weswigham) or Ron Buckton (@rbuckton) have some insight here? (See also the issue in question, #51473.)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@weswigham

Wesley Wigham (weswigham) commented Nov 10, 2022

Copy link
Copy Markdown
Member

I'm not a huge fan - we tell people not to do this all the time. Since we only want this for compat, I imagine manipulating the marker may be better. Which, on that note, can't we make the marker not be emitted by using export = ts instead of export *?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jakebailey

Jake Bailey (jakebailey) commented Nov 10, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

I haven't found a way yet. esbuild's toCommonJS helper always adds it and I haven't been able to get to do what I want.

Also, export = ts is a d.ts feature only, so that wouldn't be the solution. I have tried module.exports = ts (the equivalent outside of declarations) and that doesn't work because the right side comes from toCommonJS.

I guess I can just hack it...

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jakebailey

Jake Bailey (jakebailey) commented Nov 10, 2022

Copy link
Copy Markdown
Member Author

Wait, I'm wrong, module.exports = ts does appear to work. I'll do that instead.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jakebailey

Jake Bailey (jakebailey) commented Nov 10, 2022

Copy link
Copy Markdown
Member Author

Hm, but that only helps the bundler. It breaks everything else :(

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jakebailey Jake Bailey (jakebailey) changed the title Add an export default to our libraries Fix __importDefault when used on typescript libraries Nov 10, 2022
@jakebailey
Remove use of __toCommonJS for as workaround
2a99bd6
@weswigham

Wesley Wigham (weswigham) commented Nov 10, 2022

Copy link
Copy Markdown
Member

Also, export = ts is a d.ts feature only, so that wouldn't be the solution

Wot? It's definitely a runtime feature we transpile to module.exports = ts, we just only allow it for cjs module targets.

jakebailey reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jakebailey

Jake Bailey (jakebailey) commented Nov 10, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

With a hack in place to drop this helper:

test 4.8.4 main this PR
ts.version ✅ ✅ ✅
ts.default.version ❌ ❌ ❌
__importDefault(ts).version ❌ ✅ ❌
__importDefault(ts).default.version ✅ ❌ ✅
__importStar(ts).version ✅ ✅ ✅
__importStar(ts).default.version ✅ ❌ ✅

So, that restores the status quo.

See also evanw/esbuild#1971 but this does seem to be exactly the thing the esbuild docs refer to. I was unable to get the suggestions there to work; neither mts nor "type": "module" changed this behavior. So I think we're stuck with this hack.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jakebailey

Jake Bailey (jakebailey) commented Nov 10, 2022

Copy link
Copy Markdown
Member Author

Wot? It's definitely a runtime feature we transpile to module.exports = ts, we just only allow it for cjs module targets.

Huh, you're right. I must have tested and gotten an error for this a while ago and then assumed it was because export = was d.ts only. I could have sworn I read that...

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Jake Bailey (jakebailey) added 3 commits November 10, 2022 12:37
@jakebailey
Use export =
fe4ecf5
@jakebailey
Revert other changes
3fc3890
@jakebailey
Revert other changes
ac08bc0
@jakebailey

Jake Bailey (jakebailey) commented Nov 10, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

Okay, changed to export =. This leaves us with this state:

test 4.8.4 main this PR main --no-bundle this PR --no-bundle
ts.version ✅ ✅ ✅ ✅ ✅
ts.default.version ❌ ❌ ❌ ❌ ❌
__importDefault(ts).version ❌ ✅ ❌ ✅ ✅
__importDefault(ts).default.version ✅ ❌ ✅ ❌ ❌
__importStar(ts).version ✅ ✅ ✅ ✅ ✅
__importStar(ts).default.version ✅ ❌ ✅ ❌ ❌

So, this makes us match old TS when bundled, which was the goal. --no-bundle doesn't work quite the same, but this PR doesn't seem to change that behavior, so that's fine.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jakebailey

Jake Bailey (jakebailey) commented Nov 10, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

Wait, what, it's broken again. Ugh. Bear with me :(

I can't read. It's fine.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jakebailey
Jake Bailey (jakebailey) requested a review from Wesley Wigham (weswigham) November 10, 2022 20:45
@jakebailey Jake Bailey (jakebailey) mentioned this pull request Nov 10, 2022
Add smoke tests to CI #51464
Merged
@jakebailey

Jake Bailey (jakebailey) commented Nov 10, 2022

Copy link
Copy Markdown
Member Author

This should be final now, if you wanted to take another look. I plan to add smoke tests for this to #51464.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

weswigham
Wesley Wigham (weswigham) approved these changes Nov 10, 2022
View reviewed changes
@jakebailey
Jake Bailey (jakebailey) merged commit 10125e4 into microsoft:main Nov 10, 2022
@jakebailey
Jake Bailey (jakebailey) deleted the export-default branch November 10, 2022 21:57
@jakebailey Jake Bailey (jakebailey) mentioned this pull request Jan 25, 2024
Use 'export * from' for public API instead of 'export =' #57163
Closed
@microsoft Microsoft (microsoft) locked as resolved and limited conversation to collaborators Oct 22, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Reviewers

@weswigham Wesley Wigham (weswigham) weswigham approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@jakebailey Jake Bailey (jakebailey)

Labels

Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

__esModule in ts cause breaking change

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@jakebailey @typescript-bot @AlCalzone @weswigham
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.