-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
179 lines (176 loc) · 3.91 KB
/
gulpfile.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
'use strict'
const gulp = require('gulp')
const cleanCSS = require('gulp-clean-css')
const postcss = require('gulp-postcss')
const stylus = require('gulp-stylus')
const pxToUnits = require('postcss-px2units')
const pxToViewPort = require('postcss-px-to-viewport')
const cssnano = require('cssnano')
const presetenv = require('postcss-preset-env')
const rename = require('gulp-rename')
const toBem = require('postcss-bem-fix')
const toNested = require('postcss-nested')
const bemConfig = {
defaultNamespace: 'ce', // default namespace to use, none by default
style: 'suit', // suit or bem, suit by default,
shortcuts: {
component: 'b',
modifier: 'm',
descendent: 'e'
},
separators: {
namespace: '-',
descendent: '__',
modifier: '--'
}
}
const compile = {
/**
* 编译vw单位文件
*/
cssToVw() {
return gulp
.src('./src/*.styl')
.pipe(stylus())
.pipe(
postcss([
toBem(bemConfig),
toNested(),
pxToViewPort({
viewportWidth: 750, // (Number) The width of the viewport.
viewportHeight: 1334, // (Number) The height of the viewport.
unitPrecision: 3, // (Number) 转换的时候除不尽保留3位小数.
viewportUnit: 'vw', // (String) 转换为vw单位.
selectorBlackList: ['.ignore', '.hairlines'], // (Array) The selectors to ignore and leave as px.
minPixelValue: 1, // (Number) 小于或等于'1px'不转换为视窗单位.
mediaQuery: false, // (Boolean) Allow px to be converted in media queries.
unitToConvert: 'px'
}),
presetenv(),
cssnano({
'cssnano-preset-advanced': {
zIndex: false,
autoprefixer: true
}
})
])
)
.pipe(cleanCSS())
.pipe(
rename({
suffix: '.vw'
})
)
.pipe(gulp.dest('./lib'))
},
/**
* 编译px单位文件
*/
cssToPx() {
return gulp
.src('./src/*.styl')
.pipe(stylus())
.pipe(
postcss([
toBem(bemConfig),
toNested(),
presetenv(),
pxToUnits({
divisor: 1,
targetUnits: 'px'
})
])
)
.pipe(cleanCSS())
.pipe(
rename({
suffix: '.px'
})
)
.pipe(gulp.dest('./lib'))
},
/**
* build css
*/
cssToRelease() {
return gulp
.src('./src/*.styl')
.pipe(stylus())
.pipe(
postcss([
toBem(bemConfig),
toNested(),
presetenv(),
pxToUnits({
divisor: 1,
targetUnits: 'px'
})
])
)
.pipe(cleanCSS())
.pipe(
rename({
extname: '.css'
})
)
.pipe(gulp.dest('./lib'))
},
/**
* dev css
*/
cssToDev() {
return gulp
.src([
'./src/common/**/*.styl',
'./src/components/**/*.styl',
'./src/*.styl'
])
.pipe(stylus())
.pipe(
postcss([
toBem(bemConfig),
toNested(),
presetenv(),
pxToUnits({
divisor: 1,
targetUnits: 'px'
})
])
)
.pipe(cleanCSS())
.pipe(
rename({
extname: '.css'
})
)
.pipe(gulp.dest('./lib'))
},
}
const watch = {
/**
* 监视构建css
*/
css() {
return gulp.watch(
[
'./src/common/**',
'./src/common/**',
'./src/components/**',
'./src/mixins/**',
'./src/*.styl'
],
gulp.parallel(compile.cssToDev)
)
},
}
exports.build = gulp.parallel(
// compileCssToVw, // 构建 vw_css
// compileCssToPx, // 构建 px_css
compile.cssToRelease,
)
exports.default = gulp.series(
// compile.cssToVw, // 构建 vw_css
// compile.cssToPx, // 构建 px_css
compile.cssToDev,
gulp.parallel(watch.css)
)