diff --git a/src/hooks/useIsProjectPreviewable.ts b/src/hooks/useIsProjectPreviewable.ts index 52f019bd..e3c919ae 100644 --- a/src/hooks/useIsProjectPreviewable.ts +++ b/src/hooks/useIsProjectPreviewable.ts @@ -3,7 +3,7 @@ import { useProjectsManager } from './useProjectsManager'; /** * Hook to check if a project is previewable - * A project is previewable if it has index.html, package.json, and package-lock.json + * A project is previewable if it has index.html and package.json */ export function useIsProjectPreviewable(projectId: string) { const projectsManager = useProjectsManager(); @@ -13,13 +13,12 @@ export function useIsProjectPreviewable(projectId: string) { queryFn: async () => { try { // Check for required files - const [hasIndexHtml, hasPackageJson, hasPackageLock] = await Promise.all([ + const [hasIndexHtml, hasPackageJson] = await Promise.all([ projectsManager.fileExists(projectId, 'index.html'), projectsManager.fileExists(projectId, 'package.json'), - projectsManager.fileExists(projectId, 'package-lock.json'), ]); - return hasIndexHtml && hasPackageJson && hasPackageLock; + return hasIndexHtml && hasPackageJson; } catch (error) { console.error('Failed to check if project is previewable:', error); return false; diff --git a/src/lib/build/index.test.ts b/src/lib/build/index.test.ts index b8f52ac9..b2bbe985 100644 --- a/src/lib/build/index.test.ts +++ b/src/lib/build/index.test.ts @@ -293,4 +293,119 @@ describe('buildProject', () => { // Should try to copy public files but directory is empty expect(copyFiles).toHaveBeenCalledWith(mockFS, mockFS, '/test/project/public', '/test/project/dist'); }); + + it('should support yarn.lock when package-lock.json is missing', async () => { + const yarnLock = ` +# yarn lockfile v1 + +react@^18.0.0: + version "18.2.0" + resolved "https://registry.yarnpkg.com/react/-/react-18.2.0.tgz" + integrity sha512-... + dependencies: + loose-envify "^1.1.0" +`; + + // 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')) { + throw new Error('File not found: package-lock.json'); + } + if (path.endsWith('yarn.lock')) { + return yarnLock; + } + 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 yarn.lock + expect(result.files['stdin-abc123.js']).toEqual(new Uint8Array([1, 2, 3])); + expect(result.files['index.html']).toBeDefined(); + + // Verify yarn.lock was read + expect(mockFS.readFile).toHaveBeenCalledWith('/test/project/yarn.lock', 'utf8'); + }); + + it('should work with no lock file at all', async () => { + // 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') || path.endsWith('yarn.lock')) { + throw new Error('File not found'); + } + 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 empty packages object + expect(result.files['stdin-abc123.js']).toEqual(new Uint8Array([1, 2, 3])); + expect(result.files['index.html']).toBeDefined(); + + // Verify both lock files were attempted to be read + expect(mockFS.readFile).toHaveBeenCalledWith('/test/project/package-lock.json', 'utf8'); + expect(mockFS.readFile).toHaveBeenCalledWith('/test/project/yarn.lock', 'utf8'); + }); }); \ No newline at end of file diff --git a/src/lib/build/index.ts b/src/lib/build/index.ts index a424c05b..920d4f86 100644 --- a/src/lib/build/index.ts +++ b/src/lib/build/index.ts @@ -6,6 +6,7 @@ import { addDomainToCSP } from "@/lib/csp"; import { shakespearePlugin } from "./shakespearePlugin"; import { esmPlugin } from "./esmPlugin"; import { fsPlugin } from "./fsPlugin"; +import { convertYarnLockToPackageLock } from "./yarnLockConverter"; import type { JSRuntimeFS } from '@/lib/JSRuntime'; @@ -28,10 +29,28 @@ async function bundle( `${projectPath}/index.html`, "utf8", ); - const packageLockText = await fs.readFile( - `${projectPath}/package-lock.json`, - "utf8", - ); + + // Try to read package-lock.json first, fall back to yarn.lock + let packageLock; + try { + const packageLockText = await fs.readFile( + `${projectPath}/package-lock.json`, + "utf8", + ); + packageLock = JSON.parse(packageLockText); + } catch { + // If package-lock.json doesn't exist, try yarn.lock + try { + const yarnLockText = await fs.readFile( + `${projectPath}/yarn.lock`, + "utf8", + ); + packageLock = convertYarnLockToPackageLock(yarnLockText); + } catch { + // If neither exists, use empty packages object + packageLock = { packages: {} }; + } + } const doc = domParser.parseFromString(indexHtmlText, "text/html"); const entryPoints: string[] = []; @@ -80,7 +99,7 @@ async function bundle( plugins: [ shakespearePlugin(), fsPlugin(fs, `${projectPath}/src`), - esmPlugin(JSON.parse(packageLockText), target), + esmPlugin(packageLock, target), ], define: { "import.meta.env": JSON.stringify({}), diff --git a/src/lib/build/yarnLockConverter.test.ts b/src/lib/build/yarnLockConverter.test.ts new file mode 100644 index 00000000..aa76eb62 --- /dev/null +++ b/src/lib/build/yarnLockConverter.test.ts @@ -0,0 +1,157 @@ +import { describe, it, expect } from 'vitest'; +import { convertYarnLockToPackageLock } from './yarnLockConverter'; + +describe('convertYarnLockToPackageLock', () => { + it('should convert a simple yarn.lock entry', () => { + const yarnLock = ` +# yarn lockfile v1 + +react@^18.0.0: + version "18.2.0" + resolved "https://registry.yarnpkg.com/react/-/react-18.2.0.tgz" + integrity sha512-... +`; + + const result = convertYarnLockToPackageLock(yarnLock); + + expect(result.packages['node_modules/react']).toEqual({ + name: 'react', + version: '18.2.0', + }); + }); + + it('should convert scoped packages', () => { + const yarnLock = ` +"@babel/core@^7.0.0": + version "7.23.0" + resolved "https://registry.yarnpkg.com/@babel/core/-/core-7.23.0.tgz" + integrity sha512-... +`; + + const result = convertYarnLockToPackageLock(yarnLock); + + expect(result.packages['node_modules/@babel/core']).toEqual({ + name: '@babel/core', + version: '7.23.0', + }); + }); + + it('should parse dependencies', () => { + const yarnLock = ` +react-dom@^18.0.0: + version "18.2.0" + resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-18.2.0.tgz" + integrity sha512-... + dependencies: + loose-envify "^1.1.0" + scheduler "^0.23.0" +`; + + const result = convertYarnLockToPackageLock(yarnLock); + + expect(result.packages['node_modules/react-dom']).toEqual({ + name: 'react-dom', + version: '18.2.0', + dependencies: { + 'loose-envify': '^1.1.0', + 'scheduler': '^0.23.0', + }, + }); + }); + + it('should parse peer dependencies', () => { + const yarnLock = ` +"@testing-library/react@^14.0.0": + version "14.0.0" + resolved "https://registry.yarnpkg.com/@testing-library/react/-/react-14.0.0.tgz" + integrity sha512-... + peerDependencies: + react "^18.0.0" + react-dom "^18.0.0" +`; + + const result = convertYarnLockToPackageLock(yarnLock); + + expect(result.packages['node_modules/@testing-library/react']).toEqual({ + name: '@testing-library/react', + version: '14.0.0', + peerDependencies: { + react: '^18.0.0', + 'react-dom': '^18.0.0', + }, + }); + }); + + it('should handle multiple packages', () => { + const yarnLock = ` +react@^18.0.0: + version "18.2.0" + resolved "https://registry.yarnpkg.com/react/-/react-18.2.0.tgz" + +react-dom@^18.0.0: + version "18.2.0" + resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-18.2.0.tgz" + dependencies: + react "^18.0.0" +`; + + const result = convertYarnLockToPackageLock(yarnLock); + + expect(result.packages['node_modules/react']).toEqual({ + name: 'react', + version: '18.2.0', + }); + + expect(result.packages['node_modules/react-dom']).toEqual({ + name: 'react-dom', + version: '18.2.0', + dependencies: { + react: '^18.0.0', + }, + }); + }); + + it('should handle npm alias syntax', () => { + const yarnLock = ` +"package@npm:other-package@^1.0.0": + version "1.2.3" + resolved "https://registry.yarnpkg.com/other-package/-/other-package-1.2.3.tgz" +`; + + const result = convertYarnLockToPackageLock(yarnLock); + + expect(result.packages['node_modules/package']).toEqual({ + name: 'package', + version: '1.2.3', + }); + }); + + it('should skip entries without version', () => { + const yarnLock = ` +invalid-entry@^1.0.0: + resolved "https://registry.yarnpkg.com/invalid/-/invalid-1.0.0.tgz" + +valid-entry@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/valid/-/valid-1.0.0.tgz" +`; + + const result = convertYarnLockToPackageLock(yarnLock); + + expect(result.packages['node_modules/invalid-entry']).toBeUndefined(); + expect(result.packages['node_modules/valid-entry']).toEqual({ + name: 'valid-entry', + version: '1.0.0', + }); + }); + + it('should handle empty yarn.lock', () => { + const yarnLock = ` +# yarn lockfile v1 +`; + + const result = convertYarnLockToPackageLock(yarnLock); + + expect(result.packages).toEqual({}); + }); +}); diff --git a/src/lib/build/yarnLockConverter.ts b/src/lib/build/yarnLockConverter.ts new file mode 100644 index 00000000..b2f89db2 --- /dev/null +++ b/src/lib/build/yarnLockConverter.ts @@ -0,0 +1,172 @@ +/** + * Converts yarn.lock format to package-lock.json format for use with esmPlugin. + * This is a simplified converter that extracts enough information for dependency resolution. + */ + +interface YarnLockEntry { + version: string; + dependencies?: Record