Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement getOptimizedModuleAliases for Turbopack #56839

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion packages/next-swc/crates/next-core/src/next_import_map.rs
Original file line number Diff line number Diff line change
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
47 changes: 25 additions & 22 deletions packages/next/src/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
}
}
Expand Down Expand Up @@ -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
Expand Down
Loading