Remove comments from the *built* CSS files

The old pre-processor used for CSS, and HTML, files leaves comments intact which unnecessarily contributes to the overall size of the *built* CSS files (note that the built JavaScript files don't include comments).
Rather than trying to "hack" comment removal into the pre-processor it seems easier to use a PostCSS plugin instead. The one potential issue is that it also affects *some* whitespaces, and it's not clear to me if this'll work with the various CSS-related tests that run in mozilla-central.

Please refer to https://www.npmjs.com/package/postcss-discard-comments for additional information.
This commit is contained in:
Jonas Jenwald 2023-09-22 18:27:08 +02:00
parent 3f7060e777
commit 7413546e16
3 changed files with 45 additions and 7 deletions

View File

@ -27,6 +27,7 @@ import { mkdirp } from "mkdirp";
import path from "path";
import postcss from "gulp-postcss";
import postcssDirPseudoClass from "postcss-dir-pseudo-class";
import postcssDiscardComments from "postcss-discard-comments";
import postcssNesting from "postcss-nesting";
import { preprocessPDFJSCode } from "./external/builder/preprocessor2.mjs";
import rename from "gulp-rename";
@ -934,17 +935,26 @@ gulp.task("cmaps", async function () {
function preprocessCSS(source, defines) {
const outName = getTempFile("~preprocess", ".css");
builder.preprocess(source, outName, defines);
let out = fs.readFileSync(outName).toString();
const out = fs.readFileSync(outName).toString();
fs.unlinkSync(outName);
// Strip out all license headers in the middle.
const reg = /\n\/\* Copyright(.|\n)*?Mozilla Foundation(.|\n)*?\*\//g;
out = out.replaceAll(reg, "");
const i = source.lastIndexOf("/");
return createStringSource(source.substr(i + 1), out);
}
function discardCommentsCSS() {
let copyrightNum = 0;
function remove(comment) {
// Remove all comments, except the *first* license header.
if (comment.startsWith("Copyright") && copyrightNum++ === 0) {
return false;
}
return true;
}
return postcssDiscardComments({ remove });
}
function preprocessHTML(source, defines) {
const outName = getTempFile("~preprocess", ".html");
builder.preprocess(source, outName, defines);
@ -982,6 +992,7 @@ function buildGeneric(defines, dir) {
.pipe(
postcss([
postcssDirPseudoClass(),
discardCommentsCSS(),
postcssNesting(),
autoprefixer(AUTOPREFIXER_CONFIG),
])
@ -1069,6 +1080,7 @@ function buildComponents(defines, dir) {
.pipe(
postcss([
postcssDirPseudoClass(),
discardCommentsCSS(),
postcssNesting(),
autoprefixer(AUTOPREFIXER_CONFIG),
])
@ -1165,6 +1177,7 @@ function buildMinified(defines, dir) {
.pipe(
postcss([
postcssDirPseudoClass(),
discardCommentsCSS(),
postcssNesting(),
autoprefixer(AUTOPREFIXER_CONFIG),
])
@ -1411,12 +1424,22 @@ gulp.task(
),
preprocessCSS("web/viewer.css", defines)
.pipe(postcss([autoprefixer(MOZCENTRAL_AUTOPREFIXER_CONFIG)]))
.pipe(
postcss([
discardCommentsCSS(),
autoprefixer(MOZCENTRAL_AUTOPREFIXER_CONFIG),
])
)
.pipe(replaceMozcentralCSS())
.pipe(gulp.dest(MOZCENTRAL_CONTENT_DIR + "web")),
preprocessCSS("web/viewer-geckoview.css", gvDefines)
.pipe(postcss([autoprefixer(MOZCENTRAL_AUTOPREFIXER_CONFIG)]))
.pipe(
postcss([
discardCommentsCSS(),
autoprefixer(MOZCENTRAL_AUTOPREFIXER_CONFIG),
])
)
.pipe(replaceMozcentralCSS())
.pipe(gulp.dest(MOZCENTRAL_CONTENT_DIR + "web")),
@ -1509,6 +1532,7 @@ gulp.task(
.pipe(
postcss([
postcssDirPseudoClass(),
discardCommentsCSS(),
postcssNesting(),
autoprefixer(AUTOPREFIXER_CONFIG),
])

13
package-lock.json generated
View File

@ -48,6 +48,7 @@
"pngjs": "^7.0.0",
"postcss": "^8.4.30",
"postcss-dir-pseudo-class": "^8.0.0",
"postcss-discard-comments": "^6.0.0",
"postcss-nesting": "^12.0.1",
"prettier": "^3.0.3",
"puppeteer": "^21.2.1",
@ -16768,6 +16769,18 @@
"postcss": "^8.4"
}
},
"node_modules/postcss-discard-comments": {
"version": "6.0.0",
"resolved": "https://registry.npmjs.org/postcss-discard-comments/-/postcss-discard-comments-6.0.0.tgz",
"integrity": "sha512-p2skSGqzPMZkEQvJsgnkBhCn8gI7NzRH2683EEjrIkoMiwRELx68yoUJ3q3DGSGuQ8Ug9Gsn+OuDr46yfO+eFw==",
"dev": true,
"engines": {
"node": "^14 || ^16 || >=18.0"
},
"peerDependencies": {
"postcss": "^8.2.15"
}
},
"node_modules/postcss-load-config": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/postcss-load-config/-/postcss-load-config-3.1.0.tgz",

View File

@ -41,6 +41,7 @@
"pngjs": "^7.0.0",
"postcss": "^8.4.30",
"postcss-dir-pseudo-class": "^8.0.0",
"postcss-discard-comments": "^6.0.0",
"postcss-nesting": "^12.0.1",
"prettier": "^3.0.3",
"puppeteer": "^21.2.1",