webpack/test/Compiler-caching.test.js

307 lines
8.6 KiB
JavaScript
Raw Normal View History

2017-02-02 19:56:41 +08:00
/* globals describe, it, before, after */
"use strict";
2016-05-06 19:38:21 +08:00
const should = require("should");
const path = require("path");
const fs = require("fs");
const webpack = require("../");
const WebpackOptionsDefaulter = require("../lib/WebpackOptionsDefaulter");
2016-05-06 19:38:21 +08:00
describe("Compiler (caching)", function() {
2016-05-07 01:42:06 +08:00
this.timeout(15000);
2016-05-06 19:38:21 +08:00
function compile(entry, options, callback) {
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;
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: [],
writeFile: [],
};
const c = webpack(options);
const files = {};
2016-05-06 19:38:21 +08:00
c.outputFileSystem = {
join: function() {
return [].join.call(arguments, "/").replace(/\/+/g, "/");
},
2016-05-06 19:38:21 +08:00
mkdirp: function(path, callback) {
logs.mkdirp.push(path);
callback();
},
writeFile: function(name, content, callback) {
logs.writeFile.push(name, content);
files[name] = content.toString("utf-8");
callback();
}
};
c.plugin("compilation", (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) {
if(typeof options === "function") {
callback = options;
options = {};
}
c.run((err, stats) => {
2016-05-06 19:38:21 +08:00
if(err) throw err;
should.strictEqual(typeof stats, "object");
stats = stats.toJson({
modules: true,
reasons: true
});
should.strictEqual(typeof stats, "object");
stats.should.have.property("errors");
2017-01-02 08:44:24 +08:00
Array.isArray(stats.errors).should.be.ok();
if(options.expectErrors) {
stats.errors.length.should.be.eql(options.expectErrors);
} else {
if(stats.errors.length > 0) {
stats.errors[0].should.be.type("string");
throw new Error(stats.errors[0]);
}
2016-05-06 19:38:21 +08:00
}
stats.logs = logs;
callback(stats, files, compilerIteration++);
});
}
const postCompileCallbackStack = [];
2016-05-06 19:38:21 +08:00
function addAfterCompileCallback(callback) {
postCompileCallbackStack.push(callback);
}
c.plugin("after-compile", (stats, callback) => {
2016-05-06 19:38:21 +08:00
if(postCompileCallbackStack.length > 0) {
postCompileCallbackStack.shift(arguments);
2016-05-06 19:38:21 +08:00
}
callback();
});
2016-05-06 19:38:21 +08:00
runCompiler(callback);
return {
compilerInstance: c,
runAgain: runCompiler,
addAfterCompileCallback: addAfterCompileCallback
};
}
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();
} 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
}
before(cleanup);
after(cleanup);
2016-05-06 19:38:21 +08:00
function createTempFixture() {
// Remove previous copy if present
try {
if(fs.statSync(tempFixturePath)) {
fs.unlinkSync(aFilepath);
fs.unlinkSync(cFilepath);
fs.rmdirSync(tempFixturePath);
}
} 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);
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
};
}
it("should cache single file (with manual 1s wait) ", (done) => {
2016-05-06 19:38:21 +08:00
const options = {};
const tempFixture = createTempFixture();
2016-05-06 19:38:21 +08:00
const helper = compile("./temp-cache-fixture/c", options, (stats, files) => {
2016-05-06 19:38:21 +08:00
// Not cached the first time
stats.assets[0].name.should.be.exactly("bundle.js");
2016-05-06 19:38:21 +08:00
stats.assets[0].emitted.should.be.exactly(true);
helper.runAgain((stats, files, iteration) => {
2016-05-06 19:38:21 +08:00
// Cached the second run
stats.assets[0].name.should.be.exactly("bundle.js");
2016-05-06 19:38:21 +08:00
stats.assets[0].emitted.should.be.exactly(false);
const aContent = fs.readFileSync(tempFixture.aFilepath).toString().replace("This is a", "This is a MODIFIED");
2016-05-06 19:38:21 +08:00
setTimeout(() => {
2016-05-06 19:38:21 +08:00
fs.writeFileSync(tempFixture.aFilepath, aContent);
helper.runAgain((stats, files, iteration) => {
2016-05-06 19:38:21 +08:00
// Cached the third run
stats.assets[0].name.should.be.exactly("bundle.js");
2016-05-06 19:38:21 +08:00
stats.assets[0].emitted.should.be.exactly(true);
done();
});
}, 1100);
});
});
});
it("should cache single file (even with no timeout) ", (done) => {
2016-05-06 19:38:21 +08:00
const options = {};
const tempFixture = createTempFixture();
2016-05-06 19:38:21 +08:00
const helper = compile("./temp-cache-fixture/c", options, (stats, files) => {
2016-05-06 19:38:21 +08:00
// Not cached the first time
stats.assets[0].name.should.be.exactly("bundle.js");
2016-05-06 19:38:21 +08:00
stats.assets[0].emitted.should.be.exactly(true);
helper.runAgain((stats, files, iteration) => {
2016-05-06 19:38:21 +08:00
// Cached the second run
stats.assets[0].name.should.be.exactly("bundle.js");
2016-05-06 19:38:21 +08:00
stats.assets[0].emitted.should.be.exactly(false);
files["/bundle.js"].should.containEql("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
2016-05-07 01:42:06 +08:00
fs.writeFileSync(tempFixture.aFilepath, aContent);
2016-05-06 19:38:21 +08:00
helper.runAgain((stats, files, iteration) => {
2016-05-06 19:38:21 +08:00
2016-05-07 01:42:06 +08:00
// Cached the third run
stats.assets[0].name.should.be.exactly("bundle.js");
2016-05-07 01:42:06 +08:00
stats.assets[0].emitted.should.be.exactly(true);
2016-05-06 19:38:21 +08:00
files["/bundle.js"].should.containEql("This is a MODIFIED");
2016-05-06 19:38:21 +08:00
2016-05-07 01:42:06 +08:00
done();
});
2016-05-06 19:38:21 +08:00
});
});
});
it("should only build when modified (with manual 2s wait)", (done) => {
2016-05-06 19:38:21 +08:00
const options = {};
const tempFixture = createTempFixture();
2016-05-06 19:38:21 +08:00
const helper = compile("./temp-cache-fixture/c", options, (stats, files) => {
2016-05-06 19:38:21 +08:00
// Built the first time
2017-06-01 21:42:57 +08:00
stats.modules[0].name.should.containEql("c.js");
stats.modules[0].built.should.be.exactly(true, "c.js should have been built");
2016-05-06 19:38:21 +08:00
2017-06-01 21:42:57 +08:00
stats.modules[1].name.should.containEql("a.js");
stats.modules[1].built.should.be.exactly(true, "a.js should have been built");
2016-05-06 19:38:21 +08:00
setTimeout(() => {
helper.runAgain((stats, files, iteration) => {
2016-05-06 19:38:21 +08:00
2016-05-07 01:42:06 +08:00
// Not built when cached the second run
2017-06-01 21:42:57 +08:00
stats.modules[0].name.should.containEql("c.js");
//stats.modules[0].built.should.be.exactly(false, "c.js should not have built");
2016-05-06 19:38:21 +08:00
2017-06-01 21:42:57 +08:00
stats.modules[1].name.should.containEql("a.js");
//stats.modules[1].built.should.be.exactly(false, "a.js should not have built");
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(() => {
2016-05-07 01:42:06 +08:00
fs.writeFileSync(tempFixture.aFilepath, aContent);
2016-05-06 19:38:21 +08:00
helper.runAgain((stats, files, iteration) => {
2016-05-06 19:38:21 +08:00
2016-05-07 01:42:06 +08:00
// And only a.js built after it was modified
2017-06-01 21:42:57 +08:00
stats.modules[0].name.should.containEql("c.js");
stats.modules[0].built.should.be.exactly(false, "c.js should not have built");
2016-05-06 19:38:21 +08:00
2017-06-01 21:42:57 +08:00
stats.modules[1].name.should.containEql("a.js");
stats.modules[1].built.should.be.exactly(true, "a.js should have been built");
2016-05-06 19:38:21 +08:00
2016-05-07 01:42:06 +08:00
done();
});
}, 2100);
2016-05-06 19:38:21 +08:00
});
2016-05-07 01:42:06 +08:00
}, 4100);
2016-05-06 19:38:21 +08:00
});
});
it("should build when modified (even with no timeout)", (done) => {
2016-05-06 19:38:21 +08:00
const options = {};
const tempFixture = createTempFixture();
2016-05-06 19:38:21 +08:00
const helper = compile("./temp-cache-fixture/c", options, (stats, files) => {
2016-05-06 19:38:21 +08:00
// Built the first time
2017-06-01 21:42:57 +08:00
stats.modules[0].name.should.containEql("c.js");
stats.modules[0].built.should.be.exactly(true, "c.js should have been built");
2016-05-06 19:38:21 +08:00
2017-06-01 21:42:57 +08:00
stats.modules[1].name.should.containEql("a.js");
stats.modules[1].built.should.be.exactly(true, "a.js should have been built");
2016-05-06 19:38:21 +08:00
helper.runAgain((stats, files, iteration) => {
2016-05-06 19:38:21 +08:00
// Not built when cached the second run
2017-06-01 21:42:57 +08:00
stats.modules[0].name.should.containEql("c.js");
//stats.modules[0].built.should.be.exactly(false, "c.js should not have built");
2016-05-06 19:38:21 +08:00
2017-06-01 21:42:57 +08:00
stats.modules[1].name.should.containEql("a.js");
//stats.modules[1].built.should.be.exactly(false, "a.js should not have built");
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);
helper.runAgain((stats, files, iteration) => {
2016-05-06 19:38:21 +08:00
// And only a.js built after it was modified
2017-06-01 21:42:57 +08:00
stats.modules[0].name.should.containEql("c.js");
//stats.modules[0].built.should.be.exactly(false, "c.js should not have built");
2016-05-06 19:38:21 +08:00
2017-06-01 21:42:57 +08:00
stats.modules[1].name.should.containEql("a.js");
stats.modules[1].built.should.be.exactly(true, "a.js should have been built");
2016-05-06 19:38:21 +08:00
done();
});
});
});
});
});