1
1
import postcss from 'postcss' ;
2
- import valueParser from 'postcss-value-parser' ;
3
- import { extractICSS , replaceValueSymbols } from 'icss-utils' ;
2
+ import { extractICSS , replaceValueSymbols , replaceSymbols } from 'icss-utils' ;
4
3
import loaderUtils from 'loader-utils' ;
5
4
6
5
const pluginName = 'postcss-icss-parser' ;
7
6
7
+ function hasImportMessage ( messages , url ) {
8
+ return messages . find (
9
+ ( message ) =>
10
+ message . pluginName === pluginName &&
11
+ message . type === 'import' &&
12
+ message . item . url === url &&
13
+ message . item . media === ''
14
+ ) ;
15
+ }
16
+
8
17
export default postcss . plugin (
9
18
pluginName ,
10
19
( ) =>
@@ -14,88 +23,43 @@ export default postcss.plugin(
14
23
15
24
let index = 0 ;
16
25
17
- Object . keys ( icssImports ) . forEach ( ( key ) => {
18
- const url = loaderUtils . parseString ( key ) ;
26
+ for ( const importUrl of Object . keys ( icssImports ) ) {
27
+ const url = loaderUtils . parseString ( importUrl ) ;
19
28
20
- Object . keys ( icssImports [ key ] ) . forEach ( ( prop ) => {
29
+ for ( const token of Object . keys ( icssImports [ importUrl ] ) ) {
21
30
index += 1 ;
22
-
23
- importReplacements [ prop ] = `___CSS_LOADER_IMPORT___${ index } ___` ;
31
+ importReplacements [ token ] = `___CSS_LOADER_IMPORT___${ index } ___` ;
24
32
25
33
result . messages . push ( {
26
34
pluginName,
27
35
type : 'icss-import' ,
28
- item : { url, export : icssImports [ key ] [ prop ] , index } ,
36
+ item : { url, export : icssImports [ importUrl ] [ token ] , index } ,
29
37
} ) ;
30
38
31
- const alreadyIncluded = result . messages . find (
32
- ( message ) =>
33
- message . pluginName === pluginName &&
34
- message . type === 'import' &&
35
- message . item . url === url &&
36
- message . item . media === ''
37
- ) ;
38
-
39
- if ( alreadyIncluded ) {
40
- return ;
39
+ if ( ! hasImportMessage ( result . messages , url ) ) {
40
+ result . messages . push ( {
41
+ pluginName,
42
+ type : 'import' ,
43
+ item : { url, media : '' } ,
44
+ } ) ;
41
45
}
42
-
43
- result . messages . push ( {
44
- pluginName,
45
- type : 'import' ,
46
- item : { url, media : '' } ,
47
- } ) ;
48
- } ) ;
49
- } ) ;
50
-
51
- function replaceImportsInString ( str ) {
52
- const tokens = valueParser ( str ) ;
53
-
54
- tokens . walk ( ( node ) => {
55
- if ( node . type !== 'word' ) {
56
- return ;
57
- }
58
-
59
- const token = node . value ;
60
- const replacement = importReplacements [ token ] ;
61
-
62
- if ( replacement ) {
63
- // eslint-disable-next-line no-param-reassign
64
- node . value = replacement ;
65
- }
66
- } ) ;
67
-
68
- return tokens . toString ( ) ;
46
+ }
69
47
}
70
48
71
- // Replace tokens
72
- css . walk ( ( node ) => {
73
- // Due reusing `ast` from `postcss-loader` some plugins may remove `value`, `selector` or `params` properties
74
- if ( node . type === 'decl' && node . value ) {
75
- // eslint-disable-next-line no-param-reassign
76
- node . value = replaceImportsInString ( node . value . toString ( ) ) ;
77
- } else if ( node . type === 'rule' && node . selector ) {
78
- // eslint-disable-next-line no-param-reassign
79
- node . selector = replaceValueSymbols (
80
- node . selector . toString ( ) ,
81
- importReplacements
82
- ) ;
83
- } else if ( node . type === 'atrule' && node . params ) {
84
- // eslint-disable-next-line no-param-reassign
85
- node . params = replaceImportsInString ( node . params . toString ( ) ) ;
86
- }
87
- } ) ;
49
+ replaceSymbols ( css , importReplacements ) ;
88
50
89
- // Replace tokens in export
90
- Object . keys ( icssExports ) . forEach ( ( exportName ) => {
51
+ for ( const exportName of Object . keys ( icssExports ) ) {
91
52
result . messages . push ( {
92
53
pluginName,
93
54
type : 'export' ,
94
55
item : {
95
56
key : exportName ,
96
- value : replaceImportsInString ( icssExports [ exportName ] ) ,
57
+ value : replaceValueSymbols (
58
+ icssExports [ exportName ] ,
59
+ importReplacements
60
+ ) ,
97
61
} ,
98
62
} ) ;
99
- } ) ;
63
+ }
100
64
}
101
65
) ;
0 commit comments