11import { promises as fs } from 'fs' ;
22import path from 'path' ;
33import postcss from 'postcss' ;
4+ import { resolveId } from './resolve-id'
45
56// return custom selectors from the css root, conditionally removing them
6- export default async function getCustomPropertiesFromRoot ( root ) {
7+ export default async function getCustomPropertiesFromRoot ( root , resolver ) {
78 // initialize custom selectors
89 let customProperties = { } ;
910
@@ -17,8 +18,19 @@ export default async function getCustomPropertiesFromRoot(root) {
1718 const importPromises = [ ] ;
1819 root . walkAtRules ( 'import' , atRule => {
1920 const fileName = atRule . params . replace ( / [ ' | " ] / g, '' ) ;
20- const resolvedFileName = path . resolve ( sourceDir , fileName ) ;
21- importPromises . push ( getCustomPropertiesFromCSSFile ( resolvedFileName ) ) ;
21+
22+ if ( path . isAbsolute ( fileName ) ) {
23+ importPromises . push ( getCustomPropertiesFromCSSFile ( fileName , resolver ) ) ;
24+ } else {
25+ const promise = resolveId ( fileName , sourceDir , {
26+ paths : resolver . paths ,
27+ extensions : resolver . extensions ,
28+ moduleDirectories : resolver . moduleDirectories
29+ } )
30+ . then ( ( filePath ) => getCustomPropertiesFromCSSFile ( filePath , resolver ) )
31+ . catch ( ( ) => { } )
32+ importPromises . push ( promise )
33+ }
2234 } ) ;
2335
2436 ( await Promise . all ( importPromises ) ) . forEach ( propertiesFromImport => {
@@ -41,12 +53,12 @@ export default async function getCustomPropertiesFromRoot(root) {
4153const customPropertyRegExp = / ^ - - [ A - z ] [ \w - ] * $ / ;
4254
4355
44- async function getCustomPropertiesFromCSSFile ( from ) {
56+ async function getCustomPropertiesFromCSSFile ( from , resolver ) {
4557 try {
4658 const css = await fs . readFile ( from , 'utf8' ) ;
4759 const root = postcss . parse ( css , { from } ) ;
4860
49- return await getCustomPropertiesFromRoot ( root ) ;
61+ return await getCustomPropertiesFromRoot ( root , resolver ) ;
5062 } catch ( e ) {
5163 return { } ;
5264 }
0 commit comments