2017-01-18 18:38:06 +08:00
|
|
|
"use strict";
|
2013-07-05 20:56:16 +08:00
|
|
|
|
2017-01-18 18:38:06 +08:00
|
|
|
const path = require("path");
|
|
|
|
const fs = require("fs");
|
2018-02-25 08:20:45 +08:00
|
|
|
const mkdirp = require("mkdirp");
|
2017-01-18 18:38:06 +08:00
|
|
|
|
|
|
|
const webpack = require("../");
|
2013-07-05 20:56:16 +08:00
|
|
|
|
2018-01-24 23:00:43 +08:00
|
|
|
describe("HotModuleReplacementPlugin", () => {
|
2018-04-12 05:54:22 +08:00
|
|
|
jest.setTimeout(20000);
|
2018-02-25 18:46:17 +08:00
|
|
|
it("should not have circular hashes but equal if unmodified", done => {
|
|
|
|
const entryFile = path.join(
|
|
|
|
__dirname,
|
|
|
|
"js",
|
|
|
|
"HotModuleReplacementPlugin",
|
|
|
|
"entry.js"
|
|
|
|
);
|
|
|
|
const statsFile1 = path.join(
|
|
|
|
__dirname,
|
|
|
|
"js",
|
|
|
|
"HotModuleReplacementPlugin",
|
|
|
|
"HotModuleReplacementPlugin.test.stats1.txt"
|
|
|
|
);
|
|
|
|
const statsFile2 = path.join(
|
|
|
|
__dirname,
|
|
|
|
"js",
|
|
|
|
"HotModuleReplacementPlugin",
|
|
|
|
"HotModuleReplacementPlugin.test.stats2.txt"
|
|
|
|
);
|
|
|
|
const recordsFile = path.join(
|
|
|
|
__dirname,
|
|
|
|
"js",
|
|
|
|
"HotModuleReplacementPlugin",
|
|
|
|
"records.json"
|
|
|
|
);
|
2015-08-09 18:42:43 +08:00
|
|
|
try {
|
2018-02-25 08:20:45 +08:00
|
|
|
mkdirp.sync(path.join(__dirname, "js", "HotModuleReplacementPlugin"));
|
2018-04-12 02:31:28 +08:00
|
|
|
} catch (e) {
|
|
|
|
// empty
|
|
|
|
}
|
2015-08-09 18:42:43 +08:00
|
|
|
try {
|
|
|
|
fs.unlinkSync(recordsFile);
|
2018-04-12 02:31:28 +08:00
|
|
|
} catch (e) {
|
|
|
|
// empty
|
|
|
|
}
|
2017-01-18 18:38:06 +08:00
|
|
|
const compiler = webpack({
|
2014-03-21 23:21:59 +08:00
|
|
|
cache: false,
|
2013-07-05 20:56:16 +08:00
|
|
|
entry: entryFile,
|
|
|
|
recordsPath: recordsFile,
|
|
|
|
output: {
|
2018-02-25 08:20:45 +08:00
|
|
|
path: path.join(__dirname, "js", "HotModuleReplacementPlugin")
|
2013-07-05 20:56:16 +08:00
|
|
|
},
|
2013-12-18 06:21:49 +08:00
|
|
|
plugins: [
|
2014-02-11 20:27:41 +08:00
|
|
|
new webpack.HotModuleReplacementPlugin(),
|
2015-03-06 05:58:03 +08:00
|
|
|
new webpack.optimize.OccurrenceOrderPlugin()
|
2013-12-18 06:21:49 +08:00
|
|
|
]
|
2013-07-05 20:56:16 +08:00
|
|
|
});
|
|
|
|
fs.writeFileSync(entryFile, "1", "utf-8");
|
2017-01-18 18:38:06 +08:00
|
|
|
compiler.run((err, stats) => {
|
2018-02-25 18:46:17 +08:00
|
|
|
if (err) throw err;
|
2017-01-18 18:38:06 +08:00
|
|
|
const oldHash1 = stats.toJson().hash;
|
2015-06-13 17:45:28 +08:00
|
|
|
fs.writeFileSync(statsFile1, stats.toString());
|
2017-01-18 18:38:06 +08:00
|
|
|
compiler.run((err, stats) => {
|
2018-02-25 18:46:17 +08:00
|
|
|
if (err) throw err;
|
2017-01-18 18:38:06 +08:00
|
|
|
const lastHash1 = stats.toJson().hash;
|
2015-06-13 17:45:28 +08:00
|
|
|
fs.writeFileSync(statsFile2, stats.toString());
|
2018-01-24 23:00:43 +08:00
|
|
|
expect(lastHash1).toBe(oldHash1); // hash shouldn't change when bundle stay equal
|
2013-07-05 20:56:16 +08:00
|
|
|
fs.writeFileSync(entryFile, "2", "utf-8");
|
2017-01-18 18:38:06 +08:00
|
|
|
compiler.run((err, stats) => {
|
2018-02-25 18:46:17 +08:00
|
|
|
if (err) throw err;
|
2017-01-18 18:38:06 +08:00
|
|
|
const lastHash2 = stats.toJson().hash;
|
2015-06-13 17:45:28 +08:00
|
|
|
fs.writeFileSync(statsFile1, stats.toString());
|
2018-01-24 23:00:43 +08:00
|
|
|
expect(lastHash2).not.toBe(lastHash1); // hash should change when bundle changes
|
2013-07-05 20:56:16 +08:00
|
|
|
fs.writeFileSync(entryFile, "1", "utf-8");
|
2017-01-18 18:38:06 +08:00
|
|
|
compiler.run((err, stats) => {
|
2018-02-25 18:46:17 +08:00
|
|
|
if (err) throw err;
|
2017-01-18 18:38:06 +08:00
|
|
|
const currentHash1 = stats.toJson().hash;
|
2015-06-13 17:45:28 +08:00
|
|
|
fs.writeFileSync(statsFile2, stats.toString());
|
2018-01-24 23:00:43 +08:00
|
|
|
expect(currentHash1).not.toBe(lastHash1); // hash shouldn't change to the first hash if bundle changed back to first bundle
|
2013-07-05 20:56:16 +08:00
|
|
|
fs.writeFileSync(entryFile, "2", "utf-8");
|
2017-01-18 18:38:06 +08:00
|
|
|
compiler.run((err, stats) => {
|
2018-02-25 18:46:17 +08:00
|
|
|
if (err) throw err;
|
2017-01-18 18:38:06 +08:00
|
|
|
const currentHash2 = stats.toJson().hash;
|
2015-06-13 17:45:28 +08:00
|
|
|
fs.writeFileSync(statsFile1, stats.toString());
|
2017-01-18 18:38:06 +08:00
|
|
|
compiler.run((err, stats) => {
|
2018-02-25 18:46:17 +08:00
|
|
|
if (err) throw err;
|
2018-01-24 23:00:43 +08:00
|
|
|
expect(stats.toJson().hash).toBe(currentHash2);
|
|
|
|
expect(currentHash2).not.toBe(lastHash2);
|
|
|
|
expect(currentHash1).not.toBe(currentHash2);
|
|
|
|
expect(lastHash1).not.toBe(lastHash2);
|
2013-07-05 20:56:16 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-08-09 18:42:43 +08:00
|
|
|
});
|