Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: add option to bundle pages externals #56162

Merged
merged 1 commit into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/next/src/build/handle-externals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
3 changes: 3 additions & 0 deletions packages/next/src/server/config-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,9 @@ const configSchema = {
serverSourceMaps: {
type: 'boolean',
},
bundlePagesExternals: {
type: 'boolean',
},
},
type: 'object',
},
Expand Down
5 changes: 5 additions & 0 deletions packages/next/src/server/config-shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -769,6 +773,7 @@ export const defaultConfig: NextConfig = {
turbotrace: undefined,
typedRoutes: false,
instrumentationHook: false,
bundlePagesExternals: false,
},
}

Expand Down
5 changes: 5 additions & 0 deletions test/integration/externals-pages-bundle/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
experimental: {
bundlePagesExternals: true,
},
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions test/integration/externals-pages-bundle/pages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { foo } from 'external-package'

export async function getServerSideProps() {
return {
props: {
foo,
},
}
}

export default function Index({ foo }) {
return <div>{foo}</div>
}
18 changes: 18 additions & 0 deletions test/integration/externals-pages-bundle/test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* 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")')
})
})
Loading