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

Report on initial chunks #462

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
8 changes: 8 additions & 0 deletions __tests__/__snapshots__/main.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Shows initial stats for builds with async chunks 1`] = `
"**Total**

Files count | Total bundle size | % Changed
----------- | ----------------- | ---------
7 -> 2 | 1.34 MB -> 1.29 MB (-53.65 KB)<br />386.44 KB -> N/A (gzip) | -3.91%"
`;

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

Expand Down
17 changes: 17 additions & 0 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,23 @@ test('computes the correct module diff information', async () => {
expect(statsDiff?.total.new).toEqual(statsDiff?.total.old)
expect(statsDiff?.total.diff).toEqual(0)
expect(statsDiff?.total.diffPercentage).toEqual(0)
// No async chunks results in no initial stats being reported
expect(statsDiff?.initial).toBe(undefined)

// TODO(mariaines): add test case for initial stats for a build with async chunks
})

// TODO(mariaines): update this snapshot for testing initial stats
test('Shows initial stats for builds with async chunks', async () => {
const statsDiff = getStatsDiff(
await readJsonFile('./__mocks__/old-stats-assets.json'),
await readJsonFile('./__mocks__/new-stats-assets.json')
)
const chunkModuleDiff = getChunkModuleDiff(
await readJsonFile('./__mocks__/old-stats-with-chunks.json'),
await readJsonFile('./__mocks__/new-stats-with-chunks.json')
)
expect(printTotalAssetTable(statsDiff, chunkModuleDiff)).toMatchSnapshot()
})

test('displays module information when files are added/removed/changed', async () => {
Expand Down
65 changes: 57 additions & 8 deletions 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.

6 changes: 3 additions & 3 deletions src/get-chunk-module-diff.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type {StatsCompilation} from 'webpack'
import {chunkModuleNameToSizeMap} from './name-to-size-map'
import type {WebpackStatsDiff} from './types'
import type {WebpackStatsDiff, ChunkSizes} from './types'
import {webpackStatsDiff} from './webpack-stats-diff'

export function getChunkModuleDiff(
oldStats: Pick<StatsCompilation, 'chunks'>,
newStats: Pick<StatsCompilation, 'chunks'>
): WebpackStatsDiff | null {
): WebpackStatsDiff<ChunkSizes> | undefined {
if (!oldStats.chunks || !newStats.chunks) {
return null
return undefined
}
return webpackStatsDiff(
chunkModuleNameToSizeMap(oldStats.chunks),
Expand Down
4 changes: 2 additions & 2 deletions src/get-stats-diff.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type {StatsCompilation} from 'webpack'
import {assetNameToSizeMap} from './name-to-size-map'
import type {WebpackStatsDiff} from './types'
import type {WebpackStatsDiff, Sizes} from './types'
import {webpackStatsDiff} from './webpack-stats-diff'

export function getStatsDiff(
oldAssetStats: Pick<StatsCompilation, 'assets'>,
newAssetStats: Pick<StatsCompilation, 'assets'>
): WebpackStatsDiff {
): WebpackStatsDiff<Sizes> {
return webpackStatsDiff(
assetNameToSizeMap(oldAssetStats.assets),
assetNameToSizeMap(newAssetStats.assets)
Expand Down
6 changes: 4 additions & 2 deletions src/name-to-size-map.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {StatsCompilation} from 'webpack'
import type {Sizes} from './types'
import type {Sizes, ChunkSizes} from './types'

export function assetNameToSizeMap(
statAssets: StatsCompilation['assets'] = []
Expand Down Expand Up @@ -32,7 +32,7 @@ export function assetNameToSizeMap(

export function chunkModuleNameToSizeMap(
statChunks: StatsCompilation['chunks'] = []
): Map<string, Sizes> {
): Map<string, ChunkSizes> {
return new Map(
statChunks.flatMap(chunk => {
if (!chunk.modules) return []
Expand All @@ -43,6 +43,7 @@ export function chunkModuleNameToSizeMap(
return module.modules.map(submodule => [
submodule.name ?? '',
{
initial: chunk.initial,
size: submodule.size ?? 0,
gzipSize: null
}
Expand All @@ -52,6 +53,7 @@ export function chunkModuleNameToSizeMap(
[
module.name ?? '',
{
initial: chunk.initial,
size: module.size ?? 0,
gzipSize: null
}
Expand Down
19 changes: 13 additions & 6 deletions src/print-markdown.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {formatFileSizeIEC} from './file-sizes'
import type {AssetDiff, WebpackStatsDiff} from './types'
import type {AssetDiff, WebpackStatsDiff, Sizes, ChunkSizes} from './types'

function conditionalPercentage(number: number): string {
if ([Infinity, -Infinity].includes(number)) {
Expand Down Expand Up @@ -86,7 +86,7 @@ function printAssetTableRow(asset: AssetDiff): string {
}

export function printAssetTablesByGroup(
statsDiff: Omit<WebpackStatsDiff, 'total'>
statsDiff: Omit<WebpackStatsDiff<Sizes>, 'total'>
): string {
const statsFields = [
'added',
Expand Down Expand Up @@ -159,7 +159,7 @@ function printChunkModuleRow(chunkModule: AssetDiff): string {
}

export function printChunkModulesTable(
statsDiff: Omit<WebpackStatsDiff, 'total' | 'unchanged'> | null
statsDiff?: Omit<WebpackStatsDiff<Sizes>, 'total' | 'unchanged'>
): string {
if (!statsDiff) return ''
const changedModules = [
Expand Down Expand Up @@ -193,10 +193,17 @@ ${changedModules
}

export function printTotalAssetTable(
statsDiff: Pick<WebpackStatsDiff, 'total'>
totalStatsDiff: Pick<WebpackStatsDiff<Sizes>, 'total'>,
initialStatsDiff?: Pick<WebpackStatsDiff<ChunkSizes>, 'initial'>
): string {
return `**Total**
const result = `**Total**

${TOTAL_HEADERS}
${printAssetTableRow(statsDiff.total)}`
${printAssetTableRow(totalStatsDiff.total)}`
if (initialStatsDiff && initialStatsDiff.initial) {
return `${result}
${printAssetTableRow(initialStatsDiff.initial)}`
} else {
return result
}
}
8 changes: 4 additions & 4 deletions src/to-comment-body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import {
printChunkModulesTable,
printTotalAssetTable
} from './print-markdown'
import type {WebpackStatsDiff} from './types'
import type {WebpackStatsDiff, Sizes, ChunkSizes} from './types'

export function getIdentifierComment(key: string): string {
return `<!--- bundlestats-action-comment${key ? ` key:${key}` : ''} --->`
}

export function getCommentBody(
statsDiff: WebpackStatsDiff,
chunkModuleDiff: WebpackStatsDiff | null,
statsDiff: WebpackStatsDiff<Sizes>,
chunkModuleDiff: WebpackStatsDiff<ChunkSizes> | undefined,
title: string
): string {
return `
Expand All @@ -21,7 +21,7 @@ Hey there, this message comes from a [GitHub action](https://github.com/github/w

As this PR is updated, I'll keep you updated on how the bundle size is impacted.

${printTotalAssetTable(statsDiff)}
${printTotalAssetTable(statsDiff, chunkModuleDiff)}
${chunkModuleDiff ? `${printChunkModulesTable(chunkModuleDiff)}\n` : ''}
<details>
<summary>View detailed bundle breakdown</summary>
Expand Down
17 changes: 15 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,29 @@ export type AssetDiff = {
diffPercentage: number
}

export type Sizes = {
export interface Sizes {
size: number
gzipSize: number | null
}

export type WebpackStatsDiff = {
export interface ChunkSizes extends Sizes {
initial: boolean
}

export function isChunkSizes(sizes: Sizes): sizes is ChunkSizes {
return 'initial' in sizes
}

interface _WebpackStatsDiff {
added: AssetDiff[]
removed: AssetDiff[]
bigger: AssetDiff[]
smaller: AssetDiff[]
unchanged: AssetDiff[]
total: AssetDiff
initial?: AssetDiff
}

export type WebpackStatsDiff<T extends Sizes> = T extends ChunkSizes
? _WebpackStatsDiff
: Omit<_WebpackStatsDiff, 'initial'>
Loading