Commit 4bea437c authored by OJ Kwon's avatar OJ Kwon Committed by GitHub

feat(ecmascript): transform ts input with runtime flags (vercel/turbo#4142)

### Description

Another attempt to close WEB-659.

The crux is same as previous PR, but attempt to change the location
where it read config / bubble down the config values to the actual
transform stage. Mainly, `enable_typescript_transform` is now accepting
an option instead of boolean flag to down to
`EcmaInputTransform::Typescript`, and `get_*_module_context()` reads the
config value as needed.
parent 01f25243
......@@ -66,7 +66,9 @@ impl Transition for NextLayoutEntryTransition {
context.into(),
Value::new(EcmascriptModuleAssetType::Typescript),
EcmascriptInputTransformsVc::cell(vec![
EcmascriptInputTransform::TypeScript,
EcmascriptInputTransform::TypeScript {
use_define_for_class_fields: false,
},
EcmascriptInputTransform::React { refresh: false },
]),
context.compile_time_info(),
......
......@@ -659,7 +659,9 @@ import BOOTSTRAP from {};
Value::new(EcmascriptModuleAssetType::Typescript),
EcmascriptInputTransformsVc::cell(vec![
EcmascriptInputTransform::React { refresh: false },
EcmascriptInputTransform::TypeScript,
EcmascriptInputTransform::TypeScript {
use_define_for_class_fields: false,
},
]),
context.compile_time_info(),
),
......@@ -727,7 +729,9 @@ impl AppRouteVc {
virtual_asset.into(),
this.context,
Value::new(EcmascriptModuleAssetType::Typescript),
EcmascriptInputTransformsVc::cell(vec![EcmascriptInputTransform::TypeScript]),
EcmascriptInputTransformsVc::cell(vec![EcmascriptInputTransform::TypeScript {
use_define_for_class_fields: false,
}]),
this.context.compile_time_info(),
InnerAssetsVc::cell(indexmap! {
"ROUTE_CHUNK_GROUP".to_string() => entry
......
......@@ -28,6 +28,7 @@ pub mod react_refresh;
pub mod router;
pub mod router_source;
mod runtime;
mod typescript;
mod util;
mod web_entry_source;
......
......@@ -38,6 +38,7 @@ use crate::{
get_next_client_resolved_map,
},
react_refresh::assert_can_resolve_react_refresh,
typescript::get_typescript_transform_options,
util::foreign_code_context_condition,
};
......@@ -128,6 +129,7 @@ pub async fn get_client_module_options_context(
.await?
.is_found();
let tsconfig = get_typescript_transform_options(project_path);
let enable_webpack_loaders = {
let options = &*next_config.webpack_loaders_options().await?;
let loaders_options = WebpackLoadersOptions {
......@@ -164,7 +166,7 @@ pub async fn get_client_module_options_context(
..Default::default()
}),
enable_webpack_loaders,
enable_typescript_transform: true,
enable_typescript_transform: Some(tsconfig),
rules: vec![(
foreign_code_context_condition(next_config).await?,
module_options_context.clone().cell(),
......
......@@ -80,7 +80,9 @@ impl Transition for NextClientTransition {
context.into(),
Value::new(EcmascriptModuleAssetType::Typescript),
EcmascriptInputTransformsVc::cell(vec![
EcmascriptInputTransform::TypeScript,
EcmascriptInputTransform::TypeScript {
use_define_for_class_fields: false,
},
EcmascriptInputTransform::React { refresh: false },
]),
context.compile_time_info(),
......
......@@ -54,7 +54,9 @@ impl Transition for NextServerToClientTransition {
context.into(),
Value::new(EcmascriptModuleAssetType::Typescript),
EcmascriptInputTransformsVc::cell(vec![
EcmascriptInputTransform::TypeScript,
EcmascriptInputTransform::TypeScript {
use_define_for_class_fields: false,
},
EcmascriptInputTransform::React { refresh: false },
]),
context.compile_time_info(),
......
......@@ -101,7 +101,9 @@ impl Transition for NextEdgeTransition {
virtual_asset.into(),
context.into(),
Value::new(EcmascriptModuleAssetType::Typescript),
EcmascriptInputTransformsVc::cell(vec![EcmascriptInputTransform::TypeScript]),
EcmascriptInputTransformsVc::cell(vec![EcmascriptInputTransform::TypeScript {
use_define_for_class_fields: false,
}]),
context.compile_time_info(),
InnerAssetsVc::cell(indexmap! {
"ENTRY".to_string() => asset
......
......@@ -28,6 +28,7 @@ use crate::{
next_build::{get_external_next_compiled_package_mapping, get_postcss_package_mapping},
next_config::NextConfigVc,
next_import_map::get_next_server_import_map,
typescript::get_typescript_transform_options,
util::foreign_code_context_condition,
};
......@@ -228,6 +229,8 @@ pub async fn get_server_module_options_context(
.clone_if()
};
let tsconfig = get_typescript_transform_options(project_path);
let module_options_context = match ty.into_value() {
ServerContextType::Pages { .. } | ServerContextType::PagesData { .. } => {
let module_options_context = ModuleOptionsContext {
......@@ -239,7 +242,7 @@ pub async fn get_server_module_options_context(
enable_styled_jsx: true,
enable_postcss_transform,
enable_webpack_loaders,
enable_typescript_transform: true,
enable_typescript_transform: Some(tsconfig),
rules: vec![(
foreign_code_context_condition,
module_options_context.clone().cell(),
......@@ -258,7 +261,7 @@ pub async fn get_server_module_options_context(
enable_styled_jsx: true,
enable_postcss_transform,
enable_webpack_loaders,
enable_typescript_transform: true,
enable_typescript_transform: Some(tsconfig),
rules: vec![(
foreign_code_context_condition,
module_options_context.clone().cell(),
......@@ -279,7 +282,7 @@ pub async fn get_server_module_options_context(
enable_jsx: true,
enable_postcss_transform,
enable_webpack_loaders,
enable_typescript_transform: true,
enable_typescript_transform: Some(tsconfig),
rules: vec![(
foreign_code_context_condition,
module_options_context.clone().cell(),
......@@ -296,7 +299,7 @@ pub async fn get_server_module_options_context(
ModuleOptionsContext {
enable_postcss_transform,
enable_webpack_loaders,
enable_typescript_transform: true,
enable_typescript_transform: Some(tsconfig),
rules: vec![(
foreign_code_context_condition,
module_options_context.clone().cell(),
......@@ -315,7 +318,7 @@ pub async fn get_server_module_options_context(
enable_styled_jsx: true,
enable_postcss_transform,
enable_webpack_loaders,
enable_typescript_transform: true,
enable_typescript_transform: Some(tsconfig),
rules: vec![(
foreign_code_context_condition,
module_options_context.clone().cell(),
......@@ -333,7 +336,7 @@ pub async fn get_server_module_options_context(
#[turbo_tasks::function]
pub fn get_build_module_options_context() -> ModuleOptionsContextVc {
ModuleOptionsContext {
enable_typescript_transform: true,
enable_typescript_transform: Some(Default::default()),
..Default::default()
}
.cell()
......
......@@ -86,7 +86,9 @@ impl PageLoaderAssetVc {
loader_entry_asset,
this.client_context,
Value::new(EcmascriptModuleAssetType::Typescript),
EcmascriptInputTransformsVc::cell(vec![EcmascriptInputTransform::TypeScript]),
EcmascriptInputTransformsVc::cell(vec![EcmascriptInputTransform::TypeScript {
use_define_for_class_fields: false,
}]),
this.client_context.compile_time_info(),
InnerAssetsVc::cell(indexmap! {
"PAGE".to_string() => this.client_context.process(this.entry_asset, Value::new(ReferenceType::Entry(EntryReferenceSubType::Page)))
......
......@@ -718,7 +718,9 @@ impl SsrEntryVc {
this.context,
Value::new(EcmascriptModuleAssetType::Typescript),
EcmascriptInputTransformsVc::cell(vec![
EcmascriptInputTransform::TypeScript,
EcmascriptInputTransform::TypeScript {
use_define_for_class_fields: false,
},
EcmascriptInputTransform::React { refresh: false },
]),
this.context.compile_time_info(),
......
......@@ -167,7 +167,9 @@ fn as_es_module_asset(asset: AssetVc, context: AssetContextVc) -> EcmascriptModu
asset,
context,
Value::new(EcmascriptModuleAssetType::Typescript),
EcmascriptInputTransformsVc::cell(vec![EcmascriptInputTransform::TypeScript]),
EcmascriptInputTransformsVc::cell(vec![EcmascriptInputTransform::TypeScript {
use_define_for_class_fields: false,
}]),
context.compile_time_info(),
)
}
......@@ -250,7 +252,9 @@ fn route_executor(context: AssetContextVc, configs: InnerAssetsVc) -> AssetVc {
next_asset("entry/router.ts"),
context,
Value::new(EcmascriptModuleAssetType::Typescript),
EcmascriptInputTransformsVc::cell(vec![EcmascriptInputTransform::TypeScript]),
EcmascriptInputTransformsVc::cell(vec![EcmascriptInputTransform::TypeScript {
use_define_for_class_fields: false,
}]),
context.compile_time_info(),
configs,
)
......
use anyhow::Result;
use turbo_tasks_fs::FileSystemPathVc;
use turbopack::module_options::{TypescriptTransformOptions, TypescriptTransformOptionsVc};
use turbopack_core::{
resolve::{find_context_file, node::node_cjs_resolve_options, FindContextFileResult},
source_asset::SourceAssetVc,
};
use turbopack_ecmascript::typescript::resolve::{read_from_tsconfigs, read_tsconfigs, tsconfig};
// Get the transform options for specifically for the typescript's runtime
// outputs
#[turbo_tasks::function]
pub async fn get_typescript_transform_options(
project_path: FileSystemPathVc,
) -> Result<TypescriptTransformOptionsVc> {
let tsconfig = find_context_file(project_path, tsconfig());
let tsconfig = match *tsconfig.await? {
FindContextFileResult::Found(path, _) => Some(
read_tsconfigs(
path.read(),
SourceAssetVc::new(path).into(),
node_cjs_resolve_options(path.root()),
)
.await?,
),
FindContextFileResult::NotFound(_) => None,
};
let use_define_for_class_fields = if let Some(tsconfig) = tsconfig {
read_from_tsconfigs(&tsconfig, |json, _| {
json["compilerOptions"]["useDefineForClassFields"].as_bool()
})
.await?
.unwrap_or(false)
} else {
false
};
let ts_transform_options = TypescriptTransformOptions {
use_define_for_class_fields,
..Default::default()
};
Ok(ts_transform_options.cell())
}
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