Skip to content

Commit 2d262ce

Browse files
authored
Merge pull request #614 from atls/fix-code-runtime-css-ext-register
fix(code-runtime): support css extensions in ts-ext-register
2 parents 877c2c3 + c39725d commit 2d262ce

2 files changed

Lines changed: 71 additions & 0 deletions

File tree

runtime/code-runtime/src/ts-ext-register.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ const mapping = new Map([
1414
['.mjs', ['.mjs', '.mts']],
1515
['.cjs', ['.cjs', '.cts']],
1616
['.jsx', ['.jsx', '.tsx']],
17+
[
18+
'.css',
19+
['.css.ts', '.css.tsx', '.css.js', '.css.mjs', '.css.mts', '.css.cjs', '.css.cts', '.css'],
20+
],
1721
])
1822

1923
export const resolve: ResolveHook = (
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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

Comments
 (0)