-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgulpfile.js
112 lines (100 loc) · 2.46 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
//'use strict';
var gulp = require('gulp');
var path = require('path');
var browserSync = require('browser-sync');
var reload = browserSync.reload;
var sprity = require('sprity');
var program = require('commander');
var $ = require('gulp-load-plugins')();
var project = 'cleardemo';
// 命令行参数
// 定义参数,以及参数内容的描述
program
.version('0.0.1')
.usage('[options] [value ...]')
.option('-p, --project <string>', 'input a string arg');
//解析commandline arguments
program.parse(process.argv);
if (program.project) {
project = program.project;
}
//
const sassPath = project + '/static/sass';
const es6Path = project + '/static/es6/**/*.js';
const cssPath = project + '/static/css';
const jsPath = project + '/static/js';
const imgPath = project + '/static/img';
/* web server */
gulp.task('browser-sync', function() {
browserSync({
logPrefix: '服务启动',
server: {
baseDir: './' + project
}
});
});
/* 实时刷新 */
gulp.task('bs-reload', function() {
gulp.src(['*/*.html', project + '/static/js/**/*.js']).pipe(browserSync.reload({
stream: true
}));
});
/* sass */
var sassOptions = {
outputStyle: 'expanded',
indentType: 'tab',
indentWidth: 1,
}
var sassFile;
gulp.task('sass', function() {
var src = sassFile || sassPath + '/**/*.scss';
return gulp.src(src)
.pipe($.sass(sassOptions))
.on('error', function(e) {
$.util.log($.util.colors.red('Error: ' + e.message.replace(/\n/g, '')));
this.emit('end');
})
.pipe($.autoprefixer())
.pipe(gulp.dest(project + '/static/css'))
.pipe(reload({
stream: true
}));
});
/* sprite */
gulp.task('sprite', function() {
return sprity.src({
src: imgPath + '/sprites/**/*.png',
split: true,
style: 'sprite.scss',
processor: 'sass'
})
.pipe($.if('*.png', gulp.dest(imgPath), gulp.dest(sassPath)));
});
/* babel */
var es6File;
gulp.task('babel', function(res) {
var src = es6File || es6Path;
return gulp.src(src)
.pipe($.babel())
.on('error', function(e) {
$.util.log($.util.colors.red('Error: ' + e.message));
this.emit('end');
})
// .pipe($.rename({
// suffix: "-es5"
// }))
.pipe(gulp.dest(jsPath))
.pipe(reload({
stream: true
}));
})
/* default */
gulp.task('default', ['browser-sync'], function() {
gulp.watch(project + '/*.html').on('change', reload);
gulp.watch(sassPath + '/**/*.scss', ['sass']).on('change', function (e) {
sassFile = e.path;
});
gulp.watch(es6Path, ['babel']).on('change', function (e) {
es6File = e.path;
});
});