Unverified Commit e4bd06be authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Group small chunks in shared js section of output (#60479)

In the output shared js chunks section we're showing all shared chunks
atm, some of them might be too small (few bytes) due to code split. e.g.
in the below example, main-app is only `218B`, we're going to group all
the chunks below 10KB together into one item line

#### Before

```
+ First Load JS shared by all            81.9 kB
  ├ chunks/381-ffe155bc1c63f064.js       26.7 kB
  ├ chunks/8766613a-1f3d501627fe8359.js  53.3 kB
  ├ chunks/main-app-80d2c0fe59f30ae2.js  218 B
  └ chunks/webpack-e6edcd8ebd35b832.js   1.68 kB
```

#### After 
```
+ First Load JS shared by all            83.7 kB
  ├ chunks/4921c021-73266f862a05e4e2.js  53.3 kB
  ├ chunks/596-73310d23ef824244.js       28.6 kB
  └ other shared chunks (total)          1.83 kB
```

Closes NEXT-2046
Closes NEXT-1970
parent d6c754f3
...@@ -628,7 +628,7 @@ export async function printTreeView( ...@@ -628,7 +628,7 @@ export async function printTreeView(
'', '',
]) ])
const sharedCssFiles: string[] = [] const sharedCssFiles: string[] = []
;[ const sharedJsChunks = [
...sharedFiles ...sharedFiles
.filter((file) => { .filter((file) => {
if (file.endsWith('.css')) { if (file.endsWith('.css')) {
...@@ -640,19 +640,35 @@ export async function printTreeView( ...@@ -640,19 +640,35 @@ export async function printTreeView(
.map((e) => e.replace(buildId, '<buildId>')) .map((e) => e.replace(buildId, '<buildId>'))
.sort(), .sort(),
...sharedCssFiles.map((e) => e.replace(buildId, '<buildId>')).sort(), ...sharedCssFiles.map((e) => e.replace(buildId, '<buildId>')).sort(),
].forEach((fileName, index, { length }) => { ]
const innerSymbol = index === length - 1 ? '└' : '├'
// if the some chunk are less than 10kb or we don't know the size, we only show the total size of the rest
const tenKbLimit = 10 * 1000
let restChunkSize = 0
let restChunkCount = 0
sharedJsChunks.forEach((fileName, index, { length }) => {
const innerSymbol = index + restChunkCount === length - 1 ? '└' : '├'
const originalName = fileName.replace('<buildId>', buildId) const originalName = fileName.replace('<buildId>', buildId)
const cleanName = getCleanName(fileName) const cleanName = getCleanName(fileName)
const size = stats.sizes.get(originalName) const size = stats.sizes.get(originalName)
if (!size || size < tenKbLimit) {
restChunkCount++
restChunkSize += size || 0
return
}
messages.push([` ${innerSymbol} ${cleanName}`, prettyBytes(size), ''])
})
if (restChunkCount > 0) {
messages.push([ messages.push([
` ${innerSymbol} ${cleanName}`, ` └ other shared chunks (total)`,
typeof size === 'number' ? prettyBytes(size) : '', prettyBytes(restChunkSize),
'', '',
]) ])
}) }
} }
// If enabled, then print the tree for the app directory. // If enabled, then print the tree for the app directory.
......
import { createNextDescribe } from 'e2e-utils' import { createNextDescribe } from 'e2e-utils'
import stripAnsi from 'strip-ansi'
createNextDescribe( createNextDescribe(
'production - app dir - build output', 'production - app dir - build output',
...@@ -19,5 +20,16 @@ createNextDescribe( ...@@ -19,5 +20,16 @@ createNextDescribe(
expect(output).not.toContain('Route (pages)') expect(output).not.toContain('Route (pages)')
expect(output).not.toContain('/favicon.ico') expect(output).not.toContain('/favicon.ico')
}) })
it('should match the expected output format', async () => {
const output = stripAnsi(next.cliOutput)
expect(output).toContain('Size')
expect(output).toContain('First Load JS')
expect(output).toContain('+ First Load JS shared by all')
expect(output).toContain('└ other shared chunks (total)')
// output type
expect(output).toContain('○ (Static) prerendered as static content')
})
} }
) )
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment