webpack/test/Compiler-caching.test.js

288 lines
7.6 KiB
JavaScript
Raw Normal View History

"use strict";
2016-05-06 19:38:21 +08:00
require("./helpers/warmup-webpack");
const path = require("path");
const fs = require("graceful-fs");
const rimraf = require("rimraf");
let fixtureCount = 0;
2016-05-06 19:38:21 +08:00
2018-01-24 23:00:43 +08:00
describe("Compiler (caching)", () => {
jest.setTimeout(15000);
2016-05-06 19:38:21 +08:00
function compile(entry, options, callback) {
const webpack = require("..");
options = webpack.config.getNormalizedWebpackOptions(options);
options.mode = "none";
options.cache = true;
2016-05-06 19:38:21 +08:00
options.entry = entry;
2018-07-15 04:17:55 +08:00
options.optimization.moduleIds = "natural";
2017-12-19 04:18:07 +08:00
options.optimization.minimize = false;
2016-05-06 19:38:21 +08:00
options.context = path.join(__dirname, "fixtures");
options.output.path = "/";
2016-05-06 19:38:21 +08:00
options.output.filename = "bundle.js";
options.output.pathinfo = true;
const logs = {
mkdir: [],
2018-02-25 18:46:17 +08:00
writeFile: []
2016-05-06 19:38:21 +08:00
};
const c = webpack(options);
const files = {};
2016-05-06 19:38:21 +08:00
c.outputFileSystem = {
mkdir(path, callback) {
logs.mkdir.push(path);
const err = new Error("error");
err.code = "EEXIST";
callback(err);
2016-05-06 19:38:21 +08:00
},
2018-01-24 23:00:43 +08:00
writeFile(name, content, callback) {
2016-05-06 19:38:21 +08:00
logs.writeFile.push(name, content);
files[name] = content.toString("utf8");
2016-05-06 19:38:21 +08:00
callback();
2019-11-04 18:40:36 +08:00
},
stat(path, callback) {
callback(new Error("ENOENT"));
2016-05-06 19:38:21 +08:00
}
};
2018-02-25 18:46:17 +08:00
c.hooks.compilation.tap(
"CompilerCachingTest",
(compilation) => (compilation.bail = true)
2018-02-25 18:46:17 +08:00
);
2016-05-06 19:38:21 +08:00
let compilerIteration = 1;
2016-05-06 19:38:21 +08:00
function runCompiler(options, callback) {
2018-02-25 18:46:17 +08:00
if (typeof options === "function") {
callback = options;
options = {};
}
c.run((err, stats) => {
2018-02-25 18:46:17 +08:00
if (err) throw err;
2018-01-24 23:00:43 +08:00
expect(typeof stats).toBe("object");
2016-05-06 19:38:21 +08:00
stats = stats.toJson({
modules: true,
reasons: true
});
2018-01-24 23:00:43 +08:00
expect(typeof stats).toBe("object");
expect(stats).toHaveProperty("errors");
expect(Array.isArray(stats.errors)).toBe(true);
2018-02-25 18:46:17 +08:00
if (options.expectErrors) {
2018-01-24 23:00:43 +08:00
expect(stats.errors).toHaveLength(options.expectErrors);
2024-07-31 04:54:55 +08:00
} else if (stats.errors.length > 0) {
expect(typeof stats.errors[0]).toBe("string");
throw new Error(stats.errors[0]);
2016-05-06 19:38:21 +08:00
}
stats.logs = logs;
callback(stats, files, compilerIteration++);
});
}
runCompiler(callback);
return {
compilerInstance: c,
2017-12-20 23:51:24 +08:00
runAgain: runCompiler
2016-05-06 19:38:21 +08:00
};
}
2018-02-25 18:46:17 +08:00
const tempFixturePath = path.join(
__dirname,
"fixtures",
"temp-cache-fixture"
);
2016-05-06 19:38:21 +08:00
2025-04-22 20:42:33 +08:00
/**
* @returns {void}
*/
2016-09-14 18:04:42 +08:00
function cleanup() {
rimraf.sync(`${tempFixturePath}-*`);
2016-09-14 18:04:42 +08:00
}
2018-01-24 23:00:43 +08:00
beforeAll(cleanup);
2018-01-24 23:00:43 +08:00
afterAll(cleanup);
2016-05-06 19:38:21 +08:00
2025-04-22 20:42:33 +08:00
/**
* @returns {{ rootPath: string, aFilepath: string, cFilepath: string }} temp fixture paths
*/
2016-05-06 19:38:21 +08:00
function createTempFixture() {
const fixturePath = `${tempFixturePath}-${fixtureCount}`;
const aFilepath = path.join(fixturePath, "a.js");
const cFilepath = path.join(fixturePath, "c.js");
2016-05-06 19:38:21 +08:00
// Remove previous copy if present
rimraf.sync(fixturePath);
2016-05-06 19:38:21 +08:00
// Copy over file since we"ll be modifying some of them
fs.mkdirSync(fixturePath);
2019-11-29 20:01:04 +08:00
fs.copyFileSync(path.join(__dirname, "fixtures", "a.js"), aFilepath);
fs.copyFileSync(path.join(__dirname, "fixtures", "c.js"), cFilepath);
2016-05-06 19:38:21 +08:00
fixtureCount++;
2016-05-06 19:38:21 +08:00
return {
rootPath: fixturePath,
2025-04-22 19:09:25 +08:00
aFilepath,
cFilepath
2016-05-06 19:38:21 +08:00
};
}
it("should cache single file (with manual 1s wait)", (done) => {
const options = {};
const tempFixture = createTempFixture();
2016-05-06 19:38:21 +08:00
2025-07-08 22:46:17 +08:00
const helper = compile(tempFixture.cFilepath, options, (stats, _files) => {
// Not cached the first time
expect(stats.assets[0].name).toBe("bundle.js");
expect(stats.assets[0].emitted).toBe(true);
2025-07-08 22:46:17 +08:00
helper.runAgain((stats, _files, _iteration) => {
// Cached the second run
2018-01-24 23:00:43 +08:00
expect(stats.assets[0].name).toBe("bundle.js");
expect(stats.assets[0].emitted).toBe(false);
2016-05-06 19:38:21 +08:00
const aContent = fs
.readFileSync(tempFixture.aFilepath)
.toString()
.replace("This is a", "This is a MODIFIED");
2016-05-06 19:38:21 +08:00
setTimeout(() => {
fs.writeFileSync(tempFixture.aFilepath, aContent);
2016-05-06 19:38:21 +08:00
2025-07-08 22:46:17 +08:00
helper.runAgain((stats, _files, _iteration) => {
// Cached the third run
expect(stats.assets[0].name).toBe("bundle.js");
expect(stats.assets[0].emitted).toBe(true);
2016-05-06 19:38:21 +08:00
done();
});
}, 1100);
});
});
2016-05-06 19:38:21 +08:00
});
it("should cache single file (even with no timeout)", (done) => {
const options = {};
const tempFixture = createTempFixture();
2016-05-06 19:38:21 +08:00
2025-07-08 22:46:17 +08:00
const helper = compile(tempFixture.cFilepath, options, (stats, _files) => {
// Not cached the first time
expect(stats.assets[0].name).toBe("bundle.js");
expect(stats.assets[0].emitted).toBe(true);
2016-05-06 19:38:21 +08:00
2025-07-08 22:46:17 +08:00
helper.runAgain((stats, files, _iteration) => {
// Cached the second run
expect(stats.assets[0].name).toBe("bundle.js");
expect(stats.assets[0].emitted).toBe(false);
2016-05-06 19:38:21 +08:00
expect(files["/bundle.js"]).toMatch("This is a");
2016-05-06 19:38:21 +08:00
const aContent = fs
.readFileSync(tempFixture.aFilepath)
.toString()
.replace("This is a", "This is a MODIFIED");
2016-05-06 19:38:21 +08:00
fs.writeFileSync(tempFixture.aFilepath, aContent);
2016-05-06 19:38:21 +08:00
2025-07-08 22:46:17 +08:00
helper.runAgain((stats, files, _iteration) => {
// Cached the third run
expect(stats.assets[0].name).toBe("bundle.js");
expect(stats.assets[0].emitted).toBe(true);
2016-05-06 19:38:21 +08:00
expect(files["/bundle.js"]).toMatch("This is a MODIFIED");
2016-05-06 19:38:21 +08:00
done();
2016-05-07 01:42:06 +08:00
});
});
});
2016-05-06 19:38:21 +08:00
});
it("should only build when modified (with manual 2s wait)", (done) => {
const options = {};
const tempFixture = createTempFixture();
2016-05-06 19:38:21 +08:00
2025-07-08 22:46:17 +08:00
const helper = compile(tempFixture.cFilepath, options, (stats, _files) => {
// Built the first time
expect(stats.modules[0].name).toMatch("c.js");
expect(stats.modules[0].built).toBe(true);
2016-05-06 19:38:21 +08:00
expect(stats.modules[1].name).toMatch("a.js");
expect(stats.modules[1].built).toBe(true);
2016-05-06 19:38:21 +08:00
setTimeout(() => {
2025-07-08 22:46:17 +08:00
helper.runAgain((stats, _files, _iteration) => {
// Not built when cached the second run
expect(stats.modules[0].name).toMatch("c.js");
// expect(stats.modules[0].built).toBe(false);
2016-05-06 19:38:21 +08:00
expect(stats.modules[1].name).toMatch("a.js");
// expect(stats.modules[1].built).toBe(false);
2016-05-06 19:38:21 +08:00
const aContent = fs
.readFileSync(tempFixture.aFilepath)
.toString()
.replace("This is a", "This is a MODIFIED");
2016-05-06 19:38:21 +08:00
setTimeout(() => {
fs.writeFileSync(tempFixture.aFilepath, aContent);
2016-05-06 19:38:21 +08:00
2025-07-08 22:46:17 +08:00
helper.runAgain((stats, _files, _iteration) => {
// And only a.js built after it was modified
expect(stats.modules[0].name).toMatch("c.js");
expect(stats.modules[0].built).toBe(false);
2016-05-06 19:38:21 +08:00
expect(stats.modules[1].name).toMatch("a.js");
expect(stats.modules[1].built).toBe(true);
2016-05-06 19:38:21 +08:00
done();
});
}, 2100);
});
}, 4100);
});
2016-05-06 19:38:21 +08:00
});
it("should build when modified (even with no timeout)", (done) => {
const options = {};
const tempFixture = createTempFixture();
2016-05-06 19:38:21 +08:00
2025-07-08 22:46:17 +08:00
const helper = compile(tempFixture.cFilepath, options, (stats, _files) => {
// Built the first time
expect(stats.modules[0].name).toMatch("c.js");
expect(stats.modules[0].built).toBe(true);
expect(stats.modules[1].name).toMatch("a.js");
expect(stats.modules[1].built).toBe(true);
2025-07-08 22:46:17 +08:00
helper.runAgain((stats, _files, _iteration) => {
// Not built when cached the second run
2018-01-24 23:00:43 +08:00
expect(stats.modules[0].name).toMatch("c.js");
// expect(stats.modules[0].built).toBe(false);
2016-05-06 19:38:21 +08:00
2018-01-24 23:00:43 +08:00
expect(stats.modules[1].name).toMatch("a.js");
// expect(stats.modules[1].built).toBe(false);
const aContent = fs
.readFileSync(tempFixture.aFilepath)
.toString()
.replace("This is a", "This is a MODIFIED");
fs.writeFileSync(tempFixture.aFilepath, aContent);
2016-05-06 19:38:21 +08:00
2025-07-08 22:46:17 +08:00
helper.runAgain((stats, _files, _iteration) => {
// And only a.js built after it was modified
2018-01-24 23:00:43 +08:00
expect(stats.modules[0].name).toMatch("c.js");
// expect(stats.modules[0].built).toBe(false);
2016-05-06 19:38:21 +08:00
2018-01-24 23:00:43 +08:00
expect(stats.modules[1].name).toMatch("a.js");
expect(stats.modules[1].built).toBe(true);
2018-02-25 18:46:17 +08:00
done();
2016-05-06 19:38:21 +08:00
});
});
});
2016-05-06 19:38:21 +08:00
});
});