diff --git a/packages/next-swc/crates/next-core/src/next_import_map.rs b/packages/next-swc/crates/next-core/src/next_import_map.rs index 7a433bc11b97e..6b99cb88438db 100644 --- a/packages/next-swc/crates/next-core/src/next_import_map.rs +++ b/packages/next-swc/crates/next-core/src/next_import_map.rs @@ -54,6 +54,8 @@ pub async fn get_next_client_import_map( ) .await?; + insert_optimized_module_aliases(&mut import_map, project_path).await?; + insert_alias_option( &mut import_map, project_path, @@ -420,6 +422,8 @@ pub async fn get_next_edge_import_map( ) .await?; + insert_optimized_module_aliases(&mut import_map, project_path).await?; + insert_alias_option( &mut import_map, project_path, @@ -936,8 +940,32 @@ pub fn mdx_import_source_file() -> String { format!("{VIRTUAL_PACKAGE_NAME}/mdx-import-source") } +// Insert aliases for Next.js stubs of fetch, object-assign, and url +// Keep in sync with getOptimizedModuleAliases in webpack-config.ts +async fn insert_optimized_module_aliases( + import_map: &mut ImportMap, + project_path: Vc, +) -> Result<()> { + insert_exact_alias_map( + import_map, + project_path, + indexmap! { + "unfetch" => "next/dist/build/polyfills/fetch/index.js".to_string(), + "isomorphic-unfetch" => "next/dist/build/polyfills/fetch/index.js".to_string(), + "whatwg-fetch" => "next/dist/build/polyfills/fetch/whatwg-fetch.js".to_string(), + "object-assign" => "next/dist/build/polyfills/object-assign.js".to_string(), + "object.assign/auto" => "next/dist/build/polyfills/object.assign/auto.js".to_string(), + "object.assign/implementation" => "next/dist/build/polyfills/object.assign/implementation.js".to_string(), + "object.assign/polyfill" => "next/dist/build/polyfills/object.assign/polyfill.js".to_string(), + "object.assign/shim" => "next/dist/build/polyfills/object.assign/shim.js".to_string(), + "url" => "next/dist/compiled/native-url".to_string(), + }, + ); + Ok(()) +} + // Make sure to not add any external requests here. -pub async fn insert_next_shared_aliases( +async fn insert_next_shared_aliases( import_map: &mut ImportMap, project_path: Vc, execution_context: Vc, diff --git a/packages/next/src/build/webpack-config.ts b/packages/next/src/build/webpack-config.ts index 1664d4377787c..bf6ea0bb5b2a9 100644 --- a/packages/next/src/build/webpack-config.ts +++ b/packages/next/src/build/webpack-config.ts @@ -237,29 +237,32 @@ const devtoolRevertWarning = execOnce( let loggedSwcDisabled = false let loggedIgnoredCompilerOptions = false -function getOptimizedAliases(): { [pkg: string]: string } { - const stubWindowFetch = path.join(__dirname, 'polyfills', 'fetch', 'index.js') - const stubObjectAssign = path.join(__dirname, 'polyfills', 'object-assign.js') - - const shimAssign = path.join(__dirname, 'polyfills', 'object.assign') +// Insert aliases for Next.js stubs of fetch, object-assign, and url +// Keep in sync with insert_optimized_module_aliases in import_map.rs +function getOptimizedModuleAliases(): { [pkg: string]: string } { return { - unfetch$: stubWindowFetch, - 'isomorphic-unfetch$': stubWindowFetch, - 'whatwg-fetch$': path.join( - __dirname, - 'polyfills', - 'fetch', - 'whatwg-fetch.js' + unfetch: require.resolve('next/dist/build/polyfills/fetch/index.js'), + 'isomorphic-unfetch': require.resolve( + 'next/dist/build/polyfills/fetch/index.js' + ), + 'whatwg-fetch': require.resolve( + 'next/dist/build/polyfills/fetch/whatwg-fetch.js' + ), + 'object-assign': require.resolve( + 'next/dist/build/polyfills/object-assign.js' + ), + 'object.assign/auto': require.resolve( + 'next/dist/build/polyfills/object.assign/auto.js' + ), + 'object.assign/implementation': require.resolve( + 'next/dist/build/polyfills/object.assign/implementation.js' + ), + 'object.assign/polyfill': require.resolve( + 'next/dist/build/polyfills/object.assign/polyfill.js' + ), + 'object.assign/shim': require.resolve( + 'next/dist/build/polyfills/object.assign/shim.js' ), - 'object-assign$': stubObjectAssign, - // Stub Package: object.assign - 'object.assign/auto': path.join(shimAssign, 'auto.js'), - 'object.assign/implementation': path.join(shimAssign, 'implementation.js'), - 'object.assign$': path.join(shimAssign, 'index.js'), - 'object.assign/polyfill': path.join(shimAssign, 'polyfill.js'), - 'object.assign/shim': path.join(shimAssign, 'shim.js'), - - // Replace: full URL polyfill with platform-based polyfill url: require.resolve('next/dist/compiled/native-url'), } } @@ -888,7 +891,7 @@ export default async function getBaseWebpackConfig( ...(appDir ? { [APP_DIR_ALIAS]: appDir } : {}), [ROOT_DIR_ALIAS]: dir, [DOT_NEXT_ALIAS]: distDir, - ...(isClient || isEdgeServer ? getOptimizedAliases() : {}), + ...(isClient || isEdgeServer ? getOptimizedModuleAliases() : {}), ...(reactProductionProfiling ? getReactProfilingInProduction() : {}), // For Node server, we need to re-alias the package imports to prefer to