Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
c98c6d6f
Unverified
Commit
c98c6d6f
authored
May 10, 2024
by
hrmny
Committed by
GitHub
May 10, 2024
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(turbopack): add support for `bundlePagesRouterDependencies` (#65520)
parent
eff272f1
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
107 additions
and
36 deletions
+107
-36
next_config.rs
packages/next-swc/crates/next-core/src/next_config.rs
+17
-3
context.rs
...ages/next-swc/crates/next-core/src/next_server/context.rs
+35
-14
handle-externals.ts
packages/next/src/build/handle-externals.ts
+3
-1
config-shared.ts
packages/next/src/server/config-shared.ts
+6
-5
index.test.js
test/integration/externals-pages-bundle/test/index.test.js
+44
-11
turbopack-build-tests-manifest.json
test/turbopack-build-tests-manifest.json
+2
-2
No files found.
packages/next-swc/crates/next-core/src/next_config.rs
View file @
c98c6d6f
...
@@ -88,6 +88,18 @@ pub struct NextConfig {
...
@@ -88,6 +88,18 @@ pub struct NextConfig {
pub
dev_indicators
:
Option
<
DevIndicatorsConfig
>
,
pub
dev_indicators
:
Option
<
DevIndicatorsConfig
>
,
pub
output
:
Option
<
OutputType
>
,
pub
output
:
Option
<
OutputType
>
,
/// Enables the bundling of node_modules packages (externals) for pages
/// server-side bundles.
///
/// [API Reference](https://nextjs.org/docs/pages/api-reference/next-config-js/bundlePagesRouterDependencies)
pub
bundle_pages_router_dependencies
:
Option
<
bool
>
,
/// A list of packages that should be treated as external on the server
/// build.
///
/// [API Reference](https://nextjs.org/docs/app/api-reference/next-config-js/serverExternalPackages)
pub
server_external_packages
:
Option
<
Vec
<
String
>>
,
#[serde(rename
=
"_originalRedirects"
)]
#[serde(rename
=
"_originalRedirects"
)]
pub
original_redirects
:
Option
<
Vec
<
Redirect
>>
,
pub
original_redirects
:
Option
<
Vec
<
Redirect
>>
,
...
@@ -119,9 +131,6 @@ pub struct NextConfig {
...
@@ -119,9 +131,6 @@ pub struct NextConfig {
typescript
:
TypeScriptConfig
,
typescript
:
TypeScriptConfig
,
use_file_system_public_routes
:
bool
,
use_file_system_public_routes
:
bool
,
webpack
:
Option
<
serde_json
::
Value
>
,
webpack
:
Option
<
serde_json
::
Value
>
,
/// A list of packages that should be treated as external in the RSC server
/// build. @see [api reference](https://nextjs.org/docs/app/api-reference/next-config-js/server_external_packages)
pub
server_external_packages
:
Option
<
Vec
<
String
>>
,
}
}
#[derive(Clone,
Debug,
PartialEq,
Serialize,
Deserialize,
TraceRawVcs)]
#[derive(Clone,
Debug,
PartialEq,
Serialize,
Deserialize,
TraceRawVcs)]
...
@@ -735,6 +744,11 @@ impl NextConfig {
...
@@ -735,6 +744,11 @@ impl NextConfig {
Ok
(
config
.cell
())
Ok
(
config
.cell
())
}
}
#[turbo_tasks
::
function]
pub
fn
bundle_pages_router_dependencies
(
&
self
)
->
Vc
<
bool
>
{
Vc
::
cell
(
self
.bundle_pages_router_dependencies
.unwrap_or_default
())
}
#[turbo_tasks
::
function]
#[turbo_tasks
::
function]
pub
async
fn
server_external_packages
(
self
:
Vc
<
Self
>
)
->
Result
<
Vc
<
Vec
<
String
>>>
{
pub
async
fn
server_external_packages
(
self
:
Vc
<
Self
>
)
->
Result
<
Vc
<
Vec
<
String
>>>
{
Ok
(
Vc
::
cell
(
Ok
(
Vc
::
cell
(
...
...
packages/next-swc/crates/next-core/src/next_server/context.rs
View file @
c98c6d6f
use
std
::
iter
::
once
;
use
std
::
iter
::
once
;
use
anyhow
::
Result
;
use
anyhow
::
{
bail
,
Result
}
;
use
indexmap
::
IndexMap
;
use
indexmap
::
IndexMap
;
use
turbo_tasks
::{
Value
,
Vc
};
use
turbo_tasks
::{
Value
,
Vc
};
use
turbo_tasks_fs
::
FileSystem
;
use
turbo_tasks_fs
::
FileSystem
;
...
@@ -128,6 +128,13 @@ pub async fn get_server_resolve_options_context(
...
@@ -128,6 +128,13 @@ pub async fn get_server_resolve_options_context(
let
invalid_styled_jsx_client_only_resolve_plugin
=
let
invalid_styled_jsx_client_only_resolve_plugin
=
get_invalid_styled_jsx_resolve_plugin
(
project_path
);
get_invalid_styled_jsx_resolve_plugin
(
project_path
);
let
mut
transpile_packages
=
next_config
.transpile_packages
()
.await
?
.clone_value
();
transpile_packages
.extend
(
(
*
next_config
.optimize_package_imports
()
.await
?
)
.iter
()
.cloned
(),
);
// Always load these predefined packages as external.
// Always load these predefined packages as external.
let
mut
external_packages
:
Vec
<
String
>
=
load_next_js_templateon
(
let
mut
external_packages
:
Vec
<
String
>
=
load_next_js_templateon
(
project_path
,
project_path
,
...
@@ -135,15 +142,25 @@ pub async fn get_server_resolve_options_context(
...
@@ -135,15 +142,25 @@ pub async fn get_server_resolve_options_context(
)
)
.await
?
;
.await
?
;
let
transpile_packages
=
next_config
.transpile_packages
()
.await
?
;
let
server_external_packages
=
&*
next_config
.server_external_packages
()
.await
?
;
external_packages
.retain
(|
item
|
!
transpile_packages
.contains
(
item
));
// Add the config's own list of external packages.
let
conflicting_packages
=
transpile_packages
external_packages
.extend
(
(
*
next_config
.server_external_packages
()
.await
?
)
.iter
()
.iter
()
.cloned
(),
.filter
(|
package
|
server_external_packages
.contains
(
package
))
.collect
::
<
Vec
<
_
>>
();
if
!
conflicting_packages
.is_empty
()
{
bail!
(
"The packages specified in the 'transpilePackages' conflict with the
\
'serverExternalPackages': {:?}"
,
conflicting_packages
);
);
}
// Add the config's own list of external packages.
external_packages
.extend
(
server_external_packages
.iter
()
.cloned
());
external_packages
.retain
(|
item
|
!
transpile_packages
.contains
(
item
));
let
server_external_packages_plugin
=
ExternalCjsModulesResolvePlugin
::
new
(
let
server_external_packages_plugin
=
ExternalCjsModulesResolvePlugin
::
new
(
project_path
,
project_path
,
...
@@ -166,12 +183,16 @@ pub async fn get_server_resolve_options_context(
...
@@ -166,12 +183,16 @@ pub async fn get_server_resolve_options_context(
custom_conditions
.push
(
"react-server"
.to_string
());
custom_conditions
.push
(
"react-server"
.to_string
());
};
};
let
external_cjs_modules_plugin
=
ExternalCjsModulesResolvePlugin
::
new
(
let
external_cjs_modules_plugin
=
if
*
next_config
.bundle_pages_router_dependencies
()
.await
?
{
server_external_packages_plugin
}
else
{
ExternalCjsModulesResolvePlugin
::
new
(
project_path
,
project_path
,
project_path
.root
(),
project_path
.root
(),
ExternalPredicate
::
AllExcept
(
next_config
.transpile_packages
(
))
.cell
(),
ExternalPredicate
::
AllExcept
(
Vc
::
cell
(
transpile_packages
))
.cell
(),
*
next_config
.import_externals
()
.await
?
,
*
next_config
.import_externals
()
.await
?
,
);
)
};
let
next_external_plugin
=
NextExternalResolvePlugin
::
new
(
project_path
);
let
next_external_plugin
=
NextExternalResolvePlugin
::
new
(
project_path
);
let
next_node_shared_runtime_plugin
=
let
next_node_shared_runtime_plugin
=
...
...
packages/next/src/build/handle-externals.ts
View file @
c98c6d6f
...
@@ -181,7 +181,7 @@ export function makeExternalHandler({
...
@@ -181,7 +181,7 @@ export function makeExternalHandler({
// Absolute requires (require('/foo')) are extremely uncommon, but
// Absolute requires (require('/foo')) are extremely uncommon, but
// also have no need for customization as they're already resolved.
// also have no need for customization as they're already resolved.
if
(
!
isLocal
)
{
if
(
!
isLocal
)
{
if
(
/^
(?:
next$
)
/
.
test
(
request
))
{
if
(
/^
next$
/
.
test
(
request
))
{
return
`commonjs
${
request
}
`
return
`commonjs
${
request
}
`
}
}
...
@@ -380,6 +380,7 @@ function resolveBundlingOptOutPackages({
...
@@ -380,6 +380,7 @@ function resolveBundlingOptOutPackages({
if
(
nodeModulesRegex
.
test
(
resolvedRes
))
{
if
(
nodeModulesRegex
.
test
(
resolvedRes
))
{
const
shouldBundlePages
=
const
shouldBundlePages
=
!
isAppLayer
&&
config
.
bundlePagesRouterDependencies
&&
!
isOptOutBundling
!
isAppLayer
&&
config
.
bundlePagesRouterDependencies
&&
!
isOptOutBundling
const
shouldBeBundled
=
const
shouldBeBundled
=
shouldBundlePages
||
shouldBundlePages
||
isResourceInPackages
(
isResourceInPackages
(
...
@@ -387,6 +388,7 @@ function resolveBundlingOptOutPackages({
...
@@ -387,6 +388,7 @@ function resolveBundlingOptOutPackages({
config
.
transpilePackages
,
config
.
transpilePackages
,
resolvedExternalPackageDirs
resolvedExternalPackageDirs
)
)
if
(
!
shouldBeBundled
)
{
if
(
!
shouldBeBundled
)
{
return
`
${
externalType
}
${
request
}
`
// Externalize if not bundled or opted out
return
`
${
externalType
}
${
request
}
`
// Externalize if not bundled or opted out
}
}
...
...
packages/next/src/server/config-shared.ts
View file @
c98c6d6f
...
@@ -808,11 +808,12 @@ export interface NextConfig extends Record<string, any> {
...
@@ -808,11 +808,12 @@ export interface NextConfig extends Record<string, any> {
/**
/**
* Enables the bundling of node_modules packages (externals) for pages server-side bundles.
* Enables the bundling of node_modules packages (externals) for pages server-side bundles.
* @see https://nextjs.org/docs/pages/api-reference/next-config-js/bundlePagesRouterDependencies
*/
*/
bundlePagesRouterDependencies
?:
boolean
bundlePagesRouterDependencies
?:
boolean
/**
/**
* A list of packages that should be treated as external in the
RSC
server build.
* A list of packages that should be treated as external in the server build.
* @see https://nextjs.org/docs/app/api-reference/next-config-js/serverExternalPackages
* @see https://nextjs.org/docs/app/api-reference/next-config-js/serverExternalPackages
*/
*/
serverExternalPackages
?:
string
[]
serverExternalPackages
?:
string
[]
...
@@ -926,10 +927,10 @@ export const defaultConfig: NextConfig = {
...
@@ -926,10 +927,10 @@ export const defaultConfig: NextConfig = {
// If we're testing, and the `__NEXT_EXPERIMENTAL_PPR` environment variable
// If we're testing, and the `__NEXT_EXPERIMENTAL_PPR` environment variable
// has been set to `true`, enable the experimental PPR feature so long as it
// has been set to `true`, enable the experimental PPR feature so long as it
// wasn't explicitly disabled in the config.
// wasn't explicitly disabled in the config.
!!
(
process
.
env
.
__NEXT_TEST_MODE
&&
process
.
env
.
__NEXT_TEST_MODE
&&
process
.
env
.
__NEXT_EXPERIMENTAL_PPR
===
'true'
process
.
env
.
__NEXT_EXPERIMENTAL_PPR
===
'true'
?
true
),
:
false
,
webpackBuildWorker
:
undefined
,
webpackBuildWorker
:
undefined
,
missingSuspenseWithCSRBailout
:
true
,
missingSuspenseWithCSRBailout
:
true
,
optimizeServerReact
:
true
,
optimizeServerReact
:
true
,
...
...
test/integration/externals-pages-bundle/test/index.test.js
View file @
c98c6d6f
...
@@ -11,22 +11,55 @@ describe('bundle pages externals with config.bundlePagesRouterDependencies', ()
...
@@ -11,22 +11,55 @@ describe('bundle pages externals with config.bundlePagesRouterDependencies', ()
'production mode'
,
'production mode'
,
()
=>
{
()
=>
{
beforeAll
(
async
()
=>
{
beforeAll
(
async
()
=>
{
await
nextBuild
(
appDir
,
[]
,
{
stdout
:
true
}
)
await
nextBuild
(
appDir
,
[])
})
})
it
(
'should have no externals with the config set'
,
async
()
=>
{
it
(
'should have no externals with the config set'
,
async
()
=>
{
if
(
process
.
env
.
TURBOPACK
)
{
const
ssrPath
=
join
(
appDir
,
'.next/server/chunks/ssr'
)
const
pageBundleBasenames
=
(
await
fs
.
readdir
(
ssrPath
)).
filter
((
p
)
=>
p
.
match
(
/
\.
js$/
)
)
expect
(
pageBundleBasenames
).
not
.
toBeEmpty
()
let
allBundles
=
''
for
(
const
basename
of
pageBundleBasenames
)
{
const
output
=
await
fs
.
readFile
(
join
(
ssrPath
,
basename
),
'utf8'
)
allBundles
+=
output
}
// we don't know the name of the minified `__turbopack_external_require__`, so we just check the arguments.
expect
(
allBundles
).
not
.
toContain
(
'("external-package",!0)'
)
}
else
{
const
output
=
await
fs
.
readFile
(
const
output
=
await
fs
.
readFile
(
join
(
appDir
,
'.next/server/pages/index.js'
),
join
(
appDir
,
'.next/server/pages/index.js'
),
'utf8'
'utf8'
)
)
expect
(
output
).
not
.
toContain
(
'require("external-package")'
)
expect
(
output
).
not
.
toContain
(
'require("external-package")'
)
}
})
})
it
(
'should respect the serverExternalPackages config'
,
async
()
=>
{
it
(
'should respect the serverExternalPackages config'
,
async
()
=>
{
if
(
process
.
env
.
TURBOPACK
)
{
const
ssrPath
=
join
(
appDir
,
'.next/server/chunks/ssr'
)
const
pageBundleBasenames
=
(
await
fs
.
readdir
(
ssrPath
)).
filter
((
p
)
=>
p
.
match
(
/
\.
js$/
)
)
expect
(
pageBundleBasenames
).
not
.
toBeEmpty
()
let
allBundles
=
''
for
(
const
basename
of
pageBundleBasenames
)
{
const
output
=
await
fs
.
readFile
(
join
(
ssrPath
,
basename
),
'utf8'
)
allBundles
+=
output
}
// we don't know the name of the minified `__turbopack_external_require__`, so we just check the arguments.
expect
(
allBundles
).
toContain
(
'("opted-out-external-package",!0)'
)
}
else
{
const
output
=
await
fs
.
readFile
(
const
output
=
await
fs
.
readFile
(
join
(
appDir
,
'.next/server/pages/index.js'
),
join
(
appDir
,
'.next/server/pages/index.js'
),
'utf8'
'utf8'
)
)
expect
(
output
).
toContain
(
'require("opted-out-external-package")'
)
expect
(
output
).
toContain
(
'require("opted-out-external-package")'
)
}
})
})
}
}
)
)
...
...
test/turbopack-build-tests-manifest.json
View file @
c98c6d6f
...
@@ -8484,11 +8484,11 @@
...
@@ -8484,11 +8484,11 @@
"runtimeError"
:
false
"runtimeError"
:
false
},
},
"test/integration/externals-pages-bundle/test/index.test.js"
:
{
"test/integration/externals-pages-bundle/test/index.test.js"
:
{
"passed"
:
[],
"passed"
:
[
"failed"
:
[
"bundle pages externals with config.bundlePagesRouterDependencies production mode should have no externals with the config set"
,
"bundle pages externals with config.bundlePagesRouterDependencies production mode should have no externals with the config set"
,
"bundle pages externals with config.bundlePagesRouterDependencies production mode should respect the serverExternalPackages config"
"bundle pages externals with config.bundlePagesRouterDependencies production mode should respect the serverExternalPackages config"
],
],
"failed"
:
[],
"pending"
:
[],
"pending"
:
[],
"flakey"
:
[],
"flakey"
:
[],
"runtimeError"
:
false
"runtimeError"
:
false
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment