Skip to content

Commit

Permalink
Merge pull request #16882 from snitin315/limit-identifier-length
Browse files Browse the repository at this point in the history
fix: limit module readable identifier length in stats
  • Loading branch information
TheLarkInn committed Apr 12, 2023
2 parents 7f08e4d + 132ebd5 commit 96c5d21
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
14 changes: 14 additions & 0 deletions lib/stats/DefaultStatsPrinterPlugin.js
Expand Up @@ -10,6 +10,7 @@
/** @typedef {import("./StatsPrinter").StatsPrinterContext} StatsPrinterContext */

const DATA_URI_CONTENT_LENGTH = 16;
const MAX_MODULE_IDENTIFIER_LENGTH = 80;

const plural = (n, singular, plural) => (n === 1 ? singular : plural);

Expand Down Expand Up @@ -42,6 +43,19 @@ const getResourceName = resource => {

const getModuleName = name => {
const [, prefix, resource] = /^(.*!)?([^!]*)$/.exec(name);

if (resource.length > MAX_MODULE_IDENTIFIER_LENGTH) {
const truncatedResource = `${resource.slice(
0,
Math.min(
resource.length - /* '...(truncated)'.length */ 14,
MAX_MODULE_IDENTIFIER_LENGTH
)
)}...(truncated)`;

return [prefix, getResourceName(truncatedResource)];
}

return [prefix, getResourceName(resource)];
};

Expand Down
9 changes: 8 additions & 1 deletion test/__snapshots__/StatsTestCases.basictest.js.snap
Expand Up @@ -1193,7 +1193,7 @@ built modules 724 bytes [built]
./templates/baz.js 38 bytes [optional] [built] [code generated]
./templates/foo.js 38 bytes [optional] [built] [code generated]
./entry.js 450 bytes [built] [code generated]
./templates/ lazy ^\\\\.\\\\/.*$ include: \\\\.js$ exclude: \\\\.noimport\\\\.js$ namespace object 160 bytes [optional] [built] [code generated]
./templates/ lazy ^\\\\.\\\\/.*$ include: \\\\.js$ exclude: \\\\.noimport\\\\.js$ na...(truncated) 160 bytes [optional] [built] [code generated]
webpack x.x.x compiled successfully in X ms"
`;

Expand Down Expand Up @@ -1433,6 +1433,13 @@ asset <CLR=32,BOLD>main.js</CLR> 84 bytes <CLR=32,BOLD>[emitted]</CLR> (name: ma
webpack x.x.x compiled <CLR=32,BOLD>successfully</CLR> in X ms"
`;

exports[`StatsTestCases should print correct stats for max-external-module-readable-identifier 1`] = `
"asset main.js 1.45 KiB [emitted] (name: main)
./index.js 17 bytes [built] [code generated]
external \\"very-very-very-very-long-external-module-readable-identifier-it-should...(truncated) 42 bytes [built] [code generated]
webpack x.x.x compiled successfully in X ms"
`;

exports[`StatsTestCases should print correct stats for max-modules 1`] = `
"asset main.js 5.47 KiB [emitted] (name: main)
./index.js 181 bytes [built] [code generated]
Expand Down
@@ -0,0 +1 @@
require("test");
@@ -0,0 +1,8 @@
/** @type {import("../../../types").Configuration} */
module.exports = {
mode: "production",
entry: "./index",
externals: {
test: "commonjs very-very-very-very-long-external-module-readable-identifier-it-should-be-truncated"
}
};

0 comments on commit 96c5d21

Please sign in to comment.