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 {
48bundleWithLightningCSS ,
59transformWithLightningCSS ,
610} from './lightningcss.ts'
7- import { processWithPostCSS } from './postcss.ts'
11+ import { processWithPostCSS as runPostCSS } from './postcss.ts'
812import {
913compilePreprocessor ,
1014getPreprocessorLang ,
@@ -28,16 +32,23 @@ export function CssPlugin(
2832styles . clear ( )
2933} ,
3034
31- async load ( id ) {
35+ async transform ( code , id ) {
3236if ( ! isCssOrPreprocessor ( id ) ) return
3337
34- let code : string
38+ const cleanId = getCleanId ( id )
3539const deps : string [ ] = [ ]
3640
3741if ( 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
4354for ( 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 ,
6476id : string ,
77+ cleanId : string ,
6578deps : string [ ] ,
6679config : ResolvedConfig ,
6780logger : MinimalLogger ,
6881) : Promise < string > {
6982const lang = getPreprocessorLang ( id )
7083
7184if ( lang ) {
72- const rawCode = await readFile ( id , 'utf8' )
7385const preResult = await compilePreprocessor (
7486lang ,
75- rawCode ,
76- id ,
87+ code ,
88+ cleanId ,
7789config . css . preprocessorOptions ,
7890)
7991deps . push ( ...preResult . deps )
8092
81- return transformWithLightningCSS ( preResult . code , id , {
93+ return transformWithLightningCSS ( preResult . code , cleanId , {
8294target : config . css . target ,
8395lightningcss : config . css . lightningcss ,
8496minify : 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 , {
88103target : config . css . target ,
89104lightningcss : config . css . lightningcss ,
90105minify : 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+ )
94121deps . push ( ...bundleResult . deps )
95122return bundleResult . code
96123}
97124
98125return ''
99126}
100127
101- async function loadWithPostCSS (
128+ async function processWithPostCSS (
129+ code : string ,
102130id : string ,
131+ cleanId : string ,
103132deps : string [ ] ,
104133config : ResolvedConfig ,
105134) : Promise < string > {
106135const lang = getPreprocessorLang ( id )
107- let code : string
108136
109137if ( lang ) {
110- const rawCode = await readFile ( id , 'utf8' )
111138const preResult = await compilePreprocessor (
112139lang ,
113- rawCode ,
114- id ,
140+ code ,
141+ cleanId ,
115142config . css . preprocessorOptions ,
116143)
117144code = preResult . code
118145deps . push ( ...preResult . deps )
119- } else if ( RE_CSS . test ( id ) ) {
120- code = await readFile ( id , 'utf8' )
121- } else {
122- return ''
123146}
124147
125148const needInlineImport = code . includes ( '@import' )
126- const postcssResult = await processWithPostCSS (
149+ const postcssResult = await runPostCSS (
127150code ,
128- id ,
151+ cleanId ,
129152config . css . postcss ,
130153config . cwd ,
131154needInlineImport ,
132155)
133156code = postcssResult . code
134157deps . push ( ...postcssResult . deps )
135158
136- return transformWithLightningCSS ( code , id , {
159+ return transformWithLightningCSS ( code , cleanId , {
137160target : config . css . target ,
138161lightningcss : config . css . lightningcss ,
139162minify : config . css . minify ,
0 commit comments