11import fs from 'node:fs/promises' ;
22import path from 'node:path' ;
33import postcss from 'postcss' ;
4+ import valueParser from 'postcss-value-parser' ;
45import { resolveId } from './resolve-id.mjs' ;
56
67// return custom selectors from the css root, conditionally removing them
@@ -17,7 +18,10 @@ export default async function getCustomPropertiesFromRoot(root, resolver) {
1718 // recursively add custom properties from @import statements
1819 const importPromises = [ ] ;
1920 root . walkAtRules ( 'import' , atRule => {
20- const fileName = atRule . params . replace ( / [ ' | " ] / g, '' ) ;
21+ const fileName = parseImportParams ( atRule . params ) ;
22+ if ( ! fileName ) {
23+ return ;
24+ }
2125
2226 if ( path . isAbsolute ( fileName ) ) {
2327 importPromises . push ( getCustomPropertiesFromCSSFile ( fileName , resolver ) ) ;
@@ -39,21 +43,19 @@ export default async function getCustomPropertiesFromRoot(root, resolver) {
3943 } ) ;
4044
4145 // for each custom property declaration
42- root . walkDecls ( customPropertyRegExp , decl => {
43- const { prop } = decl ;
46+ root . walkDecls ( decl => {
47+ if ( ! decl . variable || ! decl . prop . startsWith ( '--' ) ) {
48+ return ;
49+ }
4450
4551 // write the parsed value to the custom property
46- customProperties [ prop ] = decl . value ;
52+ customProperties [ decl . prop ] = decl . value ;
4753 } ) ;
4854
4955 // return all custom properties, preferring :root properties over html properties
5056 return customProperties ;
5157}
5258
53- // match custom properties
54- const customPropertyRegExp = / ^ - - [ A - z ] [ \w - ] * $ / ;
55-
56-
5759async function getCustomPropertiesFromCSSFile ( from , resolver ) {
5860 try {
5961 const css = await fs . readFile ( from , 'utf8' ) ;
@@ -64,3 +66,44 @@ async function getCustomPropertiesFromCSSFile(from, resolver) {
6466 return { } ;
6567 }
6668}
69+
70+ function parseImportParams ( params ) {
71+ const nodes = valueParser ( params ) . nodes ;
72+ if ( ! nodes . length ) {
73+ return ;
74+ }
75+
76+ for ( let i = 0 ; i < nodes . length ; i ++ ) {
77+ const node = nodes [ i ] ;
78+ if ( node . type === 'space' || node . type === 'comment' ) {
79+ continue ;
80+ }
81+
82+ if ( node . type === 'string' ) {
83+ return node . value ;
84+ }
85+
86+ if ( node . type === 'function' && / u r l / i. test ( node . value ) ) {
87+ for ( let j = 0 ; j < node . nodes . length ; j ++ ) {
88+ const urlNode = node . nodes [ j ] ;
89+ if ( urlNode . type === 'space' || urlNode . type === 'comment' ) {
90+ continue ;
91+ }
92+
93+ if ( urlNode . type === 'word' ) {
94+ return urlNode . value ;
95+ }
96+
97+ if ( urlNode . type === 'string' ) {
98+ return urlNode . value ;
99+ }
100+
101+ return false ;
102+ }
103+ }
104+
105+ return false ;
106+ }
107+
108+ return false ;
109+ }
0 commit comments