webpack/test/Compiler-caching.test.js

314 lines
8.0 KiB
JavaScript
Raw Normal View History

2018-04-12 06:11:04 +08:00
/* globals describe, it */
"use strict";
2016-05-06 19:38:21 +08:00
const path = require("path");
const fs = require("fs");
const webpack = require("../");
const WebpackOptionsDefaulter = require("../lib/WebpackOptionsDefaulter");
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) {
options.mode = "none";
2017-09-14 15:22:29 +08:00
options = new WebpackOptionsDefaulter().process(options);
options.cache = true;
2016-05-06 19:38:21 +08:00
options.entry = entry;
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 = {
2016-05-06 19:38:21 +08:00
mkdirp: [],
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 = {
2018-01-24 23:00:43 +08:00
join() {
return [].join.call(arguments, "/").replace(/\/+/g, "/");
},
2018-01-24 23:00:43 +08:00
mkdirp(path, callback) {
2016-05-06 19:38:21 +08:00
logs.mkdirp.push(path);
callback();
},
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("utf-8");
callback();
}
};
2018-02-25 18:46:17 +08:00
c.hooks.compilation.tap(
"CompilerCachingTest",
compilation => (compilation.bail = true)
);
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);
} else {
2018-02-25 18:46:17 +08:00
if (stats.errors.length > 0) {
2018-01-24 23:00:43 +08:00
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"
);
const aFilepath = path.join(tempFixturePath, "a.js");
const cFilepath = path.join(tempFixturePath, "c.js");
2016-05-06 19:38:21 +08:00
2016-09-14 18:04:42 +08:00
function cleanup() {
function ignoreENOENT(fn) {
try {
return fn();
2018-02-25 18:46:17 +08:00
} catch (e) {
if (e.code !== "ENOENT") {
2016-09-14 18:04:42 +08:00
throw e;
}
2016-05-06 19:38:21 +08:00
}
}
ignoreENOENT(() => fs.unlinkSync(aFilepath));
ignoreENOENT(() => fs.unlinkSync(cFilepath));
ignoreENOENT(() => fs.rmdirSync(tempFixturePath));
2016-09-14 18:04:42 +08:00
}
2018-01-24 23:00:43 +08:00
beforeAll(cleanup);
afterAll(cleanup);
2016-05-06 19:38:21 +08:00
function createTempFixture() {
// Remove previous copy if present
try {
2018-02-25 18:46:17 +08:00
if (fs.statSync(tempFixturePath)) {
2016-05-06 19:38:21 +08:00
fs.unlinkSync(aFilepath);
fs.unlinkSync(cFilepath);
fs.rmdirSync(tempFixturePath);
}
2018-02-25 18:46:17 +08:00
} catch (e) {
if (e.code !== "ENOENT") {
2016-05-06 19:38:21 +08:00
throw e;
}
}
// Copy over file since we"ll be modifying some of them
2016-05-06 19:38:21 +08:00
fs.mkdirSync(tempFixturePath);
2018-02-25 18:46:17 +08:00
fs
.createReadStream(path.join(__dirname, "fixtures", "a.js"))
.pipe(fs.createWriteStream(aFilepath));
fs
.createReadStream(path.join(__dirname, "fixtures", "c.js"))
.pipe(fs.createWriteStream(cFilepath));
2016-05-06 19:38:21 +08:00
return {
rootPath: tempFixturePath,
aFilepath: aFilepath,
cFilepath: cFilepath
};
}
2018-02-25 18:46:17 +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
2018-02-25 18:46:17 +08:00
const helper = compile(
"./temp-cache-fixture/c",
options,
(stats, files) => {
// Not cached the first time
2018-01-24 23:00:43 +08:00
expect(stats.assets[0].name).toBe("bundle.js");
2018-02-25 18:46:17 +08:00
expect(stats.assets[0].emitted).toBe(true);
2016-05-06 19:38:21 +08:00
2018-02-25 18: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
2018-02-25 18:46:17 +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
2018-02-25 18:46:17 +08:00
setTimeout(() => {
fs.writeFileSync(tempFixture.aFilepath, aContent);
2016-05-06 19:38:21 +08:00
2018-02-25 18: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
2018-02-25 18:46:17 +08:00
done();
});
}, 1100);
});
}
);
2016-05-06 19:38:21 +08:00
});
2018-02-25 18:46:17 +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
2018-02-25 18:46:17 +08:00
const helper = compile(
"./temp-cache-fixture/c",
options,
(stats, files) => {
// Not cached the first time
2018-01-24 23:00:43 +08:00
expect(stats.assets[0].name).toBe("bundle.js");
2018-02-25 18:46:17 +08:00
expect(stats.assets[0].emitted).toBe(true);
2016-05-06 19:38:21 +08:00
2018-02-25 18: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
2018-02-25 18:46:17 +08:00
expect(files["/bundle.js"]).toMatch("This is a");
2016-05-06 19:38:21 +08:00
2018-02-25 18:46:17 +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
2018-02-25 18:46:17 +08:00
fs.writeFileSync(tempFixture.aFilepath, aContent);
2016-05-06 19:38:21 +08:00
2018-02-25 18: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
2018-02-25 18:46:17 +08:00
expect(files["/bundle.js"]).toMatch("This is a MODIFIED");
2016-05-06 19:38:21 +08:00
2018-02-25 18:46:17 +08:00
done();
});
2016-05-07 01:42:06 +08:00
});
2018-02-25 18:46:17 +08:00
}
);
2016-05-06 19:38:21 +08:00
});
2018-02-25 18:46:17 +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
2018-02-25 18:46:17 +08:00
const helper = compile(
"./temp-cache-fixture/c",
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
2018-02-25 18:46:17 +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
2018-02-25 18:46:17 +08:00
setTimeout(() => {
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
2018-02-25 18:46:17 +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
2018-02-25 18:46:17 +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
2018-02-25 18:46:17 +08:00
setTimeout(() => {
fs.writeFileSync(tempFixture.aFilepath, aContent);
2016-05-06 19:38:21 +08:00
2018-02-25 18: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
2018-02-25 18:46:17 +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
2018-02-25 18:46:17 +08:00
done();
});
}, 2100);
});
}, 4100);
}
);
2016-05-06 19:38:21 +08:00
});
2018-02-25 18:46:17 +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
2018-02-25 18:46:17 +08:00
const helper = compile(
"./temp-cache-fixture/c",
options,
(stats, files) => {
// Built the first time
2018-01-24 23:00:43 +08:00
expect(stats.modules[0].name).toMatch("c.js");
2018-02-25 18:46:17 +08:00
expect(stats.modules[0].built).toBe(true);
2016-05-06 19:38:21 +08:00
2018-01-24 23:00:43 +08:00
expect(stats.modules[1].name).toMatch("a.js");
2018-02-25 18:46:17 +08:00
expect(stats.modules[1].built).toBe(true);
2016-05-06 19:38:21 +08:00
helper.runAgain((stats, files, iteration) => {
2018-02-25 18:46:17 +08:00
// 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");
2018-02-25 18:46:17 +08:00
// expect(stats.modules[1].built).toBe(false);
2016-05-06 19:38:21 +08:00
2018-02-25 18:46:17 +08:00
const aContent = fs
.readFileSync(tempFixture.aFilepath)
.toString()
.replace("This is a", "This is a MODIFIED");
fs.writeFileSync(tempFixture.aFilepath, aContent);
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);
expect(stats.modules[1].name).toMatch("a.js");
expect(stats.modules[1].built).toBe(true);
done();
});
2016-05-06 19:38:21 +08:00
});
2018-02-25 18:46:17 +08:00
}
);
2016-05-06 19:38:21 +08:00
});
});