diff --git a/src/presentation/styles/theme.css b/src/presentation/styles/theme.css index c0df5bc5..da311588 100644 --- a/src/presentation/styles/theme.css +++ b/src/presentation/styles/theme.css @@ -1,4 +1,6 @@ -@import "tailwindcss"; +/* Tailwind takes class names from src/ only (1112/06-03): words in docs, tests + and scripts must not add rules to the stylesheet every page waits for. */ +@import "tailwindcss" source("../../"); @import "../design-system/tokens/primitive.css"; @import "../design-system/tokens/semantic.css"; @import "../design-system/tokens/component.css"; diff --git a/tests/fixtures/tailwind-source/outside-src.txt b/tests/fixtures/tailwind-source/outside-src.txt new file mode 100644 index 00000000..90026325 --- /dev/null +++ b/tests/fixtures/tailwind-source/outside-src.txt @@ -0,0 +1,4 @@ +A Tailwind utility written outside src/ (1112/06-03). The production +stylesheet must not contain it: Tailwind scans only src/ for class names. + +outline-[3.21px] diff --git a/tests/unit/design-system-initial-chunk.test.ts b/tests/unit/design-system-initial-chunk.test.ts index 4495208a..41167d03 100644 --- a/tests/unit/design-system-initial-chunk.test.ts +++ b/tests/unit/design-system-initial-chunk.test.ts @@ -38,19 +38,30 @@ afterAll(async () => { await Promise.all(roots.map((root) => rm(root, { recursive: true, force: true }))); }); +let built: Promise | undefined; + +/** One production build for the file's tests. */ +function build(): Promise { + built ??= (async () => { + const outDir = await mkdtemp(path.join(tmpdir(), "design-system-chunk-")); + roots.push(outDir); + const environment = Object.fromEntries( + Object.entries(process.env).filter(([name]) => !name.startsWith("VITE_")), + ); + const run = spawnSync( + path.resolve("node_modules/.bin/vite"), + ["build", "--outDir", outDir, "--emptyOutDir", "--logLevel", "error"], + { encoding: "utf8", env: environment }, + ); + expect(run.stderr).toBe(""); + expect(run.status).toBe(0); + return outDir; + })(); + return built; +} + async function initialJavascript(): Promise { - const outDir = await mkdtemp(path.join(tmpdir(), "design-system-chunk-")); - roots.push(outDir); - const environment = Object.fromEntries( - Object.entries(process.env).filter(([name]) => !name.startsWith("VITE_")), - ); - const run = spawnSync( - path.resolve("node_modules/.bin/vite"), - ["build", "--outDir", outDir, "--emptyOutDir", "--logLevel", "error"], - { encoding: "utf8", env: environment }, - ); - expect(run.stderr).toBe(""); - expect(run.status).toBe(0); + const outDir = await build(); const manifest = JSON.parse( await readFile(path.join(outDir, ".vite/manifest.json"), "utf8"), ) as Parameters[0]; @@ -73,6 +84,21 @@ describe("design-system code in the initial JavaScript (1101/08-08)", () => { expect(LAZY_ONLY_CLASSES.filter((name) => renders(code, name))).toEqual([]); }, childProcessTimeout(4_556)); + // 1112/06-03. Tailwind scans the files it is pointed at for class names. + // Words in docs, tests and scripts must not add rules to the stylesheet + // every page blocks on (tests/fixtures/tailwind-source/outside-src.txt). + it("generates utilities from src only, not from docs, tests or scripts", async () => { + const outDir = await build(); + const assets = path.join(outDir, "assets"); + const stylesheets = (await readdir(assets)).filter((file) => file.endsWith(".css")); + const css = ( + await Promise.all(stylesheets.map((file) => readFile(path.join(assets, file), "utf8"))) + ).join("\n"); + + expect(css).toContain(".ui-button"); + expect(css).not.toContain("3.21px"); + }, childProcessTimeout(4_556)); + // vite.config.ts declares these modules free of side effects, so the bundler // drops a module none of whose exports is used. An import kept only for its // effect (a stylesheet, a polyfill, a registration) would vanish with it.