-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathgulpfile.js
112 lines (100 loc) · 3.44 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
// require
var fs = require('fs');
var path = require('path');
var gulp = require('gulp');
var gutil = require('gulp-util');
var plumber = require('gulp-plumber');
var rubySass = require('gulp-ruby-sass');
var nodeSass = require('gulp-sass-china');
var connect = require('gulp-connect');
var through = require('through2');
var optimist = require('optimist');
var versions = require('./gulp/versions.js');
var hanlders = require('./gulp/hanlders.js');
// style path,由业务自己配置
var scssPath = './style/page';
var cssPath = './style/export';
// 编译器
var compilers = {
'node-sass': function(scssPath, cssPath) {
return gulp.src(scssPath + '/*.scss')
.pipe(plumber({
errorHandler: hanlders.error
}))
.pipe(nodeSass({
outputStyle: 'compressed',
importer: require('node-sass-import-once'),
importerOnce: {
css: true
}
}))
.pipe(gulp.dest(cssPath))
.on('end', hanlders.end);
},
'sass': function(scssPath, cssPath) {
return rubySass(scssPath + '/*.scss', {
style: 'compressed'
})
.pipe(plumber({
errorHandler: hanlders.error
}))
.pipe(gulp.dest(cssPath))
.on('end', hanlders.end);
}
};
// 命令: gulp compile, 进行node-sass编译 [-c node-sass/sass]
gulp.task('compile', function() {
var argv = optimist.argv,
compiler = argv.c || argv.compiler || require('./package.json').compiler;
if (compilers[compiler]) {
gutil.log(gutil.colors.yellow('使用编译器 ' + compiler + ' 编译...'));
gutil.log(gutil.colors.green('版本为 ' + versions[compiler]));
return compilers[compiler](scssPath, cssPath);
} else {
gutil.log(gutil.colors.red('找不到编译器 ' + compiler));
}
});
// 命令: gulp watch, 监听工程中scss文件变化时,执行compile操作 [-c node-sass/sass]
gulp.task('watch', function() {
gulp.watch('./**/**/*.scss', ['compile']);
});
// 命令: gulp clear, 清理 ruby sass 编译时产生的缓存
gulp.task('clear', function() {
rubySass.clearCache();
});
// 命令: gulp version, 获取Yo、Sass和Node-sass的版本信息
gulp.task('version', function() {
gutil.log(gutil.colors.green('Yo: ' + versions.yo));
gutil.log(gutil.colors.green('Sass: ' + versions.sass));
gutil.log(gutil.colors.green('Node-sass: ' + versions['node-sass']));
});
// 命令: gulp test, 测试任务
gulp.task('test', function() {
return gulp.src('./style/usage/test/test.scss')
.pipe(plumber({
errorHandler: hanlders.error
}))
.pipe(nodeSass({
outputStyle: 'compressed'
}))
.pipe(gulp.dest('./style/usage/test'));
});
gulp.task('default', function() {
gutil.log('可以使用的命令如下: ');
fs.readFileSync('./gulpfile.js', 'UTF-8').replace(/\/\/\s命令\:\sgulp\s(\w+)\,\s([^\n]+)/g, function(a, b, c) {
gutil.log(gutil.colors.green(b), gutil.colors.blue(c));
});
});
gulp.task('reload', ['compile'], function() {
return gulp.src('./**/**/*.css')
.pipe(connect.reload());
});
gulp.task('watch-reload', function() {
gulp.watch('./**/**/*.scss', ['reload']);
});
// 命令: gulp server, 启动 Web 服务
gulp.task('server', ['watch-reload'], function() {
connect.server({
livereload: true
});
});