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(
)
.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<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.
pub async fn insert_next_shared_aliases(
async fn insert_next_shared_aliases(
import_map: &mut ImportMap,
project_path: Vc<FileSystemPath>,
execution_context: Vc<ExecutionContext>,
......
......@@ -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
......
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