Skip to content

Commit

Permalink
Make webpack CommonsChunkPlugin chunking default configurable
Browse files Browse the repository at this point in the history
A while ago (in vercel#1644) @arunoda updated the default for minimum refs to
chunk a file into commons from the previous (which I believe was a
static integer, or at least was at some point), and made it so that the
file had to be used in > 50% of the pages.

The problem is that people may use pages in different ways, particularly
if they're using a custom server instead of the default page routing.
For example, I use Styled Components, and put a `styles.js` file
alongside my various page JSX files. Others may colocate JSX components
inside pages, which could be very reasonable depending on the project.

The point is, it's very possible to load up your /pages directory with
JS files that add into the default logic's perception of "page count",
since there's no singular way to determine what is and is not a page
without analyzing the code within.

I'd propose that leave this as the default behavior, since that makes
sense if you're using Next in the OOTB manner, but allow for
configuration if needed. @rauchg suggested as much in vercel#1644 but it seems
like that fell out in the final version (vercel#1659)
  • Loading branch information
George Pantazis committed Jan 22, 2018
1 parent e451218 commit 1e47062
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
8 changes: 1 addition & 7 deletions server/build/webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,7 @@ export default async function createCompiler (dir, { buildId, dev = false, quiet
return false
}

// If there are one or two pages, only move modules to common if they are
// used in all of the pages. Otherwise, move modules used in at-least
// 1/2 of the total pages into commons.
if (totalPages <= 2) {
return count >= totalPages
}
return count >= totalPages * 0.5
return config.webpackModuleShouldBeCommonInProduction({totalPages, count})
}
}),
// This chunk splits out react and react-dom in production to make sure it does not go through uglify. This saved multiple seconds on production builds.
Expand Down
9 changes: 9 additions & 0 deletions server/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ const cache = new Map()
const defaultConfig = {
webpack: null,
webpackDevMiddleware: null,
webpackModuleShouldBeCommonInProduction: function ({totalPages, count}) {
// If there are one or two pages, only move modules to common if they are
// used in all of the pages. Otherwise, move modules used in at-least
// 1/2 of the total pages into commons.
if (totalPages <= 2) {
return count >= totalPages
}
return count >= totalPages * 0.5
},
poweredByHeader: true,
distDir: '.next',
assetPrefix: '',
Expand Down

0 comments on commit 1e47062

Please sign in to comment.