diff --git a/packages/next-swc/crates/next-core/src/next_client/context.rs b/packages/next-swc/crates/next-core/src/next_client/context.rs index 50ae600e6c657..48408f8101968 100644 --- a/packages/next-swc/crates/next-core/src/next_client/context.rs +++ b/packages/next-swc/crates/next-core/src/next_client/context.rs @@ -23,9 +23,11 @@ use turbo_binding::{ env::ProcessEnvAssetVc, node::execution_context::ExecutionContextVc, turbopack::{ + condition::ContextCondition, module_options::{ module_options_context::{ModuleOptionsContext, ModuleOptionsContextVc}, - PostCssTransformOptions, WebpackLoadersOptions, + JsxTransformOptions, PostCssTransformOptions, TypescriptTransformOptions, + WebpackLoadersOptions, }, resolve_options_context::{ResolveOptionsContext, ResolveOptionsContextVc}, transition::TransitionsByNameVc, @@ -34,10 +36,12 @@ use turbo_binding::{ }, }; use turbo_tasks::{primitives::StringVc, Value}; +use turbo_tasks_fs::FileSystem; use super::transforms::get_next_client_transforms_rules; use crate::{ babel::maybe_add_babel_loader, + embed_js::next_js_fs, env::env_for_js, next_build::{get_external_next_compiled_package_mapping, get_postcss_package_mapping}, next_client::runtime_entry::{RuntimeEntriesVc, RuntimeEntry}, @@ -219,10 +223,23 @@ pub async fn get_client_module_options_context( enable_typescript_transform: Some(tsconfig), enable_mdx_rs: mdx_rs_options, decorators: Some(decorators_options), - rules: vec![( - foreign_code_context_condition(next_config).await?, - module_options_context.clone().cell(), - )], + rules: vec![ + ( + foreign_code_context_condition(next_config).await?, + module_options_context.clone().cell(), + ), + // If the module is an internal asset (i.e overlay, fallback) coming from the embedded + // FS, don't apply user defined transforms. + ( + ContextCondition::InPath(next_js_fs().root()), + ModuleOptionsContext { + enable_typescript_transform: Some(TypescriptTransformOptions::default().cell()), + enable_jsx: Some(JsxTransformOptions::default().cell()), + ..module_options_context.clone() + } + .cell(), + ), + ], custom_rules, ..module_options_context } diff --git a/packages/next-swc/crates/next-core/src/next_server/context.rs b/packages/next-swc/crates/next-core/src/next_server/context.rs index 813fb1350aca7..574d2081c85a2 100644 --- a/packages/next-swc/crates/next-core/src/next_server/context.rs +++ b/packages/next-swc/crates/next-core/src/next_server/context.rs @@ -17,21 +17,24 @@ use turbo_binding::{ ecmascript::EcmascriptInputTransform, node::execution_context::ExecutionContextVc, turbopack::{ + condition::ContextCondition, module_options::{ - ModuleOptionsContext, ModuleOptionsContextVc, PostCssTransformOptions, - WebpackLoadersOptions, + JsxTransformOptions, ModuleOptionsContext, ModuleOptionsContextVc, + PostCssTransformOptions, TypescriptTransformOptions, WebpackLoadersOptions, }, resolve_options_context::{ResolveOptionsContext, ResolveOptionsContextVc}, }, }, }; use turbo_tasks::{primitives::StringVc, Value}; +use turbo_tasks_fs::FileSystem; use super::{ resolve::ExternalCjsModulesResolvePluginVc, transforms::get_next_server_transforms_rules, }; use crate::{ babel::maybe_add_babel_loader, + embed_js::next_js_fs, next_build::{get_external_next_compiled_package_mapping, get_postcss_package_mapping}, next_config::NextConfigVc, next_import_map::get_next_server_import_map, @@ -270,6 +273,13 @@ pub async fn get_server_module_options_context( execution_context: Some(execution_context), ..Default::default() }; + + let internal_module_options_context = ModuleOptionsContext { + enable_typescript_transform: Some(TypescriptTransformOptions::default().cell()), + enable_jsx: Some(JsxTransformOptions::default().cell()), + ..module_options_context.clone() + }; + ModuleOptionsContext { enable_jsx: Some(jsx_runtime_options), enable_styled_jsx: true, @@ -280,10 +290,16 @@ pub async fn get_server_module_options_context( enable_typescript_transform: Some(tsconfig), enable_mdx_rs: mdx_rs_options, decorators: Some(decorators_options), - rules: vec![( - foreign_code_context_condition, - module_options_context.clone().cell(), - )], + rules: vec![ + ( + foreign_code_context_condition, + module_options_context.clone().cell(), + ), + ( + ContextCondition::InPath(next_js_fs().root()), + internal_module_options_context.cell(), + ), + ], custom_rules, ..module_options_context } @@ -298,6 +314,11 @@ pub async fn get_server_module_options_context( execution_context: Some(execution_context), ..Default::default() }; + let internal_module_options_context = ModuleOptionsContext { + enable_typescript_transform: Some(TypescriptTransformOptions::default().cell()), + ..module_options_context.clone() + }; + ModuleOptionsContext { enable_jsx: Some(jsx_runtime_options), enable_styled_jsx: true, @@ -308,10 +329,16 @@ pub async fn get_server_module_options_context( enable_typescript_transform: Some(tsconfig), enable_mdx_rs: mdx_rs_options, decorators: Some(decorators_options), - rules: vec![( - foreign_code_context_condition, - module_options_context.clone().cell(), - )], + rules: vec![ + ( + foreign_code_context_condition, + module_options_context.clone().cell(), + ), + ( + ContextCondition::InPath(next_js_fs().root()), + internal_module_options_context.cell(), + ), + ], custom_rules, ..module_options_context } @@ -332,6 +359,10 @@ pub async fn get_server_module_options_context( execution_context: Some(execution_context), ..Default::default() }; + let internal_module_options_context = ModuleOptionsContext { + enable_typescript_transform: Some(TypescriptTransformOptions::default().cell()), + ..module_options_context.clone() + }; ModuleOptionsContext { enable_jsx: Some(jsx_runtime_options), enable_emotion, @@ -341,10 +372,16 @@ pub async fn get_server_module_options_context( enable_typescript_transform: Some(tsconfig), enable_mdx_rs: mdx_rs_options, decorators: Some(decorators_options), - rules: vec![( - foreign_code_context_condition, - module_options_context.clone().cell(), - )], + rules: vec![ + ( + foreign_code_context_condition, + module_options_context.clone().cell(), + ), + ( + ContextCondition::InPath(next_js_fs().root()), + internal_module_options_context.cell(), + ), + ], custom_rules, ..module_options_context } @@ -354,16 +391,26 @@ pub async fn get_server_module_options_context( execution_context: Some(execution_context), ..Default::default() }; + let internal_module_options_context = ModuleOptionsContext { + enable_typescript_transform: Some(TypescriptTransformOptions::default().cell()), + ..module_options_context.clone() + }; ModuleOptionsContext { enable_postcss_transform, enable_webpack_loaders, enable_typescript_transform: Some(tsconfig), enable_mdx_rs: mdx_rs_options, decorators: Some(decorators_options), - rules: vec![( - foreign_code_context_condition, - module_options_context.clone().cell(), - )], + rules: vec![ + ( + foreign_code_context_condition, + module_options_context.clone().cell(), + ), + ( + ContextCondition::InPath(next_js_fs().root()), + internal_module_options_context.cell(), + ), + ], custom_rules, ..module_options_context } @@ -373,6 +420,10 @@ pub async fn get_server_module_options_context( execution_context: Some(execution_context), ..Default::default() }; + let internal_module_options_context = ModuleOptionsContext { + enable_typescript_transform: Some(TypescriptTransformOptions::default().cell()), + ..module_options_context.clone() + }; ModuleOptionsContext { enable_jsx: Some(jsx_runtime_options), enable_emotion, @@ -383,10 +434,16 @@ pub async fn get_server_module_options_context( enable_typescript_transform: Some(tsconfig), enable_mdx_rs: mdx_rs_options, decorators: Some(decorators_options), - rules: vec![( - foreign_code_context_condition, - module_options_context.clone().cell(), - )], + rules: vec![ + ( + foreign_code_context_condition, + module_options_context.clone().cell(), + ), + ( + ContextCondition::InPath(next_js_fs().root()), + internal_module_options_context.cell(), + ), + ], custom_rules, ..module_options_context }