diff --git a/tools/isobuild/bundler.js b/tools/isobuild/bundler.js index 2c3bef67ba..74c66c665d 100644 --- a/tools/isobuild/bundler.js +++ b/tools/isobuild/bundler.js @@ -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; }); diff --git a/tools/isobuild/minifier-plugin.js b/tools/isobuild/minifier-plugin.js index 7a34efbf37..e7ffe294a9 100644 --- a/tools/isobuild/minifier-plugin.js +++ b/tools/isobuild/minifier-plugin.js @@ -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, + ); + } +}