-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.es6.js
259 lines (231 loc) · 6.91 KB
/
gulpfile.es6.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
import gulp from 'gulp';
import gulpLoadPlugins from 'gulp-load-plugins';
import gifsicle from 'imagemin-gifsicle';
import pngquant from 'imagemin-pngquant';
import jpegtran from 'imagemin-jpegtran';
import browsersync from 'browser-sync';
import lazypipe from 'lazypipe';
import runSequence from 'run-sequence';
import config from './gulp-config.es6';
import child_process from 'child_process';
import stylish from 'jshint-stylish';
const $ = gulpLoadPlugins();
//启动本地serve
gulp.task('browsersync', () => {
browsersync(config.browsersync);
});
/**
* 删除 _assets文件
*/
gulp.task('clean', (cb) => {
return gulp.src(config.clean.build)
.pipe($.clean())
.on('close', cb);;
});
/**
* 删除 development文件夹
*/
gulp.task('clean:dev', (cb) => {
return gulp.src(config.clean.dev)
.pipe($.clean())
.on('close', cb);;
});
/**
* jekyll 设置
*/
gulp.task('jekyll', (done) => {
browsersync.notify('Compiling jekyll!');
let jekyllConfig = config.jekyll;
return child_process.spawn('bundle', ['exec', 'jekyll', 'build', '-q', '--source=' + jekyllConfig.src, '--destination=' + jekyllConfig.dest, '--config=' + jekyllConfig.config], { stdio: 'inherit' })
.on('close', done);
});
gulp.task('jekyll:rebuild', ['jekyll'], () => {
browsersync.reload();
});
/**
* images
*/
gulp.task('images', () => {
let imagesConfig = config.images;
return gulp.src(imagesConfig.src)
.pipe($.imagemin(
[ gifsicle({interlaced: true}),
jpegtran({ progressive: true }),
pngquant()
], {
optimizationLevel: 5,
verbose: true
}
))
.pipe(gulp.dest(imagesConfig.dest.build));
});
/**
* images:dev
*/
gulp.task('images:dev', () => {
let imagesConfig = config.images;
return gulp.src(imagesConfig.src)
.pipe(gulp.dest(imagesConfig.dest.dev));
});
/**
* fonts
*/
gulp.task('fonts', () => {
let fontsConfig = config.fonts;
return gulp.src(fontsConfig.src)
.pipe(gulp.dest(fontsConfig.dest.build));
})
/**
* fonts:dev
*/
gulp.task('fonts:dev', () => {
let fontsConfig = config.fonts;
return gulp.src(fontsConfig.src)
.pipe(gulp.dest(fontsConfig.dest.dev));
})
/**
* 将styles中的css文件压缩复制,已压缩文件直接复制
*/
gulp.task('styles', () => {
let stylesConfig = config.styles;
const filter = $.filter(['*.css', '!*.min.css'], { restore: true });
return gulp.src(stylesConfig.src)
.pipe($.sourcemaps.init())
.pipe(filter)
.pipe($.cleanCss({ keepSpecialComments: 0 }))
.pipe($.rename({ extname: '.min.css' }))
.pipe($.sourcemaps.write('.', { includeContent: false, sourceRoot: '_assets/styles' }))
.pipe(filter.restore())
.pipe(gulp.dest(stylesConfig.dest.build));
});
/**
* styles:dev
*/
gulp.task('styles:dev', () => {
let stylesConfig = config.styles;
const filter = $.filter(['*.css', '!*.min.css'], { restore: true });
return gulp.src(stylesConfig.src)
.pipe(filter)
.pipe($.rename({ extname: '.min.css' }))
.pipe(filter.restore())
.pipe(gulp.dest(stylesConfig.dest.dev));
});
/**
* scss 转化为css ,并且生成map文件,进行压缩
*/
gulp.task('sass', () => {
let sassConfig = config.sass;
sassConfig.options.onError = browsersync.notify;
const filter = $.filter(['**/*.css', '!**/*.map'], { restore: true });
browsersync.notify('Compiling sass!');
// 使用的是rubysass ,gem需要安装 sass,如果使用bundle install无法安装,直接使用gem安装
return $.rubySass(sassConfig.src, sassConfig.options)
.pipe($.plumber())
.pipe($.sourcemaps.init())
.pipe($.autoprefixer(config.autoprefixer))
.pipe(filter)
.pipe($.cleanCss({ keepSpecialComments: 0 }))
.pipe($.rename({ extname: '.min.css' }))
.pipe($.sourcemaps.write('.', { includeContent: false }))
.pipe(filter.restore()) //这里filter.restore要使用(),官网上的没有,会报错
.pipe(gulp.dest(sassConfig.dest.build));
});
/**
* scss 转化为css ,并且生成map文件,进行压缩
*/
gulp.task('sass:dev', () => {
let sassConfig = config.sass;
sassConfig.options.onError = browsersync.notify;
const filter = $.filter(['**/*.css', '!**/*.map'], { restore: true });
browsersync.notify('Compiling sass!');
// 使用的是rubysass ,gem需要安装 sass,如果使用bundle install无法安装,直接使用gem安装
return $.rubySass(sassConfig.src, sassConfig.options)
.pipe($.plumber())
.pipe($.sourcemaps.init())
.pipe($.autoprefixer(config.autoprefixer))
.pipe(filter)
.pipe($.rename({ extname: '.min.css' }))
.pipe($.sourcemaps.write('.', { includeContent: false }))
.pipe(filter.restore()) //这里filter.restore要使用(),官网上的没有,会报错
.pipe(gulp.dest(sassConfig.dest.dev));
});
/**
* Lint SCSS 文件 检查
*/
gulp.task('scsslint', () => {
let scssConfig = config.scsslint;
return gulp.src(scssConfig.src)
.pipe($.scssLint(scssConfig.options));
});
/**
* 检查js语法
*/
gulp.task('jshint', () => {
let jshintConfig = config.jshint;
return gulp.src(jshintConfig.src)
.pipe($.jshint('.jshintrc'))
.pipe($.jshint.reporter(stylish));
});
/**
* es6转化为es5,并进行压缩
*/
gulp.task('scripts', (done) => {
let babelConfig = config.scripts.babel;
return gulp.src(babelConfig.src)
.pipe($.babel(babelConfig.options))
.pipe($.uglify())
.pipe($.rename({ extname: '.min.js' }))
.pipe(gulp.dest(babelConfig.dest.build))
.on('close', done);
});
/**
* es6转化为es5,并进行压缩
*/
gulp.task('scripts:dev', (done) => {
let babelConfig = config.scripts.babel;
return gulp.src(babelConfig.src)
.pipe($.babel(babelConfig.options))
.pipe($.uglify())
.pipe($.rename({ extname: '.min.js' }))
.pipe(gulp.dest(babelConfig.dest.dev))
.on('close', done);
});
/**
* watch
*/
gulp.task('watch', () => {
gulp.watch(config.watch.jekyll, ['jekyll:rebuild']);
gulp.watch(config.watch.sass, ['sass:dev', 'scsslint']);
gulp.watch(config.watch.scripts, ['scripts:dev', 'jshint']);
gulp.watch(config.watch.images, ['images:dev']);
});
/**
* 运行部分task,生成调试文件
*/
gulp.task('dev', (cb) => {
runSequence('clean:dev',
[
'jekyll',
'styles:dev',
'sass:dev',
'scripts:dev',
'images:dev',
'fonts:dev'
],
'browsersync',
'watch',
// 'base64',
cb);
});
gulp.task('build', (cb) => {
runSequence('clean',
[
'styles',
'sass',
'scripts',
'images',
'fonts'
],
// 'base64',
cb);
});