-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
147 lines (145 loc) · 4.05 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
var gulp = require('gulp');
var plumber = require('gulp-plumber');
var del = require('del');
var gulpSequence = require('gulp-sequence');
var htmlmin = require('gulp-htmlmin');
var imagemin = require('gulp-imagemin');
var cssmin = require('gulp-minify-css');
var usemin = require('gulp-usemin');
var uglify = require('gulp-uglify');
var replace = require('gulp-replace');
var rev = require('gulp-rev');
var tinypng = require('gulp-tinypng');
var env = process.env.NODE_ENV || "development";
var agent = process.env.agent || 'yg';
var config = {
app: "gulp-demo",
src: "app",
dist: "dist",
env: env,
tmp: ".tmp",
entries: "app/scripts/main.js",
agent: agent
};
gulp.task('clean:dist',function(){
var dir = [config.tmp,config.dist];
var option = {dot:true};
return del(dir,option);
});
gulp.task('imagemin', function() {
return gulp.src(config.src + "/images/**/*.{png,jpg,jpeg,gif}")
.pipe(tinypng('BZ9n7RMXnXLTZfgTLAZzQ7--KDHkX4Ov'))
.pipe(gulp.dest(config.dist + "/images"));
});
gulp.task('imagemin:mobile', function() {
return gulp.src(config.src + "/mobile/images/**/*.{png,jpg,jpeg,gif}")
.pipe(tinypng('BZ9n7RMXnXLTZfgTLAZzQ7--KDHkX4Ov'))
.pipe(gulp.dest(config.dist + "/mobile/images"));
});
gulp.task('copycss', function() {
return gulp.src(["styles/*.css","mobile/styles/*.css"], {
cwd: config.src,
dot: true,
base: config.src
})
.pipe(gulp.dest(config.tmp));
});
gulp.task('copyjs', function() {
return gulp.src(["scripts/*.js","mobile/scripts/*.js"], {
cwd: config.src,
dot: true,
base: config.src
})
.pipe(gulp.dest(config.tmp));
});
gulp.task('copyimages', function() {
return gulp.src(["images/**/*.{png,jpg,jpeg,gif,webp}","mobile/images/**/*.{png,jpg,jpeg,gif,webp}"], {
cwd: config.src,
dot: true,
base: config.src
})
.pipe(gulp.dest(config.dist));
});
gulp.task('copywebp', function() {
return gulp.src(["images/**/*.webp","mobile/images/**/*.webp"], {
cwd: config.src,
dot: true,
base: config.src
})
.pipe(gulp.dest(config.dist));
});
gulp.task('copydist', function() {
return gulp.src([
"*.{ico,txt}",
"mobile/*.{ico,txt}",
"*.html",
"mobile/*.html",
"fonts/*",
"mobile/fonts/*",
"music/*",
"mobile/music/*"
], {
cwd: config.src,
dot: true,
base: config.src
})
.pipe(gulp.dest(config.dist));
});
gulp.task('replace', function() {
var reg = /\{\n*(\s)*env:.*,\n*\s*agent:.*\s*\n*\}/;
var replacement = "{env:'" + config.env + "',agent:'" + config.agent +"'}";
return gulp.src(config.tmp + "/scripts/server.config.js")
.pipe(replace(reg, replacement))
.pipe(gulp.dest(config.tmp + "/scripts"));
});
gulp.task('replace:mobile', function() {
var reg = /\{\n*(\s)*env:.*,\n*\s*agent:.*\s*\n*\}/;
var replacement = "{env:'" + config.env + "',agent:'" + config.agent +"'}";
return gulp.src(config.tmp + "/mobile/scripts/server.config.js")
.pipe(replace(reg, replacement))
.pipe(gulp.dest(config.tmp + "/mobile/scripts"));
});
gulp.task('usemin', function() {
return gulp.src(config.src + "/**/*.html")
.pipe(usemin({
js: [uglify,rev],
css: [cssmin, rev]
}))
.pipe(gulp.dest(config.dist))
});
gulp.task('htmlmin', function() {
var htmlSrc = config.dist + "/**/*.html";
return gulp.src(htmlSrc)
.pipe(htmlmin({collapseWhitespace: true}))
.pipe(gulp.dest(config.dist));
});
/**
* 发布到测试环境
*/
gulp.task('build', gulpSequence(
"clean:dist",
"copyimages",
"copycss",
"copyjs",
"replace",
"replace:mobile",
"copydist",
"usemin",
"htmlmin"
));
/**
* 发布到现网环境-图片未压缩
*/
gulp.task('deploy', gulpSequence(
"clean:dist",
"copywebp",
"imagemin",
"imagemin:mobile",
"copycss",
"copyjs",
"replace",
"replace:mobile",
"copydist",
"usemin",
"htmlmin"
));