Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

order editions based on newest target version first #164

Open
balupton opened this issue Nov 30, 2023 · 0 comments
Open

order editions based on newest target version first #164

balupton opened this issue Nov 30, 2023 · 0 comments

Comments

@balupton
Copy link
Member

currently it works by all cjs first, then all esm first

as esm is one for all or nothing, it could be older, such as case with errlop, e.g. edition-2022, edition-2017, edition-es5, edition-2017-esm

editions autoloader doesn't support esm, to resolve this editions autoloader must be constrained to require/cjs modules, or support a esm dynamic import technique (of which node.js versions I am unsure)

here is some unfinished rewrite of the editions generate to go via newest node targets first, however it has several issues (it is dependent on targets answer order), and es versions de-duplication is tied to module technique where in this rewrite they are not

		// add edition for each babel/typescript target
		if (
			answers.compilerNode === 'babel' ||
			answers.compilerNode === 'typescript'
		) {
			// bug: es versions should be tied to import
			const esVersionsTargets = new Set()
			for (const nodeVersionTarget of answers.nodeVersionsTargeted
				.slice()
				.reverse()) {
				for (const targetModule of answers.targetModules) {
					if (targetModule === 'import') {
						if (
							!versionRange(
								nodeVersionTarget,
								'>=12', // esm
							)
						)
							continue
						if (
							answers.nodeVersionsTargetedImportRange &&
							!versionRange(
								nodeVersionTarget,
								answers.nodeVersionsTargetedImportRange,
							)
						)
							continue
					} else if (targetModule === 'require') {
						if (
							answers.nodeVersionsTargetedRequireRange &&
							!versionRange(
								nodeVersionTarget,
								answers.nodeVersionsTargetedRequireRange,
							)
						)
							continue
					} else {
						throw new Error(`invalid target module for the compiler`)
					}
					if (answers.compilerNode === 'babel') {
						const directory =
							`edition-node-${nodeVersionTarget}` +
							(targetModule === 'import' ? '-esm' : '')
						editions.set(
							directory,
							new Edition({
								compiler: 'babel',
								directory,
								index: addExtension(answers.indexEntry, `js`),
								node: addExtension(answers.nodeEntry, `js`),
								browser: addExtension(answers.browserEntry, `js`),
								test: addExtension(answers.testEntry, `js`),
								bin: addExtension(answers.binEntry, `js`),
								tags: ['compiled', 'javascript', targetModule],
								targets: {
									node: nodeVersionTarget,
								},
								engines: {
									node: true,
									browsers: false,
								},
							}),
						)
					} else if (answers.compilerNode === 'typescript') {
						// fetch the latest es version for the node.js version target that typescript supports
						const esVersionTarget = intersect(
							allTypescriptTargets,
							await fetchAllCompatibleESVersionsForNodeVersions([
								nodeVersionTarget,
							]),
						)[0]
						// check that typescript supported it
						if (!esVersionTarget) continue
						// check that we haven't already generated an edition for this es version target target
						if (esVersionsTargets.has(esVersionTarget)) continue
						esVersionsTargets.add(esVersionTarget)
						// generate the edition
						const esVersionTargetLower = esVersionTarget.toLowerCase()
						const directory =
							`edition-${esVersionTargetLower}` +
							(targetModule === 'import' ? '-esm' : '')
						editions.set(
							directory,
							new Edition({
								compiler: 'typescript',
								directory,
								index: addExtension(answers.indexEntry, `js`),
								node: addExtension(answers.nodeEntry, `js`),
								browser: addExtension(answers.browserEntry, `js`),
								test: addExtension(answers.testEntry, `js`),
								bin: addExtension(answers.binEntry, `js`),
								tags: [
									'compiled',
									'javascript',
									esVersionTargetLower,
									targetModule,
								],
								targets: {
									node: nodeVersionTarget,
									es: esVersionTarget,
								},
								engines: {
									node: true,
									browsers: false,
								},
							}),
						)
					} else {
						throw new Error(`invalid target for the compiler`)
					}
				}
			}
		}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant