@@ -2,7 +2,8 @@ import postcss from 'postcss';
22
33const declWhitelist = [ 'composes' ] ,
44 declFilter = new RegExp ( `^(${ declWhitelist . join ( '|' ) } )$` ) ,
5- matchImports = / ^ ( .+ ?) \s + f r o m \s + (?: " ( [ ^ " ] + ) " | ' ( [ ^ ' ] + ) ' ) $ / ;
5+ matchImports = / ^ ( .+ ?) \s + f r o m \s + (?: " ( [ ^ " ] + ) " | ' ( [ ^ ' ] + ) ' ) $ / ,
6+ icssImport = / ^ : i m p o r t \( (?: " ( [ ^ " ] + ) " | ' ( [ ^ ' ] + ) ' ) \) / ;
67
78const processor = postcss . plugin ( 'modules-extract-imports' , function ( options ) {
89 return ( css ) => {
@@ -28,19 +29,34 @@ const processor = postcss.plugin( 'modules-extract-imports', function ( options
2829 }
2930 } ) ;
3031
31- // If we've found any imports, insert :import rules
32+ // If we've found any imports, insert or append :import rules
33+ let existingImports = { } ;
34+ css . eachRule ( rule => {
35+ let matches = icssImport . exec ( rule . selector ) ;
36+ if ( matches ) {
37+ let [ /*match*/ , doubleQuotePath , singleQuotePath ] = matches ;
38+ existingImports [ doubleQuotePath || singleQuotePath ] = rule ;
39+ }
40+ } ) ;
41+
3242 Object . keys ( imports ) . reverse ( ) . forEach ( path => {
33- let pathImports = imports [ path ] ;
34- css . prepend ( postcss . rule ( {
35- selector : `:import("${ path } ")` ,
36- raws : { after : "\n" } ,
37- nodes : Object . keys ( pathImports ) . map ( importedSymbol => postcss . decl ( {
43+
44+ let rule = existingImports [ path ] ;
45+ if ( ! rule ) {
46+ rule = postcss . rule ( {
47+ selector : `:import("${ path } ")` ,
48+ after : "\n"
49+ } ) ;
50+ css . prepend ( rule ) ;
51+ }
52+ Object . keys ( imports [ path ] ) . forEach ( importedSymbol => {
53+ rule . push ( postcss . decl ( {
3854 value : importedSymbol ,
39- prop : pathImports [ importedSymbol ] ,
40- raws : { before : "\n " } ,
55+ prop : imports [ path ] [ importedSymbol ] ,
56+ before : "\n " ,
4157 _autoprefixerDisabled : true
42- } ) )
43- } ) ) ;
58+ } ) ) ;
59+ } ) ;
4460 } ) ;
4561 } ;
4662} ) ;
0 commit comments