diff --git a/src/lib/build/index.ts b/src/lib/build/index.ts index d63fe62a..963f0864 100644 --- a/src/lib/build/index.ts +++ b/src/lib/build/index.ts @@ -13,16 +13,20 @@ await esbuild.initialize({ export async function buildProject(fs: JSRuntimeFS, projectId: string): Promise> { const indexHtml = await fs.readFile(`/projects/${projectId}/index.html`, 'utf8'); - // TODO: parse indexHtml to find entryPoints - // The file will have an absolute path like `/src/main.tsx` which must be converted to a relative path like `main.tsx` + // Parse index.html to find the main script entry point + const entryPoints = parseEntryPoints(indexHtml); + if (!entryPoints.length) { + throw new Error('No entry point script found in index.html'); + } const results = await esbuild.build({ - entryPoints: ['main.tsx'], + entryPoints, bundle: true, write: false, format: 'esm', target: 'esnext', outdir: 'dist', + jsx: 'automatic', plugins: [ lightningFsPlugin(fs, `/projects/${projectId}/src`), esmShPlugin(), @@ -31,13 +35,77 @@ export async function buildProject(fs: JSRuntimeFS, projectId: string): Promise< const dist: Record = {}; + // Process output files + const jsFiles: string[] = []; + const cssFiles: string[] = []; + for (const file of results.outputFiles) { const relativePath = file.path.replace(/^\/dist\//, ''); dist[relativePath] = file.contents; + + // Track JS and CSS files for HTML injection + if (relativePath.endsWith('.js')) { + jsFiles.push(relativePath); + } else if (relativePath.endsWith('.css')) { + cssFiles.push(relativePath); + } } - // FIXME: We need to ensure that dist files are imported inside the index.html. If a file was processed from an existing tag, its path might need to be updated, otherwise it might need to be imported (eg a css output file due to css import in js) - dist['index.html'] = new TextEncoder().encode(indexHtml); + // Update index.html with proper script and link tags + const updatedHtml = updateIndexHtml(indexHtml, jsFiles, cssFiles); + dist['index.html'] = new TextEncoder().encode(updatedHtml); return dist; +} + +function parseEntryPoints(html: string): string[] { + const parser = new DOMParser(); + const doc = parser.parseFromString(html, 'text/html'); + + // Look for script tags with type="module" and src attribute + const scripts = doc.getElementsByTagName('script'); + + return Array.from(scripts) + .filter((script) => new URL(script.src).origin === location.origin) // Only include same-origin scripts + .map((script) => new URL(script.src).pathname.replace(/^\/src\//, '')); +} + +function updateIndexHtml(originalHtml: string, jsFiles: string[], cssFiles: string[]): string { + const parser = new DOMParser(); + const doc = parser.parseFromString(originalHtml, 'text/html'); + + // Remove existing script tags that reference our built files + for (const elem of doc.getElementsByTagName('script')) { + if (new URL(elem.src).origin === location.origin) { + elem.remove(); + } + } + + // Remove existing link tags that reference our built CSS files + for (const elem of doc.getElementsByTagName('link')) { + if (elem.rel === 'stylesheet' && new URL(elem.href).origin === location.origin) { + elem.remove(); + } + } + + // Add CSS files to the head + const head = doc.head; + cssFiles.forEach((file) => { + const link = doc.createElement('link'); + link.rel = 'stylesheet'; + link.href = `/${file}`; + head.appendChild(link); + }); + + // Add JS files before the closing body tag + const body = doc.body; + jsFiles.forEach((file) => { + const script = doc.createElement('script'); + script.type = 'module'; + script.src = `/${file}`; + script.defer = true; + body.appendChild(script); + }); + + return '\n' + doc.documentElement.outerHTML; } \ No newline at end of file