Replies: 8 comments
-
@akim-bow Hi,
Yes, this was a known issue when TypeBox added ESM support. The initial recommendation was that if a sub dependency was based on CJS (in your case nestjs-typebox) then downstream implementations should also limit their builds to CJS (ensuring the same types are pulled in), but this was just the initial recommendation (as of 6 months ago)
I'm not sure I follow. TypeBox provides dual publishing for CJS and ESM which it achieves by publishing separate builds for each. Maybe there is a better way to achieve this? Ideally I would prefer to opt for ESM only (if single publishing), however limiting to ESM only would break many projects who already rely on CJS (and for historical reasons, tooling support for CJS still the most understood, meaning publishing for CJS only is likely still the most viable target for single publishing). As far as I can tell, there does not appear to be an ideal solution for mixing and matching ESM/CJS that doesn't result in configuration pushback in user applications. But am open to reviewing the current build setup and changing it if there are better solutions available today. Do you have any thoughts on a possible better setup? |
Beta Was this translation helpful? Give feedback.
-
@sinclairzx81 Hey, thanks for the detailed response. |
Beta Was this translation helpful? Give feedback.
-
However your utility for checking package integrity reports this issue. |
Beta Was this translation helpful? Give feedback.
-
There are 2 consequences following this decision listed in the link above. Probably we can check whether it's the case in the library and if so, deal with them. Removing the necessity to switch to other module system due to downstream dependency seems like a great feat to me. |
Beta Was this translation helpful? Give feedback.
-
@akim-bow Hi, thanks for the PR
Yes, I see what you mean by the "single Note that the strategy TypeBox selected was largely informed by the work of the author of Other strategies are: So all this said, I would be open to exploring alternative builds, just so long as they aligned with Requirements:
This would need researching. Unfortunately the documentation for dual publishing is very light, but there may be potential to opt for one of the other build targets. It just needs looking into and ensuring the target meets the above criteria. |
Beta Was this translation helpful? Give feedback.
-
Yes, Masquerading as CJS is serious. But my question was: is it possible to restructure and rewrite the library's code in a way, that will alleviate any runtime errors? Also edge cases, when "masquerading" leads to errors, could be covered with proper tests to prevent possibility of the errors in the future. Also i want to underline that i'm totally OK with the approach package-json-redirects. I'm just trying to change type resolution strategy still enjoying the benefits of currently settled strategy.
Attw checks not really perfect bcs they don't actually analyze your codebase to say whether it will give runtime error or not. It reviews A good example of the approach i want to achieve - @nestjs/common package. If you look at the package files, it uses single type emission. And no user has a problem with that structure judjing by open issues there. To me, forcing user to switch to different module system, forbidding him using ESM packages is a tremendous drawback. As you can see in the top message, i wasn't doing something magical, merely tried to add your wonderful library in NestJS as validator. But probably you have other opinion regarding the seriousness of this matter? |
Beta Was this translation helpful? Give feedback.
-
I believe it is possible to make your code 100% workable in single type folder approach and eager to make some researches. Just want to make sure i don't break any of the project's principle or walking the same path twice. |
Beta Was this translation helpful? Give feedback.
-
@akim-bow Hi, Hey, have closed off the PR for now as the single type directory needs additional research to go ahead. Will move this issue into discussions for now as it would be good to review the current build, but as as noted, would want to be working inline with TS and Node module resolution strategies (as working against the grain here would likely break a lot of users) Let's keep this discussion going, as well as see if it is possible to get some additional insights from the community on this. Module systems are far too complex! |
Beta Was this translation helpful? Give feedback.
-
I have NestJS in my project which uses
nestjs-typebox
package.I use ESM in my project and as a result, when i do something like this
import { type Static, Type, type TSchema } from '@sinclair/typebox'
I receive ESM types.
However,
nestjs-typebox
package relies on CJS imports and its API is based on CJS types from'@sinclair/typebox
.Turns out that ESM and CJS types aren't compatible and cannot be used interchangeably.
For example, this code fails in typescript (i use 5.4.5).
I suggest to output just a single type folder to avoid this confusion.
Beta Was this translation helpful? Give feedback.
All reactions