2020-05-10 08:43:43 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* @flow
|
|
|
|
*/
|
|
|
|
|
|
|
|
import fs from 'fs';
|
2020-12-02 22:22:59 +00:00
|
|
|
import path from 'path';
|
2020-05-10 08:43:43 +00:00
|
|
|
import CleanCSS from 'clean-css';
|
|
|
|
import crypto from 'crypto';
|
|
|
|
|
2020-12-02 22:22:59 +00:00
|
|
|
const rootdir = path.resolve(__dirname, '..');
|
2021-01-29 01:15:33 +00:00
|
|
|
const assetdir = path.resolve(__dirname, '..', 'build', 'public', 'assets');
|
|
|
|
const builddir = path.resolve(__dirname, '..', 'build');
|
2020-12-02 22:22:59 +00:00
|
|
|
|
2021-01-29 01:15:33 +00:00
|
|
|
const FOLDER = path.resolve(__dirname, '..', 'src', 'styles');
|
2020-05-17 10:53:16 +00:00
|
|
|
const FILES = [
|
|
|
|
'default.css',
|
|
|
|
'dark.css',
|
|
|
|
'light-round.css',
|
|
|
|
'dark-round.css',
|
2020-05-17 11:49:05 +00:00
|
|
|
'arkeros.css',
|
2020-05-17 10:53:16 +00:00
|
|
|
];
|
2020-05-10 08:43:43 +00:00
|
|
|
|
|
|
|
async function minifyCss() {
|
|
|
|
console.log('Minifying css');
|
|
|
|
const assets = {};
|
|
|
|
FILES.forEach((file) => {
|
|
|
|
const input = fs.readFileSync(`${FOLDER}/${file}`, 'utf8');
|
|
|
|
const options = {};
|
|
|
|
const output = new CleanCSS(options).minify(input);
|
|
|
|
if (output.warnings && output.warnings.length > 0) {
|
|
|
|
for (let i = 0; i < output.warnings.length; i += 1) {
|
|
|
|
console.log('\x1b[33m%s\x1b[0m', output.warnings[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (output.errors && output.errors.length > 0) {
|
|
|
|
for (let i = 0; i < output.errors.length; i += 1) {
|
|
|
|
console.log('\x1b[31m%s\x1b[0m', output.errors[i]);
|
|
|
|
}
|
|
|
|
throw new Error("Minify CSS Error Occured");
|
|
|
|
}
|
|
|
|
// eslint-disable-next-line max-len
|
|
|
|
console.log('\x1b[33m%s\x1b[0m', `Minified ${file} by ${Math.round(output.stats.efficiency * 100)}%`);
|
|
|
|
const hash = crypto.createHash('md5').update(output.styles).digest('hex');
|
|
|
|
const key = file.substr(0, file.indexOf('.'));
|
|
|
|
const filename = `${key}.${hash.substr(0, 8)}.css`;
|
2020-12-02 22:22:59 +00:00
|
|
|
fs.writeFileSync(`${assetdir}/${filename}`, output.styles, 'utf8');
|
2020-05-10 08:43:43 +00:00
|
|
|
assets[key] = `/assets/${filename}`;
|
|
|
|
});
|
|
|
|
const json = JSON.stringify(assets);
|
2020-12-02 22:22:59 +00:00
|
|
|
fs.writeFileSync(`${builddir}/styleassets.json`, json);
|
2020-05-10 08:43:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default minifyCss;
|