Unverified Commit 8f0f8236 authored by Jimmy Lai's avatar Jimmy Lai Committed by GitHub

perf: add option to bundle pages externals (#56162)

This PR adds an option to forcefully bundle node_modules packages in `pages` on the server. This should benefit cold boots for projects that uses pages, at the cost of build time increase.
parent 49f646da
......@@ -372,7 +372,8 @@ export function makeExternalHandler({
config.transpilePackages,
resolvedExternalPackageDirs
) ||
(isEsm && isAppLayer)
(isEsm && isAppLayer) ||
(!isAppLayer && config.experimental.bundlePagesExternals)
if (/node_modules[/\\].*\.[mc]?js$/.test(res)) {
if (isWebpackServerLayer(layer)) {
......
......@@ -520,6 +520,9 @@ const configSchema = {
serverSourceMaps: {
type: 'boolean',
},
bundlePagesExternals: {
type: 'boolean',
},
},
type: 'object',
},
......
......@@ -330,6 +330,10 @@ export interface ExperimentalConfig {
* @internal Used by the Next.js internals only.
*/
trustHostHeader?: boolean
/**
* Enables the bundling of node_modules packages (externals) for pages server-side bundles.
*/
bundlePagesExternals?: boolean
}
export type ExportPathMap = {
......@@ -769,6 +773,7 @@ export const defaultConfig: NextConfig = {
turbotrace: undefined,
typedRoutes: false,
instrumentationHook: false,
bundlePagesExternals: false,
},
}
......
module.exports = {
experimental: {
bundlePagesExternals: true,
},
}
{
"name": "external-package",
"version": "1.0.0",
"description": "External package",
"main": "index.js"
}
import { foo } from 'external-package'
export async function getServerSideProps() {
return {
props: {
foo,
},
}
}
export default function Index({ foo }) {
return <div>{foo}</div>
}
/* eslint-env jest */
import fs from 'fs-extra'
import { join } from 'path'
import { nextBuild } from 'next-test-utils'
const appDir = join(__dirname, '../')
describe('bundle pages externals with config.experimental.bundlePagesExternals', () => {
it('should have no externals with the config set', async () => {
await nextBuild(appDir, [], { stdout: true })
const output = await fs.readFile(
join(appDir, '.next/server/pages/index.js'),
'utf8'
)
expect(output).not.toContain('require("external-package")')
})
})
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