diff --git a/packages/@clc/nx/executors/esbuild/executor.mjs b/packages/@clc/nx/executors/esbuild/executor.mjs index 90da6d5b..9153052f 100644 --- a/packages/@clc/nx/executors/esbuild/executor.mjs +++ b/packages/@clc/nx/executors/esbuild/executor.mjs @@ -27,6 +27,10 @@ const runExecutor = async (options) => { bundle: true, entryPoints: eps, format, + // `import.meta` compiles to an empty object under cjs, so + // `import.meta.resolve(...)` becomes a runtime TypeError. esbuild only + // warns, which is easy to scroll past; promote it to an error. + logOverride: {'empty-import-meta': 'error'}, outExtension: {'.js': format === 'cjs' ? '.cjs' : '.mjs'}, outdir: outDir, packages: 'external', diff --git a/packages/@code-like-a-carpenter/tool-tool/src/tool.mjs b/packages/@code-like-a-carpenter/tool-tool/src/tool.mjs index cfa73cb0..1a1b7926 100644 --- a/packages/@code-like-a-carpenter/tool-tool/src/tool.mjs +++ b/packages/@code-like-a-carpenter/tool-tool/src/tool.mjs @@ -1,7 +1,7 @@ import {existsSync} from 'node:fs'; import {mkdir, readFile, writeFile} from 'node:fs/promises'; +import {createRequire} from 'node:module'; import path from 'node:path'; -import {fileURLToPath} from 'node:url'; import findUp from 'find-up'; import kebabCase from 'lodash.kebabcase'; @@ -64,17 +64,14 @@ async function addAsCliPlugin(metadata) { // Need to put this in a variable so deps doesn't add it to package.json, // which would lead to a circular dependency. const cliPackageName = '@code-like-a-carpenter/cli'; - // Reminder: import.meta.resolve works because it doesn't check for - // existence. createRequire().resolve() fails if the file does not exist. - // Since we're trying to fins package.json, we don't actually care if the - // entrypoint has been built yet. - const cliPkgPathResolvePath = fileURLToPath( - import.meta.resolve(cliPackageName) + // Resolve the package.json subpath rather than the package entrypoint: + // resolution has to succeed before the CLI has been built, and + // package.json is the one file guaranteed to be on disk. This relies on + // the CLI exporting "./package.json"; without that entry Node raises + // ERR_PACKAGE_PATH_NOT_EXPORTED no matter what is on disk. + const cliPkgPath = createRequire(rootPkgPath).resolve( + `${cliPackageName}/package.json` ); - const cliPkgPath = await findUp('package.json', { - cwd: path.dirname(cliPkgPathResolvePath), - }); - assert(cliPkgPath, 'Could not locate directory containing package.json'); const cliPkg = await readPackageJson(cliPkgPath); await addToPackageJson(cliPkg, cliPkgPath, pkg.name); return;