Skip to content

Commit

Permalink
feat: Webpack - extract reasons module name
Browse files Browse the repository at this point in the history
  • Loading branch information
vio committed Sep 24, 2023
1 parent c28bf7f commit dede7c2
Show file tree
Hide file tree
Showing 14 changed files with 81,571 additions and 13,070 deletions.
47,149 changes: 40,802 additions & 6,347 deletions fixtures/webpack-stats.baseline.json

Large diffs are not rendered by default.

47,367 changes: 40,655 additions & 6,712 deletions fixtures/webpack-stats.current.json

Large diffs are not rendered by default.

36 changes: 35 additions & 1 deletion packages/plugin-webpack-filter/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import { StatsCompilation } from 'webpack';
import { StatsCompilation, StatsModule } from 'webpack';

const PATH_IGNORE_PATTERN = '.map$';

Expand All @@ -20,15 +20,21 @@ export interface WebpackStatsFilteredChunk {
names?: Array<string>;
}

export interface WebpackStatsFilteredModuleReason {
module: string;
}

export interface WebpackStatsFilteredModule {
name: string;
size?: number;
chunks: Array<string | number>;
reasons?: Array<WebpackStatsFilteredModuleReason>;
}

export interface WebpackStatsFilteredConcatenatedModule {
name: string;
size?: number;
reasons?: Array<WebpackStatsFilteredModuleReason>;
}

export interface WebpackStatsFilteredRootModule extends WebpackStatsFilteredModule {
Expand All @@ -43,6 +49,28 @@ export interface WebpackStatsFiltered {
modules?: Array<WebpackStatsFilteredRootModule>;
}

const getReasons = (
moduleData: StatsModule,
): Array<WebpackStatsFilteredModuleReason> | undefined => {
const reasonsByModule: Record<string, WebpackStatsFilteredModuleReason> = {};

moduleData.reasons?.forEach((reason) => {
// Add the reason module only once
// webpack adds one entry for import and one entry for every reference
if (reason.module && !reasonsByModule[reason.module]) {
reasonsByModule[reason.module] = { module: reason.module };
}
});

const reasons = Object.values(reasonsByModule);

if (reasons.length === 0) {
return undefined;
}

return reasons;
};

/**
* Filter webpack stats data
*/
Expand Down Expand Up @@ -107,21 +135,27 @@ export default (
return aggConcatenatedModules;
}

const reasons = getReasons(concatenatedModule);

aggConcatenatedModules.push({
name: concatenatedModule.name,
size: concatenatedModule.size,
...(reasons && { reasons }),
});

return aggConcatenatedModules;
},
[] as Array<WebpackStatsFilteredConcatenatedModule>,
);

const reasons = getReasons(moduleStats);

agg.push({
name: moduleStats.name,
size: moduleStats.size,
chunks: moduleChunks,
...(concatenatedModules && { modules: concatenatedModules }),
...(reasons && { reasons }),
});

return agg;
Expand Down
10 changes: 10 additions & 0 deletions packages/plugin-webpack-validate/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,20 @@ describe('Webpack/validate', () => {
name: './main.js + 2 concantenated modules',
size: 10,
chunks: [1],
reasons: [
{
module: './index.js',
},
],
modules: [
{
name: './main.js',
size: 3,
reasons: [
{
module: './index.js',
},
],
},
{
name: './main-dependency-1.js',
Expand Down
10 changes: 1 addition & 9 deletions packages/plugin-webpack-validate/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,7 @@ export default (webpackSource?: any): string => {
const output = [`${I18N.INVALID}`];

failures.forEach((failure) => {
const failureOutput = ['', failure.message, `Path: ${failure.path.join('.')}`];

const failedSource = extractFailedStructure(webpackSource, failure.path);

if (failedSource) {
failureOutput.push(
`Failed structure(${failedSource.path}): ${JSON.stringify(failedSource.source, null, 2)}`,
);
}
const failureOutput = ['', failure.message, `Path: ${failure.path.join('.')}`, failure.explanation];

output.push(failureOutput.join('\n'));
});
Expand Down
15 changes: 14 additions & 1 deletion packages/plugin-webpack-validate/src/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,24 @@ export const WebpackSourceAssetHiddenStruct = type({
size: number(),
});

export const WebpackSourceModuleReasonStruct = type({
module: optional(nullable(string())),
});

export const WebpackSourceModuleStruct = type({
name: string(),
size: number(),
chunks: array(nullable(union([number(), string()]))),
modules: optional(array(type({ name: string(), size: number() }))),
modules: optional(
array(
type({
name: string(),
size: number(),
reasons: optional(array(WebpackSourceModuleReasonStruct)),
}),
),
),
reasons: optional(array(WebpackSourceModuleReasonStruct)),
});

export const WebpackSourceModuleHiddenStruct = type({
Expand Down
5 changes: 5 additions & 0 deletions packages/utils/__fixtures__/webpack-stats-1.extracted.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ module.exports = {
chunks: [1],
name: 'node_modules/package-a/node_modules/package-c/index.js',
size: 1000,
reasons: [
{
module: 'node_modules/package-a/index.js',
},
],
},
{
chunks: [1],
Expand Down
14 changes: 14 additions & 0 deletions packages/utils/__fixtures__/webpack-stats-1.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,20 @@ module.exports = {
size: 1000,
usedExports: true,
warnings: 0,
reasons: [
{
moduleId: 10,
moduleIdentifier: '/usr/share/app/node_modules/package-a/index.js',
module: 'node_modules/package-a/index.js',
userLoc: '1:0-20',
},
{
moduleId: 10,
moduleIdentifier: '/usr/share/app/node_modules/package-a/index.js',
module: 'node_modules/package-a/index.js',
userLoc: '3:0-20',
},
],
},
{
assets: [],
Expand Down
16 changes: 16 additions & 0 deletions packages/utils/src/jobs/__tests__/__snapshots__/create-job.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ exports[`createJob no baseline 1`] = `
"1",
],
"name": "node_modules/package-a/node_modules/package-c/index.js",
"reasons": [
"node_modules/package-a/index.js",
],
"value": 1000,
},
"node_modules/package-b/index.js": {
Expand Down Expand Up @@ -293,6 +296,11 @@ exports[`createJob no baseline 1`] = `
1,
],
"name": "node_modules/package-a/node_modules/package-c/index.js",
"reasons": [
{
"module": "node_modules/package-a/index.js",
},
],
"size": 1000,
},
{
Expand Down Expand Up @@ -516,6 +524,9 @@ exports[`createJob with baseline 1`] = `
"1",
],
"name": "node_modules/package-a/node_modules/package-c/index.js",
"reasons": [
"node_modules/package-a/index.js",
],
"value": 1000,
},
"node_modules/package-b/index.js": {
Expand Down Expand Up @@ -672,6 +683,11 @@ exports[`createJob with baseline 1`] = `
1,
],
"name": "node_modules/package-a/node_modules/package-c/index.js",
"reasons": [
{
"module": "node_modules/package-a/index.js",
},
],
"size": 1000,
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,9 @@ exports[`report / createReport multiple sources 1`] = `
"displayDeltaPercentage": "+100%",
"displayValue": "1000B",
"name": "node_modules/package-a/node_modules/package-c/index.js",
"reasons": [
"node_modules/package-a/index.js",
],
"value": 1000,
},
{
Expand Down Expand Up @@ -1195,6 +1198,9 @@ exports[`report / createReport single source 1`] = `
],
"displayValue": "1000B",
"name": "node_modules/package-a/node_modules/package-c/index.js",
"reasons": [
"node_modules/package-a/index.js",
],
"value": 1000,
},
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ exports[`Webpack/compare should compare all modules 1`] = `
"displayDeltaPercentage": "+100%",
"displayValue": "1000B",
"name": "node_modules/package-a/node_modules/package-c/index.js",
"reasons": [
"node_modules/package-a/index.js",
],
"value": 1000,
},
{
Expand Down
1 change: 1 addition & 0 deletions packages/utils/src/webpack/extract/__tests__/modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ describe('Webpack/extract/modules', () => {
name: 'node_modules/package-a/node_modules/package-c/index.js',
value: 1000,
chunkIds: ['1'],
reasons: ['node_modules/package-a/index.js'],
},
'node_modules/package-b/index.js': {
name: 'node_modules/package-b/index.js',
Expand Down
8 changes: 8 additions & 0 deletions packages/utils/src/webpack/extract/modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ export const extractModules = (webpackStats?: WebpackStatsFiltered): MetricsModu

modulesByName.forEach((moduleEntry, normalizedName) => {
const { name, size = 0, chunks } = moduleEntry;
const normalizedName = getModuleName(name);

// skip modules that are orphane(do not belong to any chunk)
if (!chunks || chunks?.length === 0) {
return agg;
}

const instances = chunks.length;
const duplicateInstances = instances - 1;
Expand All @@ -92,6 +98,7 @@ export const extractModules = (webpackStats?: WebpackStatsFiltered): MetricsModu
moduleCount += instances;
totalCodeSize += instances * size;

const reasons = moduleEntry.reasons?.map((reason) => getModuleName(reason.module));
if (duplicated) {
duplicateModulesCount += duplicateInstances;
duplicateCodeSize += duplicateInstances * size;
Expand All @@ -102,6 +109,7 @@ export const extractModules = (webpackStats?: WebpackStatsFiltered): MetricsModu
value: size,
chunkIds: chunks.map(normalizeChunkId),
...(duplicated && { duplicated }),
...(reasons && { reasons }),
};
}, {} as Record<string, Module>);

Expand Down
1 change: 1 addition & 0 deletions packages/utils/src/webpack/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export interface Module extends MetricRun {
name: string;
chunkIds: Array<string>;
duplicated?: boolean;
reasons?: Array<string>;
}

export type Modules = Record<string, Module>;
Expand Down

0 comments on commit dede7c2

Please sign in to comment.