Here is an attempt at a multiple ESM edition autoloader to test if the foundations are possible. It seems they are not, as it returns a promise, as dynamic imports return promises, so the exports will be promises, which is inconsistent API with the CJS Autoloader.
import { readFileSync } from 'fs'
function readJSON(path) {
const text = readFileSync(path, 'utf8')
return JSON.parse(text)
}
import path from 'path'
import url from 'url'
const root = path.dirname(url.fileURLToPath(import.meta.url))
const pkgPath = path.join(root, 'package.json')
function load() {
const data = readJSON(pkgPath)
for (const edition of data.editions) {
if (
edition.tags &&
edition.tags.includes('import') &&
edition.engines &&
edition.engines.node
) {
const editionPath = path.join(root, edition.directory, edition.entry)
return import(editionPath)
}
}
}
const d = load()
// can't export
export default d.default || d
As such, only a single ESM edition for Node seems to be able to be supported by packages.
Here is an attempt at a multiple ESM edition autoloader to test if the foundations are possible. It seems they are not, as it returns a promise, as dynamic imports return promises, so the exports will be promises, which is inconsistent API with the CJS Autoloader.
As such, only a single ESM edition for Node seems to be able to be supported by packages.