Skip to content

Commit

Permalink
use dependOn to optimize chunks
Browse files Browse the repository at this point in the history
disable vendor splitting for main and _app chunks
  • Loading branch information
sokra committed May 1, 2021
1 parent 67206a6 commit 2eba039
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
8 changes: 7 additions & 1 deletion packages/next/build/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,13 @@ export function createPagesMapping(
}

export type WebpackEntrypoints = {
[bundle: string]: string | string[]
[bundle: string]:
| string
| string[]
| {
import: string | string[]
dependOn: string | string[]
}
}

type Entrypoints = {
Expand Down
45 changes: 42 additions & 3 deletions packages/next/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,13 @@ export default async function getBaseWebpackConfig(
},
},
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: {
framework: {
chunks: 'all',
Expand Down Expand Up @@ -851,6 +857,7 @@ export default async function getBaseWebpackConfig(
setImmediate: false,
},
// Kept as function to be backwards compatible
// @ts-ignore TODO webpack 5 typings needed
entry: async () => {
return {
...(clientEntries ? clientEntries : {}),
Expand Down Expand Up @@ -1601,15 +1608,22 @@ export default async function getBaseWebpackConfig(
}

// 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
if (typeof originalEntry !== 'undefined') {
webpackConfig.entry = async () => {
const updatedEntry = async () => {
const entry: WebpackEntrypoints =
typeof originalEntry === 'function'
? await originalEntry()
: originalEntry
// 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[
CLIENT_STATIC_FILES_RUNTIME_MAIN
] as string
Expand All @@ -1620,8 +1634,33 @@ export default async function getBaseWebpackConfig(
}
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
}
// @ts-ignore webpack 5 typings needed
webpackConfig.entry = updatedEntry
}

if (!dev) {
Expand Down
2 changes: 1 addition & 1 deletion test/integration/build-output/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ describe('Build Output', () => {
expect(indexSize.endsWith('B')).toBe(true)

// 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(parseFloat(err404Size)).toBeCloseTo(3.69, 1)
Expand Down

0 comments on commit 2eba039

Please sign in to comment.