forked from jarrekk/Jalpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
86 lines (76 loc) · 3.48 KB
/
build.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
var UglifyJS = require('uglify-js');
var CleanCSS = require('clean-css');
var fs = require('fs');
require('shelljs/global');
require('colors');
var fileConf = require('./files.conf.js')
var CSSJSfiles = fileConf.CSSJSfiles;
nowDate = new Date();
nowDateStr = nowDate.toISOString().slice(0, 10).replace(/-/g, "");
// remove preceding compressed files
rm('-rf', 'static/assets/*.min.js');
rm('-rf', 'static/assets/*.min.css');
// change link/src files to new file path
sed('-i', /(.*)[0-9]{8}(.*)/, '$1' + nowDateStr + '$2', '_includes/index_head.html');
sed('-i', /(.*)[0-9]{8}(.*)/, '$1' + nowDateStr + '$2', '_includes/head.html');
sed('-i', /(.*)[0-9]{8}(.*)/, '$1' + nowDateStr + '$2', '_includes/category.html');
sed('-i', /(.*)[0-9]{8}(.*)/, '$1' + nowDateStr + '$2', '404.html');
// compress js files function
function compressjs(pagename, filename, filelist) {
console.log('Now compress ' + pagename + ' js files to ' + filename + ' ...')
var result = UglifyJS.minify(filelist, {
mangle: true,
compress: {
sequences: true,
dead_code: true,
conditionals: true,
booleans: true,
unused: true,
if_return: true,
join_vars: true,
drop_console: true
},
});
fs.writeFileSync('static/assets/' + filename, result.code);
console.log(pagename.green + " js files compress succeed. You can find it at \"static/assets\".\n".green);
}
// compress css files function
function compresscss(pagename, filename, filelist) {
console.log('Now compress ' + pagename + ' css files to ' + filename + ' ...')
var result = new CleanCSS().minify(filelist);
var output = new CleanCSS({
level: {
1: {
transform: function(propertyName, propertyValue) {
if (propertyName == 'src' && propertyValue.indexOf('node_modules/bootstrap/dist/') > -1) {
return propertyValue.replace('node_modules/bootstrap/dist/', '');
}
if (propertyName == 'src' && propertyValue.indexOf('node_modules/components-font-awesome/') > -1) {
return propertyValue.replace('node_modules/components-font-awesome/', '');
}
if (propertyName == 'src' && propertyValue.indexOf('node_modules/font-mfizz/dist/') > -1) {
return propertyValue.replace('node_modules/font-mfizz/dist/', '');
}
if (propertyName == 'background' && propertyValue.indexOf('static/img/') > -1) {
return propertyValue.replace('static/', '');
}
if (propertyName == 'background-image' && propertyValue.indexOf('static/img/') > -1) {
return propertyValue.replace('static/', '');
}
}
}
}
}).minify(result.styles);
fs.writeFileSync('static/assets/' + filename, output.styles);
console.log(pagename.green + " css files compress succeed. You can find it at \"static/assets\".\n".green);
}
for (i = 0; i < CSSJSfiles.length; i++) {
if (CSSJSfiles[i].type == 'css') {
var filename = CSSJSfiles[i].prefix + nowDateStr + '.min.css'
compresscss(CSSJSfiles[i].name, filename, CSSJSfiles[i].list)
}
if (CSSJSfiles[i].type == 'js') {
var filename = CSSJSfiles[i].prefix + nowDateStr + '.min.js'
compressjs(CSSJSfiles[i].name, filename, CSSJSfiles[i].list)
}
}