-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
179 lines (143 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
// library
var gulp = require('gulp');
var gulpSequence = require('gulp-sequence');
var argv = require('yargs').argv;
var _ = require('lodash');
var path = require('path');
var concat = require('gulp-concat');
var less = require('gulp-less');
var sourcemaps = require('gulp-sourcemaps');
var minify = require('gulp-minify');
var htmlmin = require('gulp-htmlmin');
var htmlminOptionsDefault = {
minifyJS: false, //压缩页面JS
removeComments: true
};
var htmlminOptionsDev = {
minifyJS: true, //压缩页面JS
removeComments: true
};
var htmlminOptionsBuild = {
removeComments: true, //清除HTML注释
collapseWhitespace: true, //压缩HTML
collapseBooleanAttributes: true, //省略布尔属性的值 <input checked="true"/> ==> <input />
removeEmptyAttributes: true, //删除所有空格作属性值 <input id="" /> ==> <input />
removeScriptTypeAttributes: true, //删除<script>的type="text/javascript"
removeStyleLinkTypeAttributes: true, //删除<style>和<link>的type="text/css"
minifyJS: true, //压缩页面JS
minifyCSS: true //压缩页面CSS
};
var uglify = require('gulp-uglify');
var cssnano = require('gulp-cssnano');
var jshint = require('gulp-jshint');
var imagemin = require('gulp-imagemin');
var replace = require('gulp-replace');
var fileinclude = require('gulp-file-include');
var util = require('gulp-util');
var spawn = require('child_process').spawn;
// config
var pkg = require('./package.json');
var config_data = require('./gulpConfig.json');
// variable
var modeDev = true;
var myDate = new Date();
var reloadKey = myDate.getTime();
// task
gulp.task('help', function() {
console.log(' gulp d (dev 测试环境)');
console.log(' gulp w (watch 文件监控打包)');
console.log(' gulp help gulp参数说明');
//console.log(' gulp server 测试server');
console.log(' gulp b (build 生产环境)');
//console.log(' gulp -m <module> 部分模块打包(默认全部打包)');
});
/* 默认 */
gulp.task('default', function() {
gulp.start('help');
});
gulp.task('d', function(cb) {
gulp.start('dev-task-main');
});
gulp.task('b', function(cb) {
gulp.start('build-task-main');
});
gulp.task('w', function() {
gulp.start('watch-task-main');
});
// ===============================================================
// task-main
// ===============================================================
gulp.task('task-main', function() {
util.log('task web');
if (modeDev) {
gulp.src(config_data.html)
.pipe(replace('//dev ', ''))
.pipe(replace('reload-key', reloadKey))
.pipe(fileinclude())
//.pipe(htmlmin(htmlminOptionsDev))
.pipe(gulp.dest(config_data.dest));
gulp.src(config_data.data)
.pipe(gulp.dest(config_data.dest + ''));
gulp.src(config_data.locales_en)
.pipe(gulp.dest(config_data.dest + '_locales/en/'));
gulp.src(config_data.locales_cn)
.pipe(gulp.dest(config_data.dest + '_locales/zh_CN/'));
gulp.src(config_data.images)
.pipe(gulp.dest(config_data.dest + 'images/'));
gulp.src(config_data.js)
.pipe(gulp.dest(config_data.dest + 'js/'));
gulp.src(config_data.css)
.pipe(minify())
.pipe(gulp.dest(config_data.dest + 'css/'));
gulp.src(config_data.less)
//.pipe(sourcemaps.init())
.pipe(less())
//.pipe(minify())
//.pipe(sourcemaps.write())
.pipe(gulp.dest(config_data.dest + 'css/'));
} else {
gulp.src(config_data.html)
.pipe(replace('//dev ', ''))
.pipe(replace('reload-key', reloadKey))
.pipe(fileinclude())
//.pipe(htmlmin(htmlminOptionsDev))
.pipe(gulp.dest(config_data.prod));
gulp.src(config_data.data)
.pipe(gulp.dest(config_data.prod + ''));
gulp.src(config_data.locales_en)
.pipe(gulp.dest(config_data.prod + '_locales/en/'));
gulp.src(config_data.locales_cn)
.pipe(gulp.dest(config_data.prod + '_locales/zh_CN/'));
gulp.src(config_data.images)
.pipe(gulp.dest(config_data.prod + 'images/'));
gulp.src(config_data.js)
.pipe(uglify())
.pipe(gulp.dest(config_data.prod + 'js/'));
gulp.src(config_data.css)
.pipe(minify())
.pipe(gulp.dest(config_data.prod + 'css/'));
gulp.src(config_data.less)
//.pipe(sourcemaps.init())
.pipe(less())
//.pipe(sourcemaps.write())
.pipe(gulp.dest(config_data.prod + 'css/'));
}
});
gulp.task('watch-task-main', function() {
gulp.watch(config_data.html[0], ['task-main']);
gulp.watch(config_data.js[0], ['task-main']);
gulp.watch(config_data.css[0], ['task-main']);
gulp.watch(config_data.less[0], ['task-main']);
});
gulp.task('dev-task-main', function() {
modeDev = true;
//gulpSequence('zt-public', 'task-main')();
gulp.start('task-main');
util.log('task finished.');
});
gulp.task('build-task-main', function() {
modeDev = false;
//gulpSequence('zt-public', 'task-main')();
gulp.start('task-main');
util.log('task finished.');
});