Fix LightningFS resolver

This commit is contained in:
Alex Gleason committed 2025-08-07 14:45:48 -05:00
1 parent d3c4791295
commit 9622a33cbd
1 file changed
+27 -10
+27 -10
View File
@@ -11,11 +11,18 @@ export function lightningFsPlugin(fs: JSRuntimeFS, cwd: string): Plugin {
let resolved: string;
if (args.path.startsWith('@/')) {
// Handle alias paths
resolved = path.join(cwd, args.path.slice(2));
} else if (args.path.startsWith('/')) {
// Handle absolute paths
resolved = args.path;
} else if (args.importer) {
// Handle relative imports - use the directory of the importing file
const importerDir = path.dirname(args.importer);
resolved = path.join(importerDir, args.path);
} else {
resolved = path.join(cwd, args.resolveDir, args.path);
// Initial entry point or fallback to resolveDir
resolved = path.join(cwd, args.resolveDir || '', args.path);
}
try {
@@ -40,6 +47,8 @@ export function lightningFsPlugin(fs: JSRuntimeFS, cwd: string): Plugin {
return {
contents: await fs.readFile(args.path, 'utf8'),
loader: ext as Loader,
// Set resolveDir for subsequent imports from this file
resolveDir: path.dirname(args.path),
};
});
},
@@ -50,18 +59,24 @@ async function tryFileVariants(fs: JSRuntimeFS, basePath: string): Promise<strin
const extensions = ['.ts', '.tsx', '.js', '.jsx', '.css'];
try {
await fs.stat(basePath);
return basePath;
const stat = await fs.stat(basePath);
// If it's a file, return it directly
if (stat.isFile()) {
return basePath;
}
// If it's a directory, continue to check for index files
} catch {
// Ignore
// File doesn't exist, try with extensions
}
// Try direct file with extensions
for (const ext of extensions) {
const full = basePath + ext;
try {
await fs.stat(full);
return full;
const stat = await fs.stat(full);
if (stat.isFile()) {
return full;
}
} catch {
// Ignore
}
@@ -71,12 +86,14 @@ async function tryFileVariants(fs: JSRuntimeFS, basePath: string): Promise<strin
for (const ext of extensions) {
const indexFile = path.join(basePath, 'index' + ext);
try {
await fs.stat(indexFile);
return indexFile;
const stat = await fs.stat(indexFile);
if (stat.isFile()) {
return indexFile;
}
} catch {
// Ignore
}
}
throw new Error('File not found');
}
throw new Error(`File not found: ${basePath}`);
}