|
| 1 | +import assert from 'node:assert/strict' |
| 2 | +import { mkdtemp, rm, writeFile } from 'node:fs/promises' |
| 3 | +import { tmpdir } from 'node:os' |
| 4 | +import { join } from 'node:path' |
| 5 | +import { test } from 'node:test' |
| 6 | +import { pathToFileURL } from 'node:url' |
| 7 | + |
| 8 | +import { resolve } from '../src/ts-ext-register.js' |
| 9 | + |
| 10 | +const createContext = (parentPath: string) => ({ |
| 11 | + parentURL: pathToFileURL(parentPath).href, |
| 12 | +} as never) |
| 13 | + |
| 14 | +test('should resolve css imports to css.ts source', async () => { |
| 15 | + const workspace = await mkdtemp(join(tmpdir(), 'ts-ext-register-')) |
| 16 | + const parentPath = join(workspace, 'entry.ts') |
| 17 | + |
| 18 | + try { |
| 19 | + await writeFile(join(workspace, 'global.css.ts'), 'export const style = {}', 'utf-8') |
| 20 | + |
| 21 | + const actual = resolve( |
| 22 | + './global.css', |
| 23 | + createContext(parentPath), |
| 24 | + (specifier) => ({ format: 'module', url: specifier }) as never |
| 25 | + ) as { url: string } |
| 26 | + |
| 27 | + assert.equal(actual.url, './global.css.ts') |
| 28 | + } finally { |
| 29 | + await rm(workspace, { recursive: true, force: true }) |
| 30 | + } |
| 31 | +}) |
| 32 | + |
| 33 | +test('should resolve css imports to css source if css.ts is absent', async () => { |
| 34 | + const workspace = await mkdtemp(join(tmpdir(), 'ts-ext-register-')) |
| 35 | + const parentPath = join(workspace, 'entry.ts') |
| 36 | + |
| 37 | + try { |
| 38 | + await writeFile(join(workspace, 'global.css'), 'body {}', 'utf-8') |
| 39 | + |
| 40 | + const actual = resolve( |
| 41 | + './global.css', |
| 42 | + createContext(parentPath), |
| 43 | + (specifier) => ({ format: 'module', url: specifier }) as never |
| 44 | + ) as { url: string } |
| 45 | + |
| 46 | + assert.equal(actual.url, './global.css') |
| 47 | + } finally { |
| 48 | + await rm(workspace, { recursive: true, force: true }) |
| 49 | + } |
| 50 | +}) |
| 51 | + |
| 52 | +test('should keep original specifier if css source is absent', async () => { |
| 53 | + const workspace = await mkdtemp(join(tmpdir(), 'ts-ext-register-')) |
| 54 | + const parentPath = join(workspace, 'entry.ts') |
| 55 | + |
| 56 | + try { |
| 57 | + const actual = resolve( |
| 58 | + './missing.css', |
| 59 | + createContext(parentPath), |
| 60 | + (specifier) => ({ format: 'module', url: specifier }) as never |
| 61 | + ) as { url: string } |
| 62 | + |
| 63 | + assert.equal(actual.url, './missing.css') |
| 64 | + } finally { |
| 65 | + await rm(workspace, { recursive: true, force: true }) |
| 66 | + } |
| 67 | +}) |
0 commit comments