webpack.js.org icon indicating copy to clipboard operation
webpack.js.org copied to clipboard

now that `mtimes` has been removed from watchpack, how do we get all changed/removed files?

Open maxwoo-houzz opened this issue 5 years ago • 7 comments

Feature request

What is the expected behavior?

It seems like the new version of WatchPack (v2.0.0) are getting rid of the mtimes variable. However, from my research it seems like that variable was the only way to get the list of changed files that triggered a re-compilation (source). As such, there should be an alternative way to get this list of changed files.

What is motivation or use case for adding/changing the behavior?

Seems like a pretty useful feature for the watchRun hook, as there are already a couple people (including me) that use it.

How should this be implemented in your opinion?

Watchpack actually provides the info we need, in the variable filesModified that it passes to Watching.js. However, Watching.js doesn't use it at all. Ideally I think this variable should be provided in the watchRun hook, but it should also be fine to just store it as a property just like how it's done with removedFiles, eg in Watching.js:

this.compiler.filesModified = filesModified;

Are you willing to work on this yourself? yes

maxwoo-houzz avatar Aug 27 '19 01:08 maxwoo-houzz

Sounds good, send a PR

sokra avatar Aug 27 '19 07:08 sokra

@sokra i think we should implement this in webpack@5 before stable release, can we move this in yes?

alexander-akait avatar Aug 27 '19 10:08 alexander-akait

The PR was merged last year, but I think more docs would be great, as it seems like the change is only mentioned vaguely once in https://webpack.js.org/blog/2020-10-10-webpack-5-release/#new-watching, no other documentation.

It also captures more information about filesystem while watching. It now captures mtimes and watches event times, as well as information about missing files. For this, the WatchFileSystem API changed a little bit. While on it we also converted Arrays to Sets and Objects to Maps.

Luckily I was able to figure it out - the tests were esp. helpful - and changed our usage of the plugin mentioned by @maxwoo-houzz

as there are already a couple people (including me) that use it.

like so:

class WatchRunPlugin {
    apply(compiler) {
        compiler.hooks.watchRun.tap('WatchRun', (comp) => {
            if (comp.modifiedFiles) {
                const changedFiles = Array.from(comp.modifiedFiles, (file) => `\n  ${file}`).join('');
                console.log('===============================');
                console.log('FILES CHANGED:', changedFiles);
                console.log('===============================');
            }
        });
    }
}

Schweinepriester avatar Oct 27 '20 23:10 Schweinepriester

Yes, let's documented this

alexander-akait avatar Oct 28 '20 11:10 alexander-akait

We have 4 variables here:

// Timestamps for files
this.compiler.fileTimestamps;
// Timestamps for directories
this.compiler.contextTimestamps;
// Removed files
this.compiler.removedFiles;
// Modified files
this.compiler.modifiedFiles;

alexander-akait avatar Oct 28 '20 11:10 alexander-akait

// Timestamps for directories
this.compiler.contextTimestamps;

for directories, I am seeing empty objects in compiler.contextTimestamps - is that expected?

// Removed files
this.compiler.removedFiles;
// Modified files
this.compiler.modifiedFiles;

is there also anything like addedFiles?

stefanprobst avatar Nov 21 '21 21:11 stefanprobst

is there also anything like addedFiles?

I done a console.log of each keys in compiler that contains the string file or File, and it output this :

compiler.outputFileSystem
compiler.intermediateFileSystem
compiler.inputFileSystem
compiler.watchFileSystem
compiler.modifiedFiles
compiler.removedFiles
compiler._assetEmittingWrittenFiles
compiler._assetEmittingPreviousFiles

malopgrics avatar Nov 25 '21 13:11 malopgrics