Skip to content

Fix TS issues, more correctly ship CJS and ESM#2820

Merged
RobbieTheWagner merged 45 commits intomainfrom
ts-fixes
May 24, 2024
Merged

Fix TS issues, more correctly ship CJS and ESM#2820
RobbieTheWagner merged 45 commits intomainfrom
ts-fixes

Conversation

@RobbieTheWagner
Copy link
Copy Markdown
Member

No description provided.

@RobbieTheWagner RobbieTheWagner marked this pull request as ready for review May 22, 2024 01:37
@RobbieTheWagner RobbieTheWagner changed the title Fix TS issues Fix TS issues, more correctly ship CJS and ESM May 22, 2024
Copy link
Copy Markdown

@joshkel joshkel left a comment

Choose a reason for hiding this comment

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

I'm still working through all the ins and outs involved in supporting CJS + ESM across different configurations, but some initial comments:

It looks like you've set up subpath exports so users can do code like import type { Tour } from 'shepherd.js/tour', and you're using the types-versions-wildcards approach from example-subpath-export-ts-compat to implement this. Is that correct?

Do you need subpath exports? If you make everyone import as, e.g., import type { Tour } from 'shepherd.js', then I think things will be simpler. Then you'd have the option of rolling up the types into a single .d.ts file, which might address the current "Internal resolution errors" from attw.

"*": {
"*": [
"./dist/*"
"./dist/esm/*"
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The moduleResolution: Node10 issues flagged by attw are caused by typesVersions, which is only needed to make subpath exports work with moduleResolution: Node10. tsc --traceResolution with a sample project shows that this is failing because TypeScript takes the * as a literal substitution - it tries to resolve dist/cjs/shepherd.cjs from main, substitutes it in the *, and gets dist/esm/dist/cjs/shepherd.cjs.

If you add a specific mapping for shepherd.cjs, then moduleResolution: node10 seems to work:

      "dist/cjs/shepherd.cjs": [
        "./dist/cjs/shepherd.d.cts"
      ],
      "*": [
        "./dist/cjs/*"
      ]

However, I'm not sure that you need typesVersions - see comments elsehwere.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Removing typesVersions or changing it with what you suggest here are both fine with me. I had deleted typesVersions a few times and it didn't seem to change attw at all whether I had it or not.

@RobbieTheWagner
Copy link
Copy Markdown
Member Author

@joshkel

It looks like you've set up subpath exports so users can do code like import type { Tour } from 'shepherd.js/tour', and you're using the types-versions-wildcards approach from example-subpath-export-ts-compat to implement this. Is that correct?

Yeah, more or less.

Do you need subpath exports? If you make everyone import as, e.g., import type { Tour } from 'shepherd.js', then I think things will be simpler. Then you'd have the option of rolling up the types into a single .d.ts file, which might address the current "Internal resolution errors" from attw.

I'm fine with removing subpath exports. I mostly don't know what I am doing here, so a lot of this was just trying to make stuff work. One thing to note though, I opened an issue on attw and they said Svelte is not supported, so not sure if we will be able to resolve all the issues or not, but happy to try whatever you suggest.

@RobbieTheWagner
Copy link
Copy Markdown
Member Author

@joshkel I used api-extractor to rollup the types, and I think things should work for everyone now 🎉

@RobbieTheWagner
Copy link
Copy Markdown
Member Author

@joshkel with the newly consolidate types our dist is much simpler and attw is happy, even on node10 🎉 . However, it seems like a lot of types flat out don't work and builds blow up in the real world. You can see in CI we cannot even run a build because the react package hits a bunch of type issues like this:

packages/react build: src/index.tsx:22:14 - error TS4023: Exported variable 'useShepherd' has or is using name 'StepOptions' from external module "/home/runner/work/shepherd/shepherd/shepherd.js/dist/cjs/shepherd" but cannot be named.
packages/react build: 22 export const useShepherd = () => {

Any ideas where we may have gone wrong or how to fix this?

@RobbieTheWagner
Copy link
Copy Markdown
Member Author

@joshkel I am going back to the drawing board and attempting to use rollup-plugin-ts because it is supposed to handle all this stuff for us, but I have been unable to get it to run.

@RobbieTheWagner
Copy link
Copy Markdown
Member Author

@joshkel okay, so my latest attempt just uses svelte2tsx and it seems to be pretty close to working.

┌───────────────────┬──────────────────────────────┐
│                   │ "shepherd.js"                │
├───────────────────┼──────────────────────────────┤
│ node10            │ 🥴 Internal resolution error │
├───────────────────┼──────────────────────────────┤
│ node16 (from CJS) │ 🥴 Internal resolution error │
├───────────────────┼──────────────────────────────┤
│ node16 (from ESM) │ 🟢 (ESM)                     │
├───────────────────┼──────────────────────────────┤
│ bundler           │ 🟢                           │
└───────────────────┴──────────────────────────────┘

My thoughts are that CJS doesn't work because svelte 4 only supports ESM, so svelte2tsx I would imagine only generates types for ESM, therefore CJS cannot figure out what the svelte imports are. Any ideas on a way to fix things here?

@RobbieTheWagner
Copy link
Copy Markdown
Member Author

Okay, I think I have everything green for real this time. This stuff is not easy 😅

</main>
<Footer />
</Base>
</Base> No newline at end of file
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

had to remove the changes here b/c I changed this file to use more as a layout for more pages. The script is in the demo.astro file now, but worse case we capture those in another PR

@RobbieTheWagner RobbieTheWagner merged commit 2873df6 into main May 24, 2024
@RobbieTheWagner RobbieTheWagner deleted the ts-fixes branch May 24, 2024 12:47
@github-actions github-actions bot mentioned this pull request May 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants