Refactor styles.js
Changed: - Replaced "fs" callback API with "fs/promises" promise API to centralize catching of errors and easier readability of file. - Prefixed "fs/promises" with 'node:' URI scheme to target Node.js builtin modules. - Promisified `sass.render`.
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
import fs from 'fs';
|
||||
import sass from 'node-sass';
|
||||
import paths from '../mconfig.json';
|
||||
import message from './utils/message.js';
|
||||
import postcss from './utils/postcss.js';
|
||||
import notification from './utils/notification.js';
|
||||
import { writeFile } from 'node:fs/promises';
|
||||
import { promisify } from 'node:util';
|
||||
import sass from 'node-sass';
|
||||
|
||||
const sassRender = promisify(sass.render);
|
||||
|
||||
/**
|
||||
* Compiles and minifies main Sass files to CSS.
|
||||
@@ -12,34 +15,25 @@ export async function compileStyles() {
|
||||
[
|
||||
'critical',
|
||||
'main',
|
||||
].forEach((name) => {
|
||||
].forEach(async (name) => {
|
||||
const infile = paths.styles.src + name + '.scss';
|
||||
const outfile = paths.styles.dest + name + '.css';
|
||||
|
||||
const timeLabel = `${name}.css compiled in`;
|
||||
console.time(timeLabel);
|
||||
|
||||
sass.render({
|
||||
try {
|
||||
let result = await sassRender({
|
||||
file: infile,
|
||||
omitSourceMapUrl: true,
|
||||
outFile: outfile,
|
||||
outputStyle: 'compressed',
|
||||
sourceMap: true,
|
||||
sourceMapContents: true
|
||||
}, (err, result) => {
|
||||
if (err) {
|
||||
message(`Error compiling ${name}.scss`, 'error');
|
||||
message(err.formatted);
|
||||
|
||||
notification({
|
||||
title: `${name}.scss compilation failed 🚨`,
|
||||
message: err.formatted
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (postcss) {
|
||||
postcss.process(result.css, {
|
||||
result = await postcss.process(result.css, {
|
||||
from: outfile,
|
||||
to: outfile,
|
||||
map: {
|
||||
@@ -47,7 +41,9 @@ export async function compileStyles() {
|
||||
inline: false,
|
||||
sourcesContent: true
|
||||
}
|
||||
}).then((result) => {
|
||||
});
|
||||
|
||||
if (result.warnings) {
|
||||
const warnings = result.warnings();
|
||||
if (warnings.length) {
|
||||
message(`Error processing ${name}.css`, 'warning');
|
||||
@@ -55,30 +51,16 @@ export async function compileStyles() {
|
||||
message(warn.toString());
|
||||
});
|
||||
}
|
||||
|
||||
saveStylesheet(result, outfile, name, timeLabel);
|
||||
});
|
||||
} else {
|
||||
saveStylesheet(result, outfile, name, timeLabel);
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes the CSS file, and source map, to the disk.
|
||||
*
|
||||
* @param {object} result - The compiler result.
|
||||
* @param {Buffer} result.css - The compiled CSS.
|
||||
* @param {Buffer} [result.map] - The source map.
|
||||
* @param {string} outfile - The output file path.
|
||||
* @param {string} name - The Sass entry point identifier.
|
||||
* @param {string} [timeLabel] - The console time identifier.
|
||||
*/
|
||||
function saveStylesheet(result, outfile, name, timeLabel) {
|
||||
writeFile(outfile, result.css).then(() => {
|
||||
if (result.css) {
|
||||
fs.writeFile(outfile, result.css, (err) => {
|
||||
if (err) {
|
||||
message(`${name}.css compiled`, 'success', timeLabel);
|
||||
} else {
|
||||
message(`${name}.css is empty`, 'notice', timeLabel);
|
||||
}
|
||||
}).catch((err) => {
|
||||
message(`Error compiling ${name}.scss`, 'error');
|
||||
message(err);
|
||||
|
||||
@@ -86,16 +68,10 @@ function saveStylesheet(result, outfile, name, timeLabel) {
|
||||
title: `${name}.scss compilation failed 🚨`,
|
||||
message: `Could not save stylesheet to ${name}.css`
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
message(`${name}.css compiled`, 'success', timeLabel);
|
||||
});
|
||||
}
|
||||
|
||||
if (result.map) {
|
||||
fs.writeFile(outfile + '.map', result.map, (err) => {
|
||||
if (err) {
|
||||
writeFile(outfile + '.map', result.map.toString()).catch((err) => {
|
||||
message(`Error compiling ${name}.scss`, 'error');
|
||||
message(err);
|
||||
|
||||
@@ -103,8 +79,16 @@ function saveStylesheet(result, outfile, name, timeLabel) {
|
||||
title: `${name}.scss compilation failed 🚨`,
|
||||
message: `Could not save sourcemap to ${name}.css.map`
|
||||
});
|
||||
return;
|
||||
}
|
||||
});
|
||||
}
|
||||
} catch (err) {
|
||||
message(`Error compiling ${name}.scss`, 'error');
|
||||
message(err.formatted);
|
||||
|
||||
notification({
|
||||
title: `${name}.scss compilation failed 🚨`,
|
||||
message: err.formatted
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user