webpack/test/HotModuleReplacementPlugin....

333 lines
8.5 KiB
JavaScript
Raw Permalink Normal View History

"use strict";
2013-07-05 20:56:16 +08:00
const path = require("path");
const fs = require("graceful-fs");
2018-12-09 21:26:35 +08:00
const webpack = require("..");
2013-07-05 20:56:16 +08:00
2018-01-24 23:00:43 +08:00
describe("HotModuleReplacementPlugin", () => {
jest.setTimeout(20000);
it("should not have circular hashes but equal if unmodified", (done) => {
2018-02-25 18:46:17 +08:00
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 {
fs.mkdirSync(path.join(__dirname, "js", "HotModuleReplacementPlugin"), {
recursive: true
});
2024-07-31 15:37:05 +08:00
} catch (_err) {
// empty
}
2015-08-09 18:42:43 +08:00
try {
fs.unlinkSync(recordsFile);
2024-07-31 15:37:05 +08:00
} catch (_err) {
// empty
}
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", "HotModuleReplacementPlugin")
2013-07-05 20:56:16 +08:00
},
plugins: [new webpack.HotModuleReplacementPlugin()],
optimization: {
moduleIds: "size",
chunkIds: "size"
}
2013-07-05 20:56:16 +08:00
});
fs.writeFileSync(entryFile, "1", "utf8");
compiler.run((err, stats) => {
2018-02-25 18:46:17 +08:00
if (err) throw err;
const oldHash1 = stats.toJson().hash;
fs.writeFileSync(statsFile1, stats.toString());
compiler.run((err, stats) => {
2018-02-25 18:46:17 +08:00
if (err) throw err;
const lastHash1 = stats.toJson().hash;
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
fs.writeFileSync(entryFile, "2", "utf8");
compiler.run((err, stats) => {
2018-02-25 18:46:17 +08:00
if (err) throw err;
const lastHash2 = stats.toJson().hash;
fs.writeFileSync(statsFile1, stats.toString());
2018-01-24 23:00:43 +08:00
expect(lastHash2).not.toBe(lastHash1); // hash should change when bundle changes
fs.writeFileSync(entryFile, "1", "utf8");
compiler.run((err, stats) => {
2018-02-25 18:46:17 +08:00
if (err) throw err;
const currentHash1 = stats.toJson().hash;
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
fs.writeFileSync(entryFile, "2", "utf8");
compiler.run((err, stats) => {
2018-02-25 18:46:17 +08:00
if (err) throw err;
const currentHash2 = stats.toJson().hash;
fs.writeFileSync(statsFile1, stats.toString());
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();
});
});
});
});
});
});
2018-11-30 21:24:16 +08:00
}, 120000);
2018-10-30 15:49:48 +08:00
it("output.clean=true should keep 1 last update", (done) => {
const outputPath = path.join(__dirname, "js", "HotModuleReplacementPlugin");
const entryFile = path.join(outputPath, "entry.js");
const recordsFile = path.join(outputPath, "records.json");
let step = 0;
let firstUpdate;
try {
fs.mkdirSync(outputPath, { recursive: true });
2024-07-31 15:37:05 +08:00
} catch (_err) {
// empty
}
fs.writeFileSync(entryFile, `${++step}`, "utf8");
const updates = new Set();
const hasFile = (file) => {
try {
fs.statSync(path.join(outputPath, file));
return true;
2024-07-31 15:37:05 +08:00
} catch (_err) {
return false;
}
};
const compiler = webpack({
mode: "development",
cache: false,
entry: {
0: entryFile
},
recordsPath: recordsFile,
output: {
path: outputPath,
clean: true
},
plugins: [new webpack.HotModuleReplacementPlugin()]
});
const callback = (err, stats) => {
if (err) return done(err);
const jsonStats = stats.toJson();
const hash = jsonStats.hash;
const hmrUpdateMainFileName = `0.${hash}.hot-update.json`;
switch (step) {
case 1:
expect(updates.size).toBe(0);
firstUpdate = hmrUpdateMainFileName;
break;
case 2:
expect(updates.size).toBe(1);
expect(updates.has(firstUpdate)).toBe(true);
expect(hasFile(firstUpdate)).toBe(true);
break;
case 3:
expect(updates.size).toBe(2);
for (const file of updates) {
expect(hasFile(file)).toBe(true);
}
return setTimeout(() => {
fs.writeFileSync(entryFile, `${++step}`, "utf8");
compiler.run((err) => {
if (err) return done(err);
for (const file of updates) {
expect(hasFile(file)).toBe(false);
}
done();
});
}, 10100);
}
updates.add(hmrUpdateMainFileName);
fs.writeFileSync(entryFile, `${++step}`, "utf8");
compiler.run(callback);
};
compiler.run(callback);
}, 20000);
it("should correct working when entry is Object and key is a number", (done) => {
const outputPath = path.join(__dirname, "js", "HotModuleReplacementPlugin");
2019-02-19 15:58:46 +08:00
const entryFile = path.join(outputPath, "entry.js");
2018-10-30 15:49:48 +08:00
const statsFile3 = path.join(
outputPath,
2018-10-30 15:49:48 +08:00
"HotModuleReplacementPlugin.test.stats3.txt"
);
const statsFile4 = path.join(
outputPath,
2018-10-30 15:49:48 +08:00
"HotModuleReplacementPlugin.test.stats4.txt"
);
2019-02-19 15:58:46 +08:00
const recordsFile = path.join(outputPath, "records.json");
2018-10-30 15:49:48 +08:00
try {
fs.mkdirSync(outputPath, { recursive: true });
2024-07-31 15:37:05 +08:00
} catch (_err) {
2018-10-30 15:49:48 +08:00
// empty
}
try {
fs.unlinkSync(recordsFile);
2024-07-31 15:37:05 +08:00
} catch (_err) {
2018-10-30 15:49:48 +08:00
// empty
}
const compiler = webpack({
mode: "development",
cache: false,
entry: {
2020-09-01 17:30:46 +08:00
0: entryFile
2018-10-30 15:49:48 +08:00
},
recordsPath: recordsFile,
output: {
path: outputPath
2018-10-30 15:49:48 +08:00
},
plugins: [new webpack.HotModuleReplacementPlugin()],
optimization: {
2018-11-03 20:04:13 +08:00
chunkIds: "named"
2018-10-30 15:49:48 +08:00
}
});
fs.writeFileSync(entryFile, "1", "utf8");
2018-10-30 15:49:48 +08:00
compiler.run((err, stats) => {
if (err) throw err;
const jsonStats = stats.toJson();
const hash = jsonStats.hash;
2018-11-30 18:54:43 +08:00
const chunkName = Object.keys(jsonStats.assetsByChunkName)[0];
2018-10-30 15:49:48 +08:00
fs.writeFileSync(statsFile3, stats.toString());
compiler.run((err, stats) => {
if (err) throw err;
fs.writeFileSync(statsFile4, stats.toString());
fs.writeFileSync(entryFile, "2", "utf8");
2018-10-30 15:49:48 +08:00
compiler.run((err, stats) => {
if (err) throw err;
fs.writeFileSync(statsFile3, stats.toString());
const result = JSON.parse(
2019-02-19 15:58:46 +08:00
fs.readFileSync(
2020-11-29 01:27:44 +08:00
path.join(outputPath, `0.${hash}.hot-update.json`),
"utf8"
2019-02-19 15:58:46 +08:00
)
2024-07-31 09:37:24 +08:00
).c;
2018-11-30 18:54:43 +08:00
expect(result).toEqual([chunkName]);
2018-10-30 15:49:48 +08:00
done();
});
});
});
});
it("should handle entryFile that contains path variable", (done) => {
const entryFile = path.join(
__dirname,
"js",
"HotModuleReplacementPlugin",
"[name]",
"entry.js"
);
const statsFile3 = path.join(
__dirname,
"js",
"HotModuleReplacementPlugin",
"HotModuleReplacementPlugin.test.stats3.txt"
);
const statsFile4 = path.join(
__dirname,
"js",
"HotModuleReplacementPlugin",
"HotModuleReplacementPlugin.test.stats4.txt"
);
const recordsFile = path.join(
__dirname,
"js",
"HotModuleReplacementPlugin",
"records.json"
);
try {
fs.mkdirSync(
path.join(__dirname, "js", "HotModuleReplacementPlugin", "[name]"),
{
recursive: true
}
);
2024-07-31 15:37:05 +08:00
} catch (_err) {
// empty
}
try {
fs.unlinkSync(recordsFile);
2024-07-31 15:37:05 +08:00
} catch (_err) {
// empty
}
const compiler = webpack({
mode: "development",
cache: false,
entry: {
"[name]/entry.js": entryFile
},
recordsPath: recordsFile,
output: {
filename: "[name]",
chunkFilename: "[name].js",
path: path.join(__dirname, "js", "HotModuleReplacementPlugin"),
2025-03-18 12:43:47 +08:00
hotUpdateChunkFilename: "static/webpack/[id].[fullhash].hot-update.js",
hotUpdateMainFilename: "static/webpack/[fullhash].hot-update.json"
},
plugins: [new webpack.HotModuleReplacementPlugin()],
optimization: {
2019-08-05 18:15:03 +08:00
chunkIds: "named"
}
});
fs.writeFileSync(entryFile, "1", "utf8");
compiler.run((err, stats) => {
if (err) return done(err);
fs.writeFileSync(statsFile3, stats.toString());
compiler.run((err, stats) => {
if (err) return done(err);
fs.writeFileSync(statsFile4, stats.toString());
fs.writeFileSync(entryFile, "2", "utf8");
compiler.run((err, stats) => {
if (err) return done(err);
fs.writeFileSync(statsFile3, stats.toString());
let foundUpdates = false;
2024-08-02 02:36:27 +08:00
for (const key of Object.keys(stats.compilation.assets)) {
foundUpdates =
foundUpdates ||
2024-07-31 11:11:11 +08:00
Boolean(
2024-08-02 02:36:27 +08:00
/static\/webpack\/\[name\]\/entry\.js\..*?\.hot-update\.js/.test(
key
2024-07-31 11:11:11 +08:00
)
);
2024-08-02 02:36:27 +08:00
}
expect(foundUpdates).toBe(true);
done();
});
});
});
});
2015-08-09 18:42:43 +08:00
});