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

Add PurgeCSS u-gc* tasks

This commit is contained in:
Deven Caron
2022-03-24 17:53:45 -04:00
parent de6b3d73a1
commit ad4a1c7d47
5 changed files with 176 additions and 3 deletions

View File

@@ -2,6 +2,7 @@ import loconfig from '../utils/config.js';
import message from '../utils/message.js';
import notification from '../utils/notification.js';
import postcss, { pluginsMap as postcssPluginsMap } from '../utils/postcss.js';
import { PurgeCSS } from 'purgecss';
import resolve from '../utils/template.js';
import { writeFile } from 'node:fs/promises';
import { basename } from 'node:path';
@@ -142,7 +143,10 @@ export default async function compileStyles(sassOptions = null, postcssOptions =
}
try {
await writeFile(outfile, result.css);
await writeFile(outfile, result.css).then(() => {
//Purge css once file exists.
purgeUnusedCSS(outfile, filestem);
});
if (result.css) {
message(`${label || `${filestem}.css`} compiled`, 'success', timeLabel);
@@ -212,3 +216,39 @@ function createPostCSSProcessor(pluginsListOrMap, options)
return postcss(plugins);
}
/**
* Purge unused styles of minified CSS files.
*
* @async
*
* @param {string} [outfile=false] - The path of a css file
* If missing the function stops.
* @param {string} text - The file name.
*/
async function purgeUnusedCSS(outfile = false, filestem) {
if(!outfile) {
return;
}
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)
message(`${filestem}.css purged`, 'cleaning', timeLabel);
}
}

View File

@@ -17,6 +17,10 @@ export default function message(text, type, timerID) {
console.log('✅ ', kleur.bgGreen().black(text));
break;
case 'cleaning':
console.log('🧹 ', kleur.bgGreen().black(text));
break;
case 'notice':
console.log(' ', kleur.bgBlue().black(text));
break;