2022-03-22 16:17:49 -04:00
|
|
|
import loconfig from '../utils/config.js';
|
2021-09-17 00:04:56 -04:00
|
|
|
import message from '../utils/message.js';
|
|
|
|
|
import notification from '../utils/notification.js';
|
2021-09-21 17:33:32 -04:00
|
|
|
import postcss, { pluginsMap as postcssPluginsMap } from '../utils/postcss.js';
|
2022-03-24 17:53:45 -04:00
|
|
|
import { PurgeCSS } from 'purgecss';
|
2022-03-22 16:08:29 -04:00
|
|
|
import resolve 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-21 17:30:48 -04:00
|
|
|
let postcssProcessor;
|
|
|
|
|
|
2021-09-21 17:33:32 -04:00
|
|
|
/**
|
|
|
|
|
* @const {object} defaultSassOptions - The default shared Sass options.
|
|
|
|
|
* @const {object} developmentSassOptions - The predefined Sass options for development.
|
|
|
|
|
* @const {object} productionSassOptions - The predefined Sass options for production.
|
|
|
|
|
*/
|
|
|
|
|
export const defaultSassOptions = {
|
|
|
|
|
omitSourceMapUrl: true,
|
|
|
|
|
sourceMap: true,
|
|
|
|
|
sourceMapContents: true,
|
|
|
|
|
};
|
|
|
|
|
export const developmentSassOptions = Object.assign({}, defaultSassOptions, {
|
|
|
|
|
outputStyle: 'expanded',
|
|
|
|
|
});
|
|
|
|
|
export const productionSassOptions = Object.assign({}, defaultSassOptions, {
|
|
|
|
|
outputStyle: 'compressed',
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @const {object} defaultPostCSSOptions - The default shared PostCSS options.
|
|
|
|
|
* @const {object} developmentPostCSSOptions - The predefined PostCSS options for development.
|
|
|
|
|
* @const {object} productionPostCSSOptions - The predefined PostCSS options for production.
|
|
|
|
|
*/
|
|
|
|
|
export const defaultPostCSSOptions = {
|
|
|
|
|
processor: {
|
|
|
|
|
map: {
|
|
|
|
|
annotation: false,
|
|
|
|
|
inline: false,
|
|
|
|
|
sourcesContent: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
export const developmentPostCSSOptions = Object.assign({}, defaultPostCSSOptions);
|
|
|
|
|
export const productionPostCSSOptions = Object.assign({}, defaultPostCSSOptions);
|
|
|
|
|
|
|
|
|
|
/**
|
2021-11-03 17:08:38 -04:00
|
|
|
* @const {object|boolean} developmentStylesArgs - The predefined `compileStyles()` options for development.
|
|
|
|
|
* @const {object|boolean} productionStylesArgs - The predefined `compileStyles()` options for production.
|
2021-09-21 17:33:32 -04:00
|
|
|
*/
|
|
|
|
|
export const developmentStylesArgs = [
|
|
|
|
|
developmentSassOptions,
|
|
|
|
|
developmentPostCSSOptions,
|
|
|
|
|
];
|
|
|
|
|
export const productionStylesArgs = [
|
|
|
|
|
productionSassOptions,
|
|
|
|
|
productionPostCSSOptions,
|
|
|
|
|
];
|
|
|
|
|
|
2021-09-16 14:22:20 -04:00
|
|
|
/**
|
|
|
|
|
* Compiles and minifies main Sass files to CSS.
|
2021-09-21 16:01:47 -04:00
|
|
|
*
|
2021-09-21 17:33:32 -04:00
|
|
|
* @todo Add deep merge of `postcssOptions` to better support customization
|
|
|
|
|
* of default processor options.
|
|
|
|
|
*
|
2021-09-21 16:01:47 -04:00
|
|
|
* @async
|
2021-09-21 17:33:32 -04:00
|
|
|
* @param {object} [sassOptions=null] - Customize the Sass render API options.
|
|
|
|
|
* If `null`, default production options are used.
|
|
|
|
|
* @param {object|boolean} [postcssOptions=null] - Customize the PostCSS processor API options.
|
|
|
|
|
* If `null`, default production options are used.
|
|
|
|
|
* If `false`, PostCSS processing will be ignored.
|
2021-09-21 16:01:47 -04:00
|
|
|
* @return {Promise}
|
2021-09-16 14:22:20 -04:00
|
|
|
*/
|
2021-09-21 17:33:32 -04:00
|
|
|
export default async function compileStyles(sassOptions = null, postcssOptions = null) {
|
|
|
|
|
if (sassOptions == null) {
|
|
|
|
|
sassOptions = productionSassOptions;
|
|
|
|
|
} else if (
|
|
|
|
|
sassOptions !== developmentSassOptions &&
|
|
|
|
|
sassOptions !== productionSassOptions
|
|
|
|
|
) {
|
|
|
|
|
sassOptions = Object.assign({}, defaultSassOptions, sassOptions);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (postcss) {
|
|
|
|
|
if (postcssOptions == null) {
|
|
|
|
|
postcssOptions = productionPostCSSOptions;
|
|
|
|
|
} else if (
|
|
|
|
|
postcssOptions !== false &&
|
2021-11-03 16:16:08 -04:00
|
|
|
postcssOptions !== developmentPostCSSOptions &&
|
|
|
|
|
postcssOptions !== productionPostCSSOptions
|
2021-09-21 17:33:32 -04:00
|
|
|
) {
|
|
|
|
|
postcssOptions = Object.assign({}, defaultPostCSSOptions, postcssOptions);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-17 00:26:18 -04:00
|
|
|
loconfig.tasks.styles.forEach(async ({
|
|
|
|
|
infile,
|
2021-12-03 13:22:59 -05:00
|
|
|
outfile,
|
|
|
|
|
label = null
|
2021-09-17 00:26:18 -04:00
|
|
|
}) => {
|
2021-12-03 13:22:59 -05:00
|
|
|
const filestem = basename((outfile || 'undefined'), '.css');
|
2020-11-27 16:01:54 -05:00
|
|
|
|
2021-12-03 13:22:59 -05:00
|
|
|
const timeLabel = `${label || `${filestem}.css`} compiled in`;
|
2021-09-16 14:22:20 -04:00
|
|
|
console.time(timeLabel);
|
2020-12-11 16:00:36 -05:00
|
|
|
|
2021-09-16 18:08:20 -04:00
|
|
|
try {
|
2022-03-22 16:08:29 -04:00
|
|
|
infile = resolve(infile);
|
|
|
|
|
outfile = resolve(outfile);
|
2021-09-17 00:26:18 -04:00
|
|
|
|
2021-09-21 17:33:32 -04:00
|
|
|
let result = await sassRender(Object.assign({}, sassOptions, {
|
2021-09-16 18:08:20 -04:00
|
|
|
file: infile,
|
|
|
|
|
outFile: outfile,
|
2021-09-21 17:33:32 -04:00
|
|
|
}));
|
2020-12-11 10:41:07 -05:00
|
|
|
|
2021-09-21 17:33:32 -04:00
|
|
|
if (postcss && postcssOptions) {
|
2021-09-21 17:30:48 -04:00
|
|
|
if (typeof postcssProcessor === 'undefined') {
|
2021-09-21 17:33:32 -04:00
|
|
|
postcssProcessor = createPostCSSProcessor(
|
|
|
|
|
postcssPluginsMap,
|
|
|
|
|
postcssOptions
|
|
|
|
|
);
|
2021-09-21 17:30:48 -04:00
|
|
|
}
|
|
|
|
|
|
2021-09-21 17:33:32 -04:00
|
|
|
result = await postcssProcessor.process(
|
|
|
|
|
result.css,
|
|
|
|
|
Object.assign({}, postcssOptions.processor, {
|
|
|
|
|
from: outfile,
|
|
|
|
|
to: outfile,
|
|
|
|
|
})
|
|
|
|
|
);
|
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) {
|
2021-12-03 13:22:59 -05:00
|
|
|
message(`Error processing ${label || `${filestem}.css`}`, 'warning');
|
2021-09-16 16:51:21 -04:00
|
|
|
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-10-12 12:31:02 -04:00
|
|
|
try {
|
2022-03-24 17:53:45 -04:00
|
|
|
await writeFile(outfile, result.css).then(() => {
|
2022-04-11 15:57:18 -04:00
|
|
|
// Purge CSS once file exists.
|
2022-04-11 16:19:59 -04:00
|
|
|
if (outfile) {
|
|
|
|
|
purgeUnusedCSS(outfile, filestem);
|
|
|
|
|
}
|
2022-03-24 17:53:45 -04:00
|
|
|
});
|
2021-10-12 12:31:02 -04:00
|
|
|
|
2021-09-16 18:08:20 -04:00
|
|
|
if (result.css) {
|
2021-12-03 13:22:59 -05:00
|
|
|
message(`${label || `${filestem}.css`} compiled`, 'success', timeLabel);
|
2021-09-16 18:08:20 -04:00
|
|
|
} else {
|
2021-12-03 13:22:59 -05:00
|
|
|
message(`${label || `${filestem}.css`} is empty`, 'notice', timeLabel);
|
2021-09-16 18:08:20 -04:00
|
|
|
}
|
2021-10-12 12:31:02 -04:00
|
|
|
} catch (err) {
|
2021-12-03 13:22:59 -05:00
|
|
|
message(`Error compiling ${label || `${filestem}.css`}`, 'error');
|
2021-09-15 17:29:07 -04:00
|
|
|
message(err);
|
2021-09-14 19:21:24 -04:00
|
|
|
|
2021-09-15 17:29:07 -04:00
|
|
|
notification({
|
2021-12-03 13:22:59 -05:00
|
|
|
title: `${label || `${filestem}.css`} save failed 🚨`,
|
|
|
|
|
message: `Could not save stylesheet to ${label || `${filestem}.css`}`
|
2021-09-15 15:54:53 -04:00
|
|
|
});
|
2021-10-12 12:31:02 -04:00
|
|
|
}
|
2021-09-15 15:54:53 -04:00
|
|
|
|
2021-09-16 18:08:20 -04:00
|
|
|
if (result.map) {
|
2021-10-12 12:31:02 -04:00
|
|
|
try {
|
2021-10-12 16:04:29 -04:00
|
|
|
await writeFile(outfile + '.map', result.map.toString());
|
2021-10-12 12:31:02 -04:00
|
|
|
} catch (err) {
|
2021-12-03 13:22:59 -05:00
|
|
|
message(`Error compiling ${label || `${filestem}.css.map`}`, 'error');
|
2021-09-16 18:08:20 -04:00
|
|
|
message(err);
|
2021-09-14 19:21:24 -04:00
|
|
|
|
2021-09-16 18:08:20 -04:00
|
|
|
notification({
|
2021-12-03 13:22:59 -05:00
|
|
|
title: `${label || `${filestem}.css.map`} save failed 🚨`,
|
|
|
|
|
message: `Could not save sourcemap to ${label || `${filestem}.css.map`}`
|
2021-09-16 18:08:20 -04:00
|
|
|
});
|
2021-10-12 12:31:02 -04:00
|
|
|
}
|
2021-09-15 17:29:07 -04:00
|
|
|
}
|
2021-09-16 18:08:20 -04:00
|
|
|
} catch (err) {
|
2021-12-03 13:22:59 -05:00
|
|
|
message(`Error compiling ${label || `${filestem}.scss`}`, 'error');
|
2021-09-21 16:49:26 -04:00
|
|
|
message(err.formatted || err);
|
2021-09-16 18:08:20 -04:00
|
|
|
|
|
|
|
|
notification({
|
2021-12-03 13:22:59 -05:00
|
|
|
title: `${label || `${filestem}.scss`} compilation failed 🚨`,
|
2021-09-21 16:49:26 -04:00
|
|
|
message: (err.formatted || `${err.name}: ${err.message}`)
|
2021-09-16 18:08:20 -04:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
2021-09-21 17:33:32 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates a PostCSS Processor with the given plugins and options.
|
|
|
|
|
*
|
|
|
|
|
* @param {array<(function|object)>|object<string, (function|object)>} pluginsListOrMap -
|
|
|
|
|
* A list or map of plugins.
|
|
|
|
|
* If a map of plugins, the plugin name looks up `options`.
|
|
|
|
|
* @param {object} options - The PostCSS options.
|
|
|
|
|
*/
|
|
|
|
|
function createPostCSSProcessor(pluginsListOrMap, options)
|
|
|
|
|
{
|
|
|
|
|
let plugins;
|
|
|
|
|
|
|
|
|
|
if (Array.isArray(pluginsListOrMap)) {
|
|
|
|
|
plugins = pluginsListOrMap;
|
|
|
|
|
} else {
|
|
|
|
|
plugins = [];
|
|
|
|
|
|
|
|
|
|
for (let [ name, plugin ] of Object.entries(pluginsListOrMap)) {
|
|
|
|
|
if (name in options) {
|
|
|
|
|
plugin = plugin[name](options[name]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
plugins.push(plugin);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return postcss(plugins);
|
|
|
|
|
}
|
2022-03-24 17:53:45 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Purge unused styles of minified CSS files.
|
|
|
|
|
*
|
|
|
|
|
* @async
|
|
|
|
|
*
|
2022-04-11 15:57:18 -04:00
|
|
|
* @param {string} outfile - The path of the CSS file.
|
|
|
|
|
* @param {string} filestem - The CSS file name.
|
2022-03-24 17:53:45 -04:00
|
|
|
*/
|
2022-04-11 16:19:59 -04:00
|
|
|
async function purgeUnusedCSS(outfile, filestem) {
|
2022-03-24 17:53:45 -04:00
|
|
|
|
|
|
|
|
const timeLabel = `${filestem}.css purged in`;
|
|
|
|
|
console.time(timeLabel);
|
|
|
|
|
|
|
|
|
|
const purgeCSSContentFiles = Array.from(loconfig.tasks.purgeCSS.content);
|
|
|
|
|
|
|
|
|
|
const purgeCSSResults = await new PurgeCSS().purge({
|
|
|
|
|
content: purgeCSSContentFiles,
|
|
|
|
|
css: [outfile],
|
|
|
|
|
rejected: true,
|
|
|
|
|
defaultExtractor: content => content.match(/[a-z0-9_\-\\\/\@]+/gi) || [],
|
|
|
|
|
safelist: {
|
|
|
|
|
standard: [/^((?!\bu-gc-).)*$/]
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
for(let result of purgeCSSResults) {
|
|
|
|
|
await writeFile(outfile, result.css)
|
|
|
|
|
|
2022-04-11 16:19:59 -04:00
|
|
|
message(`${filestem}.css purged`, 'chore', timeLabel);
|
2022-03-24 17:53:45 -04:00
|
|
|
}
|
|
|
|
|
}
|