Skip to content

Commit

Permalink
add option to bundke pages
Browse files Browse the repository at this point in the history
  • Loading branch information
feedthejim committed Sep 28, 2023
1 parent 30defea commit dc03ccf
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 1 deletion.
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")')
})
})

0 comments on commit dc03ccf

Please sign in to comment.