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: emit cts types #3093

Merged
merged 7 commits into from
Sep 14, 2024
Merged

fix: emit cts types #3093

merged 7 commits into from
Sep 14, 2024

Conversation

Shinigami92
Copy link
Member

closes #3087

@Shinigami92 Shinigami92 added c: bug Something isn't working p: 1-normal Nothing urgent labels Sep 6, 2024
@Shinigami92 Shinigami92 added this to the v9.0 milestone Sep 6, 2024
@Shinigami92 Shinigami92 self-assigned this Sep 6, 2024
Copy link

netlify bot commented Sep 6, 2024

Deploy Preview for fakerjs ready!

Name Link
🔨 Latest commit e07f248
🔍 Latest deploy log https://app.netlify.com/sites/fakerjs/deploys/66e2fc4e3c5b670008063cc4
😎 Deploy Preview https://deploy-preview-3093.fakerjs.dev
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@Shinigami92
Copy link
Member Author

@faker-js/maintainers how should we handle following?

test/faker.spec.ts(21,40): error TS2307: Cannot find module '..' or its corresponding type declarations.

Copy link

codecov bot commented Sep 6, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.96%. Comparing base (18ab2c7) to head (e07f248).
Report is 1 commits behind head on next.

Additional details and impacted files
@@            Coverage Diff             @@
##             next    #3093      +/-   ##
==========================================
- Coverage   99.96%   99.96%   -0.01%     
==========================================
  Files        2776     2776              
  Lines      226260   226260              
  Branches      945      942       -3     
==========================================
- Hits       226183   226182       -1     
- Misses         77       78       +1     

see 2 files with indirect coverage changes

@Shinigami92
Copy link
Member Author

The only difference between the old types, is that the .d.ts have .js suffixes and the .d.cts have .cjs suffixes. That's it 🤷
Everything else is just an exact copy, resulting in an additional ~500kb to be downloaded by million of users weekly...

@Shinigami92
Copy link
Member Author

It looks like that with this change the moduleResolution Node/Node10 is not usable anymore and the build fails with:

index.ts:2:34 - error TS2307: Cannot find module '@faker-js/faker/locale/de' or its corresponding type declarations.
  There are types at 'D:/shinigami/OpenSource/faker-playground/node_modules/@faker-js/faker/dist/locale/de.d.ts',
  but this result could not be resolved under your current 'moduleResolution' setting.
  Consider updating to 'node16', 'nodenext', or 'bundler'.

image

For whatever reason, this only affects /locale/* imports

@ST-DDT
Copy link
Member

ST-DDT commented Sep 6, 2024

@faker-js/maintainers how should we handle following?

test/faker.spec.ts(21,40): error TS2307: Cannot find module '..' or its corresponding type declarations.

It's gone, if you build the project first.

@Shinigami92
Copy link
Member Author

@faker-js/maintainers how should we handle following?

test/faker.spec.ts(21,40): error TS2307: Cannot find module '..' or its corresponding type declarations.

It's gone, if you build the project first.

But previously we only build the types and not the whole project. Do we really want now to build the whole project just to check the types? Or do we want to add a @ts-expect-error / @ts-ignore on this line?

@ST-DDT
Copy link
Member

ST-DDT commented Sep 6, 2024

But previously we only build the types and not the whole project. Do we really want now to build the whole project just to check the types?

No, we don't have to build it.

Or do we want to add a @ts-expect-error / @ts-ignore on this line?

We can do that, but I would like to understand why it doesn't work anymore.

@ST-DDT
Copy link
Member

ST-DDT commented Sep 6, 2024

It looks like that with this change the moduleResolution Node/Node10 is not usable anymore and the build fails with:

index.ts:2:34 - error TS2307: Cannot find module '@faker-js/faker/locale/de' or its corresponding type declarations.
  There are types at 'D:/shinigami/OpenSource/faker-playground/node_modules/@faker-js/faker/dist/locale/de.d.ts',
  but this result could not be resolved under your current 'moduleResolution' setting.
  Consider updating to 'node16', 'nodenext', or 'bundler'.

image

For whatever reason, this only affects /locale/* imports

If you keep the type stuff, it works:

  "types": "index.d.ts",
  "typesVersions": {
    ">=5.0": {
      "*": [
        "dist/*"
      ]
    }
  },

@ST-DDT
Copy link
Member

ST-DDT commented Sep 6, 2024

We have to add tests/playgrounds for all of this, so that it doesn't silently break.

package.json Show resolved Hide resolved
@mshima
Copy link
Contributor

mshima commented Sep 8, 2024

I played with this branch against #3095 and the missing step is to add typesVersions field again.

package.json Outdated Show resolved Hide resolved
@Shinigami92
Copy link
Member Author

Should we merge #3097 before we make such bigger changes to package.json?

@Shinigami92 Shinigami92 marked this pull request as ready for review September 9, 2024 20:43
@Shinigami92 Shinigami92 requested a review from a team as a code owner September 9, 2024 20:43
@Shinigami92 Shinigami92 requested review from ST-DDT and a team September 9, 2024 20:43
@xDivisionByZerox
Copy link
Member

Running the current PR against the playground I get a similar error @Shinigami92 got before. This time for the root import:

Could not find a declaration file for module '@faker-js/faker'. 
'C:/repos/faker/dist/index.cjs' implicitly has an 'any' type.
There are types at 'c:/repos/faker-playground/node_modules/@faker-js/faker/dist/index.d.ts', 
but this result could not be resolved under your current 'moduleResolution' setting. 
Consider updating to 'node16', 'nodenext', or 'bundler'.ts(7016)

Steps to reproduce:

  1. Checkput faker and faker-playground next to eachother
  2. Clean install dependencies in faker
  3. Build faker using pnpm run build
  4. Override faker dependency in playground
  5. Clean install dependencies in faker-playground
  6. Run build in faker-playground using pnpm run build

@Shinigami92 Shinigami92 marked this pull request as draft September 10, 2024 06:04
@Shinigami92
Copy link
Member Author

Thanks @xDivisionByZerox for the review, I will have a look into this probably this evening

package.json Outdated Show resolved Hide resolved
package.json Show resolved Hide resolved
@Shinigami92
Copy link
Member Author

Repeating #3093 (comment)

Oh my god I think I got it now 💡

"*": [

previously we had * here, but now we use .
so previously the * was put in front of everything, but the . is now a relative start from here
and this is why dist/ is now required and fixes the current problem

@Shinigami92 Shinigami92 marked this pull request as ready for review September 10, 2024 14:50
@Shinigami92
Copy link
Member Author

This PR now runs green against faker-js/playground#7 on my local machine

@Shinigami92 Shinigami92 linked an issue Sep 10, 2024 that may be closed by this pull request
10 tasks
@notaphplover
Copy link

I just tested it against my issue repro repo and worked properly 😃

package.json Show resolved Hide resolved
tsconfig.build.json Show resolved Hide resolved
test/faker.spec.ts Outdated Show resolved Hide resolved
@ST-DDT ST-DDT added the c: infra Changes to our infrastructure or project setup label Sep 12, 2024
@ST-DDT ST-DDT added p: 2-high Fix main branch and removed p: 1-normal Nothing urgent labels Sep 12, 2024
Co-authored-by: ST-DDT <ST-DDT@gmx.de>
@ST-DDT ST-DDT requested review from mshima and a team September 12, 2024 15:35
@ST-DDT
Copy link
Member

ST-DDT commented Sep 12, 2024

Team Decision

If all works well, we will merge and release this soon.
Additional testing and reviews are very much appreciated. ❤️

Copy link
Contributor

@mshima mshima left a comment

Choose a reason for hiding this comment

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

An additional recommendation is to use .d.cts types in root types and typesVersions. They are only used in node10 resolutions and probably will be used in commonjs projects.
But I haven’t tried that against playground.

@ST-DDT
Copy link
Member

ST-DDT commented Sep 14, 2024

An additional recommendation is to use .d.cts types in root types and typesVersions. They are only used in node10 resolutions and probably will be used in commonjs projects. But I haven’t tried that against playground.

Thanks for the information. Do you have a source for that recommendation?
I was unable to find it and chatgpt seemed to be confused regarding that question.

@mshima
Copy link
Contributor

mshima commented Sep 14, 2024

Thanks for the information. Do you have a source for that recommendation?
I was unable to find it and chatgpt seemed to be confused regarding that question.

https://www.typescriptlang.org/tsconfig/#moduleResolution

'node10' (previously called 'node') for Node.js versions older than v10, which only support CommonJS require. You probably won’t need to use node10 in modern code.

According to this, node10 will be used in legacy code that only supports CommonJS.
So a CommonJS type is probably better.

@Shinigami92 Shinigami92 merged commit 53ef42c into next Sep 14, 2024
23 checks passed
@Shinigami92 Shinigami92 deleted the 3087-emit-d-cts branch September 14, 2024 19:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c: bug Something isn't working c: infra Changes to our infrastructure or project setup p: 2-high Fix main branch
Projects
None yet
5 participants