-
-
Notifications
You must be signed in to change notification settings - Fork 675
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
feature: Requesting support for TS ESM/ES6 import #1442
Comments
So, also typings requires a package.json with type: module? I have to check this :/ If this is the case, we need to duplicate the typings and all should be resolved. |
I just realized that yes there's a possible alternate to the 3rd solution that simplifies: {
"exports": {
".": {
"require": "./build/cjs/index.js",
"import": "./build/esm/index.js"
},
},
"main": "./build/cjs/index.js",
"module": "./build/esm/index.js",
} AKA simply omitting the types fields. |
@kf6kjg I've tried now and it is working flawlessy: {
// ...
"type": "module"
}
{
// ...
"compilerOptions": {
// ...
"module": "esnext"
}
} If you do all correctly you will see 2 errors: 1) We need to update |
@kf6kjg This is the current {
// ...
"exports": {
".": {
"require": "./build/cjs/index.js",
"import": "./build/esm/index.js",
"types": "./build/typings/index.d.ts"
}
},
"main": "./build/cjs/index.js",
"module": "./build/esm/index.js",
"types": "./build/typings/index.d.ts",
} |
ESM code should NEVER contain So I did the following:
$ npm run build
> [email protected] prebuild
> npm run clean
> [email protected] clean
> npx npm-run-all --npm-path npm "clean:*"
> [email protected] clean:build
> npx shx rm -rf build
> [email protected] clean:coverage
> npx shx rm -rf coverage
> [email protected] build
> npx tsc --build ./tsconfig.cjs.json ./tsconfig.esm.json
> [email protected] postbuild
> npx ts-node ./scripts/package.json.ts
$ cd build/esm
$ node index.js # Exit code was 0.
$ cd ../cjs
$ node index.js # Exit code was 0.
EDIT: Just realized what you were doing in the examples folder. Exploring that space... |
@kf6kjg I know we are working on it. Is still a PR :) |
@kf6kjg Example are currently broken due to |
I've been introducing the place where I work to NX-based monorepo setups. It's been fun. Additionally in my working copy of #1400 I got to that same point where node complains about "Duplicate "graphql" modules cannot be used at the same time". This is a side effect of putting the examples in the same repository without using a monorepo. Whee, feel you there. |
Took some doing since there's no nice way in TS of omitting comments from the resulting JS files but keeping them in the TS files. See microsoft/TypeScript#14619 for the relevant issue discussing this. Fixes MichalLytek#1442
Took some doing since there's no nice way in TS of omitting comments from the resulting JS files but keeping them in the TS files. See microsoft/TypeScript#14619 for the relevant issue discussing this. Fixes MichalLytek#1442
@kf6kjg With the latest update you should have no problems now :) |
I'll wait an update 🥳🤗 |
Took some doing since there's no nice way in TS of omitting comments from the resulting JS files but keeping them in the TS files. See microsoft/TypeScript#14619 for the relevant issue discussing this. Fixes MichalLytek#1442
That was a LOT of digging. I've finally created a reproduction case. Be sure to read the README located in the patches folder to see the reason for each of the patches. These are not recommended to be included in If it weren't for This also leads to a workaround: set |
I think this can be closed by #1400 🔒 @carlocorradini Can you share your example repo with ESM usage? |
@MichalLytek I need to verify, but I expect the same underlying problem still exists. To my knowledge the root cause has not been resolved; only masked via setting |
mhmhmhm I've tried |
If you want to fix all third-party issue(s) the challenge is yours 😅🤯 |
This issue can be closed. |
I still think that neither of you understand the core of this issue. And that's me failing to communicate clearly. This issue is not about the sate of It is 100% about that fact that TypeScript dual-mode packages, such as this one, have very specific rules and that this package is using a shortcut that results in its ESM exports being ignored by TypeScript and brought into the compiler via the CommonJS compatibility system. In my OP I gave 3 recommendations. Back then option 3 was theoretical. Since then I've had to set up some pure ESM and pure CJS projects that brought in a dual-mode ESM+CJS library I created. To make that work I had to have types compiled into the JS tree: one set of types for CJS, one for ESM. To do otherwise caused either the pure CJS or the pure ESM package to fail to compile. If you need I can build proofs of concept that you can try to import into. |
@kf6kjg UNDERSTOOD 🤯🤯🤯 Personally I prefer the first option where we set |
@carlocorradini I was thinking about exploring the option 3
|
I think that if we specify the paths to the corresponding types (ESM and CJS) it should work without |
Ok it doesn't work :( |
Any updates on this? A clean, tested and working ESM module would already fix many issues in current and future usage of type-graphql. It would also help to clean up the code base to remove packages that have strange or not esm/cjs compliant exports. |
The simplest way, in my opinion, is to just delete the |
Description
Your automatic reaction will be "but we already DO" - however the current setup of this repository, even the setup in PR #1400, has an error.
When using this library in my project which has the
package.json
settingtype
set tomodule
the TS compiler throws errors throws the following when internally importing the ESM-capable libraryclass-validator
:I believe that this is happening because the contents of
type-graphql
'stypings
folder is being interpreted by TS as CommonJS instead of ESM, even though this library exports ESM Javascript in another folder.Note that I'm using TypeScript 4.9.5 against Node v18.14.2.
Proposed solution
There are a couple ways to solve this that I can see, each with its own negatives:
"type": "module"
in the rootpackage.json
and abandon CommonJS. Probably not what the author even wants to get anywhere near.package.json
containing{"type": "module"}
in thetypings
folder. This has the negative consequence that anyone who is using TypeScript while targeting CommonJS will have an error because CommonJS cannot require ESM.typings
folder in favor of placing the types with the ESM and CJS source code and altering the root package.json as follows . The main negative here is that the types are duplicated and that ESM users have to change their import path toimport { ... } from "type-graphql/esm"
..js
and.d.ts
files are kept together no extra type fields in thepackage.json
are needed.The text was updated successfully, but these errors were encountered: