test: added

This commit is contained in:
alexander.akait 2024-09-26 21:38:33 +03:00
parent 73f67fb132
commit 13dc07e996
9 changed files with 91 additions and 2 deletions

View File

@ -128,6 +128,8 @@ const createDefaultHandler = (profile, logger) => {
return defaultHandler;
};
const SKIPPED_QUEUE_CONTEXTS = ["import-module", "load-module"];
/**
* @callback ReportProgress
* @param {number} p percentage
@ -313,7 +315,7 @@ class ProgressPlugin {
* @param {T} _item item
*/
const factorizeAdd = (factorizeQueue, _item) => {
if (factorizeQueue.getContext() === "import-module") {
if (SKIPPED_QUEUE_CONTEXTS.includes(factorizeQueue.getContext())) {
skippedDependenciesCount++;
}
dependenciesCount++;
@ -333,7 +335,7 @@ class ProgressPlugin {
* @param {T} _item item
*/
const moduleAdd = (addModuleQueue, _item) => {
if (addModuleQueue.getContext() === "import-module") {
if (SKIPPED_QUEUE_CONTEXTS.includes(addModuleQueue.getContext())) {
skippedModulesCount++;
}
modulesCount++;

View File

@ -76,6 +76,12 @@ class LoaderPlugin {
)
);
}
const oldFactorizeQueueContext =
compilation.factorizeQueue.getContext();
compilation.factorizeQueue.setContext("load-module");
const oldAddModuleQueueContext =
compilation.addModuleQueue.getContext();
compilation.addModuleQueue.setContext("load-module");
compilation.buildQueue.increaseParallelism();
compilation.handleModuleCreation(
{
@ -88,6 +94,8 @@ class LoaderPlugin {
recursive: false
},
err => {
compilation.factorizeQueue.setContext(oldFactorizeQueueContext);
compilation.addModuleQueue.setContext(oldAddModuleQueueContext);
compilation.buildQueue.decreaseParallelism();
if (err) {
return callback(err);

View File

@ -0,0 +1,2 @@
export const a = "a";
export const b = "b";

View File

@ -0,0 +1,14 @@
import a, { value, random } from "./mod.js";
import { value as unrelated } from "./unrelated";
it("should have to correct values and validate on change", () => {
const step = +WATCH_STEP;
expect(a).toBe(24);
expect(value).toBe(42);
expect(random).toBeDefined();
if (step !== 0) {
expect(STATE.random === a.random).toBe(step === 1);
}
STATE.random = a.random;
});

View File

@ -0,0 +1,15 @@
/** @type {import("../../../../../").PitchLoaderDefinitionFunction} */
exports.pitch = async function (remaining) {
const callback = this.async();
const result = this.loadModule(
`${this.resourcePath}.webpack[javascript/auto]!=!${remaining}`,
(err, result) => {
if (err) {
callback(err);
return;
}
callback(null, result)
}
);
};

View File

@ -0,0 +1,4 @@
export const value = 42;
export * from "./imported.js";
export const random = Math.random();
export default 24;

View File

@ -0,0 +1 @@
export const value = 42;

View File

@ -0,0 +1 @@
export const value = 24;

View File

@ -0,0 +1,42 @@
const webpack = require("../../../../");
/** @type {import("../../../../").Configuration} */
module.exports = {
cache: {
type: "filesystem"
},
module: {
rules: [
{
test: /mod\.js$/,
use: "./loader"
}
]
},
plugins: [
new webpack.ProgressPlugin(),
{
apply(compiler) {
compiler.hooks.done.tapPromise("CacheTest", async () => {
const cache = compiler
.getCache("ProgressPlugin")
.getItemCache("counts", null);
const data = await cache.getPromise();
if (data.modulesCount !== 4) {
throw new Error(
`Wrong cached value of \`ProgressPlugin.modulesCount\` - ${data.modulesCount}, expect 4`
);
}
if (data.dependenciesCount !== 4) {
throw new Error(
`Wrong cached value of \`ProgressPlugin.dependenciesCount\` - ${data.dependenciesCount}, expect 4`
);
}
});
}
}
]
};