From c1e4f19327417904aad95d66198dd38e12f493f7 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Mon, 11 Aug 2025 17:21:29 -0700 Subject: [PATCH] Correctly resolve transitive deps --- src/lib/build/esmPlugin.ts | 12 +++++++++- src/lib/build/fsPlugin.ts | 48 +++++++++++++++++++++++--------------- src/lib/build/index.ts | 3 +++ 3 files changed, 43 insertions(+), 20 deletions(-) diff --git a/src/lib/build/esmPlugin.ts b/src/lib/build/esmPlugin.ts index 6c2d0c9b..a1d8c065 100644 --- a/src/lib/build/esmPlugin.ts +++ b/src/lib/build/esmPlugin.ts @@ -50,7 +50,17 @@ export function esmPlugin(packageLock: PackageLock, target?: string): Plugin { ? args.path.split("/").slice(0, 2).join("/") : args.path.split("/")[0]; - const packageInfo = packageLock.packages["node_modules/" + packageName]; + let packageKey = `node_modules/${packageName}`; + if (args.importer.startsWith("https://esm.sh/")) { + try { + const importerPackage = new URL(args.importer).pathname.split('/')[1]?.split('@')[0].replace(/^\*/, ''); + packageKey = `node_modules/${importerPackage}/node_modules/${packageName}`; + } catch { + // fallthrough + } + } + + const packageInfo = packageLock.packages[packageKey] || packageLock.packages[`node_modules/${packageName}`]; const packagePath = args.path.slice(packageName.length); const version = packageInfo?.version; diff --git a/src/lib/build/fsPlugin.ts b/src/lib/build/fsPlugin.ts index 4b23fd9a..1d49a9dc 100644 --- a/src/lib/build/fsPlugin.ts +++ b/src/lib/build/fsPlugin.ts @@ -57,35 +57,45 @@ async function tryFileVariants( basePath: string, ): Promise { const extensions = [".ts", ".tsx", ".js", ".jsx", ".css"]; + const stat = await statSafe(fs, basePath); - try { - await fs.stat(basePath); + // If it's a file, check if it exists + if (stat.isFile()) { return basePath; - } catch { - // Ignore + } + + // If it's a directory, try index files + if (stat.isDirectory()) { + for (const ext of extensions) { + const indexFile = path.join(basePath, "index" + ext); + const indexStat = await statSafe(fs, indexFile); + if (indexStat.isFile()) { + return indexFile; + } + } + throw new Error("Directory without index file"); } // Try direct file with extensions for (const ext of extensions) { const full = basePath + ext; - try { - await fs.stat(full); + const fullStat = await statSafe(fs, full); + if (fullStat.isFile()) { return full; - } catch { - // Ignore - } - } - - // Try directory index files - for (const ext of extensions) { - const indexFile = path.join(basePath, "index" + ext); - try { - await fs.stat(indexFile); - return indexFile; - } catch { - // Ignore } } + // If no file found, throw an error throw new Error("File not found"); } + +async function statSafe(fs: JSRuntimeFS, filePath: string): Promise<{ isFile(): boolean; isDirectory(): boolean }> { + try { + return await fs.stat(filePath); + } catch { + return { + isFile: () => false, + isDirectory: () => false, + }; + } +} \ No newline at end of file diff --git a/src/lib/build/index.ts b/src/lib/build/index.ts index 1c7eba97..343a9059 100644 --- a/src/lib/build/index.ts +++ b/src/lib/build/index.ts @@ -50,6 +50,9 @@ export async function buildProject( fsPlugin(fs, `${projectPath}/src`), esmPlugin(JSON.parse(packageLockText), target), ], + define: { + "import.meta.env": JSON.stringify({}), + }, }); const dist: Record = {};