1- import { readFile } from 'node:fs/promises'
2- import { createCssPostHooks , RE_CSS , type CssStyles } from 'tsdown/css'
1+ import {
2+ createCssPostHooks ,
3+ getCleanId ,
4+ RE_CSS ,
5+ type CssStyles ,
6+ } from 'tsdown/css'
37import {
48 bundleWithLightningCSS ,
59 transformWithLightningCSS ,
610} from './lightningcss.ts'
7- import { processWithPostCSS } from './postcss.ts'
11+ import { processWithPostCSS as runPostCSS } from './postcss.ts'
812import {
913 compilePreprocessor ,
1014 getPreprocessorLang ,
@@ -28,16 +32,23 @@ export function CssPlugin(
2832 styles . clear ( )
2933 } ,
3034
31- async load ( id ) {
35+ async transform ( code , id ) {
3236 if ( ! isCssOrPreprocessor ( id ) ) return
3337
34- let code : string
38+ const cleanId = getCleanId ( id )
3539 const deps : string [ ] = [ ]
3640
3741 if ( config . css . transformer === 'lightningcss' ) {
38- code = await loadWithLightningCSS ( id , deps , config , logger )
42+ code = await processWithLightningCSS (
43+ code ,
44+ id ,
45+ cleanId ,
46+ deps ,
47+ config ,
48+ logger ,
49+ )
3950 } else {
40- code = await loadWithPostCSS ( id , deps , config )
51+ code = await processWithPostCSS ( code , id , cleanId , deps , config )
4152 }
4253
4354 for ( const dep of deps ) {
@@ -60,80 +71,92 @@ export function CssPlugin(
6071 }
6172}
6273
63- async function loadWithLightningCSS (
74+ async function processWithLightningCSS (
75+ code : string ,
6476 id : string ,
77+ cleanId : string ,
6578 deps : string [ ] ,
6679 config : ResolvedConfig ,
6780 logger : MinimalLogger ,
6881) : Promise < string > {
6982 const lang = getPreprocessorLang ( id )
7083
7184 if ( lang ) {
72- const rawCode = await readFile ( id , 'utf8' )
7385 const preResult = await compilePreprocessor (
7486 lang ,
75- rawCode ,
76- id ,
87+ code ,
88+ cleanId ,
7789 config . css . preprocessorOptions ,
7890 )
7991 deps . push ( ...preResult . deps )
8092
81- return transformWithLightningCSS ( preResult . code , id , {
93+ return transformWithLightningCSS ( preResult . code , cleanId , {
8294 target : config . css . target ,
8395 lightningcss : config . css . lightningcss ,
8496 minify : config . css . minify ,
8597 } )
86- } else if ( RE_CSS . test ( id ) ) {
87- const bundleResult = await bundleWithLightningCSS ( id , {
98+ }
99+
100+ // Virtual modules (with query strings) can't use file-based bundling
101+ if ( id !== cleanId ) {
102+ return transformWithLightningCSS ( code , cleanId , {
88103 target : config . css . target ,
89104 lightningcss : config . css . lightningcss ,
90105 minify : config . css . minify ,
91- preprocessorOptions : config . css . preprocessorOptions ,
92- logger,
93106 } )
107+ }
108+
109+ if ( RE_CSS . test ( cleanId ) ) {
110+ const bundleResult = await bundleWithLightningCSS (
111+ cleanId ,
112+ {
113+ target : config . css . target ,
114+ lightningcss : config . css . lightningcss ,
115+ minify : config . css . minify ,
116+ preprocessorOptions : config . css . preprocessorOptions ,
117+ logger,
118+ } ,
119+ code ,
120+ )
94121 deps . push ( ...bundleResult . deps )
95122 return bundleResult . code
96123 }
97124
98125 return ''
99126}
100127
101- async function loadWithPostCSS (
128+ async function processWithPostCSS (
129+ code : string ,
102130 id : string ,
131+ cleanId : string ,
103132 deps : string [ ] ,
104133 config : ResolvedConfig ,
105134) : Promise < string > {
106135 const lang = getPreprocessorLang ( id )
107- let code : string
108136
109137 if ( lang ) {
110- const rawCode = await readFile ( id , 'utf8' )
111138 const preResult = await compilePreprocessor (
112139 lang ,
113- rawCode ,
114- id ,
140+ code ,
141+ cleanId ,
115142 config . css . preprocessorOptions ,
116143 )
117144 code = preResult . code
118145 deps . push ( ...preResult . deps )
119- } else if ( RE_CSS . test ( id ) ) {
120- code = await readFile ( id , 'utf8' )
121- } else {
122- return ''
123146 }
124147
125148 const needInlineImport = code . includes ( '@import' )
126- const postcssResult = await processWithPostCSS (
149+ const postcssResult = await runPostCSS (
127150 code ,
128- id ,
151+ cleanId ,
129152 config . css . postcss ,
130153 config . cwd ,
131154 needInlineImport ,
132155 )
133156 code = postcssResult . code
134157 deps . push ( ...postcssResult . deps )
135158
136- return transformWithLightningCSS ( code , id , {
159+ return transformWithLightningCSS ( code , cleanId , {
137160 target : config . css . target ,
138161 lightningcss : config . css . lightningcss ,
139162 minify : config . css . minify ,
0 commit comments