Skip to content

Commit

Permalink
fix: limit external module readable identifier length in stats
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 committed Apr 3, 2023
1 parent eed37e9 commit d9683a8
Showing 1 changed file with 63 additions and 9 deletions.
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 All @@ -23,6 +24,8 @@ const {
} = require("../util/comparators");
const { makePathsRelative, parseResource } = require("../util/identifier");

const MAX_EXTERNAL_MODULE_READABLE_IDENTIFIER_LENGTH = 80;

/** @typedef {import("webpack-sources").Source} Source */
/** @typedef {import("../Chunk")} Chunk */
/** @typedef {import("../ChunkGroup")} ChunkGroup */
Expand Down Expand Up @@ -314,6 +317,24 @@ const { makePathsRelative, parseResource } = require("../util/identifier");
* @property {ExtractorsByOption<Dependency, StatsModuleTraceDependency>} moduleTraceDependency
*/

/**
* @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

0 comments on commit d9683a8

Please sign in to comment.