2022-10-04 17:46:12 -04:00
|
|
|
import loconfig from '../helpers/config.js';
|
|
|
|
|
import message from '../helpers/message.js';
|
|
|
|
|
import notification from '../helpers/notification.js';
|
|
|
|
|
import {
|
|
|
|
|
createProcessor,
|
2022-03-29 21:08:48 -04:00
|
|
|
pluginsMap as postcssPluginsMap,
|
|
|
|
|
supportsPostCSS
|
2022-10-04 17:46:12 -04:00
|
|
|
} from '../helpers/postcss.js';
|
|
|
|
|
import resolve from '../helpers/template.js';
|
|
|
|
|
import { merge } from '../utils/index.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';
|
2023-09-08 17:11:27 -04:00
|
|
|
import * as sass from 'sass';
|
2022-06-06 16:28:07 -04:00
|
|
|
import { PurgeCSS } from 'purgecss';
|
2021-09-16 18:08:20 -04: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 = {
|
2024-10-16 10:07:10 -04:00
|
|
|
sourceMapIncludeSources: true,
|
2021-09-21 17:33:32 -04:00
|
|
|
sourceMap: true,
|
|
|
|
|
};
|
2022-10-06 12:28:02 -04:00
|
|
|
|
2021-09-21 17:33:32 -04:00
|
|
|
export const developmentSassOptions = Object.assign({}, defaultSassOptions, {
|
2024-10-16 10:07:10 -04:00
|
|
|
style: 'expanded',
|
2021-09-21 17:33:32 -04:00
|
|
|
});
|
|
|
|
|
export const productionSassOptions = Object.assign({}, defaultSassOptions, {
|
2024-10-16 10:07:10 -04:00
|
|
|
style: 'compressed',
|
2021-09-21 17:33:32 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @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,
|
2023-07-10 15:40:56 +02:00
|
|
|
false
|
2021-09-21 17:33:32 -04:00
|
|
|
];
|
|
|
|
|
export const productionStylesArgs = [
|
|
|
|
|
productionSassOptions,
|
|
|
|
|
productionPostCSSOptions,
|
2023-07-10 15:40:56 +02:00
|
|
|
true
|
2021-09-21 17:33:32 -04:00
|
|
|
];
|
|
|
|
|
|
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
|
|
|
*/
|
2023-07-10 15:40:56 +02:00
|
|
|
export default async function compileStyles(sassOptions = null, postcssOptions = null, purge = true) {
|
2021-09-21 17:33:32 -04:00
|
|
|
if (sassOptions == null) {
|
|
|
|
|
sassOptions = productionSassOptions;
|
|
|
|
|
} else if (
|
|
|
|
|
sassOptions !== developmentSassOptions &&
|
|
|
|
|
sassOptions !== productionSassOptions
|
|
|
|
|
) {
|
2022-10-04 17:46:12 -04:00
|
|
|
sassOptions = merge({}, defaultSassOptions, sassOptions);
|
2021-09-21 17:33:32 -04:00
|
|
|
}
|
|
|
|
|
|
2022-03-29 21:08:48 -04:00
|
|
|
if (supportsPostCSS) {
|
2021-09-21 17:33:32 -04:00
|
|
|
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
|
|
|
) {
|
2022-10-04 17:46:12 -04:00
|
|
|
postcssOptions = merge({}, defaultPostCSSOptions, postcssOptions);
|
2021-09-21 17:33:32 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-29 18:47:53 -04:00
|
|
|
/**
|
|
|
|
|
* @async
|
|
|
|
|
* @param {object} entry - The entrypoint to process.
|
|
|
|
|
* @param {string[]} entry.infile - The file to process.
|
|
|
|
|
* @param {string} entry.outfile - The file to write to.
|
|
|
|
|
* @param {?string} [entry.label] - The task label.
|
|
|
|
|
* Defaults to the outfile name.
|
|
|
|
|
* @return {Promise}
|
|
|
|
|
*/
|
2023-08-08 14:49:47 -04:00
|
|
|
loconfig.tasks.styles?.forEach(async ({
|
2021-09-17 00:26:18 -04:00
|
|
|
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
|
|
|
|
2024-10-16 10:07:10 -04:00
|
|
|
let result = sass.compile(infile, sassOptions);
|
2020-12-11 10:41:07 -05:00
|
|
|
|
2022-03-29 21:08:48 -04:00
|
|
|
if (supportsPostCSS && postcssOptions) {
|
2021-09-21 17:30:48 -04:00
|
|
|
if (typeof postcssProcessor === 'undefined') {
|
2022-10-04 17:46:12 -04:00
|
|
|
postcssProcessor = createProcessor(
|
2021-09-21 17:33:32 -04:00
|
|
|
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.
|
2023-07-10 15:40:56 +02:00
|
|
|
if (outfile && purge) {
|
2022-06-06 16:30:59 -04:00
|
|
|
purgeUnusedCSS(outfile, `${label || `${filestem}.css`}`);
|
2022-04-11 16:19:59 -04:00
|
|
|
}
|
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
|
|
|
|
2022-03-24 17:53:45 -04:00
|
|
|
/**
|
2022-06-06 16:30:59 -04:00
|
|
|
* Purge unused styles from CSS files.
|
2022-03-24 17:53:45 -04:00
|
|
|
*
|
|
|
|
|
* @async
|
|
|
|
|
*
|
2022-06-06 16:30:59 -04:00
|
|
|
* @param {string} outfile - The path of a css file
|
|
|
|
|
* If missing the function stops.
|
|
|
|
|
* @param {string} label - The CSS file label or name.
|
|
|
|
|
* @return {Promise}
|
2022-03-24 17:53:45 -04:00
|
|
|
*/
|
2022-06-06 16:30:59 -04:00
|
|
|
async function purgeUnusedCSS(outfile, label) {
|
2023-08-08 14:49:47 -04:00
|
|
|
const contentFiles = loconfig.tasks.purgeCSS?.content;
|
|
|
|
|
if (!Array.isArray(contentFiles) || !contentFiles.length) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-06 16:30:59 -04:00
|
|
|
label = label ?? basename(outfile);
|
2022-10-04 17:46:12 -04:00
|
|
|
|
2022-08-12 14:03:55 -04:00
|
|
|
const timeLabel = `${label} purged in`;
|
2022-06-06 16:30:59 -04:00
|
|
|
console.time(timeLabel);
|
2022-03-24 17:53:45 -04:00
|
|
|
|
2022-10-04 17:46:12 -04:00
|
|
|
const purgeCSSResults = await (new PurgeCSS()).purge({
|
2023-08-08 14:49:47 -04:00
|
|
|
content: contentFiles,
|
2022-06-06 16:30:59 -04:00
|
|
|
css: [ outfile ],
|
2023-07-10 15:40:56 +02:00
|
|
|
defaultExtractor: content => content.match(/[a-z0-9_\-\\\/\@]+/gi) || [],
|
|
|
|
|
fontFaces: true,
|
|
|
|
|
keyframes: true,
|
2022-03-24 17:53:45 -04:00
|
|
|
safelist: {
|
2023-07-10 15:40:56 +02:00
|
|
|
// Keep all except .u-gc-* | .u-margin-* | .u-padding-*
|
2024-01-16 15:07:09 -05:00
|
|
|
standard: [ /^(?!.*\b(u-gc-|u-margin|u-padding)).*$/ ]
|
2023-07-10 15:40:56 +02:00
|
|
|
},
|
|
|
|
|
variables: true,
|
2022-03-24 17:53:45 -04:00
|
|
|
})
|
|
|
|
|
|
2022-10-04 17:46:12 -04:00
|
|
|
for (let result of purgeCSSResults) {
|
2022-03-24 17:53:45 -04:00
|
|
|
await writeFile(outfile, result.css)
|
|
|
|
|
|
2022-06-06 16:30:59 -04:00
|
|
|
message(`${label} purged`, 'chore', timeLabel);
|
2022-03-24 17:53:45 -04:00
|
|
|
}
|
|
|
|
|
}
|