Skip to content

Commit

Permalink
Added tracking of file removal in watch-run -Closes #5072.
Browse files Browse the repository at this point in the history
  • Loading branch information
cacheflow committed Oct 12, 2018
1 parent a868789 commit 096ef0c
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 8 deletions.
3 changes: 2 additions & 1 deletion lib/Compiler.js
Expand Up @@ -128,6 +128,7 @@ class Compiler extends Tapable {
/** @type {string|null} */
this.recordsOutputPath = null;
this.records = {};
this.removedFiles = new Set();
/** @type {Map<string, number>} */
this.fileTimestamps = new Map();
/** @type {Map<string, number>} */
Expand Down Expand Up @@ -192,6 +193,7 @@ class Compiler extends Tapable {
this.running = true;
this.fileTimestamps = new Map();
this.contextTimestamps = new Map();
this.removedFiles = new Set();
return new Watching(this, watchOptions, handler);
}

Expand Down Expand Up @@ -299,7 +301,6 @@ class Compiler extends Tapable {

emitAssets(compilation, callback) {
let outputPath;

const emitFiles = err => {
if (err) return callback(err);

Expand Down
7 changes: 4 additions & 3 deletions lib/WatchIgnorePlugin.js
Expand Up @@ -38,10 +38,10 @@ class IgnoringWatchFileSystem {
dirsModified,
missingModified,
fileTimestamps,
dirTimestamps
dirTimestamps,
removedFiles
) => {
if (err) return callback(err);

for (const path of ignoredFiles) {
fileTimestamps.set(path, 1);
}
Expand All @@ -56,7 +56,8 @@ class IgnoringWatchFileSystem {
dirsModified,
missingModified,
fileTimestamps,
dirTimestamps
dirTimestamps,
removedFiles
);
},
callbackUndelayed
Expand Down
6 changes: 3 additions & 3 deletions lib/Watching.js
Expand Up @@ -50,7 +50,6 @@ class Watching {
this.compiler.emitAssets(compilation, err => {
if (err) return this._done(err);
if (this.invalid) return this._done();

this.compiler.emitRecords(err => {
if (err) return this._done(err);

Expand Down Expand Up @@ -95,7 +94,6 @@ class Watching {
this.handler(err, stats);
return;
}

this.compiler.hooks.done.callAsync(stats, () => {
this.handler(null, stats);
if (!this.closed) {
Expand Down Expand Up @@ -124,7 +122,8 @@ class Watching {
contextModified,
missingModified,
fileTimestamps,
contextTimestamps
contextTimestamps,
removedFiles
) => {
this.pausedWatcher = this.watcher;
this.watcher = null;
Expand All @@ -133,6 +132,7 @@ class Watching {
}
this.compiler.fileTimestamps = fileTimestamps;
this.compiler.contextTimestamps = contextTimestamps;
this.compiler.removedFiles = removedFiles;
this._invalidate();
},
(fileName, changeTime) => {
Expand Down
3 changes: 2 additions & 1 deletion lib/node/NodeWatchFileSystem.js
Expand Up @@ -57,7 +57,8 @@ class NodeWatchFileSystem {
changes.filter(file => dirs.includes(file)).sort(),
changes.filter(file => missing.includes(file)).sort(),
times,
times
times,
removals
);
});

Expand Down
134 changes: 134 additions & 0 deletions test/RemoveFiles.test.js
@@ -0,0 +1,134 @@
"use strict";

/* globals describe it */
const path = require("path");
const MemoryFs = require("memory-fs");
const webpack = require("../");
const fs = require("fs");
const rimraf = require("rimraf");

const createCompiler = config => {
const compiler = webpack(config);
compiler.outputFileSystem = new MemoryFs();
return compiler;
};

const tempFolderPath = path.join(__dirname, "temp");
const tempFilePath = path.join(tempFolderPath, "temp-file.js");
const tempFile2Path = path.join(tempFolderPath, "temp-file2.js");

const createSingleCompiler = () => {
return createCompiler({
entry: tempFilePath,
watch: true,
output: {
path: tempFolderPath,
filename: "bundle.js"
}
});
};

describe("RemovedFiles", () => {
jest.setTimeout(20000);

function cleanup() {
rimraf.sync(tempFolderPath);
}

beforeAll(() => {
cleanup();
fs.mkdirSync(tempFolderPath);
fs.writeFileSync(
tempFilePath,
"module.exports = function temp() {return 'temp file';};\n require('./temp-file2')",
"utf-8"
);
fs.writeFileSync(
tempFile2Path,
"module.exports = function temp2() {return 'temp file 2';};",
"utf-8"
);
});
afterAll(done => {
cleanup();
done();
});

it("should track removed files when they've been deleted in watchRun", done => {
const compiler = createSingleCompiler();
let watcher;
function handleError(err) {
if (err) done(err);
}
setTimeout(() => {
fs.unlinkSync(tempFilePath, handleError);
}, 2000);
compiler.hooks.watchRun.tap("RemovedFilesTest", (compiler, err) => {
if (err) {
done(err);
}
if (compiler.removedFiles.length > 0) {
setTimeout(() => {
expect(Array.from(compiler.removedFiles)).toContain(tempFilePath);
watcher.close();
done();
}, 500);
}
});

watcher = compiler.watch(
{
aggregateTimeout: 50
},
(err, stats) => {}
);
});

it("should not track removed files when they have not been deleted in watchRun", done => {
const compiler = createSingleCompiler();
let watcher;
compiler.hooks.watchRun.tap("RemovedFilesTest", (compiler, err) => {
if (err) {
done(err);
}
expect(Array.from(compiler.removedFiles)).toHaveLength(0);
done();
watcher.close();
});

watcher = compiler.watch(
{
aggregateTimeout: 50
},
(err, stats) => {}
);
});

it("should not track removed files when files have been modified", done => {
const compiler = createSingleCompiler();
let watcher;
function handleError(err) {
if (err) done(err);
}
let updateFile = () => {
fs.writeFile(tempFile2Path, "hello world", "utf-8", handleError);
};
updateFile();
compiler.hooks.watchRun.tap("RemovedFilesTest", (compiler, err) => {
handleError(err);
setTimeout(() => {
expect(Array.from(compiler.removedFiles)).toHaveLength(0);
watcher.close();
done();
}, 500);
watcher.close();
});

watcher = compiler.watch(
{
aggregateTimeout: 50
},
(err, stats) => {}
);
});
});

0 comments on commit 096ef0c

Please sign in to comment.