marcello3d / node-tosource

Converts JavaScript objects to source

Home Page:https://github.com/marcello3d/node-tosource

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Always resolves to CJS even when using ESM

jcbhmr opened this issue · comments

When using the usual ESM stuff, I still resolve to index.js instead of index.esm.js.

import { createRequire } from "node:module";
const require = createRequire(import.meta.url);

console.table({
  "is-what": {
    esm: import.meta.resolve("is-what"),
    cjs: require.resolve("is-what"),
  },
  tosource: {
    esm: import.meta.resolve("tosource"),
    cjs: require.resolve("tosource"),
  },
});

(Using is-what as an example of it done OK-ly)

image

I think this could be solved by using modern exports: {} conditions instead of main and module? But even this is not recommended so as to avoid the dual package hazard https://nodejs.org/api/packages.html#packages_dual_package_hazard where you can get two copies of the package code in your webpack/vite/rollup/parcel/whatever bundle.js file.

The idealized solution is to either a) only export an ESM version or b) export an ESM wrapper that just export {} from "./module.cjs" so that there's only one canonical source of code or c) only export a CJS version.

Funnily enough, in the package's current state it doesn't suffer from the dual package hazard since it currently only exports CommonJS 🤣