Resolve package.json versions

This commit is contained in:
Alex Gleason committed 2025-08-06 22:09:52 -05:00
1 parent d89555723b
commit 8dfefdc7fd
2 files changed
+22 -6

No files matched your search

+20 -5
View File
@@ -1,14 +1,29 @@
import type { Plugin } from 'esbuild-wasm';
export function esmShPlugin(): Plugin {
export function esmShPlugin(dependencies: Record<string, string>): Plugin {
return {
name: 'esm-sh',
setup(build) {
// Handle bare imports like "react"
build.onResolve({ filter: /^[^./].*/ }, (args) => ({
path: `https://esm.sh/${args.path}`,
namespace: 'http',
}));
build.onResolve({ filter: /^[^./].*/ }, async (args) => {
const packageName = args.path.startsWith('@')
? args.path.split('/').slice(0, 2).join('/')
: args.path.split('/')[0];
const version = dependencies[packageName];
const packagePath = args.path.slice(packageName.length);
if (version) {
const specifier = packagePath
? `${packageName}@${version}${packagePath}`
: `${packageName}@${version}`;
return {
path: `https://esm.sh/${specifier}`,
namespace: 'http',
};
}
});
// Handle relative or absolute imports inside esm.sh modules
build.onResolve({ filter: /.*/, namespace: 'http' }, (args) => {
+2 -1
View File
@@ -12,6 +12,7 @@ await esbuild.initialize({
export async function buildProject(fs: JSRuntimeFS, projectId: string): Promise<Record<string, Uint8Array>> {
const indexHtml = await fs.readFile(`/projects/${projectId}/index.html`, 'utf8');
const packageJson = await fs.readFile(`/projects/${projectId}/package.json`, 'utf8');
// Parse index.html to find the main script entry point
const entryPoints = parseEntryPoints(indexHtml);
@@ -29,7 +30,7 @@ export async function buildProject(fs: JSRuntimeFS, projectId: string): Promise<
jsx: 'automatic',
plugins: [
lightningFsPlugin(fs, `/projects/${projectId}/src`),
esmShPlugin(),
esmShPlugin(JSON.parse(packageJson).dependencies || {}),
],
});