-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprod.js
55 lines (54 loc) · 2.04 KB
/
prod.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
var path = require('path');
var fs = require('fs-extra');
var cleanCSS = require('clean-css');
var _ = require('lodash');
var glob = require('glob');
var cdn = 'http://cdn1984.taskforce.is';
var cdnversion = new Date().getTime();
var htmlMinifier = require('html-minifier');
var htmlCompressionOptions = {
removeComments: true,
collapseBooleanAttributes: true,
collapseWhitespace: true,
};
function cssIncImages(cssFile) {
var imgRegex = /url\s?\(['"]?(\.\.\/img.*?)\?embed(?=['"]?\))/gi;
var css = fs.readFileSync(cssFile, 'utf-8');
while (match = imgRegex.exec(css)) {
var imgPath = path.join(path.dirname(cssFile), match[1]);
try {
var img = fs.readFileSync(imgPath, 'base64');
var ext = imgPath.substr(imgPath.lastIndexOf('.') + 1);
css = css.replace(match[1]+'?embed', 'data:image/' + ext + ';base64,' + img);
} catch (err) {
console.log('Image not found (%s).', imgPath);
}
}
fs.writeFileSync(cssFile, css, 'utf-8'); // you can overwrite the original file with this line
return css;
}
fs.copy('.', '..', function(err){
if (err) {
console.error(err);
}
else {
var styles = fs.readFileSync('../css/styles.css', 'utf8');
var minimized = cleanCSS.process(styles, {relativeTo: '../css'});
fs.writeFileSync('../css/styles.css', minimized, 'utf8');
//cssIncImages('../css/styles.css');
var indexPage = fs.readFileSync('../index.html', 'utf8');
indexPage = htmlMinifier.minify(indexPage, htmlCompressionOptions);
fs.writeFileSync('../index.html', indexPage, 'utf8');
glob("../**/index.html", {}, function (er, files) {
var files = _.filter(files, function(file){
return file.substr(0,6) !== '../dev' && file.substr(0, 13) !== '../index.html' && file.substr(0,7) !== '../node'
});
_.each(files, function(filename){
var indexPage = fs.readFileSync(filename, 'utf8');
indexPage = htmlMinifier.minify(indexPage, htmlCompressionOptions);
fs.writeFileSync(filename, indexPage, 'utf8');
});
})
console.log("success!")
}
});