-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathgulpfile.js
More file actions
178 lines (140 loc) · 4.18 KB
/
Copy pathgulpfile.js
File metadata and controls
178 lines (140 loc) · 4.18 KB
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
/* eslint-env node */
var fs = require('fs'),
cheerio = require('cheerio'),
composer = require('gulp-uglify/composer'),
concat = require('gulp-concat'),
cssmin = require('gulp-cssmin'),
del = require('del'),
gulp = require('gulp'),
htmlmin = require('gulp-htmlmin'),
merge = require('merge-stream'),
replace = require('gulp-replace'),
uglifyes = require('uglify-es'),
zip = require('gulp-zip');
var minify = composer(uglifyes, console);
var htmls = ['index.html', 'terminal.html', 'cyberspace.html'];
// build config for each html file:
// config = {
// 'index.html': {
// js: [ ... ],
// externaljs: [ ... ],
// css: [ ... ]
// }
// }
var config = {};
gulp.task('default', ['build']);
gulp.task('build', ['initbuild', 'jsmin', 'cssmin', 'inline', 'zip', 'clean:tmp', 'report']);
function initHtmlBuildConfig(html) {
config[html] = {
js: [],
externaljs: [],
css: []
};
// get a list of all js scripts from our dev file
var file = l = fs.readFileSync(html, 'utf-8', function(e, data) {
return data;
});
var $ = cheerio.load(file);
$('script').each(function() {
var src = $(this).attr('src');
if (src) {
if (src.indexOf('http') === 0) {
config[html].externaljs.push(src);
} else {
config[html].js.push(src);
}
}
});
// wrap all scripts in function to close the scope (and allow better minification)
if (config[html].js.length > 0) {
config[html].js.unshift('./js/_prefix');
config[html].js.push('./js/_suffix');
}
$('link[rel="stylesheet"]').each(function() {
var src = $(this).attr('href');
config[html].css.push(src);
});
}
gulp.task('initbuild', ['clean:zip'], function() {
var stream;
htmls.forEach(initHtmlBuildConfig);
return stream;
});
gulp.task('jsmin', ['initbuild'], function() {
var tasks = htmls.map(function(html) {
return gulp.src(config[html].js)
.pipe(concat(html + '.js')) //all js files are concatenated into *.js
.pipe(minify())
.pipe(gulp.dest('./tmp'));
});
return merge(tasks);
});
gulp.task('cssmin', ['initbuild'], function() {
var tasks = htmls.map(function(html) {
return gulp.src(config[html].css)
.pipe(concat(html + '.css')) //all js files are concatenated into *.css
.pipe(cssmin())
.pipe(gulp.dest('./tmp'));
});
return merge(tasks);
});
gulp.task('inline', ['jsmin', 'cssmin'], function() {
var tasks = htmls.map(function(html) {
var c = config[html];
var js, css;
var stream = gulp.src(html)
.pipe(replace(/<.*?script.*?>.*?<\/.*?script.*?>/igm, ''))
.pipe(replace(/<.*?link.*rel="stylesheet".*?\/>/igm, ''))
if (c.externaljs.length) {
js = c.externaljs.map(function(src) { return '<script src="' + src + '"></script>'}).join();
stream = stream.pipe(replace(/<\/head>/igm, js+'</head>'));
}
if (c.js.length > 0) {
js = fs.readFileSync('./tmp/' + html + '.js', 'utf-8', function(e, data) {
return data;
});
if (html === 'cyberspace.html') {
// put scripts into head as A-Frame complains about it
stream = stream.pipe(replace(/<\/head>/igm, '<script>'+js+'</script></head>'))
} else {
stream = stream.pipe(replace(/<\/body>/igm, '<script>'+js+'</script></body>'))
}
}
if (c.css.length > 0) {
css = fs.readFileSync('./tmp/' + html + '.css', 'utf-8', function(e, data) {
return data;
});
stream = stream.pipe(replace(/<\/body>/igm, '<style>'+css+'</style></body>'))
}
stream = stream
.pipe(htmlmin({collapseWhitespace: true}))
.pipe(gulp.dest('./tmp'));
return stream;
});
return merge(tasks);
});
gulp.task('zip', ['inline'], function() {
var stream = gulp.src('./tmp/*.html')
.pipe(zip('game.zip'))
.pipe(gulp.dest('.'));
return stream;
});
gulp.task('clean:zip', function() {
return del('game.zip');
});
gulp.task('clean:tmp', ['zip'], function() {
return del('./tmp');
});
gulp.task('report', ['zip'], 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');
});