Use tsconfig to resolve paths

This commit is contained in:
Alex Gleason committed 2025-10-06 11:18:57 -05:00
1 parent d0c039b37b
commit 00d4b9aaac
5 files changed
+105 -8

No files matched your search

+7
View File
@@ -60,6 +60,7 @@
"ignore": "^7.0.5",
"input-otp": "^1.2.4",
"isomorphic-git": "^1.32.1",
"jsonc-parser": "^3.3.1",
"jszip": "^3.10.1",
"lucide-react": "^0.525.0",
"nostr-tools": "^2.15.2",
@@ -6892,6 +6893,12 @@
"dev": true,
"license": "MIT"
},
"node_modules/jsonc-parser": {
"version": "3.3.1",
"resolved": "https://registry.npmjs.org/jsonc-parser/-/jsonc-parser-3.3.1.tgz",
"integrity": "sha512-HUgH65KyejrUFPvHFPbqOY0rsFip3Bo5wb4ngvdi1EpCYWUQDC5V+Y7mZws+DLkr4M//zQJoanu1SP+87Dv1oQ==",
"license": "MIT"
},
"node_modules/jszip": {
"version": "3.10.1",
"resolved": "https://registry.npmjs.org/jszip/-/jszip-3.10.1.tgz",
+1
View File
@@ -63,6 +63,7 @@
"ignore": "^7.0.5",
"input-otp": "^1.2.4",
"isomorphic-git": "^1.32.1",
"jsonc-parser": "^3.3.1",
"jszip": "^3.10.1",
"lucide-react": "^0.525.0",
"nostr-tools": "^2.15.2",
+20 -7
View File
@@ -3,7 +3,21 @@ import { join, dirname, extname } from "path-browserify";
import type { Loader, Plugin } from "esbuild-wasm";
import type { JSRuntimeFS } from '@/lib/JSRuntime';
export function fsPlugin(fs: JSRuntimeFS, cwd: string): Plugin {
interface TsConfig {
compilerOptions?: {
baseUrl?: string;
paths?: Record<string, string[]>;
};
}
interface FsPluginOptions {
fs: JSRuntimeFS;
cwd: string;
tsconfig?: TsConfig;
}
export function fsPlugin(options: FsPluginOptions): Plugin {
const { fs, cwd, tsconfig } = options;
return {
name: "fs",
@@ -25,18 +39,17 @@ export function fsPlugin(fs: JSRuntimeFS, cwd: string): Plugin {
if (args.path.startsWith("/")) {
resolved = args.path;
} else if (args.path.startsWith("@/")) {
resolved = join(cwd, args.path.slice(2));
} else if (args.kind === "entry-point") {
resolved = join(cwd, args.path);
} else if (args.importer) {
resolved = join(cwd, "src", args.path.slice(2));
} else if (args.importer && (args.path.startsWith("./") || args.path.startsWith("../"))) {
resolved = join(dirname(args.importer), args.path);
} else if (args.importer && tsconfig?.compilerOptions?.baseUrl) {
resolved = join(cwd, tsconfig.compilerOptions.baseUrl, args.path);
} else {
return;
}
// Security: prevent accessing files outside the project directory
const projectDir = join(cwd, "..");
if (resolved.startsWith("/") && !resolved.startsWith(projectDir)) {
if (resolved.startsWith("/") && !resolved.startsWith(cwd)) {
throw new Error(`Access to files outside the project directory is not allowed: ${resolved}`);
}
+62
View File
@@ -466,4 +466,66 @@ react@^18.0.0:
// Verify package.json was read
expect(mockFS.readFile).toHaveBeenCalledWith('/test/project/package.json', 'utf8');
});
it('should read tsconfig.json when available', async () => {
const tsconfigContent = JSON.stringify({
compilerOptions: {
baseUrl: "./src",
paths: {
"@/*": ["*"]
}
}
});
// Mock required files
vi.mocked(mockFS.readFile)
.mockImplementation(async (path: string) => {
if (path.endsWith('package.json')) {
return '{"name": "test-project"}';
}
if (path.endsWith('index.html')) {
return '<html><head></head><body><script src="/src/main.tsx"></script></body></html>';
}
if (path.endsWith('package-lock.json')) {
return '{"dependencies": {}}';
}
if (path.endsWith('tsconfig.json')) {
return tsconfigContent;
}
throw new Error(`File not found: ${path}`);
});
// Mock stat to indicate no public directory
vi.mocked(mockFS.stat)
.mockImplementation(async (path: string) => {
if (path === '/test/project/public') {
throw new Error('Directory not found');
}
return { isDirectory: () => false, isFile: () => true };
});
// Mock additional filesystem operations for buildProject
vi.mocked(mockFS.readdir).mockImplementation(async (path: string) => {
if (path === '/test/project/dist') {
return [];
}
return [];
});
vi.mocked(mockFS.mkdir).mockResolvedValue(undefined);
vi.mocked(mockFS.writeFile).mockResolvedValue(undefined);
const result = await buildProject({
fs: mockFS,
projectPath: '/test/project',
domParser: mockDOMParser,
});
// Should successfully build with tsconfig.json
expect(result.files['stdin-abc123.js']).toEqual(new Uint8Array([1, 2, 3]));
expect(result.files['index.html']).toBeDefined();
// Verify tsconfig.json was attempted to be read
expect(mockFS.readFile).toHaveBeenCalledWith('/test/project/tsconfig.json', 'utf8');
});
});
+15 -1
View File
@@ -1,3 +1,4 @@
import * as JSONC from "jsonc-parser";
import { join } from "path-browserify";
import { getEsbuild } from "@/lib/esbuild";
import { copyFiles } from "@/lib/copyFiles";
@@ -37,6 +38,19 @@ async function bundle(
);
const packageJson = JSON.parse(packageJsonText);
// Try to read tsconfig.json
let tsconfig;
try {
const tsconfigText = await fs.readFile(
`${projectPath}/tsconfig.json`,
"utf8",
);
tsconfig = JSONC.parse(tsconfigText);
} catch {
// tsconfig.json is optional
tsconfig = undefined;
}
// Try to read package-lock.json first, fall back to yarn.lock
let packageLock;
let buildMode: string;
@@ -111,7 +125,7 @@ async function bundle(
assetNames: "[name]-[hash]",
plugins: [
shakespearePlugin(),
fsPlugin(fs, `${projectPath}/src`),
fsPlugin({ fs, cwd: projectPath, tsconfig }),
esmPlugin({ packageJson, packageLock, target }),
],
define: {