webpack/test/HotModuleReplacementPlugin....

76 lines
2.6 KiB
JavaScript
Raw Normal View History

"use strict";
2013-07-05 20:56:16 +08:00
2017-11-15 21:08:11 +08:00
require("should");
const path = require("path");
const fs = require("fs");
const webpack = require("../");
2013-07-05 20:56:16 +08:00
describe("HotModuleReplacementPlugin", function() {
2016-01-30 18:20:45 +08:00
this.timeout(10000);
it("should not have circular hashes but equal if unmodified", (done) => {
const entryFile = path.join(__dirname, "js", "entry.js");
const statsFile1 = path.join(__dirname, "js", "HotModuleReplacementPlugin.test.stats1.txt");
const statsFile2 = path.join(__dirname, "js", "HotModuleReplacementPlugin.test.stats2.txt");
const recordsFile = path.join(__dirname, "js", "records.json");
2015-08-09 18:42:43 +08:00
try {
fs.mkdirSync(path.join(__dirname, "js"));
} catch(e) {}
try {
fs.unlinkSync(recordsFile);
} catch(e) {}
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: {
path: path.join(__dirname, "js")
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
2015-03-06 05:58:03 +08:00
new webpack.optimize.OccurrenceOrderPlugin()
]
2013-07-05 20:56:16 +08:00
});
fs.writeFileSync(entryFile, "1", "utf-8");
compiler.run((err, stats) => {
2013-07-05 20:56:16 +08:00
if(err) throw err;
const oldHash1 = stats.toJson().hash;
fs.writeFileSync(statsFile1, stats.toString());
compiler.run((err, stats) => {
2013-07-05 20:56:16 +08:00
if(err) throw err;
const lastHash1 = stats.toJson().hash;
fs.writeFileSync(statsFile2, stats.toString());
lastHash1.should.be.eql(oldHash1, "hash shouldn't change when bundle stay equal");
2013-07-05 20:56:16 +08:00
fs.writeFileSync(entryFile, "2", "utf-8");
compiler.run((err, stats) => {
2013-07-05 20:56:16 +08:00
if(err) throw err;
const lastHash2 = stats.toJson().hash;
fs.writeFileSync(statsFile1, stats.toString());
lastHash2.should.not.be.eql(lastHash1, "hash should change when bundle changes");
2013-07-05 20:56:16 +08:00
fs.writeFileSync(entryFile, "1", "utf-8");
compiler.run((err, stats) => {
2013-07-05 20:56:16 +08:00
if(err) throw err;
const currentHash1 = stats.toJson().hash;
fs.writeFileSync(statsFile2, stats.toString());
currentHash1.should.not.be.eql(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");
compiler.run((err, stats) => {
2013-07-05 20:56:16 +08:00
if(err) throw err;
const currentHash2 = stats.toJson().hash;
fs.writeFileSync(statsFile1, stats.toString());
compiler.run((err, stats) => {
2013-07-05 20:56:16 +08:00
if(err) throw err;
stats.toJson().hash.should.be.eql(currentHash2);
currentHash2.should.not.be.eql(lastHash2);
currentHash1.should.not.be.eql(currentHash2);
lastHash1.should.not.be.eql(lastHash2);
done();
});
});
});
});
});
});
});
2015-08-09 18:42:43 +08:00
});