Unverified Commit c98c6d6f authored by hrmny's avatar hrmny Committed by GitHub

feat(turbopack): add support for `bundlePagesRouterDependencies` (#65520)

parent eff272f1
...@@ -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(
......
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));
let conflicting_packages = transpile_packages
.iter()
.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. // Add the config's own list of external packages.
external_packages.extend( external_packages.extend(server_external_packages.iter().cloned());
(*next_config.server_external_packages().await?)
.iter() external_packages.retain(|item| !transpile_packages.contains(item));
.cloned(),
);
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? {
project_path, server_external_packages_plugin
project_path.root(), } else {
ExternalPredicate::AllExcept(next_config.transpile_packages()).cell(), ExternalCjsModulesResolvePlugin::new(
*next_config.import_externals().await?, project_path,
); project_path.root(),
ExternalPredicate::AllExcept(Vc::cell(transpile_packages)).cell(),
*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 =
......
...@@ -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
} }
......
...@@ -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_EXPERIMENTAL_PPR === 'true' process.env.__NEXT_TEST_MODE &&
? true process.env.__NEXT_EXPERIMENTAL_PPR === 'true'
: false, ),
webpackBuildWorker: undefined, webpackBuildWorker: undefined,
missingSuspenseWithCSRBailout: true, missingSuspenseWithCSRBailout: true,
optimizeServerReact: true, optimizeServerReact: true,
......
...@@ -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 () => {
const output = await fs.readFile( if (process.env.TURBOPACK) {
join(appDir, '.next/server/pages/index.js'), const ssrPath = join(appDir, '.next/server/chunks/ssr')
'utf8' const pageBundleBasenames = (await fs.readdir(ssrPath)).filter((p) =>
) p.match(/\.js$/)
expect(output).not.toContain('require("external-package")') )
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(
join(appDir, '.next/server/pages/index.js'),
'utf8'
)
expect(output).not.toContain('require("external-package")')
}
}) })
it('should respect the serverExternalPackages config', async () => { it('should respect the serverExternalPackages config', async () => {
const output = await fs.readFile( if (process.env.TURBOPACK) {
join(appDir, '.next/server/pages/index.js'), const ssrPath = join(appDir, '.next/server/chunks/ssr')
'utf8' const pageBundleBasenames = (await fs.readdir(ssrPath)).filter((p) =>
) p.match(/\.js$/)
expect(output).toContain('require("opted-out-external-package")') )
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(
join(appDir, '.next/server/pages/index.js'),
'utf8'
)
expect(output).toContain('require("opted-out-external-package")')
}
}) })
} }
) )
......
...@@ -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
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