From b3ce6979c47693f47e07f7e5aa797ec8d1b4646b Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Mon, 11 Aug 2025 15:32:29 -0700 Subject: [PATCH] Load Tailwind config --- src/lib/build/esmPlugin.ts | 8 ++++++++ src/lib/build/index.ts | 37 ++++++++++++++++--------------------- 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/src/lib/build/esmPlugin.ts b/src/lib/build/esmPlugin.ts index dbb1894d..6c2d0c9b 100644 --- a/src/lib/build/esmPlugin.ts +++ b/src/lib/build/esmPlugin.ts @@ -36,6 +36,14 @@ export function esmPlugin(packageLock: PackageLock, target?: string): Plugin { name: "esm", setup(build) { + // http(s) imports + build.onResolve({ filter: /^https?:\/\// }, async (args) => { + return { + path: args.path, + namespace: "esm", + }; + }); + // Handle bare imports like "react" build.onResolve({ filter: /^[^./].*/ }, (args) => { const packageName = args.path.startsWith("@") diff --git a/src/lib/build/index.ts b/src/lib/build/index.ts index 01bdd45d..f2cfd934 100644 --- a/src/lib/build/index.ts +++ b/src/lib/build/index.ts @@ -36,7 +36,15 @@ export async function buildProject( const doc = domParser.parseFromString(indexHtmlText, "text/html"); const results = await esbuild.build({ - entryPoints: ["./main.tsx"], + stdin: { + contents: ` + import "@/main.tsx"; + import tailwindConfig from "@/../tailwind.config.ts"; + import "https://esm.sh/tailwindcss-cdn@3"; + tailwind.config = tailwindConfig; + `, + loader: "tsx", + }, bundle: true, write: false, format: "esm", @@ -53,7 +61,13 @@ export async function buildProject( for (const file of results.outputFiles) { const relativePath = file.path.replace(/^\//, ""); - dist[relativePath] = file.contents; + if (relativePath === "stdin.js") { + dist["main.js"] = file.contents; + } else if (relativePath === "stdin.css") { + dist["main.css"] = file.contents; + } else { + dist[relativePath] = file.contents; + } } // Update index.html with proper script and link tags @@ -78,23 +92,4 @@ function updateIndexHtml(doc: Document): void { style.setAttribute("rel", "stylesheet"); style.setAttribute("href", "/main.css"); doc.head.appendChild(style); - - const tailwindScript = doc.createElement("script"); - tailwindScript.setAttribute( - "src", - "https://esm.sh/@tailwindcss/browser@4", - ); - tailwindScript.setAttribute("type", "module"); - doc.head.appendChild(tailwindScript); - - const csp = doc.querySelector('meta[http-equiv="content-security-policy"]'); - if (csp) { - csp.setAttribute( - "content", - csp.getAttribute("content")?.replace( - "script-src 'self'", - "script-src 'self' https://esm.sh" - ) ?? "" - ); - } } \ No newline at end of file