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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

filter out assets hidden by Webpack #411

Merged
merged 1 commit into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions __tests__/__mocks__/new-hidden-stats-assets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"assets": [
{
"type": "hidden assets",
"filteredChildren": 5,
"size": 100500
}
]
}
4 changes: 4 additions & 0 deletions __tests__/__mocks__/old-hidden-stats-assets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"assets": [
]
}
30 changes: 30 additions & 0 deletions __tests__/__snapshots__/main.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,36 @@ app.bundle.js | 1.29 MB -> 1.04 MB (-254.35 KB)<br />N/A -> 297.38 KB (gzip) | -
No assets were unchanged"
`;

exports[`Shows stats when files are hidden 1`] = `
"**Total**

Files count | Total bundle size | % Changed
----------- | ----------------- | ---------
0 | 0 B | 0%"
`;

exports[`Shows stats when files are hidden 2`] = `
"**Added**

No assets were added

**Removed**

No assets were removed

**Bigger**

No assets were bigger

**Smaller**

No assets were smaller

**Unchanged**

No assets were unchanged"
`;

exports[`Shows stats when files are removed 1`] = `
"**Total**

Expand Down
10 changes: 10 additions & 0 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ test('Shows stats when files are unchanged', async () => {
expect(printAssetTablesByGroup(statsDiff)).toMatchSnapshot()
})

test('Shows stats when files are hidden', async () => {
const statsDiff = getStatsDiff(
await readJsonFile('./__mocks__/old-hidden-stats-assets.json'),
await readJsonFile('./__mocks__/new-hidden-stats-assets.json')
)

expect(printTotalAssetTable(statsDiff)).toMatchSnapshot()
expect(printAssetTablesByGroup(statsDiff)).toMatchSnapshot()
})

test('computes the correct module diff information', async () => {
const statsDiff = getChunkModuleDiff(
await readJsonFile('./__mocks__/old-stats-with-chunks.json'),
Expand Down
5 changes: 4 additions & 1 deletion dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

37 changes: 20 additions & 17 deletions src/name-to-size-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,28 @@ export function assetNameToSizeMap(
statAssets: StatsCompilation['assets'] = []
): Map<string, Sizes> {
return new Map(
statAssets.map(asset => {
let gzipSize: number | null = null
if (asset.related && Array.isArray(asset.related)) {
const gzipAsset = asset.related.find(
related => related.type === 'gzipped'
)
if (gzipAsset) {
gzipSize = gzipAsset.size
statAssets
// when Webpack's stats.excludeAssets is used, assets which are excluded will be grouped into an asset with type 'hidden assets'
.filter(it => !!it.name && it.type !== 'hidden assets')
.map(asset => {
let gzipSize: number | null = null
if (asset.related && Array.isArray(asset.related)) {
const gzipAsset = asset.related.find(
related => related.type === 'gzipped'
)
if (gzipAsset) {
gzipSize = gzipAsset.size
}
}
}

return [
asset.name,
{
size: asset.size,
gzipSize
}
]
})
return [
asset.name,
{
size: asset.size,
gzipSize
}
]
})
)
}

Expand Down