Skip to content

Commit 1d643eb

Browse files
authored
update webpack to 5.36.2, use dependOn (#24656)
* update webpack to 5.36.2 * use dependOn to optimize chunks * disable vendor splitting for main and _app chunks => decreases bundle size in general, gets rid of the 2 extra vendor chunks shared by all pages. ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. ## Documentation / Examples - [ ] Make sure the linting passes
1 parent a77f384 commit 1d643eb

5 files changed

Lines changed: 1058 additions & 425 deletions

File tree

‎packages/next/build/entries.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,13 @@ export function createPagesMapping(
5353
}
5454

5555
export type WebpackEntrypoints = {
56-
[bundle: string]: string | string[]
56+
[bundle: string]:
57+
| string
58+
| string[]
59+
| {
60+
import: string | string[]
61+
dependOn: string | string[]
62+
}
5763
}
5864

5965
type Entrypoints = {

‎packages/next/build/webpack-config.ts‎

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,13 @@ export default async function getBaseWebpackConfig(
469469
},
470470
},
471471
prodGranular: {
472-
chunks: 'all',
472+
// Keep main and _app chunks unsplitted in webpack 5
473+
// as we don't need a separate vendor chunk from that
474+
// and all other chunk depend on them so there is no
475+
// duplication that need to be pulled out.
476+
chunks: isWebpack5
477+
? (chunk) => !/^(main|pages\/_app)$/.test(chunk.name)
478+
: 'all',
473479
cacheGroups: {
474480
framework: {
475481
chunks: 'all',
@@ -851,6 +857,7 @@ export default async function getBaseWebpackConfig(
851857
setImmediate: false,
852858
},
853859
// Kept as function to be backwards compatible
860+
// @ts-ignore TODO webpack 5 typings needed
854861
entry: async () => {
855862
return {
856863
...(clientEntries ? clientEntries : {}),
@@ -1602,15 +1609,22 @@ export default async function getBaseWebpackConfig(
16021609
}
16031610

16041611
// Backwards compat for `main.js` entry key
1612+
// and setup of dependencies between entries
1613+
// we can't do that in the initial entry for
1614+
// backward-compat reasons
16051615
const originalEntry: any = webpackConfig.entry
16061616
if (typeof originalEntry !== 'undefined') {
1607-
webpackConfig.entry = async () => {
1617+
const updatedEntry = async () => {
16081618
const entry: WebpackEntrypoints =
16091619
typeof originalEntry === 'function'
16101620
? await originalEntry()
16111621
: originalEntry
16121622
// Server compilation doesn't have main.js
1613-
if (clientEntries && entry['main.js'] && entry['main.js'].length > 0) {
1623+
if (
1624+
clientEntries &&
1625+
Array.isArray(entry['main.js']) &&
1626+
entry['main.js'].length > 0
1627+
) {
16141628
const originalFile = clientEntries[
16151629
CLIENT_STATIC_FILES_RUNTIME_MAIN
16161630
] as string
@@ -1621,8 +1635,33 @@ export default async function getBaseWebpackConfig(
16211635
}
16221636
delete entry['main.js']
16231637

1638+
if (isWebpack5 && !isServer) {
1639+
for (const name of Object.keys(entry)) {
1640+
if (name === 'main' || name === 'amp' || name === 'react-refresh')
1641+
continue
1642+
const dependOn =
1643+
name.startsWith('pages/') && name !== 'pages/_app'
1644+
? 'pages/_app'
1645+
: 'main'
1646+
const old = entry[name]
1647+
if (typeof old === 'object' && !Array.isArray(old)) {
1648+
entry[name] = {
1649+
dependOn,
1650+
...old,
1651+
}
1652+
} else {
1653+
entry[name] = {
1654+
import: old,
1655+
dependOn,
1656+
}
1657+
}
1658+
}
1659+
}
1660+
16241661
return entry
16251662
}
1663+
// @ts-ignore webpack 5 typings needed
1664+
webpackConfig.entry = updatedEntry
16261665
}
16271666

16281667
if (!dev) {

‎packages/next/bundles/yarn.lock‎

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929
integrity sha512-cxWFQVseBm6O9Gbw1IWb8r6OS4OhSt3hPZLkFApLjM8TEXROBuQGLAH2i2gZpcXdLBIrpXuTDhH7Vbm1iXmNGA==
3030

3131
"@types/node@*":
32-
version "14.14.41"
33-
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.14.41.tgz#d0b939d94c1d7bd53d04824af45f1139b8c45615"
34-
integrity sha512-dueRKfaJL4RTtSa7bWeTK1M+VH+Gns73oCgzvYfHZywRCoPSd8EkXBL0mZ9unPTveBn+D9phZBaxuzpwjWkW0g==
32+
version "15.0.1"
33+
resolved "https://registry.yarnpkg.com/@types/node/-/node-15.0.1.tgz#ef34dea0881028d11398be5bf4e856743e3dc35a"
34+
integrity sha512-TMkXt0Ck1y0KKsGr9gJtWGjttxlZnnvDtphxUOSd0bfaR6Q1jle+sPvrzNR1urqYTWMinoKvjKfXUGsumaO1PA==
3535

3636
"@webassemblyjs/[email protected]":
3737
version "1.11.0"
@@ -164,10 +164,10 @@
164164
resolved "https://registry.yarnpkg.com/@xtuc/long/-/long-4.2.2.tgz#d291c6a4e97989b5c61d9acf396ae4fe133a718d"
165165
integrity sha512-NuHqBY1PB/D8xU6s/thBgOAiAP7HOYDQ32+BFZILJ8ivkUkAHQnWfn6WhL79Owj1qmUnoN/YPhktdIoucipkAQ==
166166

167-
acorn@^8.0.4:
168-
version "8.1.1"
169-
resolved "https://registry.yarnpkg.com/acorn/-/acorn-8.1.1.tgz#fb0026885b9ac9f48bac1e185e4af472971149ff"
170-
integrity sha512-xYiIVjNuqtKXMxlRMDc6mZUhXehod4a3gbZ1qRlM7icK4EbxUFNLhWoPblCvFtB2Y9CIqHP3CF/rdxLItaQv8g==
167+
acorn@^8.2.1:
168+
version "8.2.2"
169+
resolved "https://registry.yarnpkg.com/acorn/-/acorn-8.2.2.tgz#c4574e4fea298d6e6ed4b85ab844b06dd59f26d6"
170+
integrity sha512-VrMS8kxT0e7J1EX0p6rI/E0FbfOVcvBpbIqHThFv+f8YrZIlMfVotYcXKVPmTvPW8sW5miJzfUFrrvthUZg8VQ==
171171

172172
ajv-keywords@^3.5.2:
173173
version "3.5.2"
@@ -221,9 +221,9 @@ commander@^2.20.0:
221221
integrity sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==
222222

223223
electron-to-chromium@^1.3.634:
224-
version "1.3.718"
225-
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.718.tgz#a192981ced608978410ebc011e24ecab1bb4beb3"
226-
integrity sha512-CikzdUSShGXwjq1pcW740wK8j+KbazgHZiwzlHICejDaczM6OVsPcrZmBHPwzj9i2rj5twg20MBwp+cYZwldYA==
224+
version "1.3.723"
225+
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.723.tgz#52769a75635342a4db29af5f1e40bd3dad02c877"
226+
integrity sha512-L+WXyXI7c7+G1V8ANzRsPI5giiimLAUDC6Zs1ojHHPhYXb3k/iTABFmWjivEtsWrRQymjnO66/rO2ZTABGdmWg==
227227

228228
enhanced-resolve@^5.8.0:
229229
version "5.8.0"
@@ -438,9 +438,9 @@ terser-webpack-plugin@^5.1.1:
438438
terser "^5.5.1"
439439

440440
terser@^5.5.1:
441-
version "5.6.1"
442-
resolved "https://registry.yarnpkg.com/terser/-/terser-5.6.1.tgz#a48eeac5300c0a09b36854bf90d9c26fb201973c"
443-
integrity sha512-yv9YLFQQ+3ZqgWCUk+pvNJwgUTdlIxUk1WTN+RnaFJe2L7ipG2csPT0ra2XRm7Cs8cxN7QXmK1rFzEwYEQkzXw==
441+
version "5.7.0"
442+
resolved "https://registry.yarnpkg.com/terser/-/terser-5.7.0.tgz#a761eeec206bc87b605ab13029876ead938ae693"
443+
integrity sha512-HP5/9hp2UaZt5fYkuhNBR8YyRcT8juw8+uFbAme53iN9hblvKnLUTKkmwJG6ocWpIKf8UK4DoeWG4ty0J6S6/g==
444444
dependencies:
445445
commander "^2.20.0"
446446
source-map "~0.7.2"
@@ -471,16 +471,16 @@ watchpack@^2.0.0:
471471
source-map "^0.6.1"
472472

473473
"webpack5@npm:webpack@5":
474-
version "5.35.0"
475-
resolved "https://registry.yarnpkg.com/webpack/-/webpack-5.35.0.tgz#4db23c2b96c4e53a90c5732d7cdb301a84a33576"
476-
integrity sha512-au3gu55yYF/h6NXFr0KZPZAYxS6Nlc595BzYPke8n0CSff5WXcoixtjh5LC/8mXunkRKxhymhXmBY0+kEbR6jg==
474+
version "5.36.2"
475+
resolved "https://registry.yarnpkg.com/webpack/-/webpack-5.36.2.tgz#6ef1fb2453ad52faa61e78d486d353d07cca8a0f"
476+
integrity sha512-XJumVnnGoH2dV+Pk1VwgY4YT6AiMKpVoudUFCNOXMIVrEKPUgEwdIfWPjIuGLESAiS8EdIHX5+TiJz/5JccmRg==
477477
dependencies:
478478
"@types/eslint-scope" "^3.7.0"
479479
"@types/estree" "^0.0.47"
480480
"@webassemblyjs/ast" "1.11.0"
481481
"@webassemblyjs/wasm-edit" "1.11.0"
482482
"@webassemblyjs/wasm-parser" "1.11.0"
483-
acorn "^8.0.4"
483+
acorn "^8.2.1"
484484
browserslist "^4.14.5"
485485
chrome-trace-event "^1.0.2"
486486
enhanced-resolve "^5.8.0"

‎packages/next/compiled/webpack/bundle5.js‎

Lines changed: 986 additions & 398 deletions
Large diffs are not rendered by default.

‎test/integration/build-output/test/index.test.js‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,16 @@ describe('Build Output', () => {
9999
expect(indexSize.endsWith('B')).toBe(true)
100100

101101
// should be no bigger than 64.8 kb
102-
expect(parseFloat(indexFirstLoad)).toBeCloseTo(65.3, 1)
102+
expect(parseFloat(indexFirstLoad)).toBeCloseTo(63.5, 1)
103103
expect(indexFirstLoad.endsWith('kB')).toBe(true)
104104

105-
expect(parseFloat(err404Size)).toBeCloseTo(3.69, 1)
105+
expect(parseFloat(err404Size)).toBeCloseTo(3.04, 1)
106106
expect(err404Size.endsWith('kB')).toBe(true)
107107

108-
expect(parseFloat(err404FirstLoad)).toBeCloseTo(68.8, 0)
108+
expect(parseFloat(err404FirstLoad)).toBeCloseTo(66.3, 0)
109109
expect(err404FirstLoad.endsWith('kB')).toBe(true)
110110

111-
expect(parseFloat(sharedByAll)).toBeCloseTo(65, 1)
111+
expect(parseFloat(sharedByAll)).toBeCloseTo(63.3, 1)
112112
expect(sharedByAll.endsWith('kB')).toBe(true)
113113

114114
if (_appSize.endsWith('kB')) {
@@ -119,10 +119,10 @@ describe('Build Output', () => {
119119
expect(_appSize.endsWith(' B')).toBe(true)
120120
}
121121

122-
expect(parseFloat(webpackSize) - 950).toBeLessThanOrEqual(0)
122+
expect(parseFloat(webpackSize) - 952).toBeLessThanOrEqual(0)
123123
expect(webpackSize.endsWith(' B')).toBe(true)
124124

125-
expect(parseFloat(mainSize) - 7.3).toBeLessThanOrEqual(0)
125+
expect(parseFloat(mainSize) - 19.3).toBeLessThanOrEqual(0)
126126
expect(mainSize.endsWith('kB')).toBe(true)
127127

128128
expect(parseFloat(frameworkSize) - 42.1).toBeLessThanOrEqual(0)

0 commit comments

Comments
 (0)