Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4,878 changes: 4,292 additions & 586 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,9 @@
"fmt:check": "npm run fmt:base -- --check",
"fmt:base": "prettier --cache --ignore-unknown .",
"fix": "npm run fix:code && npm run fmt",
"install-test-webpack-versions": "./bin/install-test-webpack-versions.sh",
"test": "npm run install-test-webpack-versions && NODE_OPTIONS=--openssl-legacy-provider jest --runInBand",
"test": "NODE_OPTIONS=--openssl-legacy-provider jest --runInBand",
"test:coverage": "npm run test -- --coverage",
"test-dev": "npm run install-test-webpack-versions && NODE_OPTIONS=--openssl-legacy-provider jest --watch --runInBand"
"test-dev": "NODE_OPTIONS=--openssl-legacy-provider jest --watch --runInBand"
},
"dependencies": {
"@discoveryjs/json-ext": "^0.6.3",
Expand Down Expand Up @@ -95,7 +94,8 @@
"style-loader": "^4.0.0",
"terser-webpack-plugin": "^5.1.2",
"tinyglobby": "^0.2.15",
"webpack": "^5.98.0",
"webpack-4": "npm:webpack@^4",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah good idea!

"webpack": "^5.105.2",
"webpack-cli": "^6.0.1",
"webpack-dev-server": "^5.2.0"
},
Expand Down
49 changes: 8 additions & 41 deletions test/helpers.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,27 @@
const { readdirSync } = require("node:fs");
const path = require("node:path");
const webpack = require("webpack");

const BundleAnalyzerPlugin = require("../src/BundleAnalyzerPlugin");

/* global it */

/**
* @template T
* @typedef {() => T} FunctionReturning
*/

/**
* @template T
* @param {FunctionReturning<T>} fn memorized function
* @returns {FunctionReturning<T>} new function
*/
const memoize = (fn) => {
let cache = false;
/** @type {T | undefined} */
let result;
return () => {
if (cache) {
return /** @type {T} */ (result);
}

result = fn();
cache = true;
// Allow to clean up memory for fn
// and all dependent resources
/** @type {FunctionReturning<T> | undefined} */
(fn) = undefined;
return /** @type {T} */ (result);
};
};

const getAvailableWebpackVersions = memoize(() =>
readdirSync(path.resolve(__dirname, "./webpack-versions"), {
withFileTypes: true,
})
.filter((entry) => entry.isDirectory())
.map((dir) => dir.name),
);

function wait(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}

const webpackVersions = {
4: path.resolve(__dirname, "../node_modules/webpack-4"),
5: path.resolve(__dirname, "../node_modules/webpack"),
};

async function webpackCompile(config, version) {
if (version === undefined || version === null) {
throw new Error("Webpack version is not specified");
}

if (!getAvailableWebpackVersions().includes(version)) {
if (!webpackVersions[version]) {
throw new Error(
`Webpack version "${version}" is not available for testing`,
);
Expand All @@ -63,7 +30,7 @@ async function webpackCompile(config, version) {
let webpack;

try {
webpack = require(`./webpack-versions/${version}/node_modules/webpack`);
webpack = require(webpackVersions[version]);
} catch (err) {
throw new Error(
`Error requiring Webpack ${version}:\n${err}\n\n` +
Expand Down Expand Up @@ -140,7 +107,7 @@ function makeWebpackConfig(opts = {}) {
}

function forEachWebpackVersion(versions, cb) {
const availableVersions = getAvailableWebpackVersions();
const availableVersions = Object.keys(webpackVersions);

if (typeof versions === "function") {
cb = versions;
Expand Down
18 changes: 9 additions & 9 deletions test/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ describe("Plugin", () => {
});
});

forEachWebpackVersion(["4.44.2"], ({ it, webpackCompile }) => {
forEachWebpackVersion(["4"], ({ it, webpackCompile }) => {
// Webpack 5 doesn't support `jsonpFunction` option
it("should support webpack config with custom `jsonpFunction` name", async () => {
const config = makeWebpackConfig({
Expand Down Expand Up @@ -184,7 +184,7 @@ describe("Plugin", () => {
describe("reportTitle", () => {
it("should have a sensible default", async () => {
const config = makeWebpackConfig();
await webpackCompile(config, "4.44.2");
await webpackCompile(config, "4");
const generatedReportTitle = await getTitleFromReport();
expect(generatedReportTitle).toMatch(
/^webpack-bundle-analyzer \[.* at \d{2}:\d{2}\]/u,
Expand All @@ -198,7 +198,7 @@ describe("Plugin", () => {
reportTitle,
},
});
await webpackCompile(config, "4.44.2");
await webpackCompile(config, "4");
const generatedReportTitle = await getTitleFromReport();
expect(generatedReportTitle).toBe(reportTitle);
});
Expand All @@ -210,7 +210,7 @@ describe("Plugin", () => {
reportTitle: () => reportTitleResult,
},
});
await webpackCompile(config, "4.44.2");
await webpackCompile(config, "4");
const generatedReportTitle = await getTitleFromReport();
expect(generatedReportTitle).toBe(reportTitleResult);
});
Expand All @@ -227,7 +227,7 @@ describe("Plugin", () => {

let error = null;
try {
await webpackCompile(config, "4.44.2");
await webpackCompile(config, "4");
} catch (err) {
error = err;
}
Expand All @@ -239,23 +239,23 @@ describe("Plugin", () => {
describe("compressionAlgorithm", () => {
it("should default to gzip", async () => {
const config = makeWebpackConfig({ analyzerOpts: {} });
await webpackCompile(config, "4.44.2");
await webpackCompile(config, "4");
await expectValidReport({ parsedSize: 1317, gzipSize: 341 });
});

it("should support gzip", async () => {
const config = makeWebpackConfig({
analyzerOpts: { compressionAlgorithm: "gzip" },
});
await webpackCompile(config, "4.44.2");
await webpackCompile(config, "4");
await expectValidReport({ parsedSize: 1317, gzipSize: 341 });
});

it("should support brotli", async () => {
const config = makeWebpackConfig({
analyzerOpts: { compressionAlgorithm: "brotli" },
});
await webpackCompile(config, "4.44.2");
await webpackCompile(config, "4");
await expectValidReport({
gzipSize: undefined,
parsedSize: 1317,
Expand All @@ -268,7 +268,7 @@ describe("Plugin", () => {
const config = makeWebpackConfig({
analyzerOpts: { compressionAlgorithm: "zstd" },
});
await webpackCompile(config, "4.44.2");
await webpackCompile(config, "4");
await expectValidReport({
parsedSize: 1317,
gzipSize: undefined,
Expand Down
Loading