Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: limit module readable identifier length in stats #16882

Merged
merged 5 commits into from Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
72 changes: 63 additions & 9 deletions lib/stats/DefaultStatsFactoryPlugin.js
Expand Up @@ -6,6 +6,7 @@
"use strict";

const util = require("util");
const ExternalModule = require("../ExternalModule");
const ModuleDependency = require("../dependencies/ModuleDependency");
const formatLocation = require("../formatLocation");
const { LogType } = require("../logging/Logger");
Expand Down Expand Up @@ -314,6 +315,26 @@ const { makePathsRelative, parseResource } = require("../util/identifier");
* @property {ExtractorsByOption<Dependency, StatsModuleTraceDependency>} moduleTraceDependency
*/

const MAX_EXTERNAL_MODULE_READABLE_IDENTIFIER_LENGTH = 80;

/**
* @param {string} readableIdentifier user readable identifier of the module
* @param {Module} module base module type
* @returns {string} an elided readableIdentifier, when readableIdentifier exceeds a maximum length
*/
const truncateLongExternalModuleReadableIdentifier = (
readableIdentifier,
module
) => {
return module instanceof ExternalModule &&
readableIdentifier.length > MAX_EXTERNAL_MODULE_READABLE_IDENTIFIER_LENGTH
? readableIdentifier.substring(
0,
MAX_EXTERNAL_MODULE_READABLE_IDENTIFIER_LENGTH
) + "...(truncated)"
: readableIdentifier;
};

/**
* @template T
* @template I
Expand Down Expand Up @@ -393,7 +414,10 @@ const EXTRACT_ERROR = {
}
if (error.module) {
object.moduleIdentifier = error.module.identifier();
object.moduleName = error.module.readableIdentifier(requestShortener);
object.moduleName = truncateLongExternalModuleReadableIdentifier(
error.module.readableIdentifier(requestShortener),
error.module
);
}
if (error.loc) {
object.loc = formatLocation(error.loc);
Expand Down Expand Up @@ -1133,7 +1157,10 @@ const SIMPLE_EXTRACTORS = {
/** @type {KnownStatsModule} */
const statsModule = {
identifier: module.identifier(),
name: module.readableIdentifier(requestShortener),
name: truncateLongExternalModuleReadableIdentifier(
module.readableIdentifier(requestShortener),
module
),
nameForCondition: module.nameForCondition(),
index: moduleGraph.getPreOrderIndex(module),
preOrderIndex: moduleGraph.getPreOrderIndex(module),
Expand All @@ -1146,7 +1173,12 @@ const SIMPLE_EXTRACTORS = {
compilation.chunkGraph.getNumberOfModuleChunks(module) === 0,
dependent: rootModules ? !rootModules.has(module) : undefined,
issuer: issuer && issuer.identifier(),
issuerName: issuer && issuer.readableIdentifier(requestShortener),
issuerName:
issuer &&
truncateLongExternalModuleReadableIdentifier(
issuer.readableIdentifier(requestShortener),
issuer
),
issuerPath:
issuer &&
factory.create(`${type.slice(0, -8)}.issuerPath`, path, context),
Expand Down Expand Up @@ -1286,7 +1318,10 @@ const SIMPLE_EXTRACTORS = {
/** @type {KnownStatsModuleIssuer} */
const statsModuleIssuer = {
identifier: module.identifier(),
name: module.readableIdentifier(requestShortener)
name: truncateLongExternalModuleReadableIdentifier(
module.readableIdentifier(requestShortener),
module
)
};
Object.assign(object, statsModuleIssuer);
if (profile) {
Expand All @@ -1308,16 +1343,25 @@ const SIMPLE_EXTRACTORS = {
? reason.originModule.identifier()
: null,
module: reason.originModule
? reason.originModule.readableIdentifier(requestShortener)
? truncateLongExternalModuleReadableIdentifier(
reason.originModule.readableIdentifier(requestShortener),
reason.originModule
)
: null,
moduleName: reason.originModule
? reason.originModule.readableIdentifier(requestShortener)
? truncateLongExternalModuleReadableIdentifier(
reason.originModule.readableIdentifier(requestShortener),
reason.originModule
)
: null,
resolvedModuleIdentifier: reason.resolvedOriginModule
? reason.resolvedOriginModule.identifier()
: null,
resolvedModule: reason.resolvedOriginModule
? reason.resolvedOriginModule.readableIdentifier(requestShortener)
? truncateLongExternalModuleReadableIdentifier(
reason.resolvedOriginModule.readableIdentifier(requestShortener),
reason.resolvedOriginModule
)
: null,
type: reason.dependency ? reason.dependency.type : null,
active: reason.isActive(runtime),
Expand Down Expand Up @@ -1445,7 +1489,10 @@ const SIMPLE_EXTRACTORS = {
module: origin.module ? origin.module.identifier() : "",
moduleIdentifier: origin.module ? origin.module.identifier() : "",
moduleName: origin.module
? origin.module.readableIdentifier(requestShortener)
? truncateLongExternalModuleReadableIdentifier(
origin.module.readableIdentifier(requestShortener),
origin.module
)
: "",
loc: formatLocation(origin.loc),
request: origin.request
Expand All @@ -1468,8 +1515,15 @@ const SIMPLE_EXTRACTORS = {
} = context;
object.originIdentifier = origin.identifier();
object.originName = origin.readableIdentifier(requestShortener);
object.originName = truncateLongExternalModuleReadableIdentifier(
origin.readableIdentifier(requestShortener),
origin
);
object.moduleIdentifier = module.identifier();
object.moduleName = module.readableIdentifier(requestShortener);
object.moduleName = truncateLongExternalModuleReadableIdentifier(
module.readableIdentifier(requestShortener),
module
);
const dependencies = Array.from(
moduleGraph.getIncomingConnections(module)
)
Expand Down
7 changes: 7 additions & 0 deletions test/__snapshots__/StatsTestCases.basictest.js.snap
Expand Up @@ -1435,6 +1435,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"
}
};