Skip to content

Commit

Permalink
Integrate Webpack 5 watch mode from upstream
Browse files Browse the repository at this point in the history
Integrates the changes from:
sysgears/webpack-virtual-modules#69
  • Loading branch information
syvb committed Oct 21, 2020
1 parent 6c50e32 commit d1dc851
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions lib/virtual.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ function createWebpackData(result) {
}).bind({ result: result });
}

function getModulePath(filePath, compiler) {
return path.isAbsolute(filePath) ? filePath : path.join(compiler.context, filePath);
}

/**
* @param {Compiler} compiler - the webpack compiler
*/
Expand Down Expand Up @@ -51,8 +55,6 @@ function VirtualModulesPlugin(compiler) {
this._virtualFiles[file] = { stats: stats, contents: contents };
setData(statStorage, file, createWebpackData(stats));
setData(fileStorage, file, createWebpackData(contents));
compiler.fileTimestamps instanceof Map &&
compiler.fileTimestamps.set(file, +stats.mtime);
var segments = file.split(/[\\/]/);
var count = segments.length - 1;
var minCount = segments[0] ? 1 : 0;
Expand Down Expand Up @@ -103,13 +105,6 @@ function VirtualModulesPlugin(compiler) {

const watchRunHook = (watcher, callback) => {
this._watcher = watcher.compiler || watcher;
const virtualFiles = compiler.inputFileSystem._virtualFiles;
if (virtualFiles) {
Object.keys(virtualFiles).forEach(function(file) {
compiler.fileTimestamps instanceof Map &&
compiler.fileTimestamps.set(file, +virtualFiles[file].stats.mtime);
});
}
callback();
};

Expand Down Expand Up @@ -145,6 +140,7 @@ VirtualModulesPlugin.prototype.writeModule = function(filePath, contents) {
ctime: time,
birthtime: time
});
var modulePath = getModulePath(filePath, this.compiler);

// When using the WatchIgnorePlugin (https://github.com/webpack/webpack/blob/52184b897f40c75560b3630e43ca642fcac7e2cf/lib/WatchIgnorePlugin.js),
// the original watchFileSystem is stored in `wfs`. The following "unwraps" the ignoring
Expand All @@ -155,6 +151,27 @@ VirtualModulesPlugin.prototype.writeModule = function(filePath, contents) {
finalWatchFileSystem = finalWatchFileSystem.wfs;
}
this.compiler.inputFileSystem._writeVirtualFile(filePath, stats, contents);
if (finalWatchFileSystem &&
(finalWatchFileSystem.watcher.fileWatchers.size ||
finalWatchFileSystem.watcher.fileWatchers.length)
) {
var fileWatchers = finalWatchFileSystem.watcher.fileWatchers instanceof Map ?
Array.from(finalWatchFileSystem.watcher.fileWatchers.values()) :
finalWatchFileSystem.watcher.fileWatchers;
fileWatchers.forEach(function(fileWatcher) {
if (fileWatcher.path === modulePath) {
delete fileWatcher.directoryWatcher._cachedTimeInfoEntries;
fileWatcher.directoryWatcher.setFileTime(
filePath,
time,
false,
false,
null
);
fileWatcher.emit("change", time, null);
}
});
}
};

function getData(storage, key) {
Expand Down

0 comments on commit d1dc851

Please sign in to comment.