Skip to content

Commit

Permalink
Implement getOptimizedModuleAliases for Turbopack
Browse files Browse the repository at this point in the history
  • Loading branch information
timneutkens committed Oct 14, 2023
1 parent 5d9f419 commit bfc3d0f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
30 changes: 29 additions & 1 deletion packages/next-swc/crates/next-core/src/next_import_map.rs
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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>,
Expand Down
6 changes: 4 additions & 2 deletions packages/next/src/build/webpack-config.ts
Expand Up @@ -237,7 +237,9 @@ const devtoolRevertWarning = execOnce(
let loggedSwcDisabled = false
let loggedIgnoredCompilerOptions = false

function getOptimizedAliases(): { [pkg: string]: string } {
// 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 } {
const stubWindowFetch = path.join(__dirname, 'polyfills', 'fetch', 'index.js')
const stubObjectAssign = path.join(__dirname, 'polyfills', 'object-assign.js')

Expand Down Expand Up @@ -888,7 +890,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
Expand Down

0 comments on commit bfc3d0f

Please sign in to comment.