diff --git a/src/lib/build/esmPlugin.test.ts b/src/lib/build/esmPlugin.test.ts index 41cd1130..7efc20a3 100644 --- a/src/lib/build/esmPlugin.test.ts +++ b/src/lib/build/esmPlugin.test.ts @@ -2,8 +2,50 @@ import { describe, it, expect, vi, afterEach } from "vitest"; import { isEffectivelyEmptyModule, resolveEmptyModuleFallback, + stripAssetUrlWildcard, } from "./esmPlugin"; +describe("stripAssetUrlWildcard", () => { + it("strips esm.sh's leading /* wildcard prefix from asset URLs", () => { + expect( + stripAssetUrlWildcard( + "https://esm.sh/*@fontsource/playfair-display@5.2.8/files/x.woff2", + ), + ).toBe("https://esm.sh/@fontsource/playfair-display@5.2.8/files/x.woff2"); + }); + + it("preserves the query string (e.g. the lp breadcrumb)", () => { + expect( + stripAssetUrlWildcard( + "https://esm.sh/*@fontsource/foo@1/files/y.woff?lp=node_modules%252F%2540fontsource%252Ffoo", + ), + ).toBe( + "https://esm.sh/@fontsource/foo@1/files/y.woff?lp=node_modules%252F%2540fontsource%252Ffoo", + ); + }); + + it("leaves URLs without a wildcard prefix unchanged", () => { + const url = "https://esm.sh/@fontsource/foo@1/files/y.woff2?lp=abc"; + expect(stripAssetUrlWildcard(url)).toBe(url); + }); + + it("only strips a leading /* in the path, not a * elsewhere", () => { + const url = "https://esm.sh/@scope/pkg@1/a*b/file.woff2"; + expect(stripAssetUrlWildcard(url)).toBe(url); + }); + + it("returns the input unchanged when it is not a valid URL", () => { + expect(stripAssetUrlWildcard("not a url")).toBe("not a url"); + }); + + it("produces a url() value with no literal /* (the Tailwind tokenizer trap)", () => { + const fixed = stripAssetUrlWildcard( + "https://esm.sh/*@fontsource/playfair-display@5.2.8/files/x.woff2", + ); + expect(`src: url(${fixed})`).not.toContain("/*"); + }); +}); + describe("isEffectivelyEmptyModule", () => { it("treats a file with only source-map comments as empty", () => { const source = diff --git a/src/lib/build/esmPlugin.ts b/src/lib/build/esmPlugin.ts index 0b6f9b10..ae53b64a 100644 --- a/src/lib/build/esmPlugin.ts +++ b/src/lib/build/esmPlugin.ts @@ -416,7 +416,10 @@ export function esmPlugin(options: EsmPluginOptions): Plugin { const ext = extname(fullURL.pathname).slice(1); if (ext && !["ts", "tsx", "js", "jsx", "mjs", "cjs", "css", "json"].includes(ext)) { return { - path: fullURL.toString(), + // Strip esm.sh's `/*` wildcard prefix so the URL is safe to embed + // verbatim in CSS `url()` (a literal `/*` there is misread as a CSS + // comment by some parsers, including the Tailwind v4 runtime). + path: stripAssetUrlWildcard(fullURL.toString()), external: true, }; } @@ -491,8 +494,11 @@ export function esmPlugin(options: EsmPluginOptions): Plugin { // Convert `?url` imports to URL strings pointing to the ESM CDN build.onLoad({ filter: /.*/, namespace: "esm-file-external" }, (args) => { + // Strip esm.sh's `/*` wildcard prefix: these URLs are asset references + // that may be embedded in CSS, where a literal `/*` in `url()` breaks + // some CSS parsers (including the Tailwind v4 runtime). return { - contents: `export default ${JSON.stringify(args.path)};`, + contents: `export default ${JSON.stringify(stripAssetUrlWildcard(args.path))};`, loader: "js", }; }); @@ -524,6 +530,37 @@ function extractPackageName(esmURL: string): { pkg?: string; version?: string } return { pkg, version }; } +/** + * Remove esm.sh's leading `/*` wildcard prefix from an asset URL's path. + * + * Shakespeare resolves CDN modules through esm.sh's `*` ("externalize all + * dependencies") prefix, e.g. `https://esm.sh/*@fontsource/x@1/...`. That `*` + * only influences how esm.sh builds JavaScript modules; for raw static assets + * (fonts, images, wasm, …) it is meaningless and esm.sh serves the identical + * file with or without it. + * + * The prefix is harmful when the asset URL is written verbatim into CSS: + * `src: url(https://esm.sh/*@fontsource/...woff2)` contains a literal `/*` + * inside an unquoted `url()`, which several CSS parsers (notably the in-browser + * Tailwind v4 tokenizer) misread as the start of a CSS comment. The + * tokenizer then scans past the rule's closing `}`, leaving the `@font-face` + * rule unterminated and throwing "Missing closing } at font-face". + * + * Stripping the wildcard from asset URLs keeps the emitted CSS parseable by all + * CSS parsers. JS module URLs are left untouched (they are not passed through + * this helper) since they legitimately rely on the `*` prefix. + */ +export function stripAssetUrlWildcard(urlStr: string): string { + try { + const url = new URL(urlStr); + if (!url.pathname.startsWith("/*")) return urlStr; + url.pathname = url.pathname.replace(/^\/\*/, "/"); + return url.toString(); + } catch { + return urlStr; + } +} + /** * Determine whether a chunk of JavaScript is an "empty" runtime module — i.e. * it contains no executable code and no ES `import`/`export` statements once