@@ -2,6 +2,8 @@ import process from "node:process";
22import * as path from "pathe" ;
33import * as fs from "node:fs/promises" ;
44import MagicString , { Bundle } from "magic-string" ;
5+ import queryString from "query-string" ;
6+ import cssSelectorExtract from "css-selector-extract" ;
57import type { PreprocessorGroup } from "svelte/compiler" ;
68import type { Config as SvelteKitConfig } from "@sveltejs/kit" ;
79import type { UserConfig as ViteConfig } from "vite" ;
@@ -72,10 +74,15 @@ function matchAllImports(str: string) {
7274 while ( ( match = globalRegex . exec ( str ) ) !== null ) {
7375 const start = match . index ;
7476 const end = start + match [ 0 ] . length ;
77+ const {
78+ url : file ,
79+ query,
80+ } = queryString . parseUrl ( match [ 1 ] . substring ( 1 , match [ 1 ] . length - 1 ) ) ;
7581 matches . push ( {
7682 start,
7783 end,
78- file : match [ 1 ] . substring ( 1 , match [ 1 ] . length - 1 ) ,
84+ file,
85+ query,
7986 } ) ;
8087 }
8188 return matches ;
@@ -96,7 +103,7 @@ export function importCSSPreprocess(): PreprocessorGroup {
96103 state . clone ( ) . remove ( start , end ) ;
97104 const out = [ ] ;
98105 const deps = [ ] ;
99- for ( const { start, end, file } of imports . reverse ( ) ) {
106+ for ( const { start, end, file, query } of imports . reverse ( ) ) {
100107 // Right
101108 if ( lastStart != null ) {
102109 out . push ( remove ( lastStart , content . length ) . remove ( 0 , end ) ) ;
@@ -105,8 +112,22 @@ export function importCSSPreprocess(): PreprocessorGroup {
105112 }
106113 const absPath = await getAbsPath ( { filename, file } ) ;
107114 deps . push ( absPath ) ;
108- const text = ( await fs . readFile ( absPath ) ) . toString ( ) ;
109- out . push ( new MagicString ( text , { filename : absPath } ) ) ;
115+
116+ const cssText = ( await fs . readFile ( absPath ) ) . toString ( ) ;
117+
118+ let replaceText : string | undefined = undefined ;
119+
120+ if ( Object . keys ( query ) . length > 0 ) {
121+ const selectedCss : string = await cssSelectorExtract . process ( {
122+ css : cssText ,
123+ filters : Object . keys ( query ) ,
124+ } ) ;
125+ replaceText = selectedCss ;
126+ } else {
127+ replaceText = cssText ;
128+ }
129+
130+ out . push ( new MagicString ( replaceText , { filename : absPath } ) ) ;
110131 lastStart = start ;
111132 }
112133
0 commit comments