From 32958e2d34de2ced66d120be3f3cc2bdf6c173fc Mon Sep 17 00:00:00 2001 From: zodern Date: Fri, 14 May 2021 16:36:18 -0500 Subject: [PATCH] Track which files build plugins use the content of --- tools/isobuild/bundler.js | 9 +++++- tools/isobuild/compiler-plugin.js | 5 ++- tools/isobuild/compiler.js | 52 ++++++++++++++++++++++--------- 3 files changed, 50 insertions(+), 16 deletions(-) diff --git a/tools/isobuild/bundler.js b/tools/isobuild/bundler.js index 88ae89475d..dd5b2d84b8 100644 --- a/tools/isobuild/bundler.js +++ b/tools/isobuild/bundler.js @@ -1161,7 +1161,7 @@ class Target { .computeJsOutputFilesMap(sourceBatches); sourceBatches.forEach(batch => { - const { unibuild } = batch; + const { unibuild, sourceRoot } = batch; // Depend on the source files that produced these resources. this.watchSet.merge(unibuild.watchSet); @@ -1171,6 +1171,13 @@ class Target { // XXX assumes that this merges cleanly this.watchSet.merge(unibuild.pkg.pluginWatchSet); + unibuild.resources.forEach(resource => { + let absPath = files.pathJoin(sourceRoot, resource.path); + if (resource._dataUsed !== false) { + this.watchSet.addFile(absPath, resource.hash); + } + }); + const entry = jsOutputFilesMap.get(unibuild.pkg.name || null); if (entry && entry.importScannerWatchSet) { // Populated in PackageSourceBatch._watchOutputFiles, based on the diff --git a/tools/isobuild/compiler-plugin.js b/tools/isobuild/compiler-plugin.js index 9e75edaab6..494919dffb 100644 --- a/tools/isobuild/compiler-plugin.js +++ b/tools/isobuild/compiler-plugin.js @@ -899,7 +899,9 @@ class OutputResource { servePath, // Remember the source hash so that changes to the source that // disappear after compilation can still contribute to the hash. - _inputHash: resourceSlot.inputResource.hash, + // Bypassing SourceResource.hash getter so if the compiler plugin doesn't + // use the resource's content we don't unnecessarily mark it as used. + _inputHash: resourceSlot.inputResource._hash, }); } @@ -1518,6 +1520,7 @@ export class PackageSourceBatch { } static _watchOutputFiles(jsOutputFilesMap) { + return jsOutputFilesMap; // Watch all output files produced by computeJsOutputFilesMap. jsOutputFilesMap.forEach(entry => { entry.files.forEach(file => { diff --git a/tools/isobuild/compiler.js b/tools/isobuild/compiler.js index 3ecdb82d61..030b65b02b 100644 --- a/tools/isobuild/compiler.js +++ b/tools/isobuild/compiler.js @@ -558,13 +558,12 @@ api.addAssets('${relPath}', 'client').`); const hash = optimisticHashOrNull(absPath); const file = { contents, hash }; - if (fileOptions && ! fileOptions.lazy) { - watchSet.addFile(absPath, hash); - } else { - // Lazy files might not ultimately be used by the current unibuild/bundle, - // so we add them to the watchSet using a provisional status that may be - // updated later, at the end of PackageSourceBatch.computeJsOutputFilesMap. + // When files are handled by a new-style compiler plugin, the SourceResource + // class tracks if each file is actually used. + if (classification.isNonLegacySource()) { watchSet.addPotentiallyUnusedFile(absPath, hash); + } else { + watchSet.addFile(absPath, hash); } Console.nudge(true); @@ -598,16 +597,13 @@ api.addAssets('${relPath}', 'client').`); if (classification.isNonLegacySource()) { // This is source used by a new-style compiler plugin; it will be fully // processed later in the bundler. - resources.push({ - type: "source", - extension: classification.extension || null, - usesDefaultSourceProcessor: - !! classification.usesDefaultSourceProcessor, + resources.push(new SourceResource({ + classification, data: contents, path: relPath, - hash: hash, - fileOptions: fileOptions - }); + hash, + fileOptions + })); return; } @@ -1060,3 +1056,31 @@ export const KNOWN_ISOBUILD_FEATURE_PACKAGES = { // compilation using fibers and/or futures. 'isobuild:async-plugins': ['1.6.1'], }; + +class SourceResource { + type = "source"; + + constructor({ classification, data, path, hash, fileOptions }) { + this.type = "source"; + this.extension = classification.extension || null; + this.usesDefaultSourceProcessor = !!classification.usesDefaultSourceProcessor; + this.path = path; + this.fileOptions = fileOptions; + + // Is set to true if the resource's hash or data is accessed, which can be + // used to track if the file's content was used during the build process + this._dataUsed = false; + this._hash = hash; + this._data = data; + } + + get hash () { + this._dataUsed = true; + return this._hash; + } + + get data () { + this._dataUsed = true; + return this._data; + } +}