日本語

Fixing Lighthouse Minify CSS and Minify JavaScript Warnings

​​Lighthouse​CSS,esbuild,gzip,JavaScript,Performance

When Lighthouse reports "Minify CSS" or "Minify JavaScript" (in the Japanese UI, these appear as the equivalents of "CSS minification" and "JavaScript minification"), you can fix them by minifying the CSS and JavaScript files before serving them. Minify means removing whitespace and comments and rewriting code in a shorter form. These items matter when you want a page to display faster or when you want to reduce how much a phone has to download.

On the page I tested, replacing the files with esbuild-minified versions cleared both warnings. The combined transfer size of the CSS and JavaScript dropped from 844,908 bytes to 309,252 bytes on a server without gzip, and from 133,324 bytes to 58,798 bytes on a server with gzip.

Note

The test environment was Ubuntu 24.04, Node.js 22, Lighthouse 13.6.0 (the npm package), and esbuild 0.28.2. I ran Lighthouse from the command line with the default settings (mobile), only the performance category, and one run per page. Chrome was Chrome for Testing 153, specified with the CHROME_PATH environment variable. I used Playwright 1.63.0 to check how the page behaved. The test page used the unminified versions of bootstrap.css from Bootstrap 5.3.3 and lodash.js from lodash 4.17.21, plus small CSS and JavaScript files I wrote for the test. I served the page from a small Node.js server on the same VM, in two setups: without gzip, and with gzip (level 6). I did not test the Lighthouse panel in Chrome DevTools, PageSpeed Insights, desktop settings, minification with other tools such as Vite, webpack, or Terser, WordPress plugins, or Brotli compression.

The page with the warnings

The test page is a simple table for recording a cat's weight. It loads these four files.

File Size (bytes) Contents
css/bootstrap.css 281,046 Unminified Bootstrap
css/app.css 477 Test CSS I wrote (with comments)
js/lodash.js 544,098 Unminified lodash
js/app.js 18,573 Test JavaScript I wrote (with comments)

I served the page without gzip and ran Lighthouse. I extracted the minify-related parts from the JSON result with a small script (summarize.mjs), and got the following.

console
$ npx lighthouse http://localhost:8080/ --only-categories=performance --output=json --output=html --output-path=./out/src-identity --chrome-flags="--headless=new --no-sandbox" --quiet
exit: 0
$ node summarize.mjs out/src-identity.report.json
lighthouseVersion: 13.6.0  formFactor: mobile  throttling: simulate
performance score: 66  FCP: 5.5 s  LCP: 5.5 s  TBT: 0 ms
unminified-css: score=0 scoreDisplayMode=metricSavings title="Minify CSS" displayValue="Est savings of 46 KiB" metricSavings={"FCP":300,"LCP":300}
  /css/bootstrap.css  totalBytes=281222  wastedBytes=46937  wastedPercent=16.7
unminified-javascript: score=0 scoreDisplayMode=metricSavings title="Minify JavaScript" displayValue="Est savings of 401 KiB" metricSavings={"FCP":2400,"LCP":2400}
  /js/lodash.js  totalBytes=544098  wastedBytes=398556  wastedPercent=73.3
  /js/app.js  totalBytes=18573  wastedBytes=12094  wastedPercent=65.1

(Lines listing the loaded files are omitted.)

In the HTML report, opening the "Minify JavaScript" item shows a table with each file's transfer size and its estimated savings (Est Savings).

Lighthouse report with the Minify JavaScript item expanded. It shows Est savings of 401 KiB. The table shows that /js/lodash.js could save 389.2 KiB of its 531.3 KiB, and /js/app.js could save 11.8 KiB of its 18.1 KiB.
Report for a server without gzip

I wrote app.css with plenty of comments and whitespace, but it did not appear in the list. I explain why in a later section.

In Lighthouse 13.6.0, both items are in the "Diagnostics" group (the Japanese UI shows this group under the equivalent of "diagnostics"), and they have a weight of 0 in the performance score. The report marks them as "Unscored." These items do not count toward the score themselves, but making the files smaller changes metrics such as FCP (First Contentful Paint, the time until something first appears on the page), and that changes the score.

Replacing files with esbuild-minified versions

I used esbuild to minify. For both CSS and JavaScript, adding --minify is enough. I put the original files in src/ and wrote the minified output to dist/.

console
$ npx esbuild src/css/bootstrap.css src/css/app.css src/js/lodash.js src/js/app.js --minify --outdir=dist --outbase=src

  dist/css/bootstrap.css  227.6kb
  dist/js/lodash.js        71.1kb
  dist/js/app.js            2.5kb
  dist/css/app.css          155b 

--outbase=src keeps the folder structure (css/ and js/) under dist/. I copied the HTML to dist/ unchanged, so the paths in the <link> and <script> tags did not change.

According to npx esbuild --help, --minify turns on three options at once: --minify-whitespace removes whitespace, --minify-identifiers shortens variable and other names, and --minify-syntax rewrites code into equivalent shorter forms. In my test app.js, function names became one character long, and 1000 became 1e3.

js
(function(){"use strict";function c(n){const o=n/1e3;return _.round(o,2)}function e(n){const o=n/1e3;return _.round(o,2)}...

(I show only the beginning because the line is long.)

The @license comment at the top of lodash.js and the /*! comment at the top of bootstrap.css remained after minification. The esbuild --help text describes --legal-comments, which controls where license comments (legal comments) go. It says the default is inline (keep them in place) when not bundling.

I used Playwright to confirm that the page still behaved the same after minification. The number of table rows (40), the number of rows added since the previous day (19), the average sentence, and the color of the added rows were all the same as before minification. Both versions produced zero console errors.

Reports after replacement

I served the original and minified files, both with and without gzip, and compared the Lighthouse results. Transfer sizes are the values of network-requests in the Lighthouse results.

Delivery CSS transfer size JavaScript transfer size Warnings Score FCP
No minify, no gzip 281,872 563,036 Both CSS and JS 66 5.5 s
Minified, no gzip 233,526 75,726 None 93 2.6 s
No minify, gzip 34,077 99,247 Both CSS and JS 99 1.7 s
Minified, gzip 31,353 27,445 None 100 1.3 s

(Transfer sizes are in bytes and are the sum of the two files. The FCP is Lighthouse's simulated estimate for a mobile connection, from a single run.)

On the minified pages, with or without gzip, both "Minify CSS" and "Minify JavaScript" passed, and neither appeared in the list.

CSS and JavaScript shrank by very different amounts. Without gzip, lodash.js went from 544,098 bytes to 72,832 bytes, but bootstrap.css went only from 281,046 bytes to 233,022 bytes. With gzip, the CSS transfer size dropped from 34,077 bytes to 31,353 bytes, which is less than 10%.

Lighthouse's estimate only counts whitespace and comments

Looking at the code of Lighthouse 13.6.0, Lighthouse calculates the estimated savings as follows.

  1. Count the characters in the file after removing whitespace and comments. License comments such as /*! and @license are counted without being removed.
  2. Multiply the removed fraction by the file's transfer size (for JavaScript, the size excluding the response headers).
  3. If the fraction is small (under 5% for CSS, under 10% for JavaScript) or the estimated savings are under 2,048 bytes, the file is not listed.

Chrome's official explanation of "Minify CSS" also says the estimate is a conservative value based on comments and whitespace in the CSS, and that tools which minify can save more than the estimate.

The estimate does not include shortening variable names, so JavaScript shrank more than estimated. Based on the estimate, lodash.js without gzip should have dropped by 398,556 bytes, to 145,542 bytes. With esbuild's --minify, it went to 72,832 bytes. With only --minify-whitespace, esbuild produced 147,393 bytes, which is close to Lighthouse's estimate.

lodash.js processed with esbuild Size After gzip
No processing 544,098 97,552
Whitespace removed only 147,393 36,316
Whitespace removed and syntax shortened 144,347 36,019
--minify (identifiers also shortened) 72,832 26,471

(Unit: bytes. The gzip size is for level 6 compression.)

In the other direction, the CSS did not shrink as much as the estimate when served with gzip. The estimate for bootstrap.css was 5,592 bytes, but the actual transfer size went from 33,507 bytes to 31,020 bytes, a reduction of only 2,487 bytes. I also measured with Node.js's zlib. After gzip, bootstrap.css was 33,308 bytes before whitespace removal and 30,903 bytes after, a difference of 2,405 bytes. The estimate multiplies the fraction of the unminified size (16.7%) directly by the post-gzip transfer size, so it does not match the actual difference.

Serving with gzip changes which files are listed

When I served the original files from a server with gzip, the "Minify JavaScript" estimate dropped from 401 KiB to 70 KiB, and app.js disappeared from the list.

Minify JavaScript item on a server with gzip. It shows Est savings of 70 KiB. The table lists only /js/lodash.js, which could save 69.8 KiB of its 95.3 KiB.
Report for a server with gzip
console
$ node summarize.mjs out/src-gzip.report.json
lighthouseVersion: 13.6.0  formFactor: mobile  throttling: simulate
performance score: 99  FCP: 1.7 s  LCP: 1.7 s  TBT: 10 ms
unminified-css: score=0.5 scoreDisplayMode=metricSavings title="Minify CSS" displayValue="Est savings of 5 KiB" metricSavings={"FCP":0,"LCP":0}
  /css/bootstrap.css  totalBytes=33507  wastedBytes=5592  wastedPercent=16.7
unminified-javascript: score=0 scoreDisplayMode=metricSavings title="Minify JavaScript" displayValue="Est savings of 70 KiB" metricSavings={"FCP":450,"LCP":450}
  /js/lodash.js  totalBytes=97552  wastedBytes=71458  wastedPercent=73.3

(Lines listing the loaded files are omitted.)

app.js was still unminified, and 65% of it was whitespace and comments. Even so, its transfer size after gzip was only 1,489 bytes. Sixty-five percent of that is still under 2,048 bytes, so it was not listed. The same reason explains why app.css (477 bytes) did not appear even without gzip.

So a file that is not in the Lighthouse list is not necessarily minified. When I minified app.js with esbuild, it went from 18,573 bytes to 2,531 bytes, and its transfer size after gzip went from 1,489 bytes to 768 bytes.

Point

Minification and compression such as gzip work independently. In this test, doing both gave the smallest transfer size (58,798 bytes for CSS and JavaScript combined). After minifying, check the Content-Encoding header in the response to confirm that your server compresses CSS and JavaScript.

​Posted by 86