2021-09-17 00:26:18 -04:00
|
|
|
import loconfig from '../../loconfig.json';
|
2021-09-17 00:04:56 -04:00
|
|
|
import message from '../utils/message.js';
|
|
|
|
|
import notification from '../utils/notification.js';
|
2021-09-17 00:26:18 -04:00
|
|
|
import postcss from '../utils/postcss.js';
|
|
|
|
|
import template from '../utils/template.js';
|
2021-09-16 18:08:20 -04:00
|
|
|
import { writeFile } from 'node:fs/promises';
|
2021-09-17 00:26:18 -04:00
|
|
|
import { basename } from 'node:path';
|
2021-09-16 18:08:20 -04:00
|
|
|
import { promisify } from 'node:util';
|
|
|
|
|
import sass from 'node-sass';
|
|
|
|
|
|
|
|
|
|
const sassRender = promisify(sass.render);
|
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-17 00:26:18 -04:00
|
|
|
loconfig.tasks.styles.forEach(async ({
|
|
|
|
|
infile,
|
|
|
|
|
outfile
|
|
|
|
|
}) => {
|
|
|
|
|
const name = basename((outfile || 'undefined'), '.scss');
|
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 18:08:20 -04:00
|
|
|
try {
|
2021-09-17 00:26:18 -04:00
|
|
|
infile = template(infile);
|
|
|
|
|
outfile = template(outfile);
|
|
|
|
|
|
2021-09-16 18:08:20 -04:00
|
|
|
let result = await sassRender({
|
|
|
|
|
file: infile,
|
|
|
|
|
omitSourceMapUrl: true,
|
|
|
|
|
outFile: outfile,
|
|
|
|
|
outputStyle: 'compressed',
|
|
|
|
|
sourceMap: true,
|
|
|
|
|
sourceMapContents: true
|
|
|
|
|
});
|
2020-12-11 10:41:07 -05:00
|
|
|
|
2021-09-15 17:29:07 -04:00
|
|
|
if (postcss) {
|
2021-09-16 18:08:20 -04:00
|
|
|
result = await postcss.process(result.css, {
|
2021-09-15 17:29:07 -04:00
|
|
|
from: outfile,
|
|
|
|
|
to: outfile,
|
|
|
|
|
map: {
|
|
|
|
|
annotation: false,
|
|
|
|
|
inline: false,
|
|
|
|
|
sourcesContent: true
|
|
|
|
|
}
|
2021-09-16 18:08:20 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (result.warnings) {
|
2021-09-16 16:51:21 -04:00
|
|
|
const warnings = result.warnings();
|
|
|
|
|
if (warnings.length) {
|
|
|
|
|
message(`Error processing ${name}.css`, 'warning');
|
|
|
|
|
warnings.forEach((warn) => {
|
|
|
|
|
message(warn.toString());
|
|
|
|
|
});
|
|
|
|
|
}
|
2021-09-16 18:08:20 -04:00
|
|
|
}
|
2021-09-15 17:29:07 -04:00
|
|
|
}
|
2020-11-27 16:01:54 -05:00
|
|
|
|
2021-09-16 18:08:20 -04:00
|
|
|
writeFile(outfile, result.css).then(() => {
|
|
|
|
|
if (result.css) {
|
|
|
|
|
message(`${name}.css compiled`, 'success', timeLabel);
|
|
|
|
|
} else {
|
|
|
|
|
message(`${name}.css is empty`, 'notice', timeLabel);
|
|
|
|
|
}
|
|
|
|
|
}).catch((err) => {
|
2021-09-15 17:29:07 -04:00
|
|
|
message(`Error compiling ${name}.scss`, 'error');
|
|
|
|
|
message(err);
|
2021-09-14 19:21:24 -04:00
|
|
|
|
2021-09-15 17:29:07 -04:00
|
|
|
notification({
|
|
|
|
|
title: `${name}.scss compilation failed 🚨`,
|
|
|
|
|
message: `Could not save stylesheet to ${name}.css`
|
2021-09-15 15:54:53 -04:00
|
|
|
});
|
2021-09-16 18:08:20 -04:00
|
|
|
});
|
2021-09-15 15:54:53 -04:00
|
|
|
|
2021-09-16 18:08:20 -04:00
|
|
|
if (result.map) {
|
|
|
|
|
writeFile(outfile + '.map', result.map.toString()).catch((err) => {
|
|
|
|
|
message(`Error compiling ${name}.scss`, 'error');
|
|
|
|
|
message(err);
|
2021-09-14 19:21:24 -04:00
|
|
|
|
2021-09-16 18:08:20 -04:00
|
|
|
notification({
|
|
|
|
|
title: `${name}.scss compilation failed 🚨`,
|
|
|
|
|
message: `Could not save sourcemap to ${name}.css.map`
|
|
|
|
|
});
|
2021-09-15 17:29:07 -04:00
|
|
|
});
|
|
|
|
|
}
|
2021-09-16 18:08:20 -04:00
|
|
|
} catch (err) {
|
|
|
|
|
message(`Error compiling ${name}.scss`, 'error');
|
|
|
|
|
message(err.formatted);
|
|
|
|
|
|
|
|
|
|
notification({
|
|
|
|
|
title: `${name}.scss compilation failed 🚨`,
|
|
|
|
|
message: err.formatted
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|