-
Notifications
You must be signed in to change notification settings - Fork 946
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
Support Typescript's moduleResolution: "node16" #2307
Comments
Hey @amiller-gh, I think it's fair to say we're keen to support TypeScript as best we can (we are currently slowly converting Turf modules to TypeScript). This seems reasonably straight forward to resolve - do you have any links to any surrounding information/context? I've looked at the TypeScript page on moduleResolution but I didn't see any mention of the exports fields/types. |
+1, seems like TypeScript doesn't know where the typings is when using ES Module. |
Firebase Admin uses it in its AngularFire seems to use Chalks ES uses only |
I tried to hard-code my |
getting the error declaration not found. any solutions? |
@JamesLMilner the documentation you're looking for to fix this issue is here in the Typescript docs. It should be as simple as adding a |
Adding
|
I made a PR on this. Pretty straightforward. |
I made a temp patch usable through pnpm's |
An alternative patch that will also partially satisfy https://github.com/arethetypeswrong/arethetypeswrong.github.io. I also highly recommend @Turf using ATTW CLI or website to verify that their bundle works properly with both CJS and ESM. Usage is pretty dead simple, see the readme on their repo. Note that the patch below is a partial satisfaction because officially you're supposed to ship a different types file for CJS as opposed to ESM, i.e. I'm also going to summon @Andarist and @andrewbranch here whom are both experts in the field of properly defining ESM/CJS/types and have helped me understand so much about this whole topic. diff --git a/package.json b/package.json
index 1946d013646f884c74f2d5c7bf94d282154accdc..17cdc18d1858018ea9d41190c7ba53da3788b6d8 100644
--- a/package.json
+++ b/package.json
@@ -48,8 +48,14 @@
"exports": {
"./package.json": "./package.json",
".": {
- "import": "./dist/es/index.js",
- "require": "./dist/js/index.js"
+ "import": {
+ "types": "./index.d.ts",
+ "default": "./dist/es/index.js"
+ },
+ "require": {
+ "types": "./index.d.ts",
+ "default": "./dist/js/index.js"
+ }
}
},
"browser": "turf.min.js", |
The proposed patch above doesn't look correct to me because it tries to reuse the same declaration file for both formats. A single file can only be interpreted either as a module or as a script. You want two files (often even with the same content) so they can independently represent your module ( You could try to copy over your |
Hi all. Incoming PR soon to hopefully address this issue and more. Would appreciate your input on the direction. Aiming to support both CJS and ESM. Please have a look at the below and let me know (in theory) how it looks: tsconfig.shared.json:
Contents of dist/ directory in each package generated by tsup:
The two d.ts files are identical, though duplicated as trying to reuse one for both CJS and ESM seems to be the source of some problems. package.json extract
No Retro-testing a few existing issues seems promising so far. Any obvious gotchas or oddities? |
I would highly recommend against this. It's really easy to add the entrypoints and there is 0 downsides. Refer to my comment above for how to define I have recently fixed up all of the Sapphire projects that I maintain so you can also take a looksie here
|
Thanks @favna. Really appreciate it. Based on your advice I will:
I notice in Sapphire package.json you don't set the type explicitly to commonjs (implied as the default node interpretation currently yes?). What do you think about the advice at the end of this topic to always declare the module type explicitly? Any reason not to?
|
…h d.ts entries. Turfjs#2307 (comment) Specifying overall module type as commonjs explicitly.
Hmm so there is a very basic reason I don't have that and that's that I just didn't properly look into it yet. That said, similar to the monorepos The one thing that bothers me a bit with setting {
"compilerOptions": {
"module": "node16",
"moduleResolution": "node16",
"verbatimModuleSyntax": true,
"noEmit": true,
"strict": true
}
} Now running
In a sense valid because the package.json is saying you're writing CJS code, but annoying nonetheless. Now the fix for that is easy and that is to change to tsconfig to: {
"compilerOptions": {
- "module": "node16",
- "moduleResolution": "node16",
+ "module": "es2022",
+ "moduleResolution": "bundler",
"verbatimModuleSyntax": true,
"noEmit": true,
"strict": true
}
} which indicates to TypeScript that you're compiling with a bundler (i.e. Lastly regarding this point:
Truth be told I'm not the biggest fan either but I'm even less a fan of mixing CJS and ESM files in the same directory (and our @sapphire/framework would go completely badonkadonk if it would because it reads files from the file system) so as long as we're compiling for 2 ecosystems I'd say it's the best solution we got. And as for this one:
Yeah I totally get it. I just never had any request from a community member or reason otherwise to expose the |
* Moving to pnpm as package manager. Also taking the opportunity to tidy up our cjs / esm situation using tsup (instead of tsc and rollup). Can't get monorepolint to work with new setup so disabling for the time being. * Changing all Turf imports to use named imports. Adding a few missing named exports. Updating github workflow actions to pnpm instead of yarn. * Updating typescript module and moduleResolution mechanisms to "node16". Not tracking nodenext just yet. Changing tsup command to generate d.ts files that arethetypeswrong approves of. * Tweaking github workflows to explicitely install pnpm. * Looks like node needs the chosen cache binary installed first. Trying without cache as a first step. * Seem to be hitting the tsx bug described here: privatenumber/tsx#421 Upgrading to latest tsx to remedy. * Forgot to update lock file after updating tsx dep in package.json files. * Sorting modules exported from @turf/turf alphabetically. Preparatory commit to add unexported modules. * Converting index.mjs to typescript, and simplifying re-exported imports, and ordering alphabetically. Added in a couple of packages - convex, booleanValid and nearestNeighbourAnalysis. Rollup now only used in packages/turf so merging project root base config into there. Also now only using rollup in packages/turf to do final conversion to web module. JS and d.ts files generated the same as other modules, using tsup. * Including tslib in @turf/turf build now that it's a TS module. * Lot of per-package tsconfig items now on the tsup command line, so stripping back tsconfig.json to the bare minimum. Adding tsconfig.json to packages that while still only JS, are now processed by tsup. * Same as last commit - simplifying tsconfig.json - except these are the subset of packages that had multiple entry points e.g. 3rd party code hosted locally in lib/ * Incorporating some suggestions from @favna on being more explicit with d.ts entries. #2307 (comment) Specifying overall module type as commonjs explicitly. * Reapplying cjsInterop / splitting workaround for CJS exports that we had on build target command line earlier in this PR in to tsup config file. Also disabling treeshaking as this was generating a warning for CJS files and causing the workaround above to not work. * Upgrading rollup and related plugins. Updating to recommended modern browserslist config in babel.config.json e.g. previous chrome 67 and edge 17 are 5 years old, ie11 is just ... ugh * Fluffed the browserslist config - need to cover es5 as well. * Update .github/workflows/turf.yml to use actions/checkout v4. Co-authored-by: Jeroen Claassens <[email protected]> --------- Co-authored-by: Jeroen Claassens <[email protected]>
This is a bug in at least
@turf/[email protected]
and potentially other packages.Typescript has recently released a new
moduleResolution
option that when set tonode16
ornodenext
will use new ESM resolution logic. This includes an understanding of the new "exports" package.json field.Any module in this monorepo that currently uses the exports field (like @turf/envelope) must now specify the types in the export config as well, otherwise Typescript will complain that it can't find the types file if you turn on
moduleResolution: "node16"
.E.g. Turn this:
Into this:
Until then, using this package in Typescript with the new module resolution scheme results in the error
TS7016: Could not find a declaration file for module '@turf/envelope'.
The text was updated successfully, but these errors were encountered: