Closed maximilianMairinger closed 3 years ago
Id be happy to help if you could give me a quick intro into how you build
What is the problem you are trying to solve?
@planttheidea Thanks for the quick response.
Some of my packages provide a cjs and esm build. I use typescript with the compiler option "module": "esnext"
(for esm) / "module": "commonjs"
(for cjs) to transpile to the different targets. My packages are written in esm and import this library with import clone from "fast-copy"
(as described in your type declaration) thus targeting the default export. When this gets transpiled to esm nothing changes, regarding the imports and it works properly (as the build fast-copy.esm.js
exports export default copy
). Only when it gets transpiled to cjs, typescript resolves my import clone from "fast-copy"
as const clone = require("fast-copy").default
(correctly, as I imported the default). But now (since it's require
d) fast-copy.cjs.js
is imported, which doesn't provide a default export.
I understand the concern that "only cjs users" do not want to write const clone = require("fast-copy").default
but rather const clone = require("fast-copy")
, but this breaks compatibility for build scripts. My suggestion would be to export on default as well as the main export in the cjs build. Thus:
module.exports = copy
copy.default = copy
Doing it the other way around is to the best of my knowledge not possible, as there is no way to declare a main export in esm (nor as way to type it with typescript).
In doing some investigation, it looks like this is specific to using TSC as a compiler (Babel does not have this issue), and was intentional architectural decision! Sometimes I don't understand the rigidity that TS maintainers have about some opinions, but whatever.
I think you're idea is a reasonable "least bad" option. I can make this change.
This should be available as-of 2.1.1
. If you have any issues, let me know!
Great, thanks! I didn't know that this was a tsc specific issue, sorry.
dist/fast-copy.cjs.js
exportsand
dist/fast-copy.esm.js
exportsRequest: Make
dist/fast-copy.cjs.js
export on default as well