mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-03 07:57:22 +00:00
Fix font @font-face runtime error by stripping esm.sh /* from asset URLs
Root cause of the Tailwind v4 'Missing closing } at font-face' runtime error: font assets imported via a CSS @import (e.g. @fontsource packages) were emitted as url(https://esm.sh/*@fontsource/...woff2). The /* from esm.sh's wildcard ('externalize all deps') prefix sits inside an unquoted url(), and the in-browser Tailwind v4 tokenizer misreads it as the start of a CSS comment, scanning past the rule's closing } and throwing. esm.sh's '*' only affects JS module builds; for raw static assets it is meaningless and the same file is served without it. Strip the '/*' prefix from externalized asset URLs (and ?url asset imports) so emitted CSS url() values are parseable by all CSS parsers, not just the ones tolerant of '/*' inside url(). JS module URLs are untouched. Adds unit tests for stripAssetUrlWildcard.
This commit is contained in:
2 files changed
+81
-2
No files matched your search
@@ -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/[email protected]/files/x.woff2",
|
||||
),
|
||||
).toBe("https://esm.sh/@fontsource/[email protected]/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/[email protected]/files/x.woff2",
|
||||
);
|
||||
expect(`src: url(${fixed})`).not.toContain("/*");
|
||||
});
|
||||
});
|
||||
|
||||
describe("isEffectivelyEmptyModule", () => {
|
||||
it("treats a file with only source-map comments as empty", () => {
|
||||
const source =
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user