jsSHA icon indicating copy to clipboard operation
jsSHA copied to clipboard

emit *.d.mts type files for esm entries

Open Sec-ant opened this issue 3 years ago • 4 comments

Fix typescript module resolution issues when moduleResolution is set to node16 or nodenext in a module type package. Some discussions can be found here: https://github.com/pmndrs/zustand/issues/1381#issuecomment-1290296210

Here is a minimal reproduction of this issue: https://stackblitz.com/edit/node-o5zatf?file=src/index.ts and a patch-fixed version: https://stackblitz.com/edit/node-u9yq9u?file=src/index.ts (the linter is problematic but build result is good)

Sec-ant avatar Nov 02 '22 06:11 Sec-ant

Hi @Sec-ant! First off, thank you for taking the time to help with keeping up with the forever-changing Javascript/Typescript environment; this is probably my least favorite part of maintaining this project.

I would really like to avoid having the ship duplicate files if at all possible. Do you know if https://stackoverflow.com/a/72530967 would work?

Caligatio avatar Nov 02 '22 17:11 Caligatio

Do you know if https://stackoverflow.com/a/72530967 would work?

I've tried this approach in the zustand repo and again I tested it in jssha and I'm afraid it won't work.

".": {
  "import": {
    "types": "./dist/sha.d.ts",
    "default": "./dist/sha.mjs"
  },
  "require": "./dist/sha.js"
},

image

Conditional exports indeed can explicitly set types and default to import, but import doesn't necessarily mean it is an ES module. If the imported package type file is not under a type: module scope or not named as *.d.mts, TSC will see it as a commonjs module type file, even if it is imported, and thus causing a mismatch.

I think the only way at the time being is to ship two sets of types for both cjs and es module entries.

Sec-ant avatar Nov 03 '22 03:11 Sec-ant

Doh! Let me see if I can get rollup to do the right calls to produce .d.mts files. I realize that the .d.ts and .d.mts files can currently be the same thing but I'm sure something will cause issues in the future if I take shortcuts :)

This might take me a bit as we're imminently going to have a child and sleep deprivation + Javascript tooling is a bad combination.

Caligatio avatar Nov 03 '22 16:11 Caligatio

Hey, no rush at all! Many congrats on the arrival of a new life and I wish you all the best!

I realize that the .d.ts and .d.mts files can currently be the same thing but I'm sure something will cause issues in the future if I take shortcuts :)

Yeah, they are actually not necessarily the same thing. If you have relative imports or exports in the source code, node ESM requires mandatory file extensions in those statements and typescript will leave them alone when compiling. So, the emitted declaration files can be different with those emitted by CJS modules. But in this case, for now, their contents are the same.

I can settle on my fork for the time being and I'll update here if I find some less patchy way to deal with this issue.

Sec-ant avatar Nov 04 '22 02:11 Sec-ant