Skip to content
This repository was archived by the owner on Jul 31, 2019. It is now read-only.

Commit 07213fd

Browse files
author
黄凤栗
committed
add: generate different css files with fontGear
1 parent 9bb2748 commit 07213fd

28 files changed

+2293
-7
lines changed

README.md

+15
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,24 @@ After processing:
7575
- `remPrecision`: number, default `6`
7676
- `addPrefixToSelector`: function
7777
- `dprList`: array, default `[3, 2, 1]`
78+
- `fontGear`: array, default `[-1, 0, 1, 2, 3, 4]`
79+
- `enableFontSetting`: boolean, default `false`
80+
- `addFontSizeToSelector`: function
81+
- `outputCSSFile`: function
7882

7983
## Change Log
8084

85+
* add: generate different css files with fontGear
86+
* support custom `fontGear`
87+
* support custom `enableFontSetting`
88+
* support custom `addFontSizeToSelector`
89+
* support custom `outputCSSFile`
90+
91+
92+
### 0.5.0
93+
94+
* support custom `dprList`
95+
8196
### 0.4.0
8297

8398
* support custom `dprList`

index.js

+41-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
'use strict';
22

33
var postcss = require('postcss')
4+
var path = require('path')
5+
var fs = require('fs')
46

57
var valueRegExp = /(dpr|rem|url)\((.+?)(px)?\)/
68
var dprRegExp = /dpr\((\d+(?:\.\d+)?)px\)/
@@ -15,17 +17,34 @@ module.exports = postcss.plugin('postcss-flexible', function (options) {
1517
var baseDpr = options.baseDpr || 2
1618
var remUnit = options.remUnit || 75
1719
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]
1822
var addPrefixToSelector = options.addPrefixToSelector || function (selector, prefix) {
1923
if (/^html/.test(selector)) {
2024
return selector.replace(/^html/, 'html' + prefix)
2125
}
2226
return prefix + ' ' + selector
2327
}
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+
}
2442
var dprList = (options.dprList || [3, 2, 1]).sort().reverse()
43+
fontGear = fontGear.sort().reverse()
2544
var urlRegExp = new RegExp('url\\([\'"]?\\S+?@(' + dprList.join('|') + ')x\\S+?[\'"]?\\)')
2645

2746
// get calculated value of px or rem
28-
function getCalcValue (value, dpr) {
47+
function getCalcValue (value, dpr, gear) {
2948
var valueGlobalRegExp = new RegExp(valueRegExp.source, 'g')
3049

3150
function getValue(val, type) {
@@ -39,7 +58,7 @@ module.exports = postcss.plugin('postcss-flexible', function (options) {
3958
}
4059
} else if ($1 === 'dpr') {
4160
if (dpr) {
42-
return getValue($2 * dpr / baseDpr, 'px')
61+
return getValue(addFontSizeToSelector($2, gear, baseDpr) * dpr / baseDpr, 'px')
4362
}
4463
} else if ($1 === 'rem') {
4564
return getValue($2 / remUnit, 'rem')
@@ -65,11 +84,10 @@ module.exports = postcss.plugin('postcss-flexible', function (options) {
6584
})
6685
}
6786

68-
function handleMobile (rule) {
87+
function handleMobile (rule, gear) {
6988
if (rule.selector.indexOf('[data-dpr="') !== -1) {
7089
return
7190
}
72-
7391
var newRules = []
7492
var hasDecls = false
7593

@@ -78,7 +96,8 @@ module.exports = postcss.plugin('postcss-flexible', function (options) {
7896
selectors: rule.selectors.map(function (sel) {
7997
return addPrefixToSelector(sel, '[data-dpr="' + dprList[i] + '"]')
8098
}),
81-
type: rule.type
99+
type: rule.type,
100+
customGear: gear
82101
})
83102
newRules.push(newRule)
84103
}
@@ -93,7 +112,7 @@ module.exports = postcss.plugin('postcss-flexible', function (options) {
93112
newRules.forEach(function (newRule, index) {
94113
var newDecl = postcss.decl({
95114
prop: decl.prop,
96-
value: getCalcValue(decl.value, dprList[index])
115+
value: getCalcValue(decl.value, dprList[index % dprList.length], newRule.customGear)
97116
})
98117
newRule.append(newDecl)
99118
})
@@ -107,6 +126,7 @@ module.exports = postcss.plugin('postcss-flexible', function (options) {
107126
}
108127
})
109128

129+
// insert the updated rules into its parent Node
110130
if (hasDecls) {
111131
newRules.forEach(function (newRule) {
112132
rule.parent.insertAfter(rule, newRule)
@@ -117,8 +137,22 @@ module.exports = postcss.plugin('postcss-flexible', function (options) {
117137
if (!rule.nodes.length) {
118138
rule.remove()
119139
}
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 {
120155
}
121-
122156
root.walkRules(function (rule) {
123157
desktop ? handleDesktop(rule) : handleMobile(rule)
124158
})

0 commit comments

Comments
 (0)