Open JamieDanielson opened 4 months ago
Hi @JamieDanielson
That's a pretty detailed list. Thanks! :) Is it a proposal or already has a consensus with other maintainers?
- Change the package type to
module
inpackage.json
We don't need this step do we? If "exports" in package.json has the necessary "import" entries, then leaving empty or explicitly "type": "commonjs"
should be fine.
- Change the package type to
module
inpackage.json
We don't need this step do we? If "exports" in package.json has the necessary "import" entries, then leaving empty or explicitly
"type": "commonjs"
should be fine.
I have to research more to confirm. I'd read something about either how the code is transpiled or how the types are generated that require type: module
but offhand I don't quite remember. All the examples I've seen of folks doing this approach, they updated the type. But as time goes on and more features come out to help with interop, I can see some of these requirements potentially going away (aside from minimum TypeScript and Node versions to take advantage of said features). When we're ready to move forward with this (will likely want more testing in place first) then we can revisit whether this change is needed.
This is all anecdotal; I've been trying to figure out how to dual-publish for a while and I imagine there are multiple "correct" approaches. A big part of the struggle comes from TypeScripts's behaviors not being entirely documented (like, if we just wrote typesafe mjs and eliminated the typescript compile step, we pretty wouldn't every have folks complaining about cjs/esm issues). In any case, this is what I believe has to be done to dual publish (at least as far as package.json
and build outputs):
.cjs
files and .mjs
files and set exports accordingly (this means the package.json module
field doesn't matter to node's resolver).d.cts
and .d.mts
type definitions and set exports accordingly. In particular, you need separate types
entries for both import
and require
. (i still apparently don't have this quite right as I only discovered publint recently; it's telling me i should be using .d.cts
where i'm currently using .d.ts
)"type": "module"
in package.json anyway, otherwise tsc in the consuming project will typecheck against the cts versions, which is probably not what you want. (this may be related to my above comment about .d.ts
vs .d.cts
, but I don't think so).So, this is a really long way to say, "yes, i think the type
in package.json
remains necessary".
If it's helpful, I've got this repo seemingly dual-publishing correctly (aside from the .d.ts
vs .d.cts
issue).
Is your feature request related to a problem? Please describe.
Our packages currently publish directories of ESM builds, but they do not follow ESM spec and therefore cause problems for some end users trying to use them. For example, see issue #3989 .
Required (for each package) to fully support ESM
./fileName.js
(may require adjustments to imports/exports that import directories)module
inpackage.json
package.json
(*, import, require
)tsconfig.esm.json
if it doesn't already existtype:module
andtype:commonjs
in apackage.json
.eslintrc.js
to.eslintrc.cjs
(or change to ESM syntax instead of usingmodule.exports
).tsconfig
s to parse.cjs
extensionsts-node/esm
loader (also requires install ofts-node
)Maybe Required, otherwise Recommended
nodenext
intsconfig.esm.json
, which integrates with node's native ESM support and gives warnings for missing.js
suffixes.tsconfig.esm.json
ts-mocha
against the ts source code)... idea from this blogThis (closed) PR can be used as a reference.
I would argue that we need a testing strategy in place to verify these changes, BEFORE we make any changes, as our current ESM tests cover a lot of ground but not the problems solved with this change. There are a few issues floating around I believe, but for now I will link #4008 to build out smoke / integration tests.
Related Issues that may be resolved with these changes:
3989
4106
4392
4396
4642
4794
4842