Files
shakespeare/src/lib/build/esmPlugin.test.ts
T
Alex Gleason 317c923532 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.
2026-06-05 10:34:38 -05:00

175 lines
5.6 KiB
TypeScript

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 =
"\n//# sourceMappingURL=index.browser.mjs.map\n//# sourceMappingURL=index.browser.mjs.map";
expect(isEffectivelyEmptyModule(source)).toBe(true);
});
it("treats a jsDelivr-bannered empty bundle as empty", () => {
const source = [
"/**",
" * Bundled by jsDelivr using Rollup v2.79.2 and Terser v5.48.0.",
" * Original file: /npm/@solana/[email protected]/dist/index.browser.mjs",
" */",
"//# sourceMappingURL=/sm/abc.map",
].join("\n");
expect(isEffectivelyEmptyModule(source)).toBe(true);
});
it("treats a 'use strict' prologue with no code as empty", () => {
expect(isEffectivelyEmptyModule("'use strict';\n//# sourceMappingURL=x.map")).toBe(true);
});
it("treats a bare `export {};` as empty", () => {
expect(isEffectivelyEmptyModule("export {};")).toBe(true);
});
it("does not treat a module with real exports as empty", () => {
expect(isEffectivelyEmptyModule("export const x = 1;")).toBe(false);
});
it("does not treat a module with imports as empty", () => {
expect(isEffectivelyEmptyModule("import './side-effect.js';")).toBe(false);
});
it("does not treat a module with executable code as empty", () => {
expect(isEffectivelyEmptyModule("console.log('hi');")).toBe(false);
});
});
describe("resolveEmptyModuleFallback", () => {
afterEach(() => {
vi.restoreAllMocks();
});
it("synthesizes an empty module when jsDelivr runtime is empty", async () => {
const fetchMock = vi.fn(async () => ({
ok: true,
text: async () => "\n//# sourceMappingURL=index.browser.mjs.map",
})) as unknown as typeof fetch;
vi.stubGlobal("fetch", fetchMock);
const result = await resolveEmptyModuleFallback(
"https://esm.sh/*@solana/rpc-parsed-types?target=esnext",
new Map(),
);
expect(result).toBe("export {};\n");
expect(fetchMock).toHaveBeenCalledWith(
"https://cdn.jsdelivr.net/npm/@solana/rpc-parsed-types/+esm",
);
});
it("recovers the version from the lockfile index", async () => {
const fetchMock = vi.fn(async () => ({
ok: true,
text: async () => "//# sourceMappingURL=x.map",
})) as unknown as typeof fetch;
vi.stubGlobal("fetch", fetchMock);
const nameVersionToPaths = new Map<string, string[]>([
["@solana/[email protected]", ["node_modules/@solana/rpc-parsed-types"]],
]);
await resolveEmptyModuleFallback(
"https://esm.sh/*@solana/rpc-parsed-types?target=esnext",
nameVersionToPaths,
);
expect(fetchMock).toHaveBeenCalledWith(
"https://cdn.jsdelivr.net/npm/@solana/[email protected]/+esm",
);
});
it("returns undefined for a non-empty package (does not mask real failures)", async () => {
const fetchMock = vi.fn(async () => ({
ok: true,
text: async () => "export const isNumber = () => true;",
})) as unknown as typeof fetch;
vi.stubGlobal("fetch", fetchMock);
const result = await resolveEmptyModuleFallback(
"https://esm.sh/*[email protected]?target=esnext",
new Map(),
);
expect(result).toBeUndefined();
});
it("returns undefined when the jsDelivr fallback also fails", async () => {
const fetchMock = vi.fn(async () => ({
ok: false,
text: async () => "",
})) as unknown as typeof fetch;
vi.stubGlobal("fetch", fetchMock);
const result = await resolveEmptyModuleFallback(
"https://esm.sh/*@solana/rpc-parsed-types?target=esnext",
new Map(),
);
expect(result).toBeUndefined();
});
it("does not attempt recovery for non-JS asset requests", async () => {
const fetchMock = vi.fn();
vi.stubGlobal("fetch", fetchMock);
const result = await resolveEmptyModuleFallback(
"https://esm.sh/[email protected]/styles.css?target=esnext",
new Map(),
);
expect(result).toBeUndefined();
expect(fetchMock).not.toHaveBeenCalled();
});
});