I have been struggling with packaging an NPM package so that it bundles both CommonJS and ES modules but either are imported with the same absolute module path. Not only the main module.
For example, I might have a where both builds have their or directory within the package
/package
/modules
index.js
submodule.js
/node
index.js
submodule.js
I want to be able to import transparently so that the right module get loaded on either node or browser (or webpack, etc).
Thus, a call to require('package/submodule')
would load /package/node/submodule.js
and import submodule from 'package/submodule'
would load /package/modules/submodule.js
Is that at all possible, especially so that it does not affect consumers and works with older versions of node?
I tried various stuff like conditional exports, type=module
but ran into issues where jest + babel-jest tries to import the wrong module.