-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmake.js
executable file
·153 lines (108 loc) · 3.1 KB
/
make.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
#!/usr/bin/env node
// based on https://github.com/kjbekkelund/js-build
require('shelljs/make');
require('colors');
var fs = require('fs');
var glob = require('glob');
var path = require('path');
var UglifyJS = require("uglify-js");
var uglifycss = require('uglifycss');
var imageinliner = require('imageinliner');
var zlib = require('zlib');
var hogan = require('hogan.js');
var npmBin = require('npm-bin');
var isWin = (process.platform === 'win32');
/*** CONFIG ********/
var outputDir = process.env.OUTPUT_DIR || path.join('build');
var webapp = path.join('src');
var outputHtmlFile = path.join(outputDir, 'index.html');
/*** TARGETS ********/
target.all = function() {
target.build();
};
target.build = function() {
createCleanDir(outputDir);
target.buildImg();
target.buildHtml();
target.compress();
echo();echo();
success("Build succeeded!");
};
/*** APP FUNCTIONS ********/
target.buildImg = function() {
var srcImageFolder = path.join(webapp, 'images');
var outputImageFolder = path.join(outputDir, 'images');
cp('-R', srcImageFolder, outputDir);
var pngs = glob.sync(path.join(outputImageFolder, '*.png'));
section('Optimizing svgs');
res = npmBin('svgo', ['-f ' + outputImageFolder], {silent: true});
done(res);
};
target.buildHtml = function() {
var htmlFile = path.join(webapp, 'index.mustache');
section('Building HTML → ' + htmlFile);
renderAndWriteMustache(htmlFile, outputHtmlFile, {
css: minifyCss(),
js: minifyJs()
});
};
target.compress = function() {
gzip(outputHtmlFile);
};
var renderAndWriteMustache = function(from, to, data) {
var mustache = fs.readFileSync(from).toString();
var template = hogan.compile(mustache);
var html = template.render(data);
fs.writeFileSync(to, html);
success();
};
var minifyCss = function() {
var mainCssFile = path.join(webapp, 'css', 'style.css');
var uglified = uglifycss.processFiles([mainCssFile]);
return imageinliner.css(uglified, {
cssBasePath: outputDir,
compressOutput: true
});
};
var minifyJs = function() {
var mainJsFile = path.join(webapp, 'js', 'main.js');
return UglifyJS.minify(mainJsFile).code;
};
var gzip = function(file) {
var gzip = zlib.createGzip();
var input = fs.createReadStream(file);
var output = fs.createWriteStream(file + '.gz');
section('Gzipping ' + file);
input.pipe(gzip).pipe(output);
success();
};
/*** HELPER FUNCTIONS ********/
var createCleanDir = function(dir) {
if (test('-d', dir)) {
rm('-rf', dir);
}
mkdir('-p', dir);
return dir;
};
var section = function(header) {
echo();
echo(' ' + header.bold);
};
var done = function(res) {
if (res.code === 0) {
success();
} else {
fail();
}
};
var success = function(text) {
text = text || 'done';
var s = isWin ? '»' : '✓';
echo(' ' + s.green + ' ' + text.green);
};
var fail = function(text) {
text = text || 'failed';
var s = isWin ? '×' : '✘';
echo(' ' + s.red + ' ' + text.red);
exit(1);
};