1- import postcss from 'postcss' ;
1+ import postcss from 'postcss'
2+ import replaceSymbols from './replace-symbols'
23
3- const matchImports = / ^ ( .+ ?) \s + f r o m \s + ( " [ ^ " ] * " | ' [ ^ ' ] * ' ) $ /
4+ const matchImports = / ^ ( .+ ?) \s + f r o m \s + ( " [ ^ " ] * " | ' [ ^ ' ] * ' | [ \w - ] + ) $ /
45const matchLet = / (?: , \s + | ^ ) ( [ \w - ] + ) : ? \s + ( " [ ^ " ] * " | ' [ ^ ' ] * ' | [ ^ , ] + ) \s ? / g
5- const matchConstName = / [ \w - ] + / g
66const matchImport = / ^ ( [ \w - ] + ) (?: \s + a s \s + ( [ \w - ] + ) ) ? /
77let options = { }
88let importIndex = 0
99let createImportedName = options && options . createImportedName || ( ( importName /*, path*/ ) => `i__const_${ importName . replace ( / \W / g, '_' ) } _${ importIndex ++ } ` )
1010
11- const replace = ( declarations , object , propName ) => {
12- let matches
13- while ( matches = matchConstName . exec ( object [ propName ] ) ) {
14- let replacement = declarations [ matches [ 0 ] ]
15- if ( replacement ) {
16- object [ propName ] = object [ propName ] . slice ( 0 , matches . index ) + replacement + object [ propName ] . slice ( matchConstName . lastIndex )
17- matchConstName . lastIndex -= matches [ 0 ] . length - replacement . length
18- }
19- }
20- }
21-
2211export default css => {
2312 /* Find any local let rules and store them*/
24- let declarations = { }
13+ let translations = { }
2514 css . eachAtRule ( / ^ - ? l e t $ / , atRule => {
2615 let matches
2716 while ( matches = matchLet . exec ( atRule . params ) ) {
2817 let [ /*match*/ , key , value ] = matches
29- declarations [ key ] = value
18+ translations [ key ] = value
19+ atRule . removeSelf ( )
3020 }
3121 } )
3222
33- console . log ( declarations )
3423 /* We want to export anything defined by now, but don't add it to the CSS yet or
3524 it well get picked up by the replacement stuff */
36- let exportDeclarations = Object . keys ( declarations ) . map ( key => postcss . decl ( {
37- value : declarations [ key ] ,
25+ let exportDeclarations = Object . keys ( translations ) . map ( key => postcss . decl ( {
26+ value : translations [ key ] ,
3827 prop : key ,
3928 before : "\n " ,
4029 _autoprefixerDisabled : true
@@ -46,23 +35,27 @@ export default css => {
4635 let matches = matchImports . exec ( atRule . params )
4736 if ( matches ) {
4837 let [ /*match*/ , aliases , path ] = matches
38+ // We can use constants for path names
39+ if ( translations [ path ] ) path = translations [ path ]
4940 let imports = aliases . split ( / \s * , \s * / ) . map ( alias => {
5041 let tokens = matchImport . exec ( alias )
5142 if ( tokens ) {
5243 let [ /*match*/ , theirName , myName = theirName ] = tokens
5344 let importedName = createImportedName ( myName )
54- declarations [ myName ] = importedName
45+ translations [ myName ] = importedName
5546 return { theirName, importedName}
5647 } else {
5748 throw new Error ( `@import statement "${ alias } " is invalid!` )
5849 }
5950 } )
6051 importAliases . push ( { path, imports} )
52+ atRule . removeSelf ( )
6153 }
6254 } )
6355
56+ console . log ( translations )
6457 /* Perform replacements */
65- css . eachDecl ( decl => replace ( declarations , decl , 'value' ) )
58+ replaceSymbols ( css , translations )
6659
6760 /* Add import rules */
6861 importAliases . forEach ( ( { path, imports} ) => {
0 commit comments