This repository was archived by the owner on Dec 3, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathgulpfile.js
195 lines (173 loc) · 4.88 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
var lr = require('tiny-lr'),
server = lr(),
gulp = require('gulp'),
compass = require('gulp-compass'),
livereload = require('gulp-livereload'),
uglify = require('gulp-uglify'),
plumber = require('gulp-plumber'),
webserver = require('gulp-webserver'),
opn = require('opn'),
concat = require('gulp-concat'),
clean = require('gulp-clean'),
imagemin = require('gulp-imagemin'),
pngquant = require('imagemin-pngquant'),
rename = require("gulp-rename"),
zip = require('gulp-zip'),
copy = require("gulp-copy"),
tinypng = require('gulp-tinypng'),
sftp = require('gulp-sftp'),
config = require('./config.json');
//压缩javascript 文件,压缩后文件放入build/js下
gulp.task('minifyjs',function(){
gulp.src('js/*.js')
.pipe(uglify())
.pipe(gulp.dest('./js/min'))
});
//合并build/js文件夹下的所有javascript 文件为一个main.js放入build/js下
gulp.task('alljs', function() {
return gulp.src('./build/js/*.js')
.pipe(concat('main.min.js'))
.pipe(gulp.dest('./js/min'));
});
//重命名project.md 文件
gulp.task('rename', function() {
return gulp.src("./Project.md")
.pipe(rename("README.md"))
.pipe(gulp.dest("./build"));
});
//开启本地 Web 服务器功能
gulp.task('webserver', function() {
gulp.src( './' )
.pipe(webserver({
host: config.localserver.host,
port: config.localserver.port,
livereload: true,
directoryListing: false
}));
});
//通过浏览器打开本地 Web服务器 路径
gulp.task('openbrowser', function() {
opn( 'http://' + config.localserver.host + ':' + config.localserver.port );
});
//Compass 进行SASS 代码
gulp.task('compass', function() {
gulp.src('./sass/*.scss')
.pipe(plumber())
.pipe(compass({
config_file: './config.rb'
}));
});
//多余文件删除
gulp.task('clean', function () {
return gulp.src('./.sass-cache')
.pipe(clean({force: true}))
.pipe(gulp.dest('./clean'));
});
//压缩图片
gulp.task('imagemin', function () {
return gulp.src('images/*')
.pipe(imagemin({
progressive: true,
svgoPlugins: [{removeViewBox: false}],
use: [pngquant()]
}))
.pipe(gulp.dest('./build/images'));
});
//压缩图片 - tinypng
gulp.task('tinypng', function () {
gulp.src('images/*.{png,jpg,jpeg}')
.pipe(tinypng(config.tinypngapi))
.pipe(gulp.dest('./build/images'));
});
//将相关项目文件复制到build 文件夹下
gulp.task('buildfiles', function() {
//根目录文件
gulp.src('./*.{php,html,css,png}')
.pipe(gulp.dest('./build'));
//CSS文件
gulp.src('./css/*')
.pipe(gulp.dest('./build/css'));
//压缩后的js文件
gulp.src('./js/min/*')
.pipe(gulp.dest('./build/js'));
});
//文件监控
gulp.task('watch', function () {
server.listen(35729, function (err) {
if (err){
return console.log(err);
}
});
gulp.watch('./sass/*.scss', function (e) {
gulp.run('compass');
server.changed({
body: {
files: [e.path]
}
});
});
gulp.watch(['./sass/*.scss','./*.html','./*.php','./*.css','./js/*.js'], function (e) {
server.changed({
body: {
files: [e.path]
}
});
});
});
//默认任务
gulp.task('default', function(){
console.log('Starting Gulp tasks, enjoy coding!');
gulp.run('compass');
gulp.run('watch');
gulp.run('webserver');
gulp.run('openbrowser');
});
//项目完成提交任务
gulp.task('build', function(){
gulp.run('imagemin');
gulp.run('compass');
gulp.run('minifyjs');
gulp.run('alljs');
gulp.run('buildfiles');
gulp.run('rename');
//gulp.run('clean');
});
//项目完成提交任务
gulp.task('build2', function(){
gulp.run('tinypng');
gulp.run('compass');
gulp.run('minifyjs');
gulp.run('alljs');
gulp.run('buildfiles');
gulp.run('rename');
//gulp.run('clean');
});
//打包主体build 文件夹并按照时间重命名
gulp.task('zip', function(){
function checkTime(i) {
if (i < 10) {
i = "0" + i
}
return i
}
var d=new Date();
var year=d.getFullYear();
var month=checkTime(d.getMonth() + 1);
var day=checkTime(d.getDate());
var hour=checkTime(d.getHours());
var minute=checkTime(d.getMinutes());
return gulp.src('./build/**')
.pipe(zip( config.project+'-'+year+month+day +hour+minute+'.zip'))
.pipe(gulp.dest('./'));
});
//上传到远程服务器任务
gulp.task('upload', function () {
return gulp.src('./build/**')
.pipe(sftp({
host: config.sftp.host,
user: config.sftp.user,
port: config.sftp.port,
key: config.sftp.key,
remotePath: config.sftp.remotePath
}));
});