Improve resolution of imports in esmPlugin in some cases

This commit is contained in:
Alex Gleason committed 2025-09-18 15:20:56 -05:00
1 parent f43e582a78
commit 4b42f4cedb
1 file changed
+21
+21
View File
@@ -44,6 +44,23 @@ export function esmPlugin(packageLock: PackageLock, target?: string): Plugin {
};
});
// node built-in modules
build.onResolve({ filter: /^node:/ }, (args) => {
const moduleName = args.path.slice(5);
switch (moduleName) {
case "buffer":
return {
path: `https://esm.sh/${moduleName}?target=${target ?? "esnext"}`,
namespace: "esm",
};
default:
return {
errors: [{ text: `Module not found: ${args.path}` }],
};
}
});
// Handle bare imports like "react"
build.onResolve({ filter: /^[^./].*/ }, (args) => {
const packageName = args.path.startsWith("@")
@@ -63,6 +80,10 @@ export function esmPlugin(packageLock: PackageLock, target?: string): Plugin {
keys.push(`node_modules/${packageName}`);
if (packageName.startsWith("@jsr/")) {
keys.push(`node_modules/@${packageName.slice(5).replace("__", "/")}`);
}
const packageInfo = keys
.map((key) => packageLock.packages[key])
.find((info) => info);