Skip to content

Commit

Permalink
update webpack-sources for performance problem in hashing
Browse files Browse the repository at this point in the history
add `stats.reasonsSpace` and `stats.groupReasonsByOrigin` to control large set of reasons
`detailed` preset limites all spaces to 1000 by default

fixes #13825
  • Loading branch information
sokra committed Jul 20, 2021
1 parent a87dba4 commit 67e7729
Show file tree
Hide file tree
Showing 16 changed files with 175 additions and 20 deletions.
8 changes: 8 additions & 0 deletions declarations/WebpackOptions.d.ts
Expand Up @@ -2481,6 +2481,10 @@ export interface StatsOptions {
* Group modules by their type.
*/
groupModulesByType?: boolean;
/**
* Group reasons by their origin module.
*/
groupReasonsByOrigin?: boolean;
/**
* Add the hash of the compilation.
*/
Expand Down Expand Up @@ -2561,6 +2565,10 @@ export interface StatsOptions {
* Add information about the reasons why modules are included.
*/
reasons?: boolean;
/**
* Space to display reasons (groups will be collapsed to fit this space).
*/
reasonsSpace?: number;
/**
* Add information about assets that are related to other assets (like SourceMaps for assets).
*/
Expand Down
34 changes: 32 additions & 2 deletions lib/stats/DefaultStatsFactoryPlugin.js
Expand Up @@ -1182,11 +1182,14 @@ const SIMPLE_EXTRACTORS = {
type,
compilation: { moduleGraph }
} = context;
object.reasons = factory.create(
const groupsReasons = factory.create(
`${type.slice(0, -8)}.reasons`,
Array.from(moduleGraph.getIncomingConnections(module)),
context
);
const limited = spaceLimited(groupsReasons, options.reasonsSpace);
object.reasons = limited.children;
object.filteredReasons = limited.filteredChildren;
},
usedExports: (
object,
Expand Down Expand Up @@ -1764,6 +1767,16 @@ const moduleGroup = (children, modules) => {
};
};

const reasonGroup = (children, reasons) => {
let active = false;
for (const reason of children) {
active = active || reason.active;
}
return {
active
};
};

/** @type {Record<string, (groupConfigs: GroupConfig[], context: StatsFactoryContext, options: NormalizedStatsOptions) => void>} */
const ASSETS_GROUPERS = {
_: (groupConfigs, context, options) => {
Expand Down Expand Up @@ -2074,7 +2087,24 @@ const RESULT_GROUPERS = {
"compilation.modules": MODULES_GROUPERS("module"),
"chunk.modules": MODULES_GROUPERS("chunk"),
"chunk.rootModules": MODULES_GROUPERS("root-of-chunk"),
"module.modules": MODULES_GROUPERS("nested")
"module.modules": MODULES_GROUPERS("nested"),
"module.reasons": {
groupReasonsByOrigin: groupConfigs => {
groupConfigs.push({
getKeys: reason => {
return [reason.module];
},
createGroup: (key, children, reasons) => {
return {
type: "from origin",
module: key,
children,
...reasonGroup(children, reasons)
};
}
});
}
}
};

// remove a prefixed "!" that can be specified to reverse sort order
Expand Down
8 changes: 6 additions & 2 deletions lib/stats/DefaultStatsPresetPlugin.js
Expand Up @@ -50,6 +50,7 @@ const NAMED_PRESETS = {
modulesSpace: Infinity,
chunkModulesSpace: Infinity,
assetsSpace: Infinity,
reasonsSpace: Infinity,
children: true
},
detailed: {
Expand All @@ -72,8 +73,9 @@ const NAMED_PRESETS = {
logging: true,
runtimeModules: true,
exclude: false,
modulesSpace: Infinity,
assetsSpace: Infinity
modulesSpace: 1000,
assetsSpace: 1000,
reasonsSpace: 1000
},
minimal: {
all: false,
Expand Down Expand Up @@ -194,6 +196,8 @@ const DEFAULTS = {
depth: OFF_FOR_TO_STRING,
cachedAssets: OFF_FOR_TO_STRING,
reasons: OFF_FOR_TO_STRING,
reasonsSpace: (o, { forToString }) => (forToString ? 15 : Infinity),
groupReasonsByOrigin: ON_FOR_TO_STRING,
usedExports: OFF_FOR_TO_STRING,
providedExports: OFF_FOR_TO_STRING,
optimizationBailout: OFF_FOR_TO_STRING,
Expand Down
60 changes: 51 additions & 9 deletions lib/stats/DefaultStatsPrinterPlugin.js
Expand Up @@ -374,6 +374,10 @@ const SIMPLE_PRINTERS = {
"modules"
)}`
: undefined,
"module.filteredReasons": filteredReasons =>
filteredReasons > 0
? `${filteredReasons} ${plural(filteredReasons, "reason", "reasons")}`
: undefined,
"module.filteredChildren": filteredChildren =>
filteredChildren > 0
? `${filteredChildren} ${plural(filteredChildren, "module", "modules")}`
Expand All @@ -393,6 +397,10 @@ const SIMPLE_PRINTERS = {
"moduleReason.active": (active, { formatFlag }) =>
active ? undefined : formatFlag("inactive"),
"moduleReason.resolvedModule": (module, { magenta }) => magenta(module),
"moduleReason.filteredChildren": filteredChildren =>
filteredChildren > 0
? `${filteredChildren} ${plural(filteredChildren, "reason", "reasons")}`
: undefined,

"module.profile.total": (value, { formatTime }) => formatTime(value),
"module.profile.resolving": (value, { formatTime }) =>
Expand Down Expand Up @@ -590,6 +598,7 @@ const ITEM_NAMES = {
"module.modules[]": "module",
"module.children[]": "module",
"module.reasons[]": "moduleReason",
"moduleReason.children[]": "moduleReason",
"module.issuerPath[]": "moduleIssuer",
"chunk.origins[]": "chunkOrigin",
"chunk.modules[]": "module",
Expand Down Expand Up @@ -721,6 +730,7 @@ const PREFERRED_ORDERS = {
"usedExports",
"optimizationBailout",
"reasons",
"filteredReasons",
"issuerPath",
"profile",
"modules",
Expand All @@ -734,7 +744,9 @@ const PREFERRED_ORDERS = {
"module",
"resolvedModule",
"loc",
"explanation"
"explanation",
"children",
"filteredChildren"
],
"module.profile": [
"total",
Expand Down Expand Up @@ -1019,10 +1031,32 @@ const SIMPLE_ELEMENT_JOINERS = {
chunkGroupAsset: joinOneLine,
chunkGroupChildGroup: joinOneLine,
chunkGroupChild: joinOneLine,
// moduleReason: (items, { moduleReason }) => {
// let hasName = false;
// return joinOneLine(
// items.filter(item => {
// switch (item.element) {
// case "moduleId":
// if (moduleReason.moduleId === moduleReason.module && item.content)
// hasName = true;
// break;
// case "module":
// if (hasName) return false;
// break;
// case "resolvedModule":
// return (
// moduleReason.module !== moduleReason.resolvedModule &&
// item.content
// );
// }
// return true;
// })
// );
// },
moduleReason: (items, { moduleReason }) => {
let hasName = false;
return joinOneLine(
items.filter(item => {
return joinExplicitNewLine(
items.map(item => {
switch (item.element) {
case "moduleId":
if (moduleReason.moduleId === moduleReason.module && item.content)
Expand All @@ -1032,13 +1066,21 @@ const SIMPLE_ELEMENT_JOINERS = {
if (hasName) return false;
break;
case "resolvedModule":
return (
moduleReason.module !== moduleReason.resolvedModule &&
item.content
);
if (moduleReason.module === moduleReason.resolvedModule)
return false;
break;
case "children":
if (item.content) {
return {
...item,
content: `\n${item.content}\n`
};
}
break;
}
return true;
})
return item;
}),
" "
);
},
"module.profile": joinInBrackets,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -27,7 +27,7 @@
"tapable": "^2.1.1",
"terser-webpack-plugin": "^5.1.3",
"watchpack": "^2.2.0",
"webpack-sources": "^2.3.0"
"webpack-sources": "^2.3.1"
},
"peerDependenciesMeta": {
"webpack-cli": {
Expand Down
2 changes: 1 addition & 1 deletion schemas/WebpackOptions.check.js

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions schemas/WebpackOptions.json
Expand Up @@ -4210,6 +4210,10 @@
"description": "Group modules by their type.",
"type": "boolean"
},
"groupReasonsByOrigin": {
"description": "Group reasons by their origin module.",
"type": "boolean"
},
"hash": {
"description": "Add the hash of the compilation.",
"type": "boolean"
Expand Down Expand Up @@ -4314,6 +4318,10 @@
"description": "Add information about the reasons why modules are included.",
"type": "boolean"
},
"reasonsSpace": {
"description": "Space to display reasons (groups will be collapsed to fit this space).",
"type": "number"
},
"relatedAssets": {
"description": "Add information about assets that are related to other assets (like SourceMaps for assets).",
"type": "boolean"
Expand Down
3 changes: 2 additions & 1 deletion test/TestCases.template.js
Expand Up @@ -252,7 +252,8 @@ const describeCases = config => {
const statOptions = {
preset: "verbose",
colors: false,
modules: true
modules: true,
reasonsSpace: 1000
};
fs.mkdirSync(outputDirectory, { recursive: true });
fs.writeFileSync(
Expand Down
26 changes: 26 additions & 0 deletions test/__snapshots__/Cli.test.js.snap
Expand Up @@ -7753,6 +7753,19 @@ Object {
"multiple": false,
"simpleType": "boolean",
},
"stats-group-reasons-by-origin": Object {
"configs": Array [
Object {
"description": "Group reasons by their origin module.",
"multiple": false,
"path": "stats.groupReasonsByOrigin",
"type": "boolean",
},
],
"description": "Group reasons by their origin module.",
"multiple": false,
"simpleType": "boolean",
},
"stats-hash": Object {
"configs": Array [
Object {
Expand Down Expand Up @@ -8058,6 +8071,19 @@ Object {
"multiple": false,
"simpleType": "boolean",
},
"stats-reasons-space": Object {
"configs": Array [
Object {
"description": "Space to display reasons (groups will be collapsed to fit this space).",
"multiple": false,
"path": "stats.reasonsSpace",
"type": "number",
},
],
"description": "Space to display reasons (groups will be collapsed to fit this space).",
"multiple": false,
"simpleType": "number",
},
"stats-related-assets": Object {
"configs": Array [
Object {
Expand Down
@@ -0,0 +1,8 @@
/** @type {import("../../../../").RawLoaderDefinition<{ count: string }>} */
module.exports = function () {
const options = this.getOptions();
return `import thing from "./module";
export default [${Array.from({ length: +options.count }, () => "thing").join(
", "
)}].reduce((a, b) => a + b);`;
};
11 changes: 11 additions & 0 deletions test/cases/large/many-replacements/index.js
@@ -0,0 +1,11 @@
import a from "./generate-many-replacements-loader?count=1000!./module";
import b from "./generate-many-replacements-loader?count=10000!./module";
import c from "./generate-many-replacements-loader?count=100000!./module";
import d from "./generate-many-replacements-loader?count=1000000!./module";

it("should compile fine", () => {
expect(a).toBe(1000);
expect(b).toBe(10000);
expect(c).toBe(100000);
expect(d).toBe(1000000);
});
1 change: 1 addition & 0 deletions test/cases/large/many-replacements/module.js
@@ -0,0 +1 @@
export default 1;
3 changes: 3 additions & 0 deletions test/cases/large/many-replacements/test.config.js
@@ -0,0 +1,3 @@
module.exports = {
timeout: 120000
};
3 changes: 3 additions & 0 deletions test/cases/large/many-replacements/test.filter.js
@@ -0,0 +1,3 @@
module.exports = function (config) {
return !process.env.CI;
};
10 changes: 10 additions & 0 deletions types.d.ts
Expand Up @@ -10899,6 +10899,11 @@ declare interface StatsOptions {
*/
groupModulesByType?: boolean;

/**
* Group reasons by their origin module.
*/
groupReasonsByOrigin?: boolean;

/**
* Add the hash of the compilation.
*/
Expand Down Expand Up @@ -11004,6 +11009,11 @@ declare interface StatsOptions {
*/
reasons?: boolean;

/**
* Space to display reasons (groups will be collapsed to fit this space).
*/
reasonsSpace?: number;

/**
* Add information about assets that are related to other assets (like SourceMaps for assets).
*/
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Expand Up @@ -6157,10 +6157,10 @@ webpack-sources@^1.1.0:
source-list-map "^2.0.0"
source-map "~0.6.1"

webpack-sources@^2.3.0:
version "2.3.0"
resolved "https://registry.yarnpkg.com/webpack-sources/-/webpack-sources-2.3.0.tgz#9ed2de69b25143a4c18847586ad9eccb19278cfa"
integrity sha512-WyOdtwSvOML1kbgtXbTDnEW0jkJ7hZr/bDByIwszhWd/4XX1A3XMkrbFMsuH4+/MfLlZCUzlAdg4r7jaGKEIgQ==
webpack-sources@^2.3.1:
version "2.3.1"
resolved "https://registry.yarnpkg.com/webpack-sources/-/webpack-sources-2.3.1.tgz#570de0af163949fe272233c2cefe1b56f74511fd"
integrity sha512-y9EI9AO42JjEcrTJFOYmVywVZdKVUfOvDUPsJea5GIr1JOEGFVqwlY2K098fFoIjOkDzHn2AjRvM8dsBZu+gCA==
dependencies:
source-list-map "^2.0.1"
source-map "^0.6.1"
Expand Down

0 comments on commit 67e7729

Please sign in to comment.