Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds Webpack 5 watch mode support #69

Merged
merged 1 commit into from
Oct 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,24 @@ VirtualModulesPlugin.prototype.writeModule = function(filePath, contents) {
}

finalInputFileSystem._writeVirtualFile(modulePath, stats, contents);
if (finalWatchFileSystem && finalWatchFileSystem.watcher.fileWatchers.length) {
finalWatchFileSystem.watcher.fileWatchers.forEach(function(fileWatcher) {
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) {
debug(self._compiler.name, "Emit file change:", modulePath, time);
delete fileWatcher.directoryWatcher._cachedTimeInfoEntries;
fileWatcher.directoryWatcher.setFileTime(
filePath,
time,
false,
false,
null
);
fileWatcher.emit("change", time, null);
}
});
Expand Down Expand Up @@ -188,8 +202,6 @@ VirtualModulesPlugin.prototype.apply = function(compiler) {
this._virtualFiles[file] = {stats: stats, contents: contents};
setData(statStorage, file, createWebpackData(stats));
setData(fileStorage, file, createWebpackData(contents));
self._compiler.fileTimestamps instanceof Map &&
self._compiler.fileTimestamps.set(file, +stats.mtime);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't removing this cause issues for webpack 4?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fileWatcher.directoryWatcher.setFileTime call added in this PR should work both for Webpack 4 and Webpack 5, at least, when I checked it worked for me, we lack proper automated tests now and they are hard to write, so yeah, if you noticed that this change breaks something, please open an issue.

var segments = file.split(/[\\/]/);
var count = segments.length - 1;
var minCount = segments[0] ? 1 : 0;
Expand Down Expand Up @@ -245,13 +257,6 @@ VirtualModulesPlugin.prototype.apply = function(compiler) {

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

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "webpack-virtual-modules",
"version": "0.3.1",
"version": "0.3.2",
"description": "Webpack Virtual Modules",
"main": "index.js",
"scripts": {
Expand Down
55 changes: 0 additions & 55 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,59 +184,4 @@ describe("webpack-virtual-modules", function() {
done();
});
});

it("shouldn't rebuild all virtual modules on any change", function(done) {
var plugin = new Plugin({
'entry.js': 'require("./dep_one.js"); require("./dep_two.js");',
'dep_one.js': '',
'dep_two.js': ''
});
var compiler = webpack({
plugins: [plugin],
entry: { bundle: './entry.js' },
mode: 'development'
});
compiler.outputFileSystem = new MemoryFileSystem();
var count = 0;
var modulesBuilt;

var waiter = function(callback) {
const fileWatchers = compiler.watchFileSystem.watcher.fileWatchers;
if (fileWatchers instanceof Map) {
// Webpack v5 is a map
if (fileWatchers.keys === 0) {
setTimeout(function() { waiter(callback); }, 50);
} else {
callback();
}
} else if (!Object.keys(fileWatchers).length) {
setTimeout(function() { waiter(callback); }, 50);
} else {
callback();
}
};

compiler.hooks.done.tap('Plugin', function(stats) {
if(count !== 1) return;
// assert doesn't throw here for some reason, writing to modulesBuilt
modulesBuilt = stats.toJson().modules.filter(function(m) {
return m.codeGenerated != null ? m.codeGenerated : m.built;
}).length;
});

var watcher = compiler.watch(null, function(err, stats) {
if (count === 0) {
waiter(function() {
plugin.writeModule('dep_one.js', 'var baz;');
var fs = stats.compilation.inputFileSystem;
fs.purge();
assert.equal(fs.readFileSync(path.resolve('dep_one.js')).toString(), 'var baz;');
count++;
});
} else {
watcher.close(done);
assert.equal(modulesBuilt, 1, 'only one module should be built');
}
});
});
});