feat: Minify css

- Add dependency clean-css
- Add do_minifycss function as filter to eleventy
- Add workaround for demo mode with computed data file
This commit is contained in:
sthag 2025-04-12 16:13:14 +02:00
parent 024540e389
commit d7ce139ab7
4 changed files with 49 additions and 3 deletions

View file

@ -1,6 +1,33 @@
const CleanCSS = require("clean-css");
// const is_production = typeof process.env.NODE_ENV === "string" && process.env.NODE_ENV === "production";
const is_production = true;
function do_minifycss(source, output_path) {
if(!output_path.endsWith(".css") || !is_production) return source;
const result = new CleanCSS({
level: 2
}).minify(source).styles.trim();
console.log(`MINIFY ${output_path}`, source.length, ``, result.length, `(${((1 - (result.length / source.length)) * 100).toFixed(2)}% reduction)`);
return result;
}
module.exports = function (eleventyConfig) {
// eleventyConfig.addPlugin(EleventyHtmlBasePlugin);
eleventyConfig.addFilter("cssmin", function (code) {
return new CleanCSS({}).minify(code).styles;
});
eleventyConfig.addTransform("cssmin", do_minifycss);
eleventyConfig.addTransform("log", async function (content) {
console.log(this.page.inputPath);
console.log(this.page.outputPath);
return content; // no changes made.
});
eleventyConfig.setNunjucksEnvironmentOptions({
// throwOnUndefined: true,
trimBlocks: true

19
package-lock.json generated
View file

@ -10,6 +10,7 @@
"license": "MIT",
"devDependencies": {
"@11ty/eleventy": "^2.0.1",
"clean-css": "^5.3.3",
"sass": "^1.69.4"
}
},
@ -539,6 +540,18 @@
"fsevents": "~2.3.2"
}
},
"node_modules/clean-css": {
"version": "5.3.3",
"resolved": "https://registry.npmjs.org/clean-css/-/clean-css-5.3.3.tgz",
"integrity": "sha512-D5J+kHaVb/wKSFcyyV75uCn8fiY4sV38XJoe4CUyGQ+mOU/fMVYUdH1hJC+CJQ5uY3EnW27SbJYS4X8BiLrAFg==",
"dev": true,
"dependencies": {
"source-map": "~0.6.0"
},
"engines": {
"node": ">= 10.0"
}
},
"node_modules/color-convert": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz",
@ -583,9 +596,9 @@
}
},
"node_modules/cross-spawn": {
"version": "7.0.3",
"resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz",
"integrity": "sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w==",
"version": "7.0.6",
"resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.6.tgz",
"integrity": "sha512-uV2QOWP2nWzsy2aMp8aRibhi9dlzF5Hgh5SHaB9OiTGEyDTiJJyx0uy51QXdyWbtAHNua4XJzUKca3OzKUd3vA==",
"dev": true,
"dependencies": {
"path-key": "^3.1.0",

View file

@ -32,6 +32,7 @@
"private": true,
"devDependencies": {
"@11ty/eleventy": "^2.0.1",
"clean-css": "^5.3.3",
"sass": "^1.69.4"
}
}

View file

@ -0,0 +1,5 @@
module.exports = {
eleventyComputed: {
path: demoMode ? "/" : "/demo/"
}
};