Unverified Commit dc1b565f authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Implement getOptimizedModuleAliases for Turbopack (#56839)

This ensures `import url from 'url'` works in the edge runtime when using Turbopack. It also ensures the stubs for fetch / object.assignare applied to the client and edge compilation.
parent 5d9f4193
...@@ -54,6 +54,8 @@ pub async fn get_next_client_import_map( ...@@ -54,6 +54,8 @@ pub async fn get_next_client_import_map(
) )
.await?; .await?;
insert_optimized_module_aliases(&mut import_map, project_path).await?;
insert_alias_option( insert_alias_option(
&mut import_map, &mut import_map,
project_path, project_path,
...@@ -420,6 +422,8 @@ pub async fn get_next_edge_import_map( ...@@ -420,6 +422,8 @@ pub async fn get_next_edge_import_map(
) )
.await?; .await?;
insert_optimized_module_aliases(&mut import_map, project_path).await?;
insert_alias_option( insert_alias_option(
&mut import_map, &mut import_map,
project_path, project_path,
...@@ -936,8 +940,32 @@ pub fn mdx_import_source_file() -> String { ...@@ -936,8 +940,32 @@ pub fn mdx_import_source_file() -> String {
format!("{VIRTUAL_PACKAGE_NAME}/mdx-import-source") 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<FileSystemPath>,
) -> 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. // 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, import_map: &mut ImportMap,
project_path: Vc<FileSystemPath>, project_path: Vc<FileSystemPath>,
execution_context: Vc<ExecutionContext>, execution_context: Vc<ExecutionContext>,
......
...@@ -237,29 +237,32 @@ const devtoolRevertWarning = execOnce( ...@@ -237,29 +237,32 @@ const devtoolRevertWarning = execOnce(
let loggedSwcDisabled = false let loggedSwcDisabled = false
let loggedIgnoredCompilerOptions = false let loggedIgnoredCompilerOptions = false
function getOptimizedAliases(): { [pkg: string]: string } { // Insert aliases for Next.js stubs of fetch, object-assign, and url
const stubWindowFetch = path.join(__dirname, 'polyfills', 'fetch', 'index.js') // Keep in sync with insert_optimized_module_aliases in import_map.rs
const stubObjectAssign = path.join(__dirname, 'polyfills', 'object-assign.js') function getOptimizedModuleAliases(): { [pkg: string]: string } {
const shimAssign = path.join(__dirname, 'polyfills', 'object.assign')
return { return {
unfetch$: stubWindowFetch, unfetch: require.resolve('next/dist/build/polyfills/fetch/index.js'),
'isomorphic-unfetch$': stubWindowFetch, 'isomorphic-unfetch': require.resolve(
'whatwg-fetch$': path.join( 'next/dist/build/polyfills/fetch/index.js'
__dirname, ),
'polyfills', 'whatwg-fetch': require.resolve(
'fetch', 'next/dist/build/polyfills/fetch/whatwg-fetch.js'
'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'), url: require.resolve('next/dist/compiled/native-url'),
} }
} }
...@@ -888,7 +891,7 @@ export default async function getBaseWebpackConfig( ...@@ -888,7 +891,7 @@ export default async function getBaseWebpackConfig(
...(appDir ? { [APP_DIR_ALIAS]: appDir } : {}), ...(appDir ? { [APP_DIR_ALIAS]: appDir } : {}),
[ROOT_DIR_ALIAS]: dir, [ROOT_DIR_ALIAS]: dir,
[DOT_NEXT_ALIAS]: distDir, [DOT_NEXT_ALIAS]: distDir,
...(isClient || isEdgeServer ? getOptimizedAliases() : {}), ...(isClient || isEdgeServer ? getOptimizedModuleAliases() : {}),
...(reactProductionProfiling ? getReactProfilingInProduction() : {}), ...(reactProductionProfiling ? getReactProfilingInProduction() : {}),
// For Node server, we need to re-alias the package imports to prefer to // For Node server, we need to re-alias the package imports to prefer to
......
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