From 00d4b9aaac19b4c57d31ec8823feba8e0c3f462e Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Mon, 6 Oct 2025 11:18:57 -0500 Subject: [PATCH] Use tsconfig to resolve paths --- package-lock.json | 7 +++++ package.json | 1 + src/lib/build/fsPlugin.ts | 27 +++++++++++----- src/lib/build/index.test.ts | 62 +++++++++++++++++++++++++++++++++++++ src/lib/build/index.ts | 16 +++++++++- 5 files changed, 105 insertions(+), 8 deletions(-) diff --git a/package-lock.json b/package-lock.json index 8aea64c8..0b3bdaf8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index cfb24d13..20cf933c 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/lib/build/fsPlugin.ts b/src/lib/build/fsPlugin.ts index a6abf2ea..553a2aa2 100644 --- a/src/lib/build/fsPlugin.ts +++ b/src/lib/build/fsPlugin.ts @@ -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; + }; +} + +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}`); } diff --git a/src/lib/build/index.test.ts b/src/lib/build/index.test.ts index bc202928..865ece6f 100644 --- a/src/lib/build/index.test.ts +++ b/src/lib/build/index.test.ts @@ -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 ''; + } + 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'); + }); }); \ No newline at end of file diff --git a/src/lib/build/index.ts b/src/lib/build/index.ts index a2d34e8e..933c36f1 100644 --- a/src/lib/build/index.ts +++ b/src/lib/build/index.ts @@ -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: {