1
- import postcss from 'postcss'
2
- import replaceSymbols , { replaceAll } from 'icss-replace-symbols'
1
+ const postcss = require ( 'postcss' )
2
+ const { default : replaceSymbols , replaceAll } = require ( 'icss-replace-symbols' )
3
3
4
4
const matchImports = / ^ ( .+ ?| \( [ \s \S ] + ?\) ) \s + f r o m \s + ( " [ ^ " ] * " | ' [ ^ ' ] * ' | [ \w - ] + ) $ /
5
5
const matchValueDefinition = / (?: \s + | ^ ) ( [ \w - ] + ) : ? \s + ( .+ ?) \s * $ / g
6
6
const matchImport = / ^ ( [ \w - ] + ) (?: \s + a s \s + ( [ \w - ] + ) ) ? /
7
7
let options = { }
8
8
let importIndex = 0
9
- let createImportedName = options && options . createImportedName || ( ( importName /*, path*/ ) => `i__const_${ importName . replace ( / \W / g, '_' ) } _${ importIndex ++ } ` )
9
+ let createImportedName =
10
+ ( options && options . createImportedName ) ||
11
+ ( ( importName /*, path*/ ) =>
12
+ `i__const_${ importName . replace ( / \W / g, '_' ) } _${ importIndex ++ } ` )
10
13
11
- export default postcss . plugin ( 'postcss-modules-values' , ( ) => ( css , result ) => {
14
+ module . exports = postcss . plugin ( 'postcss-modules-values' , ( ) => (
15
+ css ,
16
+ result
17
+ ) => {
12
18
let importAliases = [ ]
13
19
let definitions = { }
14
20
15
21
const addDefinition = atRule => {
16
22
let matches
17
- while ( matches = matchValueDefinition . exec ( atRule . params ) ) {
18
- let [ /*match*/ , key , value ] = matches
23
+ while ( ( matches = matchValueDefinition . exec ( atRule . params ) ) ) {
24
+ let [ , key , value ] = matches
19
25
// Add to the definitions, knowing that values can refer to each other
20
26
definitions [ key ] = replaceAll ( definitions , value )
21
27
atRule . remove ( )
@@ -25,20 +31,23 @@ export default postcss.plugin('postcss-modules-values', () => (css, result) => {
25
31
const addImport = atRule => {
26
32
let matches = matchImports . exec ( atRule . params )
27
33
if ( matches ) {
28
- let [ /*match*/ , aliases , path ] = matches
34
+ let [ , aliases , path ] = matches
29
35
// We can use constants for path names
30
36
if ( definitions [ path ] ) path = definitions [ path ]
31
- let imports = aliases . replace ( / ^ \( \s * ( [ \s \S ] + ) \s * \) $ / , '$1' ) . split ( / \s * , \s * / ) . map ( alias => {
32
- let tokens = matchImport . exec ( alias )
33
- if ( tokens ) {
34
- let [ /*match*/ , theirName , myName = theirName ] = tokens
35
- let importedName = createImportedName ( myName )
36
- definitions [ myName ] = importedName
37
- return { theirName, importedName }
38
- } else {
39
- throw new Error ( `@import statement "${ alias } " is invalid!` )
40
- }
41
- } )
37
+ let imports = aliases
38
+ . replace ( / ^ \( \s * ( [ \s \S ] + ) \s * \) $ / , '$1' )
39
+ . split ( / \s * , \s * / )
40
+ . map ( alias => {
41
+ let tokens = matchImport . exec ( alias )
42
+ if ( tokens ) {
43
+ let [ , /*match*/ theirName , myName = theirName ] = tokens
44
+ let importedName = createImportedName ( myName )
45
+ definitions [ myName ] = importedName
46
+ return { theirName, importedName }
47
+ } else {
48
+ throw new Error ( `@import statement "${ alias } " is invalid!` )
49
+ }
50
+ } )
42
51
importAliases . push ( { path, imports } )
43
52
atRule . remove ( )
44
53
}
@@ -59,11 +68,13 @@ export default postcss.plugin('postcss-modules-values', () => (css, result) => {
59
68
60
69
/* We want to export anything defined by now, but don't add it to the CSS yet or
61
70
it well get picked up by the replacement stuff */
62
- let exportDeclarations = Object . keys ( definitions ) . map ( key => postcss . decl ( {
63
- value : definitions [ key ] ,
64
- prop : key ,
65
- raws : { before : "\n " }
66
- } ) )
71
+ let exportDeclarations = Object . keys ( definitions ) . map ( key =>
72
+ postcss . decl ( {
73
+ value : definitions [ key ] ,
74
+ prop : key ,
75
+ raws : { before : '\n ' }
76
+ } )
77
+ )
67
78
68
79
/* If we have no definitions, don't continue */
69
80
if ( ! Object . keys ( definitions ) . length ) return
@@ -75,7 +86,7 @@ export default postcss.plugin('postcss-modules-values', () => (css, result) => {
75
86
if ( exportDeclarations . length > 0 ) {
76
87
let exportRule = postcss . rule ( {
77
88
selector : `:export` ,
78
- raws : { after : "\n" }
89
+ raws : { after : '\n' }
79
90
} )
80
91
exportRule . append ( exportDeclarations )
81
92
css . prepend ( exportRule )
@@ -85,13 +96,13 @@ export default postcss.plugin('postcss-modules-values', () => (css, result) => {
85
96
importAliases . reverse ( ) . forEach ( ( { path, imports } ) => {
86
97
let importRule = postcss . rule ( {
87
98
selector : `:import(${ path } )` ,
88
- raws : { after : "\n" }
99
+ raws : { after : '\n' }
89
100
} )
90
101
imports . forEach ( ( { theirName, importedName } ) => {
91
102
importRule . append ( {
92
103
value : theirName ,
93
104
prop : importedName ,
94
- raws : { before : " \n " }
105
+ raws : { before : ' \n ' }
95
106
} )
96
107
} )
97
108
0 commit comments