-
Notifications
You must be signed in to change notification settings - Fork 2
/
gulpfile.js
137 lines (103 loc) · 3.21 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
/* eslint-env node */
var fs = require('fs'),
cheerio = require('cheerio'),
gulp = require('gulp'),
cssmin = require('gulp-cssmin'),
concat = require('gulp-concat'),
htmlmin = require('gulp-htmlmin'),
rimraf = require('gulp-rimraf'),
replace = require('gulp-replace'),
uglify = require('gulp-uglify'),
zip = require('gulp-zip'),
exclude_min = [], //eg ['js/lib/jsfxr.min.js']
config = { js: [] };
gulp.task('build', ['initbuild', 'jsmin', 'cssmin', 'inline', 'zip', 'clean', 'report']);
gulp.task('initbuild', function() {
var stream, html, $, src, js = [], css = [];
// delete prev files
stream = gulp.src('game.zip')
.pipe(rimraf());
// get a list of all js scripts from our dev file
html = fs.readFileSync('index.html', 'utf-8', function(e, data) {
return data;
});
$ = cheerio.load(html);
$('script').each(function() {
src = $(this).attr('src');
if (exclude_min.indexOf(src) === -1) { //exclude already minified files
js.push(src);
}
});
config.js = js;
$('link[rel="stylesheet"]').each(function() {
src = $(this).attr('href');
if (exclude_min.indexOf(src) === -1) { //exclude already minified files
css.push(src);
}
});
config.css = css;
return stream;
});
gulp.task('jsmin', ['initbuild'], function() {
var stream = gulp.src(config.js)
.pipe(concat('g.js')) //all js files are concatenated into g.js
.pipe(uglify())
.pipe(gulp.dest('./tmp'));
return stream;
});
gulp.task('cssmin', ['initbuild'], function() {
var stream = gulp.src(config.css)
.pipe(concat('s.css'))//all js files are concatenated into s.css
.pipe(cssmin())
.pipe(gulp.dest('./tmp'));
return stream;
});
gulp.task('inline', ['jsmin', 'cssmin'], function() {
var i, extra_js = '';
var js = fs.readFileSync('./tmp/g.js', 'utf-8', function(e, data) {
return data;
});
//include already minified files
for (i = 0; i < exclude_min.length; i += 1) {
console.log(exclude_min[i]);
extra_js += fs.readFileSync(exclude_min[i], 'utf-8', function(e, data) {
return data;
});
}
console.log(extra_js.length, 'OK', exclude_min);
var css = fs.readFileSync('./tmp/s.css', 'utf-8', function(e, data) {
return data;
});
var stream = gulp.src('index.html')
.pipe(replace(/<.*?script.*?>.*?<\/.*?script.*?>/igm, ''))
.pipe(replace(/<\/body>/igm, '<script>'+extra_js+' '+js+'</script></body>'))
.pipe(replace(/<.*?link.*rel="stylesheet".*?\/>/igm, ''))
.pipe(replace(/<\/head>/igm, '<style>'+css+'</style></head>'))
.pipe(htmlmin({collapseWhitespace: true}))
.pipe(gulp.dest('./tmp'));
return stream;
});
gulp.task('zip', ['inline'], function() {
var stream = gulp.src('./tmp/index.html')
.pipe(zip('game.zip'))
.pipe(gulp.dest('.'));
return stream;
});
gulp.task('clean', ['zip'], function() {
var stream = gulp.src('./tmp')
.pipe(rimraf());
return stream;
});
gulp.task('report', ['clean'], function() {
var stat = fs.statSync('game.zip'),
limit = 1024 * 13,
size = stat.size,
remaining = limit - size,
percentage = (remaining / limit) * 100;
percentage = Math.round(percentage * 100) / 100;
console.log('\n\n-------------');
console.log('BYTES USED: ' + stat.size);
console.log('BYTES REMAINING: ' + remaining);
console.log(percentage +'%');
console.log('-------------\n\n');
});