* @fileoverview A small wrapper around
* third_party/node/node_modules/postcss and
* third_party/node/node_modules/postcss-minify to process an explicit list of
* HTML or CSS input files, by minifying their CSS contents (removing any blank
* space and comments, no other transformations made).
*/
const postcss =
require('../../third_party/node/node_modules/postcss/lib/postcss.js');
const postcssMinify =
require('../../third_party/node/node_modules/postcss-minify/index.js');
const assert = require('assert');
const fs = require('fs/promises');
const path = require('path');
const REGEX = /<style([^>]*)?>(?<content>[^<]+)<\/style>/d;
async function processCssFile(inputFile, outputFile) {
const contents = await fs.readFile(inputFile, {encoding: 'utf8'});
const result = await postcss([
postcssMinify
]).process(contents, {from: undefined, to: undefined});
await fs.mkdir(path.dirname(outputFile), {recursive: true});
return fs.writeFile(
outputFile, result.css.replace(/\n$/, ''), {enconding: 'utf8'});
}
async function processHtmlFile(inputFile, outputFile) {
const contents = await fs.readFile(inputFile, {encoding: 'utf8'});
let minifiedContents = contents;
const match = contents.match(REGEX);
if (match !== null) {
const result =
await postcss([
postcssMinify
]).process(match.groups['content'], {from: undefined, to: undefined});
const indices = match.indices.groups['content'];
minifiedContents = contents.substring(0, indices[0]) +
result.css.replace(/\n$/, '') + contents.substring(indices[1]);
}
await fs.mkdir(path.dirname(outputFile), {recursive: true});
return fs.writeFile(outputFile, minifiedContents, {enconding: 'utf8'});
}
function main() {
const args = {
inputDir: process.argv[2],
outputDir: process.argv[3],
inputFiles: process.argv.slice(4),
}
for (const f of args.inputFiles) {
assert(f.endsWith('.html') || f.endsWith('.css'));
if (f.endsWith('.html')) {
processHtmlFile(
path.join(args.inputDir, f), path.join(args.outputDir, f));
} else if (f.endsWith('.css')) {
processCssFile(path.join(args.inputDir, f), path.join(args.outputDir, f));
}
}
}
main();