toolz/config/purgecss.mjs

37 lines
1,007 B
JavaScript
Raw Permalink Normal View History

import { PurgeCSS } from 'purgecss'
import path from 'path'
import config from './config.js' // Make sure the config file is also compatible with ESM
import fs from 'fs'
import chalk from 'chalk'
2023-02-04 18:22:03 +08:00
const pages = config.pages
2023-02-25 19:31:48 +08:00
const options = pages.map((page) => {
const css = path.join(config.build, `css/${page}.css`)
const content = [
path.join(config.build, `${page}.html`),
path.join(config.build, `js/${page}.js`)
]
return {
css: [css],
content: content
}
})
2023-02-04 18:22:03 +08:00
2023-02-25 19:31:48 +08:00
Promise.all(options.map((option) => new PurgeCSS().purge(option))).then(
(results) => {
results.forEach((result, i) => {
const css = result[0].css
const cssFile = path.join(config.build, `css/${pages[i]}.css`)
console.log(chalk.green(`File: ${cssFile}`))
console.log(
`Original size: ${(
fs.statSync(path.join(config.build, `css/${pages[i]}.css`))
.size / 1024
).toFixed(2)}KB`
)
console.log(`Optimized size: ${(css.length / 1024).toFixed(2)}KB`)
fs.writeFileSync(cssFile, css)
})
}
)