2021-09-14 19:21:24 -04:00
|
|
|
import autoprefixer from 'autoprefixer';
|
2021-09-16 14:22:20 -04:00
|
|
|
import fs from 'fs';
|
2020-11-27 16:01:54 -05:00
|
|
|
import sass from 'node-sass';
|
2021-09-14 19:21:24 -04:00
|
|
|
import postcss from 'postcss';
|
2020-11-27 16:01:54 -05:00
|
|
|
import paths from '../mconfig.json';
|
|
|
|
|
import message from './utils/message.js';
|
2021-09-15 17:26:19 -04:00
|
|
|
import notification from './utils/notification.js';
|
2020-11-27 16:01:54 -05:00
|
|
|
|
2021-09-16 14:22:20 -04:00
|
|
|
/**
|
|
|
|
|
* Compiles and minifies main Sass files to CSS.
|
|
|
|
|
*/
|
2021-09-15 17:22:39 -04:00
|
|
|
export async function compileStyles() {
|
2021-09-16 14:22:20 -04:00
|
|
|
[
|
|
|
|
|
'critical',
|
|
|
|
|
'main',
|
|
|
|
|
].forEach((name) => {
|
|
|
|
|
const infile = paths.styles.src + name + '.scss';
|
|
|
|
|
const outfile = paths.styles.dest + name + '.css';
|
2020-11-27 16:01:54 -05:00
|
|
|
|
2021-09-16 14:22:20 -04:00
|
|
|
const timeLabel = `${name}.css compiled in`;
|
|
|
|
|
console.time(timeLabel);
|
2020-12-11 16:00:36 -05:00
|
|
|
|
2021-09-16 14:22:20 -04:00
|
|
|
sass.render({
|
|
|
|
|
file: infile,
|
2021-09-14 19:21:24 -04:00
|
|
|
omitSourceMapUrl: true,
|
2021-09-16 14:22:20 -04:00
|
|
|
outFile: outfile,
|
|
|
|
|
outputStyle: 'compressed',
|
2021-09-14 19:21:24 -04:00
|
|
|
sourceMap: true,
|
|
|
|
|
sourceMapContents: true
|
2021-09-16 14:22:20 -04:00
|
|
|
}, (err, result) => {
|
|
|
|
|
if (err) {
|
|
|
|
|
message(`Error compiling ${name}.scss`, 'error');
|
|
|
|
|
message(err.formatted);
|
2020-11-27 16:01:54 -05:00
|
|
|
|
2021-09-16 14:22:20 -04:00
|
|
|
notification({
|
|
|
|
|
title: `${name}.scss compilation failed 🚨`,
|
|
|
|
|
message: err.formatted
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-12-11 10:41:07 -05:00
|
|
|
|
2021-09-15 15:54:53 -04:00
|
|
|
postcss([ autoprefixer ]).process(result.css, {
|
|
|
|
|
from: outfile,
|
|
|
|
|
to: outfile,
|
|
|
|
|
map: {
|
|
|
|
|
annotation: false,
|
|
|
|
|
inline: false,
|
|
|
|
|
sourcesContent: true
|
2021-09-16 14:22:20 -04:00
|
|
|
}
|
2021-09-15 15:54:53 -04:00
|
|
|
}).then((result) => {
|
|
|
|
|
result.warnings().forEach((warn) => {
|
|
|
|
|
message(`Error prefixing ${name}.css`, 'error');
|
|
|
|
|
message(warn.toString());
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
fs.writeFile(outfile, result.css, (err) => {
|
|
|
|
|
if (err) {
|
|
|
|
|
message(`Error compiling ${name}.scss`, 'error');
|
|
|
|
|
message(err);
|
2020-11-27 16:01:54 -05:00
|
|
|
|
2021-09-15 15:54:53 -04:00
|
|
|
notification({
|
|
|
|
|
title: `${name}.scss compilation failed 🚨`,
|
|
|
|
|
message: `Could not save stylesheet to ${name}.css`
|
|
|
|
|
});
|
|
|
|
|
return;
|
2021-09-14 19:21:24 -04:00
|
|
|
}
|
|
|
|
|
|
2021-09-15 15:54:53 -04:00
|
|
|
message(`${name}.css compiled`, 'success', timeLabel);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (result.map) {
|
|
|
|
|
fs.writeFile(outfile + '.map', result.map.toString(), (err) => {
|
2021-09-14 19:21:24 -04:00
|
|
|
if (err) {
|
|
|
|
|
message(`Error compiling ${name}.scss`, 'error');
|
|
|
|
|
message(err);
|
|
|
|
|
|
|
|
|
|
notification({
|
|
|
|
|
title: `${name}.scss compilation failed 🚨`,
|
2021-09-15 15:54:53 -04:00
|
|
|
message: `Could not save sourcemap to ${name}.css.map`
|
2021-09-14 19:21:24 -04:00
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
});
|
2021-09-15 15:54:53 -04:00
|
|
|
}
|
2021-09-16 14:22:20 -04:00
|
|
|
});
|
|
|
|
|
});
|
2020-11-27 16:01:54 -05:00
|
|
|
});
|
2021-09-15 17:24:33 -04:00
|
|
|
};
|