1
1
var postcss = require ( 'postcss' )
2
2
var nested = require ( 'postcss-nested' )
3
+ var selectorParser = require ( 'postcss-selector-parser' )
3
4
var hash = require ( 'hash-sum' )
4
5
5
- var currentClass
6
- var rootRE = / : r o o t \b / g
7
6
var liftRE = / ^ ( h t m l | h e a d | b o d y ) \b /
7
+ var scopeRE = / : s c o p e \b /
8
8
var processRoot = postcss . plugin ( 'process-root' , function ( ) {
9
9
return function ( root ) {
10
10
var lifted = 0
@@ -13,13 +13,23 @@ var processRoot = postcss.plugin('process-root', function () {
13
13
}
14
14
root . each ( function ( node ) {
15
15
node . each ( function ( node ) {
16
- var sel = node . selector
17
- if ( liftRE . test ( sel ) ) {
18
- lift ( node )
19
- } else if ( rootRE . test ( sel ) ) {
20
- // replace :root selector
21
- node . selector = sel . replace ( rootRE , currentClass )
22
- lift ( node )
16
+ var kept = [ ]
17
+ selectorParser ( function ( selectors ) {
18
+ selectors . each ( function ( selector ) {
19
+ var sel = selector . toString ( )
20
+ if ( liftRE . test ( sel ) ) {
21
+ lift ( node . clone ( {
22
+ selector : sel
23
+ } ) )
24
+ } else {
25
+ kept . push ( sel )
26
+ }
27
+ } )
28
+ } ) . process ( node . selector )
29
+ if ( ! kept . length ) {
30
+ node . removeSelf ( )
31
+ } else {
32
+ node . selector = kept . join ( ',' ) . replace ( scopeRE , '&' )
23
33
}
24
34
} )
25
35
} )
@@ -29,7 +39,7 @@ var processRoot = postcss.plugin('process-root', function () {
29
39
module . exports = function ( css ) {
30
40
this . cacheable ( )
31
41
var cb = this . async ( )
32
- var cls = currentClass = '.v-' + hash ( this . resourcePath )
42
+ var cls = '.v-' + hash ( this . resourcePath )
33
43
css = cls + '{' + css + '}'
34
44
postcss ( [ processRoot , nested ] )
35
45
. process ( css )
0 commit comments