mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-19 14:48:08 +02:00
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:
parent
3f7060e777
commit
7413546e16
3 changed files with 45 additions and 7 deletions
38
gulpfile.mjs
38
gulpfile.mjs
|
@ -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),
|
||||
])
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue