/
Gulpfile.js
393 lines (329 loc) · 11.6 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
'use strict';
const del = require('del');
const fs = require('fs');
const gulp = require('gulp');
const uglify = require('gulp-uglify');
const sourcemaps = require('gulp-sourcemaps');
const plumber = require('gulp-plumber');
const connect = require('gulp-connect');
const rename = require("gulp-rename");
const imagemin = require('gulp-imagemin');
const minifyHTML = require('gulp-minify-html');
const minifyInline = require('gulp-minify-inline');
const htmlAutoprefixer = require("gulp-html-autoprefixer");
const replace = require('gulp-replace');
const gm = require("gm");
const converter = require('image-to-icon-converter');
let VERSION = gulp.env.version;
if (!VERSION) {
VERSION = "v1";
}
let IS_DEBUG = !gulp.env.release;
let profile = gulp.env.profile;
if (!profile) {
profile = "gd";
}
let IS_OPTIMIZE_IMG = !!gulp.env.optmize;
console.log("是否调试模式:", IS_DEBUG);
console.log("PROFILE:", profile);
console.log("OPTIMIZE:", IS_OPTIMIZE_IMG);
console.log("版本:", VERSION);
let SRC_PATH = "../laya-client/bin/h5";
let DIST_PATH = "dist/web";
let DIST_ROOT_PATH = "dist";
let paths = {
layaJs: [SRC_PATH + '/myLaya.max.js'],
images: [SRC_PATH + '/**/*.png', SRC_PATH + '/**/*.jpg'],
json: SRC_PATH + '/res/**/*.json',
index: 'web/index.html',
cordovaIndex: 'web/cordovaIndex.html',
icon: `web/icon/${profile}-icon.png`,
splash: `web/splash.png`,
splashLand: `web/splash-land.png`,
htmlImage: 'web/**/*.png',
audio: [SRC_PATH + '/**/*.mp3'],
favicon: 'web/favicon.ico',
docker: ['docker/Dockerfile']
};
function distPath(path) {
if (path == null) {
return DIST_PATH;
}
return DIST_PATH + path;
}
gulp.task('clean', function () {
return del(['dist/*'], {force: true});
});
gulp.task('laya', function () {
let pipe = gulp.src(paths.layaJs)
.pipe(plumber())
.pipe(sourcemaps.init());
if (!IS_DEBUG) {
pipe = pipe.pipe(uglify());
}
return pipe
.pipe(rename(function (path) {
path.basename = path.basename.replace(".max", ".min");
}))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(distPath()))
.pipe(connect.reload());
});
gulp.task('images', function () {
let pipe = gulp.src(paths.images)
.pipe(plumber());
if (IS_OPTIMIZE_IMG) {
pipe = pipe
.pipe(imagemin({
verbose: true
}));
}
return pipe
.pipe(gulp.dest(distPath()));
});
gulp.task('json', function () {
return gulp.src(paths.json)
.pipe(plumber())
.pipe(gulp.dest(distPath("/res")));
});
gulp.task('audio', function () {
return gulp.src(paths.audio)
.pipe(plumber())
.pipe(gulp.dest(distPath()));
});
gulp.task('htmlImage', function () {
return gulp.src(paths.htmlImage)
.pipe(plumber())
.pipe(gulp.dest(distPath()));
});
let htmlMinifyOption = {empty: true, comments: true};
let autoprefixerOption = {browsers: ['> 1%', 'IE 7'], cascade: false};
let minifyInlineOptions = {
js: {
output: {
comments: true
}
}
};
gulp.task("index", function () {
let result = gulp.src(paths.index)
.pipe(plumber());
result = result.pipe(replace('${version}', VERSION));
result = result.pipe(htmlAutoprefixer(autoprefixerOption));
if (!IS_DEBUG) {
result = result.pipe(minifyHTML(htmlMinifyOption))
.pipe(minifyInline(minifyInlineOptions))
}
result = result.pipe(gulp.dest(distPath()));
return result.pipe(connect.reload());
});
gulp.task("cordovaIndex", function () {
let result = gulp.src(paths.cordovaIndex)
.pipe(plumber());
result = result.pipe(rename(function (path) {
path.basename = "index";
}));
result = result.pipe(replace('${version}', VERSION));
result = result.pipe(htmlAutoprefixer(autoprefixerOption));
if (!IS_DEBUG) {
result = result.pipe(minifyHTML(htmlMinifyOption))
.pipe(minifyInline(minifyInlineOptions))
}
result = result.pipe(gulp.dest(distPath()));
return result.pipe(connect.reload());
});
gulp.task('icons', function (cb) {
return new Promise(function (resolve, reject) {
try {
let iconSize = [57, 60, 72, 114, 144, 180];
let count = iconSize.length + 1;
let errors = [];
let endFun = (error) => {
if (error) {
errors.push(error);
}
if ((count--) == 0) {
if (error.length > 0) {
console.log(errors);
reject(new Error(errors));
} else {
resolve();
}
}
};
let dirPath = distPath();
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath);
}
dirPath = distPath(`/icon/`);
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath);
}
iconSize.forEach((size) => {
gm(paths.icon)
.resize(size, size)
.noProfile()
.gravity('Center')
.write(distPath(`/icon/${size}.png`), function (err) {
if (!err) {
endFun();
} else {
endFun(err);
}
});
});
gm(paths.icon)
.resize(128, 128)
.noProfile()
.gravity('Center')
.write(distPath(`/favicon.ico`), function (err) {
if (!err) {
endFun();
} else {
endFun(err);
}
});
} catch (error) {
console.log(error);
reject(error);
}
});
});
var iosIconInfo = [
{w: 57, h: 57, name: "icon.png"},
{w: 114, h: 114, name: "icon@2x.png"},
{w: 40, h: 40, name: "icon-40.png"},
{w: 80, h: 80, name: "icon-40@2x.png"},
{w: 50, h: 50, name: "icon-50.png"},
{w: 100, h: 100, name: "icon-50@2x.png"},
{w: 60, h: 60, name: "icon-60.png"},
{w: 120, h: 120, name: "icon-60@2x.png"},
{w: 180, h: 180, name: "icon-60@3x.png"},
{w: 72, h: 72, name: "icon-72.png"},
{w: 114, h: 114, name: "icon-72@2x.png"},
{w: 76, h: 76, name: "icon-76.png"},
{w: 152, h: 152, name: "icon-76@2x.png"},
{w: 29, h: 29, name: "icon-small.png"},
{w: 58, h: 58, name: "icon-small@2x.png"},
{w: 87, h: 87, name: "icon-small@3x.png"},
];
var androidIconInfo = [
{w: 72, h: 72, name: "drawable-hdpi-icon.png"},
{w: 36, h: 36, name: "drawable-ldpi-icon.png"},
{w: 48, h: 48, name: "drawable-mdpi-icon.png"},
{w: 96, h: 96, name: "drawable-xhdpi-icon.png"},
{w: 144, h: 144, name: "drawable-xxhdpi-icon.png"},
{w: 192, h: 192, name: "drawable-xxxhdpi-icon.png"},
];
gulp.task('androidIcons', icons(androidIconInfo, "android", "icon", paths.icon));
gulp.task('iosIcons', icons(iosIconInfo, "ios", "icon", paths.icon));
var androidSplashInfoLand = [
// {w: 320, h: 200, name: "drawable-land-ldpi-screen.jpg"},
// {w: 480, h: 320, name: "drawable-land-mdpi-screen.jpg"},
// {w: 800, h: 480, name: "drawable-land-hdpi-screen.jpg"},
// {w: 1280, h: 720, name: "drawable-land-xhdpi-screen.jpg"},
// {w: 1600, h: 960, name: "drawable-land-xxhdpi-screen.jpg"},
{w: 1920, h: 1280, name: "drawable-land-xxxhdpi-screen.jpg"},
];
var iosSplashInfoLand = [
{h: 1242, w: 2208, name: "Default-Landscape-736h.jpg"},
{h: 1536, w: 2048, name: "Default-Landscape@2x~ipad.jpg"},
{h: 760, w: 1024, name: "Default-Landscape~ipad.jpg"},
];
var iosSplashInfo = [
{h: 1136, w: 640, name: "Default-568h@2x~iphone.jpg"},
{h: 1334, w: 750, name: "Default-667h.jpg"},
{h: 2208, w: 1242, name: "Default-736h.jpg"},
{h: 2048, w: 1536, name: "Default-Portrait@2x~ipad.jpg"},
{h: 1024, w: 768, name: "Default-Portrait~ipad.jpg"},
{h: 960, w: 640, name: "Default@2x~iphone.jpg"},
{h: 480, w: 320, name: "Default~iphone.jpg"},
];
gulp.task('androidSplash', icons(androidSplashInfoLand, "android", "splash", paths.splashLand));
gulp.task('iosSplashLand', icons(iosSplashInfoLand, "ios", "splash", paths.splashLand));
// gulp.task('iosSplash', icons(iosSplashInfo, "ios","splash", paths.splash));
function icons(iconInfos, type, dirType, source) {
return function icons(cb) {
return new Promise(function (resolve, reject) {
try {
let count = iconInfos.length + 1;
let errors = [];
let endFun = (error) => {
if (error) {
errors.push(error);
}
if ((count--) == 0) {
console.log("结束");
if (error.length > 0) {
console.log(errors);
reject(new Error(errors));
} else {
resolve();
}
}
};
let dirPath = distPath();
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath);
}
dirPath = distPath(`/icon/`);
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath);
}
iconInfos.forEach((item) => {
gm(source)
.resize(item.w, item.h)
.noProfile()
.gravity('Center')
.write((`../cordova/resources/${type}/${dirType}/${item.name}`), function (err) {
if (!err) {
endFun();
} else {
endFun(err);
}
});
;
});
} catch (error) {
console.log(error);
reject(error);
}
});
}
}
gulp.task('favicon', function () {
return gulp.src(paths.favicon)
.pipe(plumber())
.pipe(gulp.dest(distPath()));
});
gulp.task('docker', function () {
return gulp.src(paths.docker)
.pipe(plumber())
.pipe(gulp.dest(DIST_ROOT_PATH));
});
gulp.task('dist', ['laya', 'images', 'json', 'index', 'icons', 'favicon', 'docker', 'audio']);
gulp.task('default', ['watch', 'connect']);
gulp.task('cordovaClean', function () {
return del(['../cordova/www/*'], {force: true});
});
gulp.task('cordovaIcon', function () {
return gulp.src(['dist/web/icon/180.png'])
.pipe(plumber())
.pipe(gulp.dest("../cordova/icon"));
});
// 标注一个依赖,依赖的任务必须在这个任务开始之前被完成
gulp.task('cordova', ['cordovaIcon', 'cordovaClean', 'laya', 'images', 'json', 'cordovaIndex', 'favicon', 'docker', 'audio'], function () {
// 现在任务 'one' 已经完成了
console.log("cordova start");
return gulp.src(['dist/web/**/*'])
.pipe(plumber())
.pipe(gulp.dest("../cordova/www"));
});
gulp.task('webclient', ['htmlImage', 'laya', 'images', 'json', 'favicon', 'docker', 'audio'], function () {
// 现在任务 'one' 已经完成了
console.log("webclient start");
return gulp.src(['dist/web/**/*'])
.pipe(plumber())
.pipe(gulp.dest("../../server/project/distribution/src/static/" + profile))
.pipe(gulp.dest("../../server/project/mj-client/src/main/resources/static/" ));
});