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

Commit bbb96d0

Browse files
author
黄凤栗
committed
update README-zh-CN
1 parent 03f820f commit bbb96d0

File tree

2 files changed

+177
-1
lines changed

2 files changed

+177
-1
lines changed

README-zh-CN.md

+176
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
# postcss-flexible 0.5.x使用及配置方法
2+
------
3+
4+
postcss-flexible 0.5.x此次主要更新目的是为了满足用户根据不同的fontGear获得不同的css数据结构的需求。本次更新主要增加以下自定义配置项:
5+
6+
> * `fontGear`: array, default `[-1, 0, 1, 2, 3, 4]`, 需要使用的字体档位
7+
> * `enableFontSetting`: boolean, default `false`, 是否开启字体档位功能
8+
> * `addFontSizeToSelector`: function, 用户自定义如何针对不同档位输出计算后的值
9+
> * `outputCSSFile`: function, 用户自定义输出css
10+
11+
12+
------
13+
14+
## 主要函数介绍
15+
16+
addFontSizeToSelector(originFontSize, gear, baseDpr) -> {Number}
17+
在原始css中定义的值(originFontSize),根据字体档位(gear)和基础dpr(baseDpr),通过插件计算,返回生成新的值。插件默认为每一个档位之间的差值为1px。
18+
### 函数定义
19+
```javascript
20+
var addFontSizeToSelector = function (originFontSize, gear, baseDpr) {
21+
if (!gear) {
22+
gear = 0
23+
}
24+
if (!enableFontSetting) {
25+
return originFontSize
26+
}
27+
if (options.addFontSizeToSelector) {
28+
return options.addFontSizeToSelector(originFontSize, gear, baseDpr)
29+
}
30+
return +originFontSize + gear*baseDpr
31+
```
32+
### 函数参数
33+
| 参数名 | 类型 | 描述 |
34+
| :- |
35+
| `originFontSize`| string \| Number | 原始css中定义的值 |
36+
| `gear` | Number | 字体档位 |
37+
| `baseDpr` | Number | 基础dpr |
38+
### 返回值
39+
返回一个数字类型的值(Number)
40+
41+
outputCSSFile(gear, clonedRoot) -> {undefined}
42+
在用户开启字体档位功能后,用户可重写outputCSSFile方法,插件在每次遍历fontGear生成修改后的clonedRoot后,会调用此函数,并将clonedRoot作为参数传递,用户可以自定义以何种方式处理该css(可输出文件,也可在控制台中打印)
43+
### 函数定义
44+
```javascript
45+
var outputCSSFile = options.outputCSSFile || function (gear, clonedRoot) {}
46+
```
47+
### 函数参数
48+
| 参数名 | 类型 | 描述 |
49+
| :- |
50+
| `gear`| string \| Number | 字体档位 |
51+
| `clonedRoot` | postcss结构 | 修改后的postcss结构 |
52+
### 返回值
53+
无返回值
54+
55+
56+
----------
57+
58+
59+
## 如何使用
60+
### webpack配置
61+
若用户开启输出字体档位功能,需要在webpack.config.js中对postcss-flexible插件添加对应属性,以下为将不同字体档位的css文件输出到static目录(生产环境在根目录)下的配置文件片段:
62+
63+
```javascript
64+
// 因为要输出文件的关系,这里需要引入fs
65+
import fs from 'fs'
66+
67+
68+
function deleteFolderRecursive (path) {
69+
if (fs.existsSync(path)) {
70+
fs.readdirSync(path).forEach(function (file) {
71+
const curPath = path + '/' + file
72+
if (fs.statSync(curPath).isDirectory()) { // recurse
73+
deleteFolderRecursive(curPath)
74+
} else { // delete file
75+
fs.unlinkSync(curPath)
76+
}
77+
})
78+
fs.rmdirSync(path)
79+
}
80+
}
81+
if (__PROD__) {
82+
// 生产环境下将fontGear文件输出到dist目录,需要在每次compile的时候判断是否存在dist目录,若不存在 则生成,并且在dist目录下生成fontGear目录;若存在fontGear目录则需删除其中的文件
83+
if (!fs.existsSync(paths.dist())) {
84+
fs.mkdirSync(paths.dist())
85+
fs.mkdirSync(paths.dist('fontGear'))
86+
} else {
87+
deleteFolderRecursive(paths.dist('fontGear'))
88+
if (!fs.existsSync(paths.dist('fontGear'))) {
89+
fs.mkdirSync(paths.dist('fontGear'))
90+
}
91+
}
92+
} else {
93+
// 非生产环境下需要在每次compile时删除static下的fontGear目录及文件
94+
if (fs.existsSync(paths.src('static/fontGear'))) {
95+
deleteFolderRecursive(paths.src('static/fontGear'))
96+
}
97+
}
98+
99+
// ------------------------------------
100+
// Plugins
101+
// ------------------------------------
102+
// 用于保存每次调用生成的postcss数据结构,以避免生成重复的样式声明
103+
const cachedRoot = {}
104+
105+
const vueLoaderOptions = {
106+
postcss: pack => {
107+
return [
108+
require('postcss-import')({
109+
path: paths.src(`themes/${config.theme}`)
110+
}),
111+
require('postcss-url')({
112+
basePath: paths.src('static')
113+
}),
114+
require('postcss-cssnext')({
115+
// see: https://github.com/ai/browserslist#queries
116+
browsers: 'Android >= 4, iOS >= 7',
117+
features: {
118+
customProperties: {
119+
variables: require(paths.src(`themes/${config.theme}/variables`))
120+
}
121+
}
122+
}),
123+
require('postcss-browser-reporter')(),
124+
require('postcss-reporter')(),
125+
// 由于开启postcss-flexible的字体档位功能直接输出文件的关系,这里将插件放在postcss插件的最后一 个,以保证其他插件的功能均已转换完成
126+
require('postcss-flexible')({
127+
remUnit: 75,
128+
enableFontSetting: true, // 开启字体档位功能
129+
fontGear: [-1, 0, 1, 2, 3, 4], // 自定义字体档位
130+
// 自定义输出函数
131+
outputCSSFile: (gear, clonedRoot) => {
132+
const content = clonedRoot.toString()
133+
if (!(gear in cachedRoot)) {
134+
cachedRoot[gear] = []
135+
}
136+
// 只将没有输出过的postcss结构写入到对应的文件中
137+
if (cachedRoot[gear].indexOf(content) === -1) {
138+
if (__PROD__) {
139+
fs.writeFileSync(paths.dist(`fontGear/fontGear_${gear}.css`), clonedRoot, {
140+
encoding: 'utf8',
141+
flag: 'a'
142+
})
143+
} else {
144+
if (!fs.existsSync(paths.src('static/fontGear'))) {
145+
fs.mkdirSync(paths.src('static/fontGear'))
146+
}
147+
fs.writeFileSync(paths.src(`static/fontGear/fontGear_${gear}.css`), content, {
148+
encoding: 'utf8',
149+
flag: 'a'
150+
})
151+
}
152+
cachedRoot[gear].push(content)
153+
}
154+
}
155+
})
156+
]
157+
},
158+
autoprefixer: false
159+
}
160+
161+
```
162+
### 项目中引入
163+
在入口文件中获取字体档位,并向根元素添加data-fontgear属性,根据字体档位加载对应的css文件:
164+
```javascript
165+
if (window.Bridge) {
166+
const appFontSet = window.Bridge.require('im.app.setting.font.size')
167+
const appFontNum = appFontSet.getCurrentGearValue() || 0
168+
const root = document.documentElement
169+
root.setAttribute('data-fontgear', appFontNum)
170+
const link = document.createElement('link')
171+
link.rel = 'stylesheet'
172+
link.type = 'text/css'
173+
link.href = `./fontGear/fontGear_${appFontNum}.css`
174+
document.head.appendChild(link)
175+
}
176+
```

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "postcss-flexible",
3-
"version": "0.5.2",
3+
"version": "0.5.3",
44
"description": "flexible transformer for flexible",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)