Skip to content

Commit e52624a

Browse files
authored
fix: resolve .css.ts files instead of externalizing them (#173)
* fix: move css externalization after dts resolution check CSS imports should only be externalized if they cannot be resolved as DTS files. This ensures CSS-related files are properly handled after attempting DTS resolution first. * fix: resolve .css.ts files instead of externalizing them Move the CSS externalization guard to after resolution so that .css.ts files (e.g. vanilla-extract) are properly resolved and bundled into declarations, while real .css imports remain externalized. Closes #136
1 parent 02122cb commit e52624a

File tree

7 files changed

+47
-6
lines changed

7 files changed

+47
-6
lines changed

src/resolver.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,6 @@ export function createDtsResolvePlugin({
5353
moduleSideEffects: sideEffects,
5454
}
5555

56-
// Guard: Externalize non-code imports
57-
if (RE_CSS.test(id)) {
58-
debug('Externalizing css import:', id)
59-
return external
60-
}
61-
6256
// Get Rolldown's resolution first for fallback and policy checks
6357
const rolldownResolution = await this.resolve(id, importer, options)
6458
debug(
@@ -86,6 +80,11 @@ export function createDtsResolvePlugin({
8680

8781
// If resolution failed, error or externalize
8882
if (!dtsResolution) {
83+
if (RE_CSS.test(id)) {
84+
debug('Externalizing css import:', id)
85+
return external
86+
}
87+
8988
debug('Unresolvable dts import:', id, 'from', importer)
9089

9190
const isFileImport = isFilePath(id)

tests/__snapshots__/index.test.ts.snap

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ declare const shared1 = "shared1";
7070
export { shared1 as t };"
7171
`;
7272
73+
exports[`css.ts files 1`] = `
74+
"// index.d.ts
75+
//#region tests/fixtures/css-ts/file.css.d.ts
76+
declare const myClass: string;
77+
//#endregion
78+
export { myClass };"
79+
`;
80+
7381
exports[`cyclic import 1`] = `
7482
"// a-B8uCZscZ.d.ts
7583
//#region b.d.ts
@@ -359,6 +367,14 @@ type LibType = import_stub_lib.LibType;
359367
export { type LibType };"
360368
`;
361369
370+
exports[`real css imports are externalized 1`] = `
371+
"// index.d.ts
372+
//#region tests/fixtures/css-real/index.d.ts
373+
declare const foo: number;
374+
//#endregion
375+
export { foo };"
376+
`;
377+
362378
exports[`resolve dts 1`] = `
363379
"// index.d.ts
364380
//#region tests/fixtures/resolve-dts/mod.d.ts

tests/fixtures/css-real/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import './styles.css'
2+
export const foo: number = 1

tests/fixtures/css-real/styles.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.main {
2+
color: blue;
3+
}

tests/fixtures/css-ts/file.css.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const myClass: string = 'foo'

tests/fixtures/css-ts/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './file.css'

tests/index.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,25 @@ test('tsgo with custom path', async () => {
529529
expect(snapshot).toMatchSnapshot()
530530
})
531531

532+
// https://github.com/sxzz/rolldown-plugin-dts/issues/136
533+
test('css.ts files', async () => {
534+
const root = path.resolve(dirname, 'fixtures/css-ts')
535+
const { snapshot } = await rolldownBuild(path.resolve(root, 'index.ts'), [
536+
dts({ emitDtsOnly: true }),
537+
])
538+
expect(snapshot).toMatchSnapshot()
539+
})
540+
541+
// https://github.com/rolldown/tsdown/issues/170
542+
test('real css imports are externalized', async () => {
543+
const root = path.resolve(dirname, 'fixtures/css-real')
544+
const { snapshot } = await rolldownBuild(path.resolve(root, 'index.ts'), [
545+
dts({ emitDtsOnly: true }),
546+
])
547+
expect(snapshot).toMatchSnapshot()
548+
expect(snapshot).not.toContain('.main')
549+
})
550+
532551
test('sub namespace', async () => {
533552
const { snapshot } = await rolldownBuild(
534553
path.resolve(dirname, 'fixtures/sub-namespace.ts'),

0 commit comments

Comments
 (0)