2019-09-13 09:27:16 +08:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const path = require("path");
|
|
|
|
const MemoryFs = require("memory-fs");
|
|
|
|
const webpack = require("..");
|
|
|
|
const fs = require("graceful-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,
|
|
|
|
output: {
|
|
|
|
path: tempFolderPath,
|
|
|
|
filename: "bundle.js"
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
function cleanup() {
|
|
|
|
rimraf.sync(tempFolderPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
function createFiles() {
|
|
|
|
fs.mkdirSync(tempFolderPath);
|
|
|
|
|
|
|
|
// Set file timestamps to 5 seconds earlier,
|
|
|
|
// otherwise the newly-created files will trigger the webpack watch mode to re-compile.
|
|
|
|
let fakeTime = new Date(Date.now() - 5000);
|
|
|
|
fs.writeFileSync(
|
|
|
|
tempFilePath,
|
|
|
|
"module.exports = function temp() {return 'temp file';};\n require('./temp-file2')",
|
|
|
|
"utf-8"
|
|
|
|
);
|
|
|
|
fs.utimesSync(tempFilePath, fakeTime, fakeTime);
|
|
|
|
fs.writeFileSync(
|
|
|
|
tempFile2Path,
|
|
|
|
"module.exports = function temp2() {return 'temp file 2';};",
|
|
|
|
"utf-8"
|
|
|
|
);
|
|
|
|
fs.utimesSync(tempFile2Path, fakeTime, fakeTime);
|
|
|
|
}
|
|
|
|
|
2019-09-23 15:24:15 +08:00
|
|
|
describe("ChangesAndRemovals", () => {
|
2019-09-13 09:27:16 +08:00
|
|
|
if (process.env.NO_WATCH_TESTS) {
|
|
|
|
it.skip("watch tests excluded", () => {});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
jest.setTimeout(10000);
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
cleanup();
|
|
|
|
createFiles();
|
|
|
|
});
|
|
|
|
afterEach(() => {
|
|
|
|
cleanup();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should track modified files when they've been modified in watchRun", done => {
|
|
|
|
const compiler = createSingleCompiler();
|
|
|
|
let watcher;
|
|
|
|
|
|
|
|
compiler.hooks.watchRun.tap("ModifiedFilesTest", compiler => {
|
2019-10-22 05:59:50 +08:00
|
|
|
if (!compiler.modifiedFiles) return;
|
2019-09-13 09:27:16 +08:00
|
|
|
const modifications = Array.from(compiler.modifiedFiles);
|
2019-10-22 05:59:50 +08:00
|
|
|
expect(modifications).toContain(tempFilePath);
|
|
|
|
watcher.close(done);
|
2019-09-13 09:27:16 +08:00
|
|
|
});
|
|
|
|
|
2019-10-22 05:59:50 +08:00
|
|
|
watcher = compiler.watch({ aggregateTimeout: 50 }, err => {
|
|
|
|
if (err) done(err);
|
|
|
|
});
|
2019-09-13 09:27:16 +08:00
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
fs.appendFileSync(tempFilePath, "\nlet x = 'file modified';");
|
|
|
|
}, 1000);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should not track modified files during initial watchRun", done => {
|
|
|
|
const compiler = createSingleCompiler();
|
|
|
|
let watcher;
|
2019-10-22 05:59:50 +08:00
|
|
|
const watchRunFinished = new Promise(resolve => {
|
|
|
|
compiler.hooks.watchRun.tap("ModifiedFilesTest", compiler => {
|
|
|
|
expect(compiler.modifiedFiles).toBe(undefined);
|
2019-09-13 09:27:16 +08:00
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
watcher = compiler.watch({ aggregateTimeout: 50 }, () => {});
|
|
|
|
|
|
|
|
watchRunFinished.then(() => {
|
2019-10-22 05:59:50 +08:00
|
|
|
watcher.close(done);
|
2019-09-13 09:27:16 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-10-22 05:59:50 +08:00
|
|
|
it("should not track removed files when tracking modified files", done => {
|
2019-09-13 09:27:16 +08:00
|
|
|
const compiler = createSingleCompiler();
|
|
|
|
let watcher;
|
|
|
|
|
|
|
|
compiler.hooks.watchRun.tap("ModifiedFilesTest", compiler => {
|
2019-10-22 05:59:50 +08:00
|
|
|
if (!compiler.modifiedFiles) return;
|
2019-09-13 09:27:16 +08:00
|
|
|
const modifications = Array.from(compiler.modifiedFiles);
|
2019-10-22 05:59:50 +08:00
|
|
|
expect(modifications).toHaveLength(0);
|
|
|
|
watcher.close(done);
|
2019-09-13 09:27:16 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
watcher = compiler.watch({ aggregateTimeout: 50 }, () => {});
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
fs.unlinkSync(tempFilePath);
|
|
|
|
}, 1000);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should track removed files when they've been deleted in watchRun", done => {
|
|
|
|
const compiler = createSingleCompiler();
|
|
|
|
let watcher;
|
|
|
|
|
|
|
|
compiler.hooks.watchRun.tap("RemovedFilesTest", compiler => {
|
2019-10-22 05:59:50 +08:00
|
|
|
if (!compiler.removedFiles) return;
|
2019-09-13 09:27:16 +08:00
|
|
|
const removals = Array.from(compiler.removedFiles);
|
2019-10-22 05:59:50 +08:00
|
|
|
expect(removals).toContain(tempFilePath);
|
|
|
|
watcher.close(done);
|
2019-09-13 09:27:16 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
watcher = compiler.watch({ aggregateTimeout: 50 }, () => {});
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
fs.unlinkSync(tempFilePath);
|
|
|
|
}, 1000);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should not track removed files during initial watchRun", done => {
|
|
|
|
const compiler = createSingleCompiler();
|
|
|
|
let watcher;
|
|
|
|
|
2019-10-22 05:59:50 +08:00
|
|
|
const watchRunFinished = new Promise(resolve => {
|
2019-09-13 09:27:16 +08:00
|
|
|
compiler.hooks.watchRun.tap("RemovedFilesTest", compiler => {
|
2019-10-22 05:59:50 +08:00
|
|
|
expect(compiler.removedFiles).toBe(undefined);
|
2019-09-13 09:27:16 +08:00
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
watcher = compiler.watch({ aggregateTimeout: 50 }, () => {});
|
|
|
|
|
|
|
|
watchRunFinished.then(() => {
|
|
|
|
watcher.close(done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should not track removed files when files have been modified", done => {
|
|
|
|
const compiler = createSingleCompiler();
|
|
|
|
let watcher;
|
|
|
|
|
|
|
|
compiler.hooks.watchRun.tap("RemovedFilesTest", compiler => {
|
2019-10-22 05:59:50 +08:00
|
|
|
if (!compiler.removedFiles) return;
|
2019-09-13 09:27:16 +08:00
|
|
|
const removals = Array.from(compiler.removedFiles);
|
2019-10-22 05:59:50 +08:00
|
|
|
expect(removals).toHaveLength(0);
|
|
|
|
watcher.close(done);
|
2019-09-13 09:27:16 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
watcher = compiler.watch({ aggregateTimeout: 50 }, () => {});
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
fs.appendFileSync(tempFilePath, "\nlet x = 'file modified';");
|
|
|
|
}, 1000);
|
|
|
|
});
|
|
|
|
});
|