Skip to content

Multiple ESM Edition Autoloader #83

Description

@balupton

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions