Add file.readAndWatchFileWithHash for css minifiers

This commit is contained in:
zodern
2022-02-01 18:58:29 -06:00
parent b6a7f7c33d
commit 64bbff2a18
2 changed files with 36 additions and 4 deletions

View File

@@ -1119,8 +1119,7 @@ class Target {
// Takes a CssOutputResource and returns a string of minified CSS,
// or null to indicate no minification occurred.
// TODO Cache result by resource hash?
minifyCssResource(resource) {
minifyCssResource: (resource) => {
if (! minifiersByExt.css ||
minifyMode === "development") {
// Indicates the caller should use the original resource.data
@@ -1142,6 +1141,7 @@ class Target {
arch: target.arch,
minifier: minifiersByExt.css,
minifyMode,
watchSet: this.watchSet
}).map(file => file.contents("utf8")).join("\n");
}
});
@@ -1691,6 +1691,7 @@ class ClientTarget extends Target {
arch: this.arch,
minifier: minifierDef,
minifyMode,
watchSet: this.watchSet
});
}
@@ -1900,10 +1901,11 @@ function minifyCssFiles (files, {
arch,
minifier,
minifyMode,
watchSet
}) {
const inputHashesByCssFile = new Map;
const sources = files.map(file => {
const cssFile = new CssFile(file, { arch });
const cssFile = new CssFile(file, { arch, watchSet });
inputHashesByCssFile.set(cssFile, file.hash());
return cssFile;
});

View File

@@ -1,4 +1,14 @@
import buildmessage from '../utils/buildmessage.js';
import {
readAndWatchFileWithHash,
} from '../fs/watch';
import {
optimisticReadFile,
optimisticHashOrNull,
} from "../fs/optimistic";
import {
convertToPosixPath
} from '../fs/files';
const buildPluginModule = require('./build-plugin.js');
class InputFile extends buildPluginModule.InputFile {
@@ -7,6 +17,7 @@ class InputFile extends buildPluginModule.InputFile {
this._source = source;
this._arch = options.arch;
this._watchSet = options.watchSet;
this._minifiedFiles = [];
}
@@ -75,5 +86,24 @@ export class CssFile extends InputFile {
addStylesheet(options) {
this._minifiedFiles.push({ ...options });
}
}
readAndWatchFileWithHash(path, { cache } = {}) {
const filePath = convertToPosixPath(path);
if (cache) {
const hash = optimisticHashOrNull(filePath);
const contents = optimisticReadFile(filePath);
this._watchSet.addFile(filePath, hash);
return {
hash,
contents
}
}
return readAndWatchFileWithHash(
this._watchSet,
filePath,
);
}
}