File size: 1,636 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { nextTestSetup } from 'e2e-utils'

describe('app-dir build size', () => {
  const { next, isNextStart, skipped } = nextTestSetup({
    files: __dirname,
    skipDeployment: true,
  })

  if (skipped) {
    return
  }

  if (isNextStart) {
    it('should have correct size in build output', async () => {
      const regex = /(\S+)\s+([\d.]+\s\w+)\s+([\d.]+\s\w+)/g
      const matches = [...next.cliOutput.matchAll(regex)]

      const result = matches.reduce((acc, match) => {
        const [, path, size, firstLoadJS] = match

        acc[path] = { size, firstLoadJS }
        return acc
      }, {})

      // convert pretty-bytes format into bytes so we can compare units
      const sizeToBytes = (size: string) => {
        const units = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
        const [value, unit] = size.split(' ', 2)
        const exp = units.indexOf(unit)
        return parseFloat(value) * Math.pow(1024, exp)
      }

      const index = result['/']
      const foo = result['/foo']

      expect(sizeToBytes(index.firstLoadJS)).toBeGreaterThan(0)
      expect(sizeToBytes(foo.firstLoadJS)).toBeGreaterThan(0)

      // index route is a page with no client JS, so it could serve zero additional JS (size = 0)
      // foo route is a page with client references, so it has to serve additional non-shared JS
      expect(sizeToBytes(foo.size)).toBeGreaterThan(0)

      // foo has a client component, so it should be larger than index
      expect(sizeToBytes(foo.size)).toBeGreaterThan(sizeToBytes(index.size))
    })
  } else {
    it('should skip next dev for now', () => {})
    return
  }
})