1
0
mirror of https://github.com/locomotivemtl/locomotive-boilerplate.git synced 2026-01-15 00:55:08 +08:00
Files
locomotive-boilerplate/build/styles.js

89 lines
2.9 KiB
JavaScript
Raw Normal View History

import autoprefixer from 'autoprefixer';
import fs from 'fs';
import sass from 'node-sass';
import postcss from 'postcss';
import paths from '../mconfig.json';
import message from './utils/message.js';
import notification from './utils/notification.js';
/**
* Compiles and minifies main Sass files to CSS.
*/
2021-09-15 17:22:39 -04:00
export async function compileStyles() {
[
'critical',
'main',
].forEach((name) => {
const infile = paths.styles.src + name + '.scss';
const outfile = paths.styles.dest + name + '.css';
const timeLabel = `${name}.css compiled in`;
console.time(timeLabel);
2020-12-11 16:00:36 -05:00
sass.render({
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;
}
postcss([ autoprefixer ]).process(result.css, {
from: outfile,
to: outfile,
map: {
annotation: false,
inline: false,
sourcesContent: true
}
}).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);
notification({
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.toString(), (err) => {
if (err) {
message(`Error compiling ${name}.scss`, 'error');
message(err);
notification({
title: `${name}.scss compilation failed 🚨`,
message: `Could not save sourcemap to ${name}.css.map`
});
return;
}
});
}
});
});
});
2021-09-15 17:24:33 -04:00
};