Unverified Commit 9d495499 authored by OJ Kwon's avatar OJ Kwon Committed by GitHub

fix(next-core): apply image-loader alias to the remaining context (#62070)

### What?

Fix custom image loader not being reflected in some contexts and running
the default image loader when next.config.js is present. For Webpack,
this alias is specified in `createWebpackAlias`
(https://github.com/vercel/next.js/blob/8d28d5954e98060e3a478c6b82acb0fb11c5d6f2/packages/next/src/build/create-compiler-aliases.ts#L22)
and this setting seems to be context-agnostic in base
(https://github.com/vercel/next.js/blob/8d28d5954e98060e3a478c6b82acb0fb11c5d6f2/packages/next/src/build/webpack-config.ts#L649).

Closes PACK-2509
parent 8d28d595
...@@ -54,6 +54,7 @@ pub async fn get_next_client_import_map( ...@@ -54,6 +54,7 @@ pub async fn get_next_client_import_map(
project_path, project_path,
execution_context, execution_context,
next_config, next_config,
false,
) )
.await?; .await?;
...@@ -248,6 +249,7 @@ pub async fn get_next_server_import_map( ...@@ -248,6 +249,7 @@ pub async fn get_next_server_import_map(
project_path, project_path,
execution_context, execution_context,
next_config, next_config,
false,
) )
.await?; .await?;
...@@ -373,6 +375,7 @@ pub async fn get_next_edge_import_map( ...@@ -373,6 +375,7 @@ pub async fn get_next_edge_import_map(
project_path, project_path,
execution_context, execution_context,
next_config, next_config,
true,
) )
.await?; .await?;
...@@ -494,21 +497,6 @@ async fn insert_next_server_special_aliases( ...@@ -494,21 +497,6 @@ async fn insert_next_server_special_aliases(
external_if_node(project_path, "next/dist/compiled/@opentelemetry/api"), external_if_node(project_path, "next/dist/compiled/@opentelemetry/api"),
); );
let image_config = next_config.image_config().await?;
if let Some(loader_file) = image_config.loader_file.as_deref() {
import_map.insert_exact_alias(
"next/dist/shared/lib/image-loader",
request_to_import_mapping(project_path, loader_file),
);
if runtime == NextRuntime::Edge {
import_map.insert_exact_alias(
"next/dist/esm/shared/lib/image-loader",
request_to_import_mapping(project_path, loader_file),
);
}
}
match ty { match ty {
ServerContextType::Pages { .. } | ServerContextType::PagesApi { .. } => {} ServerContextType::Pages { .. } | ServerContextType::PagesApi { .. } => {}
ServerContextType::PagesData { .. } => {} ServerContextType::PagesData { .. } => {}
...@@ -714,6 +702,7 @@ async fn insert_next_shared_aliases( ...@@ -714,6 +702,7 @@ async fn insert_next_shared_aliases(
project_path: Vc<FileSystemPath>, project_path: Vc<FileSystemPath>,
execution_context: Vc<ExecutionContext>, execution_context: Vc<ExecutionContext>,
next_config: Vc<NextConfig>, next_config: Vc<NextConfig>,
is_runtime_edge: bool,
) -> Result<()> { ) -> Result<()> {
let package_root = next_js_fs().root(); let package_root = next_js_fs().root();
...@@ -837,6 +826,21 @@ async fn insert_next_shared_aliases( ...@@ -837,6 +826,21 @@ async fn insert_next_shared_aliases(
turbopack_binding::turbopack::node::embed_js::embed_fs().root(), turbopack_binding::turbopack::node::embed_js::embed_fs().root(),
); );
let image_config = next_config.image_config().await?;
if let Some(loader_file) = image_config.loader_file.as_deref() {
import_map.insert_exact_alias(
"next/dist/shared/lib/image-loader",
request_to_import_mapping(project_path, loader_file),
);
if is_runtime_edge {
import_map.insert_exact_alias(
"next/dist/esm/shared/lib/image-loader",
request_to_import_mapping(project_path, loader_file),
);
}
}
Ok(()) Ok(())
} }
......
...@@ -13677,13 +13677,13 @@ ...@@ -13677,13 +13677,13 @@
"runtimeError": false "runtimeError": false
}, },
"test/integration/next-image-new/loader-config-edge-runtime/test/index.test.ts": { "test/integration/next-image-new/loader-config-edge-runtime/test/index.test.ts": {
"passed": [], "passed": [
"failed": [
"Image Loader Config with Edge Runtime dev mode should add \"src\" to img1 based on the loader config", "Image Loader Config with Edge Runtime dev mode should add \"src\" to img1 based on the loader config",
"Image Loader Config with Edge Runtime dev mode should add \"src\" to img2 based on the loader prop", "Image Loader Config with Edge Runtime dev mode should add \"src\" to img2 based on the loader prop",
"Image Loader Config with Edge Runtime dev mode should add \"srcset\" to img1 based on the loader config", "Image Loader Config with Edge Runtime dev mode should add \"srcset\" to img1 based on the loader config",
"Image Loader Config with Edge Runtime dev mode should add \"srcset\" to img2 based on the loader prop" "Image Loader Config with Edge Runtime dev mode should add \"srcset\" to img2 based on the loader prop"
], ],
"failed": [],
"pending": [ "pending": [
"Image Loader Config with Edge Runtime production mode should add \"src\" to img1 based on the loader config", "Image Loader Config with Edge Runtime production mode should add \"src\" to img1 based on the loader config",
"Image Loader Config with Edge Runtime production mode should add \"src\" to img2 based on the loader prop", "Image Loader Config with Edge Runtime production mode should add \"src\" to img2 based on the loader prop",
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