From 81ed7444e3023087eaabfc08dd8dc525bcb60cd8 Mon Sep 17 00:00:00 2001 From: Jelle De Loecker Date: Sun, 3 Mar 2024 23:09:52 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20`postcss-prune-var`=20depende?= =?UTF-8?q?ncy=20to=20remove=20unused=20variables=20from=20CSS=20files?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 1 + lib/core/middleware.js | 14 +++++++++++++- package.json | 1 + 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index afe53c7..26e7acc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ * Add the abstract `Meta` field class: these should be used for fields that are not directly stored in the database * Add association aliases as a meta field to model schemas * Add `should_add_exports()` SCSS function +* Add `postcss-prune-var` dependency to remove unused variables from CSS files ## 1.4.0-alpha.3 (2024-02-25) diff --git a/lib/core/middleware.js b/lib/core/middleware.js index 4e64759..df53baa 100644 --- a/lib/core/middleware.js +++ b/lib/core/middleware.js @@ -16,6 +16,7 @@ let publicDirs = alchemy.shared('public.directories', new Deck()), regenerator_runtime, sass_functions, babel_polyfill, + postcss_prune, babel_preset, babel_async, autoprefixer, @@ -93,6 +94,7 @@ if (alchemy.settings.frontend.stylesheet.enable_scss) { if (alchemy.settings.frontend.stylesheet.enable_post !== false) { postcss = alchemy.use('postcss'); autoprefixer = alchemy.use('autoprefixer'); + postcss_prune = alchemy.use('postcss-prune-var')(); } if (alchemy.settings.frontend.javascript.enable_babel) { @@ -1009,7 +1011,16 @@ Alchemy.setMethod(function findAssetPath(assetFile, directories, callback) { * @param {Function} callback */ function doPostCss(css, options, callback) { - postcss([autoprefixer]).process(css, {from: undefined}).then(function gotCssResult(result) { + + let postcss_config = [ + autoprefixer + ]; + + if (options.prune !== false) { + postcss_config.push(postcss_prune); + } + + postcss(postcss_config).process(css, {from: undefined}).then(function gotCssResult(result) { if (options?.post_css_only) { return callback(null, result); @@ -1486,6 +1497,7 @@ Alchemy.setMethod(async function extractCSSExports(css_path) { let result = await findStylesheet(css_path, { post_css_only: true, add_exports: true, + prune: false, compile: true }); diff --git a/package.json b/package.json index b70a54c..17d691f 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ "mongodb" : "~6.1.0", "ncp" : "~2.0.0", "postcss" : "~8.4.31", + "postcss-prune-var": "~1.1.2", "protoblast" : "~0.9.1", "semver" : "~7.5.4", "socket.io" : "~4.7.2",