-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathatomcss-loader.js
More file actions
225 lines (204 loc) · 6.3 KB
/
atomcss-loader.js
File metadata and controls
225 lines (204 loc) · 6.3 KB
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
/**
* 原子类定义如下:
*
* margin:m、ml、mr、mt、mb、mx、my
* padding:p、pl、pr、pt、pb、px、py
* width:w、w-p
* height:h、h-p
* 四方向:l、r、t、b
* 行高:lh
* 字体:fs、fw
* border-radius:br
*
*/
const fs = require('fs');
let oClassNameMap = {
// margin
'.m': 'margin:$px',
'.ml': 'margin-left:$px',
'.mr': 'margin-right:$px',
'.mt': 'margin-top:$px',
'.mb': 'margin-bottom:$px',
'.mx': 'margin-left:$px;margin-right:$px',
'.my': 'margin-top:$px;margin-bottom:$px',
// margin !important
'.mi': 'margin:$px !important',
'.mli': 'margin-left:$px !important',
'.mri': 'margin-right:$px !important',
'.mti': 'margin-top:$px !important',
'.mbi': 'margin-bottom:$px !important',
'.mxi': 'margin-left:$px !important;margin-right:$px !important',
'.myi': 'margin-top:$px !important;margin-bottom:$px !important',
// padding
'.p': 'padding:$px',
'.pl': 'padding-left:$px',
'.pr': 'padding-right:$px',
'.pt': 'padding-top:$px',
'.pb': 'padding-bottom:$px',
'.px': 'padding-left:$px;padding-right:$px',
'.py': 'padding-top:$px;padding-bottom:$px',
// padding !important
'.pi': 'padding:$px !important',
'.pli': 'padding-left:$px !important',
'.pri': 'padding-right:$px !important',
'.pti': 'padding-top:$px !important',
'.pbi': 'padding-bottom:$px !important',
'.pxi': 'padding-left:$px !important;padding-right:$px !important',
'.pyi': 'padding-top:$px !important;padding-bottom:$px !important',
// width
'.w': 'width:$px',
'.wi': 'width:$px !important',
'.mw': 'min-width:$px',
'.wp': 'width:$%',
'.wpi': 'width:$% !important',
// height
'.h': 'height:$px',
'.hi': 'height:$px !important',
'.mh': 'min-height:$px',
'.hp': 'height:$%',
'.hpi': 'height:$% !important',
// 四方向
'.l': 'left:$px',
'.r': 'right:$px',
'.t': 'top:$px',
'.b': 'bottom:$px',
// 行高
'.lh': 'line-height:$px',
// 字体
'.fs': 'font-size:$px',
'.fw': 'font-weight:$',
// background
'.bgs': 'background-size:$px',
// border
'.br': 'border-radius:$px',
// 颜色
'.c': 'color: #',
'.bgc': 'background-color: #',
// z-index
'.z': 'z-index: $',
};
let oAtomConfig = {};
// 读取配置文件,如果不存在,就是用默认的配置文件
try {
oAtomConfig = require(__dirname + '/../../atomcss.config.js');
} catch (e) {
oAtomConfig = require(__dirname + '/atomcss.config.js');
}
// 如果模式为 rem,则将 px 替换为 rem
if (oAtomConfig.mode === 'rem') {
for (let key in oClassNameMap) {
oClassNameMap[key] = oClassNameMap[key].replace(/\$px/gi, '$rem');
}
}
oClassNameMap = Object.assign(oClassNameMap, oAtomConfig.config);
// 生成正则表达式
let sAtomRegExp = '';
for (let key in oClassNameMap) {
let value = oClassNameMap[key];
// 数值原子类的正则
if (value.indexOf('$') != -1) {
sAtomRegExp += `\\${key}-[0-9]+|`;
}
// 色值原子类正则
else if (value.indexOf('#') != -1) {
sAtomRegExp += `\\${key}-[0-9a-fA-F]+|`;
// 通用原子类的正则
} else {
sAtomRegExp += `\\${key}|`;
}
}
// 去掉最后一个 | 符号
sAtomRegExp = sAtomRegExp.substr(0, sAtomRegExp.length - 1);
module.exports = function (sSource) {
// 从 vue 文件中提取 pug 代码
let sPugString, sHtmlString, sClassString;
try {
// 匹配 pug
sPugString = sSource.match(
/<template lang=("|')pug("|')>([\s\S]*)<\/template>/g
);
// 匹配 html
sHtmlString = sSource.match(/<template>([\s\S]*)<\/template>/g);
// html 文本需要特殊处理
if (sPugString) {
sClassString = sPugString[0];
} else if (sHtmlString) {
sClassString =
'.' +
(sHtmlString[0].match(/class=("|')([a-zA-Z0-9 \- _]*)("|')/gi) || [])
.map((item) =>
item
.replace(/class=('|")|("|')/g, '')
.split(' ')
.join('.')
)
.join('.');
}
} catch (e) {
console.warn(e);
return sSource;
}
// 没有找到 template 模板,则无需处理
if (!sClassString) return sSource;
// 支持 pug 文件内 include 的文件的编译
let pugFileList = sClassString.match(/include \S*\.pug/g) || [];
pugFileList.forEach((file) => {
let filePath =
this.resourcePath.substr(0, this.resourcePath.lastIndexOf('/') + 1) +
file.replace('include ', '');
// pug 文件改变,重新编译主文件
this.addDependency(filePath);
let content = fs.readFileSync(filePath, 'utf-8');
sClassString = sClassString.replace(file, content);
});
let atomReg = new RegExp(sAtomRegExp, 'ig');
// 获取 pc 端原子类类名数组,并剔除重复的类名
function uniq(value, index, self) {
return self.indexOf(value) === index;
}
let aClassName = (sClassString.match(atomReg) || []).filter(uniq);
// 输出 debug 数据
this.query.debug && console.log('\n文件:', this.resourcePath, this.query);
this.query.debug && console.log('desktop 类名:', aClassName);
// 原子类样式接收数组
let aStyleStr = [];
// 开始生成原子类
aClassName.forEach((item) => {
let bColorFlag =
oClassNameMap[item.split('-')[0]] &&
oClassNameMap[item.split('-')[0]].indexOf('$') == -1 &&
oClassNameMap[item.split('-')[0]].indexOf('#') != -1 &&
/^[0-9a-fA-F]+$/.test(item.split('-')[1]); // 十六进制
// 色值类
if (bColorFlag) {
let tmp = item.split('-');
if (tmp.length > 1) {
let sKey = tmp.slice(0, tmp.length - 1).join('-');
let nValue = '#' + tmp[tmp.length - 1];
oClassNameMap[sKey] &&
aStyleStr.push(
`${item}{${oClassNameMap[sKey].replace(/\#/g, nValue)}}`
);
}
}
// 数值类
else if (/\d+/.test(item)) {
let tmp = item.split('-');
if (tmp.length > 1) {
let sKey = tmp.slice(0, tmp.length - 1).join('-');
let nValue = tmp[tmp.length - 1];
oClassNameMap[sKey] &&
aStyleStr.push(
`${item}{${oClassNameMap[sKey].replace(/\$/g, nValue)}}`
);
}
// 通用类
} else {
let sKey = item;
oClassNameMap[sKey] && aStyleStr.push(`${item}{${oClassNameMap[sKey]}}`);
}
});
// 输出 debug 数据
this.query.debug && console.log('desktop 样式:', aStyleStr);
return `${sSource}\n<style>\n${aStyleStr.join('')}</style>\n`;
};