1
1
'use strict' ;
2
2
3
3
var postcss = require ( 'postcss' )
4
+ var path = require ( 'path' )
5
+ var fs = require ( 'fs' )
4
6
5
7
var valueRegExp = / ( d p r | r e m | u r l ) \( ( .+ ?) ( p x ) ? \) /
6
8
var dprRegExp = / d p r \( ( \d + (?: \. \d + ) ? ) p x \) /
@@ -15,17 +17,34 @@ module.exports = postcss.plugin('postcss-flexible', function (options) {
15
17
var baseDpr = options . baseDpr || 2
16
18
var remUnit = options . remUnit || 75
17
19
var remPrecision = options . remPrecision || 6
20
+ var enableFontSetting = options . enableFontSetting || false
21
+ var fontGear = Object . prototype . toString . call ( options . fontGear ) === '[object Array]' ? options . fontGear : [ - 1 , 0 , 1 , 2 , 3 , 4 ]
18
22
var addPrefixToSelector = options . addPrefixToSelector || function ( selector , prefix ) {
19
23
if ( / ^ h t m l / . test ( selector ) ) {
20
24
return selector . replace ( / ^ h t m l / , 'html' + prefix )
21
25
}
22
26
return prefix + ' ' + selector
23
27
}
28
+ var addFontSizeToSelector = function ( originFontSize , gear = 0 , baseDpr = 2 ) {
29
+ if ( ! enableFontSetting ) {
30
+ return originFontSize
31
+ }
32
+ if ( options . addFontSizeToSelector ) {
33
+ return options . addFontSizeToSelector ( originFontSize , gear , baseDpr )
34
+ }
35
+ return + originFontSize + gear * baseDpr
36
+ }
37
+ var outputCSSFile = options . outputCSSFile || function ( gear , clonedRoot ) {
38
+ gear !== undefined && fs . writeFileSync ( path . join ( __dirname , 'test/fontGear/fontGear_' + gear + '.css' ) , clonedRoot , {
39
+ encoding : 'utf8'
40
+ } )
41
+ }
24
42
var dprList = ( options . dprList || [ 3 , 2 , 1 ] ) . sort ( ) . reverse ( )
43
+ fontGear = fontGear . sort ( ) . reverse ( )
25
44
var urlRegExp = new RegExp ( 'url\\([\'"]?\\S+?@(' + dprList . join ( '|' ) + ')x\\S+?[\'"]?\\)' )
26
45
27
46
// get calculated value of px or rem
28
- function getCalcValue ( value , dpr ) {
47
+ function getCalcValue ( value , dpr , gear ) {
29
48
var valueGlobalRegExp = new RegExp ( valueRegExp . source , 'g' )
30
49
31
50
function getValue ( val , type ) {
@@ -39,7 +58,7 @@ module.exports = postcss.plugin('postcss-flexible', function (options) {
39
58
}
40
59
} else if ( $1 === 'dpr' ) {
41
60
if ( dpr ) {
42
- return getValue ( $2 * dpr / baseDpr , 'px' )
61
+ return getValue ( addFontSizeToSelector ( $2 , gear , baseDpr ) * dpr / baseDpr , 'px' )
43
62
}
44
63
} else if ( $1 === 'rem' ) {
45
64
return getValue ( $2 / remUnit , 'rem' )
@@ -65,11 +84,10 @@ module.exports = postcss.plugin('postcss-flexible', function (options) {
65
84
} )
66
85
}
67
86
68
- function handleMobile ( rule ) {
87
+ function handleMobile ( rule , gear ) {
69
88
if ( rule . selector . indexOf ( '[data-dpr="' ) !== - 1 ) {
70
89
return
71
90
}
72
-
73
91
var newRules = [ ]
74
92
var hasDecls = false
75
93
@@ -78,7 +96,8 @@ module.exports = postcss.plugin('postcss-flexible', function (options) {
78
96
selectors : rule . selectors . map ( function ( sel ) {
79
97
return addPrefixToSelector ( sel , '[data-dpr="' + dprList [ i ] + '"]' )
80
98
} ) ,
81
- type : rule . type
99
+ type : rule . type ,
100
+ customGear : gear
82
101
} )
83
102
newRules . push ( newRule )
84
103
}
@@ -93,7 +112,7 @@ module.exports = postcss.plugin('postcss-flexible', function (options) {
93
112
newRules . forEach ( function ( newRule , index ) {
94
113
var newDecl = postcss . decl ( {
95
114
prop : decl . prop ,
96
- value : getCalcValue ( decl . value , dprList [ index ] )
115
+ value : getCalcValue ( decl . value , dprList [ index % dprList . length ] , newRule . customGear )
97
116
} )
98
117
newRule . append ( newDecl )
99
118
} )
@@ -107,6 +126,7 @@ module.exports = postcss.plugin('postcss-flexible', function (options) {
107
126
}
108
127
} )
109
128
129
+ // insert the updated rules into its parent Node
110
130
if ( hasDecls ) {
111
131
newRules . forEach ( function ( newRule ) {
112
132
rule . parent . insertAfter ( rule , newRule )
@@ -117,8 +137,22 @@ module.exports = postcss.plugin('postcss-flexible', function (options) {
117
137
if ( ! rule . nodes . length ) {
118
138
rule . remove ( )
119
139
}
140
+ // output the css file with different fontGear
141
+ if ( hasDecls && outputCSSFile ) {
142
+ outputCSSFile ( gear , clonedRoot )
143
+ }
144
+ }
145
+ if ( enableFontSetting ) {
146
+ for ( var j = 0 ; j < fontGear . length ; j ++ ) {
147
+ var gear = fontGear [ j ]
148
+ // clone the root element so that the operation blow won't distructe the origin root element
149
+ var clonedRoot = root . clone ( )
150
+ clonedRoot . walkRules ( function ( rule ) {
151
+ desktop ? handleDesktop ( rule ) : handleMobile ( rule , gear )
152
+ } )
153
+ }
154
+ } else {
120
155
}
121
-
122
156
root . walkRules ( function ( rule ) {
123
157
desktop ? handleDesktop ( rule ) : handleMobile ( rule )
124
158
} )
0 commit comments