Closed jcbhmr closed 4 months ago
I had struggled a lot with the export types of my packages in the past, but I finally found a setup that works recently. The export types I have just fixed in the latest version: https://arethetypeswrong.github.io/?p=is-what%404.1.10
I think once all packages use the latest version of is-what, dual package hazard should not occur anymore, unless the project set up or any of the dependencies has an issue where it imports both CJS and ESM for some reason. : S So imo this is not really an issue. Sorry!
Actually, my problem isn't the types. Its that this library is bundled as two separate duplicate versions: One of CJS and one for ESM when it should probably be just one with the second format just re-exporting the stuff from the "base" version. For example, CJS everything with index.mjs that just exports the cjs stuff.
You can see the demo here: https://stackblitz.com/edit/vitejs-vite-mpcory?file=vite.config.ts,index.js,dist%2Fassets%2Findex-27addc3e.js&terminal=dev
Note that that demo has TWO DUPLICATE versions of the is-what package. THATs the dual package hazard. 🤣
It's caused by Node.js following the require condition and the import condition BOTH and then seeing two different files when those conditions are resolved. Then Vite just takes those and bundles them as two separate things. Ideally they'd both point to a single canonical source with a wrapper ESM or CJS that re-exports the other
Does that make sense as to what my concern is? 🤔
@jcbhmr kinda... 🤔 😅 well i'm open to jump on the ESM only wagon soonish. IF ESM means I don't need to wrack my head in tooling hell anymore, than I'm all for that. I just wanna write beautiful code and not think about how stuff gets bundled tbh. : P
I feel like we can still add a few more functions to the current version, and once everything's stable in a few weeks or so we can do a major ESM only node v16 release.
kinda... 🤔 😅
Let me try again 😆 hopefully this doesn't confuse you more lmao
import
condition or the require
condition depending on if import {} from "is-what"
or const {} = require("is-what")
.import
in one .mjs
file and require()
in another .cjs
file, you'll import two completely different files with different code.require("is-what")
they get the require: ...
export condition, even if you use import
and get the import: ...
condition.require()
copy and one import
copy.If instead, you do:
// index.mjs
// You can import CJS from MJS, but you CAN'T require() MJS from CJS 😭
export { isArray, isObject } from "./index.cjs"
dist/
index.mjs
index.cjs
isArray.cjs
isObject.cjs
...
Then that fixes it! Now the require
and import
conditions will map to the same underlying code instead of two duplicate versions of it. Thus, your bundler when it follows the import
and require
imports will end up with a single canonical source that's just re-exported in the MJS case.
@mesqueeb Does that make it more or less clear? 🤔😂
I hit the dual package hazard! 🤣 Which means there are duplicate functions in my bundle because I use
import {} from "is-what"
and then one of my dependencies usesrequire("is-what")
😱https://nodejs.org/api/packages.html#dual-package-hazard 👇 A possible solution as suggested by Node.js docs ☝
Then again, this might not be an issue! 😆