ecmarkup
Version:
Custom element definitions and core utilities for markup that specifies ECMAScript and related technologies.
66 lines (65 loc) • 2.59 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.minifyGeneratedFiles = minifyGeneratedFiles;
const html_minifier_terser_1 = require("html-minifier-terser");
const CleanCSS = require("clean-css");
const svgo_1 = require("svgo");
const terser_1 = require("terser");
const path = require("path");
const htmlMinifierOptions = {
caseSensitive: true,
collapseBooleanAttributes: true,
collapseWhitespace: false,
decodeEntities: true,
html5: true,
minifyCSS: true,
minifyJS: true,
removeAttributeQuotes: true,
removeComments: true,
removeEmptyAttributes: true,
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
sortAttributes: true,
sortClassName: true,
useShortDoctype: true,
};
const cleanCss = new CleanCSS({ level: 2 });
const svgoConfig = {
multipass: true,
plugins: [{ name: 'preset-default' }, 'removeViewBox'],
};
async function minifyGeneratedFiles(files, log) {
const result = new Map();
for (const [key, value] of files) {
const ext = key != null ? path.extname(key) : null;
if (ext === '.html' || key === null) {
const html = typeof value === 'string' ? value : value.toString('utf-8');
log === null || log === void 0 ? void 0 : log(`Minifying ${key !== null && key !== void 0 ? key : 'stdout'}...`);
const minified = await (0, html_minifier_terser_1.minify)(html, htmlMinifierOptions);
result.set(key, minified);
}
else if (ext === '.css') {
const css = typeof value === 'string' ? value : value.toString('utf-8');
log === null || log === void 0 ? void 0 : log(`Minifying ${key}...`);
const output = cleanCss.minify(css);
result.set(key, output.styles);
}
else if (ext === '.svg') {
const svg = typeof value === 'string' ? value : value.toString('utf-8');
log === null || log === void 0 ? void 0 : log(`Minifying ${key}...`);
const optimized = (0, svgo_1.optimize)(svg, svgoConfig);
result.set(key, optimized.data);
}
else if (ext === '.js') {
const js = typeof value === 'string' ? value : value.toString('utf-8');
log === null || log === void 0 ? void 0 : log(`Minifying ${key}...`);
const output = await (0, terser_1.minify)(js);
result.set(key, output.code);
}
else {
result.set(key, value);
}
}
return result;
}