Commit 5f203b83 authored by Jason Miller's avatar Jason Miller Committed by Joe Haddad

Improve serverless build performance (#9155)

* Improve serverless build performance

Next's serverless build uses a custom resolver, injected the via Webpack's `externals` option that delegates out to `resolve-request` to more aggressively pull node modules into the serverless bundles.

When profiling a large Next.js application, I noticed an excessive number of filesystem calls near the end of the build. I narrowed this down to the serverless compiler pass, and eventually the custom resolver function that is the subject of this PR.  As it turns out, around half of the calls to the `externals` custom resolver function are for a `request` path that is a relative import. Next doesn't have any specific logic to apply to relative imports - it only needs to special-case bare module resolution.

Adding a simple bypass for relative module resolution reduces the number of blocking filesystem-bound `resolveRequest()` calls by 50% on a certain well-known Next.js website. This also results in a 30% reduction in production build times for incremental builds.

* Explain more in the comment
parent 61a9cfb3
...@@ -334,6 +334,15 @@ export default async function getBaseWebpackConfig( ...@@ -334,6 +334,15 @@ export default async function getBaseWebpackConfig(
return callback() return callback()
} }
// Relative requires don't need custom resolution, because they
// are relative to requests we've already resolved here.
// Absolute requires (require('/foo')) are extremely uncommon, but
// also have no need for customization as they're already resolved.
const start = request.charAt(0);
if (start === '.' || start === '/') {
return callback();
}
// Resolve the import with the webpack provided context, this // Resolve the import with the webpack provided context, this
// ensures we're resolving the correct version when multiple // ensures we're resolving the correct version when multiple
// exist. // exist.
......
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