|
| 1 | +import assert from 'node:assert/strict' |
| 2 | +import fs from 'node:fs/promises' |
| 3 | +import os from 'node:os' |
| 4 | +import path from 'node:path' |
| 5 | +import test from 'node:test' |
| 6 | + |
| 7 | +import { cssWithMeta } from '../src/css.ts' |
| 8 | +import type { CssResolver } from '../src/types.js' |
| 9 | + |
| 10 | +interface Project { |
| 11 | + root: string |
| 12 | + file: (rel: string) => string |
| 13 | + writeFile: (rel: string, contents: string) => Promise<string> |
| 14 | + cleanup: () => Promise<void> |
| 15 | +} |
| 16 | + |
| 17 | +async function createProject(prefix: string): Promise<Project> { |
| 18 | + const root = await fs.mkdtemp(path.join(os.tmpdir(), prefix)) |
| 19 | + const writeFile = async (rel: string, contents: string): Promise<string> => { |
| 20 | + const target = path.join(root, rel) |
| 21 | + await fs.mkdir(path.dirname(target), { recursive: true }) |
| 22 | + await fs.writeFile(target, contents, 'utf8') |
| 23 | + return target |
| 24 | + } |
| 25 | + return { |
| 26 | + root, |
| 27 | + file: rel => path.join(root, rel), |
| 28 | + writeFile, |
| 29 | + cleanup: () => fs.rm(root, { recursive: true, force: true }), |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +async function realpathAll(paths: string[]): Promise<string[]> { |
| 34 | + return Promise.all(paths.map(filePath => fs.realpath(filePath))) |
| 35 | +} |
| 36 | + |
| 37 | +test('css walker dedupes style modules and preserves discovery order', async () => { |
| 38 | + const project = await createProject('knighted-css-walker-order-') |
| 39 | + try { |
| 40 | + await project.writeFile('styles/reset.css', '/* reset */\n.reset { color: #111; }') |
| 41 | + await project.writeFile('styles/shared.css', '/* shared */\n.shared { color: #222; }') |
| 42 | + await project.writeFile( |
| 43 | + 'components/widget.css', |
| 44 | + '/* widget */\n.widget { color: #333; }', |
| 45 | + ) |
| 46 | + await project.writeFile('styles/async.css', '/* async */\n.async { color: #444; }') |
| 47 | + await project.writeFile( |
| 48 | + 'styles/nested/deep.css', |
| 49 | + '/* deep */\n.deep { color: #555; }', |
| 50 | + ) |
| 51 | + |
| 52 | + const entrySource = `import './styles/reset.css' |
| 53 | + import './shared.ts' |
| 54 | + await import('./async-entry.ts') |
| 55 | +` |
| 56 | + await project.writeFile('entry.ts', entrySource) |
| 57 | + |
| 58 | + const sharedSource = `import './styles/shared.css' |
| 59 | + import './components/widget.ts' |
| 60 | + import './styles/reset.css' |
| 61 | +` |
| 62 | + await project.writeFile('shared.ts', sharedSource) |
| 63 | + |
| 64 | + const widgetSource = "import './widget.css'\n" |
| 65 | + await project.writeFile('components/widget.ts', widgetSource) |
| 66 | + |
| 67 | + const asyncEntrySource = `import './styles/async.css' |
| 68 | +export async function load() { |
| 69 | + await import('./nested/deep.ts') |
| 70 | +} |
| 71 | +` |
| 72 | + await project.writeFile('async-entry.ts', asyncEntrySource) |
| 73 | + |
| 74 | + await project.writeFile('nested/deep.ts', "import '../styles/nested/deep.css'\n") |
| 75 | + |
| 76 | + const { css, files } = await cssWithMeta(project.file('entry.ts')) |
| 77 | + |
| 78 | + const expectedOrder = [ |
| 79 | + project.file('styles/reset.css'), |
| 80 | + project.file('styles/shared.css'), |
| 81 | + project.file('components/widget.css'), |
| 82 | + project.file('styles/async.css'), |
| 83 | + project.file('styles/nested/deep.css'), |
| 84 | + ] |
| 85 | + |
| 86 | + assert.deepEqual(await realpathAll(files), await realpathAll(expectedOrder)) |
| 87 | + |
| 88 | + const markers = [ |
| 89 | + '/* reset */', |
| 90 | + '/* shared */', |
| 91 | + '/* widget */', |
| 92 | + '/* async */', |
| 93 | + '/* deep */', |
| 94 | + ] |
| 95 | + for (const marker of markers) { |
| 96 | + const occurrences = css.split(marker).length - 1 |
| 97 | + assert.equal(occurrences, 1, `expected ${marker} to appear exactly once`) |
| 98 | + } |
| 99 | + for (let i = 1; i < markers.length; i += 1) { |
| 100 | + const prevIndex = css.indexOf(markers[i - 1]) |
| 101 | + const nextIndex = css.indexOf(markers[i]) |
| 102 | + assert.ok( |
| 103 | + prevIndex >= 0 && nextIndex > prevIndex, |
| 104 | + `${markers[i - 1]} should precede ${markers[i]}`, |
| 105 | + ) |
| 106 | + } |
| 107 | + } finally { |
| 108 | + await project.cleanup() |
| 109 | + } |
| 110 | +}) |
| 111 | + |
| 112 | +test('css walker honors custom resolver mappings for nonstandard specifiers', async () => { |
| 113 | + const project = await createProject('knighted-css-walker-resolver-') |
| 114 | + try { |
| 115 | + await project.writeFile('styles/global.css', '/* global */\n.global { color: #666; }') |
| 116 | + await project.writeFile('styles/button.css', '/* button */\n.button { color: #777; }') |
| 117 | + |
| 118 | + const entrySource = `import '@pkg/global.css' |
| 119 | +import './view.ts' |
| 120 | +` |
| 121 | + await project.writeFile('entry.ts', entrySource) |
| 122 | + |
| 123 | + const viewSource = "import '@shared/button.css'\n" |
| 124 | + await project.writeFile('view.ts', viewSource) |
| 125 | + |
| 126 | + const resolver: CssResolver = async specifier => { |
| 127 | + if (specifier.startsWith('@pkg/')) { |
| 128 | + const relative = specifier.replace(/^@pkg\//, 'styles/') |
| 129 | + return project.file(relative) |
| 130 | + } |
| 131 | + if (specifier === '@shared/button.css') { |
| 132 | + return project.file('styles/button.css') |
| 133 | + } |
| 134 | + return undefined |
| 135 | + } |
| 136 | + |
| 137 | + const { css, files } = await cssWithMeta(project.file('entry.ts'), { |
| 138 | + resolver, |
| 139 | + }) |
| 140 | + |
| 141 | + const expected = [ |
| 142 | + project.file('styles/global.css'), |
| 143 | + project.file('styles/button.css'), |
| 144 | + ] |
| 145 | + |
| 146 | + assert.deepEqual(await realpathAll(files), await realpathAll(expected)) |
| 147 | + assert.ok(css.includes('/* global */')) |
| 148 | + assert.ok(css.includes('/* button */')) |
| 149 | + } finally { |
| 150 | + await project.cleanup() |
| 151 | + } |
| 152 | +}) |
0 commit comments