/
minify-css.js
64 lines (53 loc) · 1.77 KB
/
minify-css.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
#!/usr/bin/env node
/**
* This script is a command line wrapper for `css-min`, which will
* minify the CSS generated by the IDS Enterprise build system.
*/
// -------------------------------------
// Requirements
// -------------------------------------
const cssmin = require('cssmin');
const commandLineArgs = require('yargs').argv;
const logger = require('./logger');
const getFileContents = require('./build/get-file-contents');
const writeFile = require('./build/write-file');
const config = require('./configs/cssmin').cssmin;
// -------------------------------------
// Functions
// -------------------------------------
function minify(srcFilePath, targetFilePath) {
const css = getFileContents(srcFilePath);
const minified = cssmin(css);
return writeFile(targetFilePath, minified).then((err) => {
if (err) {
logger('error', `Error minifying "${srcFilePath}": ${err}`);
return;
}
// Only log if not in --verbose mode (file logger has more detailed results)
if (!commandLineArgs.verbose) {
logger('success', `Successfully minified "${targetFilePath}"`);
}
});
}
function minifyCSS() {
return new Promise((resolve, reject) => {
if (!config.dist || !config.dist.files) {
throw new Error('Need to have target CSS files passed in for minifier');
}
const files = Object.keys(config.dist.files);
const processes = [];
files.forEach((targetFileName) => {
const srcFileName = config.dist.files[targetFileName][0];
processes.push(minify(srcFileName, targetFileName));
});
return Promise.all(processes).then(() => {
resolve();
}).catch((e) => {
reject(e);
});
});
}
// -------------------------------------
// Main
// -------------------------------------
module.exports = minifyCSS();