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(package.json): Provide correct types for moduleResolution: node, node10, and node16 #16

Merged
merged 11 commits into from
Jun 3, 2024

Conversation

manudeli
Copy link
Member

@manudeli manudeli commented May 31, 2024

I left comments to share intentions

@manudeli manudeli self-assigned this May 31, 2024
Copy link

vercel bot commented May 31, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
es-toolkit ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 3, 2024 1:10pm

@manudeli manudeli marked this pull request as ready for review May 31, 2024 13:10
@manudeli manudeli requested a review from raon0211 May 31, 2024 13:10
@raon0211
Copy link
Collaborator

raon0211 commented Jun 1, 2024

This is actually intended behavior. (exports will be overriden when it is packed into a tarball) Closing this pull request for the reason.

@raon0211 raon0211 closed this Jun 1, 2024
@manudeli
Copy link
Member Author

manudeli commented Jun 1, 2024

This is actually intended behavior. (exports will be overriden when it is packed into a tarball) Closing this pull request for the reason.

@raon0211 because of this point, I made this Pull Request. I want to check why we need to override exports field with publishConfig.

For testing or developing this package correctly, if we should use this package in another workspace in this repo or any other places, In my opinion, we should use built JavaScript files, not TypeScript files, to use same files with library users. because TypeScript file can be transpiled differently depending on the user's environment. so I want to ensure that library maintainer and library users use same files, and I believe that is an important point in the process of developing a library.

@manudeli manudeli reopened this Jun 1, 2024
@raon0211
Copy link
Collaborator

raon0211 commented Jun 1, 2024

In my opinion, when this package is published, library users only see the built JavaScript file, not the TypeScript code. Therefore, all library users interact with the JavaScript code, not the TypeScript.

I believe the issue arises only when we use the es-toolkit library within this workspace. However, the TypeScript files will be transpiled consistently, and they cannot be transpiled differently across user environments, since we control all the compile options within this workspace.

Additionally, using TypeScript directly greatly simplifies the development process. For instance, we don't have to build JavaScript every time we change the source code. Personally, I think this is the direction the JavaScript ecosystem is heading. We have the types as comments proposal in TC39 (as a language spec), and some JavaScript runtimes like Deno natively support TypeScript.

What do you think?

@manudeli
Copy link
Member Author

manudeli commented Jun 1, 2024

Personally, I think this is the direction the JavaScript ecosystem is heading. We have the types as comments proposal in TC39 (as a language spec), and some JavaScript runtimes like Deno natively support TypeScript.

Thanks, I understood why you want to override exports field with publishConfig. I reverted it in 3281a7a

but we should fix these errors https://arethetypeswrong.github.io/?p=es-toolkit%401.0.2.

image

types field is required in my opinion to support TypeScript users.

package.json Outdated Show resolved Hide resolved
@manudeli manudeli changed the title fix(package.json): remove unnecessary exports overriding fix(package.json): add types field Jun 1, 2024
.gitignore Outdated Show resolved Hide resolved
.scripts/postbuild.sh Outdated Show resolved Hide resolved
@raon0211
Copy link
Collaborator

raon0211 commented Jun 3, 2024

Thanks for your recommendation for arethetypeswrong.io! I nearly fixed our type imports as the following image.

image

However it is notoriously difficult to fix the "masquerading as CJS" part (since the dual package itself is difficult and adding TypeScript greatly messes it up); I guess this should be fixed with either we integrate tshy (which is not PnP ready) or we go ESM only (after the release of Node.js 22).

raon0211 and others added 2 commits June 3, 2024 22:09
Co-authored-by: Jonghyeon Ko <jonghyeon@toss.im>
Co-authored-by: Jonghyeon Ko <jonghyeon@toss.im>
Copy link
Collaborator

@raon0211 raon0211 left a comment

Choose a reason for hiding this comment

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

Thanks for your contribution!

@manudeli
Copy link
Member Author

manudeli commented Jun 3, 2024

Thanks for your contribution!

Wait a moment!

@raon0211 raon0211 changed the title fix(package.json): add types field fix(package.json): Provide correct types for moduleResolution: node, node10, and node16 Jun 3, 2024
Copy link
Member Author

@manudeli manudeli left a comment

Choose a reason for hiding this comment

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

@raon0211 Let's fix attw errors..!

Comment on lines +30 to 32
"types": "./dist/index.d.ts",
"import": "./esm/index.mjs",
"require": "./dist/index.js"
Copy link
Member Author

@manudeli manudeli Jun 3, 2024

Choose a reason for hiding this comment

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

However it is notoriously difficult to fix the "masquerading as CJS" part; I guess this should be fixed with either we integrate tshy (which is not PnP ready) or we go ESM only (after the release of Node.js 22).

In my opnion, this may fix our errors. I learned this way fixing from maintaining @suspensive/react to support cjs, esm for all environments

Suggested change
"types": "./dist/index.d.ts",
"import": "./esm/index.mjs",
"require": "./dist/index.js"
"import": {
"types": "./dist/index.d.ts",
"default": "./esm/index.mjs"
},
"require": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}

Copy link
Collaborator

@raon0211 raon0211 Jun 3, 2024

Choose a reason for hiding this comment

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

Actually it doesn't, because in order to be a "ESM" declaration,

  • The type's extension should be .mts (and TypeScript's cli does not support changing extensions of declarations)
  • And for each type declaration (e.g. in ./esm/index.d.mts), we have to specify extensions in its type.
-export * from './array';
+export * from './array/index.mjs';

However this is notoriously difficult in current TypeScript environments unless we introduce some good libraries like tshy. Meanwhile tshy does not support Yarn PnP.

Copy link
Member Author

Choose a reason for hiding this comment

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

then How about using tsup? I think tsup support this quite easily!
https://github.com/suspensive/react/blob/main/packages/react/package.json#L28-L36

Copy link
Member Author

@manudeli manudeli Jun 3, 2024

Choose a reason for hiding this comment

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

@raon0211 Fast merging to fix TypeScript error and next Pull Request for tsup is also good for me. If you agree tsup way, I'll make new Pull Request for this!

Copy link
Collaborator

@raon0211 raon0211 Jun 3, 2024

Choose a reason for hiding this comment

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

Actually tsup does not support our usecase neither.

In order to be tree-shaken well, it is necessary for us to use code splitting. However I find that tsup (and its internal implemenation, esbuild) does not support code splitting properly as rollup's preserveModules.

For example, while rollup completely splits our JavaScript files like this:
image

esbuild's splitting option only makes some chunks and does not split our project files completely.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe we might try just using tsup's declaration generation...

@manudeli manudeli requested a review from raon0211 June 3, 2024 13:15
@raon0211
Copy link
Collaborator

raon0211 commented Jun 3, 2024

Will merge this pull request, and will handle integration of tsup as a seperate one. Thanks for your contribution!

@raon0211 raon0211 merged commit 848f2c6 into main Jun 3, 2024
7 checks passed
@manudeli manudeli deleted the fix/exports/remove-overriding branch June 3, 2024 13:30
@po4tion
Copy link
Contributor

po4tion commented Jul 3, 2024

Hello @raon0211 !
I have a question. Why do we need this file(.scripts/postbuild.sh)?

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

Successfully merging this pull request may close these issues.

3 participants