Unverified Commit 1d643eb6 authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

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
parent a77f3845
...@@ -53,7 +53,13 @@ export function createPagesMapping( ...@@ -53,7 +53,13 @@ export function createPagesMapping(
} }
export type WebpackEntrypoints = { export type WebpackEntrypoints = {
[bundle: string]: string | string[] [bundle: string]:
| string
| string[]
| {
import: string | string[]
dependOn: string | string[]
}
} }
type Entrypoints = { type Entrypoints = {
......
...@@ -469,7 +469,13 @@ export default async function getBaseWebpackConfig( ...@@ -469,7 +469,13 @@ export default async function getBaseWebpackConfig(
}, },
}, },
prodGranular: { prodGranular: {
chunks: 'all', // Keep main and _app chunks unsplitted in webpack 5
// as we don't need a separate vendor chunk from that
// and all other chunk depend on them so there is no
// duplication that need to be pulled out.
chunks: isWebpack5
? (chunk) => !/^(main|pages\/_app)$/.test(chunk.name)
: 'all',
cacheGroups: { cacheGroups: {
framework: { framework: {
chunks: 'all', chunks: 'all',
...@@ -851,6 +857,7 @@ export default async function getBaseWebpackConfig( ...@@ -851,6 +857,7 @@ export default async function getBaseWebpackConfig(
setImmediate: false, setImmediate: false,
}, },
// Kept as function to be backwards compatible // Kept as function to be backwards compatible
// @ts-ignore TODO webpack 5 typings needed
entry: async () => { entry: async () => {
return { return {
...(clientEntries ? clientEntries : {}), ...(clientEntries ? clientEntries : {}),
...@@ -1602,15 +1609,22 @@ export default async function getBaseWebpackConfig( ...@@ -1602,15 +1609,22 @@ export default async function getBaseWebpackConfig(
} }
// Backwards compat for `main.js` entry key // Backwards compat for `main.js` entry key
// and setup of dependencies between entries
// we can't do that in the initial entry for
// backward-compat reasons
const originalEntry: any = webpackConfig.entry const originalEntry: any = webpackConfig.entry
if (typeof originalEntry !== 'undefined') { if (typeof originalEntry !== 'undefined') {
webpackConfig.entry = async () => { const updatedEntry = async () => {
const entry: WebpackEntrypoints = const entry: WebpackEntrypoints =
typeof originalEntry === 'function' typeof originalEntry === 'function'
? await originalEntry() ? await originalEntry()
: originalEntry : originalEntry
// Server compilation doesn't have main.js // Server compilation doesn't have main.js
if (clientEntries && entry['main.js'] && entry['main.js'].length > 0) { if (
clientEntries &&
Array.isArray(entry['main.js']) &&
entry['main.js'].length > 0
) {
const originalFile = clientEntries[ const originalFile = clientEntries[
CLIENT_STATIC_FILES_RUNTIME_MAIN CLIENT_STATIC_FILES_RUNTIME_MAIN
] as string ] as string
...@@ -1621,8 +1635,33 @@ export default async function getBaseWebpackConfig( ...@@ -1621,8 +1635,33 @@ export default async function getBaseWebpackConfig(
} }
delete entry['main.js'] delete entry['main.js']
if (isWebpack5 && !isServer) {
for (const name of Object.keys(entry)) {
if (name === 'main' || name === 'amp' || name === 'react-refresh')
continue
const dependOn =
name.startsWith('pages/') && name !== 'pages/_app'
? 'pages/_app'
: 'main'
const old = entry[name]
if (typeof old === 'object' && !Array.isArray(old)) {
entry[name] = {
dependOn,
...old,
}
} else {
entry[name] = {
import: old,
dependOn,
}
}
}
}
return entry return entry
} }
// @ts-ignore webpack 5 typings needed
webpackConfig.entry = updatedEntry
} }
if (!dev) { if (!dev) {
......
...@@ -29,9 +29,9 @@ ...@@ -29,9 +29,9 @@
integrity sha512-cxWFQVseBm6O9Gbw1IWb8r6OS4OhSt3hPZLkFApLjM8TEXROBuQGLAH2i2gZpcXdLBIrpXuTDhH7Vbm1iXmNGA== integrity sha512-cxWFQVseBm6O9Gbw1IWb8r6OS4OhSt3hPZLkFApLjM8TEXROBuQGLAH2i2gZpcXdLBIrpXuTDhH7Vbm1iXmNGA==
"@types/node@*": "@types/node@*":
version "14.14.41" version "15.0.1"
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.14.41.tgz#d0b939d94c1d7bd53d04824af45f1139b8c45615" resolved "https://registry.yarnpkg.com/@types/node/-/node-15.0.1.tgz#ef34dea0881028d11398be5bf4e856743e3dc35a"
integrity sha512-dueRKfaJL4RTtSa7bWeTK1M+VH+Gns73oCgzvYfHZywRCoPSd8EkXBL0mZ9unPTveBn+D9phZBaxuzpwjWkW0g== integrity sha512-TMkXt0Ck1y0KKsGr9gJtWGjttxlZnnvDtphxUOSd0bfaR6Q1jle+sPvrzNR1urqYTWMinoKvjKfXUGsumaO1PA==
"@webassemblyjs/ast@1.11.0": "@webassemblyjs/ast@1.11.0":
version "1.11.0" version "1.11.0"
...@@ -164,10 +164,10 @@ ...@@ -164,10 +164,10 @@
resolved "https://registry.yarnpkg.com/@xtuc/long/-/long-4.2.2.tgz#d291c6a4e97989b5c61d9acf396ae4fe133a718d" resolved "https://registry.yarnpkg.com/@xtuc/long/-/long-4.2.2.tgz#d291c6a4e97989b5c61d9acf396ae4fe133a718d"
integrity sha512-NuHqBY1PB/D8xU6s/thBgOAiAP7HOYDQ32+BFZILJ8ivkUkAHQnWfn6WhL79Owj1qmUnoN/YPhktdIoucipkAQ== integrity sha512-NuHqBY1PB/D8xU6s/thBgOAiAP7HOYDQ32+BFZILJ8ivkUkAHQnWfn6WhL79Owj1qmUnoN/YPhktdIoucipkAQ==
acorn@^8.0.4: acorn@^8.2.1:
version "8.1.1" version "8.2.2"
resolved "https://registry.yarnpkg.com/acorn/-/acorn-8.1.1.tgz#fb0026885b9ac9f48bac1e185e4af472971149ff" resolved "https://registry.yarnpkg.com/acorn/-/acorn-8.2.2.tgz#c4574e4fea298d6e6ed4b85ab844b06dd59f26d6"
integrity sha512-xYiIVjNuqtKXMxlRMDc6mZUhXehod4a3gbZ1qRlM7icK4EbxUFNLhWoPblCvFtB2Y9CIqHP3CF/rdxLItaQv8g== integrity sha512-VrMS8kxT0e7J1EX0p6rI/E0FbfOVcvBpbIqHThFv+f8YrZIlMfVotYcXKVPmTvPW8sW5miJzfUFrrvthUZg8VQ==
ajv-keywords@^3.5.2: ajv-keywords@^3.5.2:
version "3.5.2" version "3.5.2"
...@@ -221,9 +221,9 @@ commander@^2.20.0: ...@@ -221,9 +221,9 @@ commander@^2.20.0:
integrity sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ== integrity sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==
electron-to-chromium@^1.3.634: electron-to-chromium@^1.3.634:
version "1.3.718" version "1.3.723"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.718.tgz#a192981ced608978410ebc011e24ecab1bb4beb3" resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.723.tgz#52769a75635342a4db29af5f1e40bd3dad02c877"
integrity sha512-CikzdUSShGXwjq1pcW740wK8j+KbazgHZiwzlHICejDaczM6OVsPcrZmBHPwzj9i2rj5twg20MBwp+cYZwldYA== integrity sha512-L+WXyXI7c7+G1V8ANzRsPI5giiimLAUDC6Zs1ojHHPhYXb3k/iTABFmWjivEtsWrRQymjnO66/rO2ZTABGdmWg==
enhanced-resolve@^5.8.0: enhanced-resolve@^5.8.0:
version "5.8.0" version "5.8.0"
...@@ -438,9 +438,9 @@ terser-webpack-plugin@^5.1.1: ...@@ -438,9 +438,9 @@ terser-webpack-plugin@^5.1.1:
terser "^5.5.1" terser "^5.5.1"
terser@^5.5.1: terser@^5.5.1:
version "5.6.1" version "5.7.0"
resolved "https://registry.yarnpkg.com/terser/-/terser-5.6.1.tgz#a48eeac5300c0a09b36854bf90d9c26fb201973c" resolved "https://registry.yarnpkg.com/terser/-/terser-5.7.0.tgz#a761eeec206bc87b605ab13029876ead938ae693"
integrity sha512-yv9YLFQQ+3ZqgWCUk+pvNJwgUTdlIxUk1WTN+RnaFJe2L7ipG2csPT0ra2XRm7Cs8cxN7QXmK1rFzEwYEQkzXw== integrity sha512-HP5/9hp2UaZt5fYkuhNBR8YyRcT8juw8+uFbAme53iN9hblvKnLUTKkmwJG6ocWpIKf8UK4DoeWG4ty0J6S6/g==
dependencies: dependencies:
commander "^2.20.0" commander "^2.20.0"
source-map "~0.7.2" source-map "~0.7.2"
...@@ -471,16 +471,16 @@ watchpack@^2.0.0: ...@@ -471,16 +471,16 @@ watchpack@^2.0.0:
source-map "^0.6.1" source-map "^0.6.1"
"webpack5@npm:webpack@5": "webpack5@npm:webpack@5":
version "5.35.0" version "5.36.2"
resolved "https://registry.yarnpkg.com/webpack/-/webpack-5.35.0.tgz#4db23c2b96c4e53a90c5732d7cdb301a84a33576" resolved "https://registry.yarnpkg.com/webpack/-/webpack-5.36.2.tgz#6ef1fb2453ad52faa61e78d486d353d07cca8a0f"
integrity sha512-au3gu55yYF/h6NXFr0KZPZAYxS6Nlc595BzYPke8n0CSff5WXcoixtjh5LC/8mXunkRKxhymhXmBY0+kEbR6jg== integrity sha512-XJumVnnGoH2dV+Pk1VwgY4YT6AiMKpVoudUFCNOXMIVrEKPUgEwdIfWPjIuGLESAiS8EdIHX5+TiJz/5JccmRg==
dependencies: dependencies:
"@types/eslint-scope" "^3.7.0" "@types/eslint-scope" "^3.7.0"
"@types/estree" "^0.0.47" "@types/estree" "^0.0.47"
"@webassemblyjs/ast" "1.11.0" "@webassemblyjs/ast" "1.11.0"
"@webassemblyjs/wasm-edit" "1.11.0" "@webassemblyjs/wasm-edit" "1.11.0"
"@webassemblyjs/wasm-parser" "1.11.0" "@webassemblyjs/wasm-parser" "1.11.0"
acorn "^8.0.4" acorn "^8.2.1"
browserslist "^4.14.5" browserslist "^4.14.5"
chrome-trace-event "^1.0.2" chrome-trace-event "^1.0.2"
enhanced-resolve "^5.8.0" enhanced-resolve "^5.8.0"
......
This diff is collapsed.
...@@ -99,16 +99,16 @@ describe('Build Output', () => { ...@@ -99,16 +99,16 @@ describe('Build Output', () => {
expect(indexSize.endsWith('B')).toBe(true) expect(indexSize.endsWith('B')).toBe(true)
// should be no bigger than 64.8 kb // should be no bigger than 64.8 kb
expect(parseFloat(indexFirstLoad)).toBeCloseTo(65.3, 1) expect(parseFloat(indexFirstLoad)).toBeCloseTo(63.5, 1)
expect(indexFirstLoad.endsWith('kB')).toBe(true) expect(indexFirstLoad.endsWith('kB')).toBe(true)
expect(parseFloat(err404Size)).toBeCloseTo(3.69, 1) expect(parseFloat(err404Size)).toBeCloseTo(3.04, 1)
expect(err404Size.endsWith('kB')).toBe(true) expect(err404Size.endsWith('kB')).toBe(true)
expect(parseFloat(err404FirstLoad)).toBeCloseTo(68.8, 0) expect(parseFloat(err404FirstLoad)).toBeCloseTo(66.3, 0)
expect(err404FirstLoad.endsWith('kB')).toBe(true) expect(err404FirstLoad.endsWith('kB')).toBe(true)
expect(parseFloat(sharedByAll)).toBeCloseTo(65, 1) expect(parseFloat(sharedByAll)).toBeCloseTo(63.3, 1)
expect(sharedByAll.endsWith('kB')).toBe(true) expect(sharedByAll.endsWith('kB')).toBe(true)
if (_appSize.endsWith('kB')) { if (_appSize.endsWith('kB')) {
...@@ -119,10 +119,10 @@ describe('Build Output', () => { ...@@ -119,10 +119,10 @@ describe('Build Output', () => {
expect(_appSize.endsWith(' B')).toBe(true) expect(_appSize.endsWith(' B')).toBe(true)
} }
expect(parseFloat(webpackSize) - 950).toBeLessThanOrEqual(0) expect(parseFloat(webpackSize) - 952).toBeLessThanOrEqual(0)
expect(webpackSize.endsWith(' B')).toBe(true) expect(webpackSize.endsWith(' B')).toBe(true)
expect(parseFloat(mainSize) - 7.3).toBeLessThanOrEqual(0) expect(parseFloat(mainSize) - 19.3).toBeLessThanOrEqual(0)
expect(mainSize.endsWith('kB')).toBe(true) expect(mainSize.endsWith('kB')).toBe(true)
expect(parseFloat(frameworkSize) - 42.1).toBeLessThanOrEqual(0) expect(parseFloat(frameworkSize) - 42.1).toBeLessThanOrEqual(0)
......
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