-
Notifications
You must be signed in to change notification settings - Fork 0
/
decl.js
272 lines (215 loc) · 6.5 KB
/
decl.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
const path = require('path')
const postcss = require('postcss')
const postcssScss = require('postcss-scss')
const fs = require('fs-extra')
const htmlparser2 = require('htmlparser2')
const sourcesPath = '../../src'
const declMap = {}
const base64Map = {}
const flexKeys = ['flex', 'flex-center-xy', 'flex-column', 'flex-row']
let flexTotal = 0
let onece = 0
const strSize = (str, charset = 'utf8') => {
let total = 0
charset = charset.toLowerCase() || ''
for (let i = 0; i < str.length; i++) {
const charCode = str.charCodeAt(i)
if (charset === 'utf-16' || charset === 'utf16') {
total += charCode <= 0xffff ? 2 : 4
} else {
if (charCode <= 0x007f) {
total += 1
} else if (charCode <= 0x07ff) {
total += 2
} else if (charCode <= 0xffff) {
total += 3
} else {
total += 4
}
}
}
return total
}
// https://www.postcss.com.cn/
// 根据路径获取文件、文件夹
const getFiles = filePath => fs.readdirSync(path.join(__dirname, filePath))
// 根据 file path 径获取 file stats
const getStatsSync = filePath => {
return new Promise(resolve => {
fs.stat(path.join(__dirname, filePath), (err, stats) => {
if (!err) resolve(stats)
})
})
}
// 获取当前处理的模块所有文件的路径集合
const getDelModuleFilesPath = (() => {
// 处理模块的 config 路径
const filePaths = []
return async function(filePath) {
const files = getFiles(filePath)
for (const file of files) {
const nextLevelFilePath = `${filePath}/${file}`
const stats = await getStatsSync(nextLevelFilePath)
// 为文件夹则继续查找路径
stats.isDirectory()
? // eslint-disable-next-line no-caller
await arguments.callee(nextLevelFilePath)
: filePaths.push(nextLevelFilePath)
}
return filePaths
}
})()
const getScssFilePath = filesPath =>
filesPath.filter(path => /.scss$/.test(path))
const collectDecl = postcss.plugin('collect-decl', scssPath => {
return root => {
root.walkDecls(decl => {
const { prop, value } = decl
const declKey = `${prop}:${value}`
if (value.includes('data:image')) {
base64Map[scssPath] = (base64Map[scssPath] || '') + value
} else {
declMap[declKey] = (declMap[declKey] || 0) + 1
}
})
root.walkAtRules(rule => {})
}
})
const parserWxml = code => {
const parser = new htmlparser2.Parser({
onattribute(name, value) {
if (name === 'class') {
const valArr = value.split(' ')
const matchNumbers = flexKeys.reduce((pre, cur) => {
if (valArr.find(el => el === cur)) {
pre += 1
}
return pre
}, 0)
// console.log(222, name, value)
if (matchNumbers > 1) {
// 在同一个元素上使用 flexKeys 中的类多次
flexTotal += 1
// console.log('重复', matchNumbers, value.split(' '))
} else if (matchNumbers === 1 && !valArr.find(el => el === 'flex')) {
// 使用 flexKeys 中的类且不是 flex 类,可替换为 flex 类,减少体积
onece += 1
// console.log(333, valArr)
}
}
}
})
parser.write(code)
parser.end()
return []
}
const parserScss = (css, scssPath) => {
postcss([collectDecl(scssPath)])
.process(css, { parser: postcssScss })
.then(result => {})
}
getDelModuleFilesPath(sourcesPath)
.then(filePaths => {
const scssFilePaths = getScssFilePath(filePaths)
return scssFilePaths
})
.then(res => {
for (const scssPath of res) {
const scssSource = fs.readFileSync(scssPath, {
encoding: 'utf-8'
})
const wxmlPath = scssPath.replace('.scss', '.wxml')
const sourceWxml =
fs.existsSync(wxmlPath) &&
fs.readFileSync(wxmlPath, {
encoding: 'utf-8'
})
parserScss(scssSource, scssPath)
// if (wxmlPath.includes('refundApply')) {
parserWxml(sourceWxml)
// }
}
console.log('在同一个元素上使用 flexKeys 中的类多次:', `${flexTotal} 次`)
console.log('使用 flexKeys 中的类且不是 flex 类:', `${onece} 次`)
const list = []
const base64List = []
// console.log(111, declMap)
const getStr = (name, counts) =>
new Array(counts).fill(`${name};`).reduce((pre, cur) => pre + cur, '')
const getStrSize = str => (strSize(str) / 1000).toFixed(1)
// 样式 decl
for (const key in declMap) {
const counts = declMap[key]
const declStr = getStr(key, counts)
list.push({
name: key,
counts,
size: getStrSize(declStr)
})
}
// base64
for (const key in base64Map) {
const base64 = base64Map[key]
base64List.push({
name: key,
size: getStrSize(base64)
})
}
//
// const descList = list.sort((a, b) => b.counts - a.counts)
const sizeBeyondOneKbList = list
.filter(el => el.size >= 1)
.sort((a, b) => b.size - a.size)
const sizeBeyondOneKbTotals = sizeBeyondOneKbList.reduce(
(pre, cur) => {
pre.name.push(cur.name)
pre.count.push(cur.counts)
pre.val.push(cur.size)
pre.size = pre.size + Number(cur.size)
return pre
},
{
name: [],
count: [],
val: [],
size: 0
}
)
const listDecs = list.sort((a, b) => b.counts - a.counts)
const countBeyond100 = listDecs.filter(el => el.counts >= 100)
const countBeyond100Totals = countBeyond100.reduce(
(pre, cur) => {
pre.name.push(cur.name)
pre.count.push(cur.counts)
pre.val.push(cur.size)
pre.size = pre.size + Number(cur.size)
return pre
},
{
name: [],
count: [],
val: [],
size: 0
}
)
const base64ListDecs = base64List.sort((a, b) => b.size - a.size)
const base64ListTotals = base64ListDecs.reduce(
(pre, cur) => {
pre.name.push(cur.name.slice(10))
pre.val.push(cur.size)
pre.size = pre.size + Number(cur.size)
return pre
},
{
name: [],
val: [],
size: 0
}
)
// delivery/pages/account/account.scss
console.log('base64:', base64ListTotals)
console.log('重复设置 99 次:', countBeyond100Totals)
console.log('样式属性大小超 1KB 的属性总计:', sizeBeyondOneKbTotals)
// fs.writeJSONSync('./base64.json', base64ListDecs)
// fs.writeJSONSync('./declMorethanOneKB.json', sizeBeyondOneKbList)
})