|
| 1 | +import { execFileSync } from "node:child_process"; |
| 2 | +import { existsSync, mkdtempSync, writeFileSync, rmSync } from "node:fs"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import path from "node:path"; |
| 5 | + |
| 6 | +const repoRoot = path.resolve(__dirname, ".."); |
| 7 | +const loaderPath = path.resolve(repoRoot, "build/loader.mjs"); |
| 8 | +const fixturesIn = path.resolve(__dirname, "./fixtures/in"); |
| 9 | + |
| 10 | +beforeAll(() => { |
| 11 | + if (!existsSync(loaderPath)) { |
| 12 | + throw new Error( |
| 13 | + `Loader build artifact missing at ${loaderPath}. Run \`make compile\` first.`, |
| 14 | + ); |
| 15 | + } |
| 16 | +}); |
| 17 | + |
| 18 | +function runLoaderScript(script, options = {}) { |
| 19 | + return execFileSync( |
| 20 | + process.execPath, |
| 21 | + ["--import", loaderPath, "--input-type=module", "-e", script], |
| 22 | + { |
| 23 | + cwd: options.cwd || repoRoot, |
| 24 | + encoding: "utf8", |
| 25 | + env: { ...process.env, ...(options.env || {}) }, |
| 26 | + }, |
| 27 | + ).trim(); |
| 28 | +} |
| 29 | + |
| 30 | +it("returns the token map for a CSS file via JS import", () => { |
| 31 | + const cssPath = path.join(fixturesIn, "classes.css"); |
| 32 | + const out = runLoaderScript( |
| 33 | + `import s from ${JSON.stringify(cssPath)}; process.stdout.write(JSON.stringify(s));`, |
| 34 | + ); |
| 35 | + const tokens = JSON.parse(out); |
| 36 | + expect(Object.keys(tokens).sort()).toEqual(["article", "title"]); |
| 37 | + expect(tokens.title).toMatch(/title/); |
| 38 | +}); |
| 39 | + |
| 40 | +it("composes tokens across imported CSS files", () => { |
| 41 | + const cssPath = path.join(fixturesIn, "composes.css"); |
| 42 | + const out = runLoaderScript( |
| 43 | + `import s from ${JSON.stringify(cssPath)}; process.stdout.write(JSON.stringify(s));`, |
| 44 | + ); |
| 45 | + const tokens = JSON.parse(out); |
| 46 | + expect(tokens).toHaveProperty("title"); |
| 47 | + expect(tokens).toHaveProperty("figure"); |
| 48 | + expect(tokens.title.split(/\s+/).length).toBeGreaterThanOrEqual(2); |
| 49 | + expect(tokens.figure.split(/\s+/).length).toBeGreaterThanOrEqual(2); |
| 50 | +}); |
| 51 | + |
| 52 | +it("applies options from postcss-modules.config.cjs in cwd", () => { |
| 53 | + const dir = mkdtempSync(path.join(tmpdir(), "pcm-loader-")); |
| 54 | + try { |
| 55 | + writeFileSync( |
| 56 | + path.join(dir, "postcss-modules.config.cjs"), |
| 57 | + 'module.exports = { generateScopedName: (name) => "_test_" + name };\n', |
| 58 | + ); |
| 59 | + const cssPath = path.join(dir, "x.css"); |
| 60 | + writeFileSync(cssPath, ".foo { color: red; }\n.bar { color: blue; }\n"); |
| 61 | + const out = runLoaderScript( |
| 62 | + `import s from ${JSON.stringify(cssPath)}; process.stdout.write(JSON.stringify(s));`, |
| 63 | + { cwd: dir }, |
| 64 | + ); |
| 65 | + expect(JSON.parse(out)).toEqual({ foo: "_test_foo", bar: "_test_bar" }); |
| 66 | + } finally { |
| 67 | + rmSync(dir, { recursive: true, force: true }); |
| 68 | + } |
| 69 | +}); |
| 70 | + |
| 71 | +it("honors POSTCSS_MODULES_CONFIG env var", () => { |
| 72 | + const dir = mkdtempSync(path.join(tmpdir(), "pcm-loader-env-")); |
| 73 | + try { |
| 74 | + const configPath = path.join(dir, "my-config.cjs"); |
| 75 | + writeFileSync( |
| 76 | + configPath, |
| 77 | + 'module.exports = { generateScopedName: (name) => "_env_" + name };\n', |
| 78 | + ); |
| 79 | + const cssPath = path.join(dir, "x.css"); |
| 80 | + writeFileSync(cssPath, ".a {}\n.b {}\n"); |
| 81 | + const out = runLoaderScript( |
| 82 | + `import s from ${JSON.stringify(cssPath)}; process.stdout.write(JSON.stringify(s));`, |
| 83 | + { cwd: dir, env: { POSTCSS_MODULES_CONFIG: configPath } }, |
| 84 | + ); |
| 85 | + expect(JSON.parse(out)).toEqual({ a: "_env_a", b: "_env_b" }); |
| 86 | + } finally { |
| 87 | + rmSync(dir, { recursive: true, force: true }); |
| 88 | + } |
| 89 | +}); |
| 90 | + |
| 91 | +it("surfaces missing-file errors with the source path", () => { |
| 92 | + const cssPath = path.join(fixturesIn, "does-not-exist.css"); |
| 93 | + expect(() => { |
| 94 | + runLoaderScript( |
| 95 | + `import s from ${JSON.stringify(cssPath)}; process.stdout.write(JSON.stringify(s));`, |
| 96 | + ); |
| 97 | + }).toThrow(/does-not-exist\.css/); |
| 98 | +}); |
0 commit comments