|
| 1 | +import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; |
| 2 | +import { tmpdir } from "node:os"; |
| 3 | +import { join, resolve } from "node:path"; |
| 4 | +import { describe, expect, it } from "vitest"; |
| 5 | +import config, { stableStylesCssPlugin } from "./vite.config"; |
| 6 | + |
| 7 | +const packageJson = JSON.parse(readFileSync(resolve(__dirname, "package.json"), "utf8")) as { |
| 8 | + publishConfig: { exports: Record<string, string> }; |
| 9 | +}; |
| 10 | +const subpaths = JSON.parse(readFileSync(resolve(__dirname, "package-subpaths.json"), "utf8")) as { |
| 11 | + subpaths: Record<string, { runtime: string }>; |
| 12 | +}; |
| 13 | + |
| 14 | +// Rollup's own asset/chunk types aren't imported here; the plugin only reads |
| 15 | +// `type` and `fileName`, so a minimal shape is enough to drive it. |
| 16 | +function asset(fileName: string): { type: "asset"; fileName: string } { |
| 17 | + return { type: "asset", fileName }; |
| 18 | +} |
| 19 | +function chunk(fileName: string): { type: "chunk"; fileName: string } { |
| 20 | + return { type: "chunk", fileName }; |
| 21 | +} |
| 22 | + |
| 23 | +function callWriteBundle(dir: string, bundle: Record<string, unknown>): void { |
| 24 | + const writeBundle = stableStylesCssPlugin().writeBundle as ( |
| 25 | + options: { dir: string }, |
| 26 | + bundle: Record<string, unknown>, |
| 27 | + ) => void; |
| 28 | + writeBundle({ dir }, bundle); |
| 29 | +} |
| 30 | + |
| 31 | +describe("build.rollupOptions", () => { |
| 32 | + it("does not override assetFileNames, so every asset stays content-hashed", () => { |
| 33 | + // Regression for the cache bug this PR fixed: /assets/* is served with a |
| 34 | + // one-year immutable Cache-Control by filename convention alone |
| 35 | + // (packages/cli/src/server/studioServer.ts), so an unhashed name there |
| 36 | + // sticks to every CLI user's cache for a year. |
| 37 | + expect(config.build?.rollupOptions?.output).toBeUndefined(); |
| 38 | + }); |
| 39 | +}); |
| 40 | + |
| 41 | +describe("./styles.css export path", () => { |
| 42 | + it("resolves outside dist/assets, so it never inherits the immutable cache header", () => { |
| 43 | + const publishedPath = packageJson.publishConfig.exports["./styles.css"]; |
| 44 | + const runtimePath = subpaths.subpaths["./styles.css"]?.runtime; |
| 45 | + expect(publishedPath).toBe("./dist/styles.css"); |
| 46 | + expect(runtimePath).toBe("./dist/styles.css"); |
| 47 | + for (const path of [publishedPath, runtimePath]) { |
| 48 | + expect(path?.startsWith("./dist/assets/")).toBe(false); |
| 49 | + } |
| 50 | + }); |
| 51 | +}); |
| 52 | + |
| 53 | +describe("stableStylesCssPlugin", () => { |
| 54 | + function tmpDir(): string { |
| 55 | + return mkdtempSync(join(tmpdir(), "styles-css-plugin-")); |
| 56 | + } |
| 57 | + |
| 58 | + it("copies the single CSS asset, unhashed, to dist root, ignoring non-asset bundle entries", () => { |
| 59 | + const dir = tmpDir(); |
| 60 | + try { |
| 61 | + writeFileSync(join(dir, "app-abc123.css"), "body{color:red}"); |
| 62 | + callWriteBundle(dir, { |
| 63 | + "app-abc123.css": asset("app-abc123.css"), |
| 64 | + "app-def456.js": chunk("app-def456.js"), |
| 65 | + // A chunk can't really carry a `.css` name, but proving the `type` |
| 66 | + // guard (not just the filename suffix) does the filtering keeps the |
| 67 | + // count right if a future asset kind ever ends in `.css` too. |
| 68 | + "fake.css": chunk("fake.css"), |
| 69 | + }); |
| 70 | + expect(readFileSync(join(dir, "styles.css"), "utf8")).toBe("body{color:red}"); |
| 71 | + } finally { |
| 72 | + rmSync(dir, { recursive: true, force: true }); |
| 73 | + } |
| 74 | + }); |
| 75 | + |
| 76 | + it("throws instead of guessing when the build emits zero or several CSS assets", () => { |
| 77 | + const dir = tmpDir(); |
| 78 | + try { |
| 79 | + expect(() => callWriteBundle(dir, { "app.js": chunk("app.js") })).toThrow(/found 0/); |
| 80 | + writeFileSync(join(dir, "a.css"), "a"); |
| 81 | + writeFileSync(join(dir, "b.css"), "b"); |
| 82 | + expect(() => |
| 83 | + callWriteBundle(dir, { "a.css": asset("a.css"), "b.css": asset("b.css") }), |
| 84 | + ).toThrow(/found 2/); |
| 85 | + } finally { |
| 86 | + rmSync(dir, { recursive: true, force: true }); |
| 87 | + } |
| 88 | + }); |
| 89 | +}); |
0 commit comments