Skip to content

Commit

Permalink
refactor(next-core): deprecate custom_ecmascript_transforms
Browse files Browse the repository at this point in the history
  • Loading branch information
kwonoj committed May 11, 2023
1 parent fa1370f commit 89f5f22
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 80 deletions.
25 changes: 15 additions & 10 deletions packages/next-swc/crates/next-core/src/next_client/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ use turbo_binding::{
free_var_references,
},
dev::{react_refresh::assert_can_resolve_react_refresh, DevChunkingContextVc},
ecmascript::EcmascriptInputTransform,
ecmascript::TransformPluginVc,
ecmascript_plugin::transform::directives::server::ServerDirectiveTransformer,
env::ProcessEnvAssetVc,
node::execution_context::ExecutionContextVc,
turbopack::{
Expand Down Expand Up @@ -196,10 +197,19 @@ pub async fn get_client_module_options_context(

let enable_emotion = *get_emotion_compiler_config(next_config).await?;

let mut source_transforms = vec![];
if let Some(relay_transform_plugin) = *get_relay_transform_plugin(next_config).await? {
source_transforms.push(relay_transform_plugin);
}
let source_transforms = vec![
*get_relay_transform_plugin(next_config).await?,
Some(TransformPluginVc::cell(Box::new(
ServerDirectiveTransformer::new(
// ServerDirective is not implemented yet and always reports an issue.
// We don't have to pass a valid transition name yet, but the API is prepared.
&StringVc::cell("TODO".to_string()),
),
))),
]
.into_iter()
.flatten()
.collect();

let custom_ecma_transform_plugins = Some(CustomEcmascriptTransformPluginsVc::cell(
CustomEcmascriptTransformPlugins {
Expand All @@ -209,11 +219,6 @@ pub async fn get_client_module_options_context(
));

let module_options_context = ModuleOptionsContext {
custom_ecmascript_transforms: vec![EcmascriptInputTransform::ServerDirective(
// ServerDirective is not implemented yet and always reports an issue.
// We don't have to pass a valid transition name yet, but the API is prepared.
StringVc::cell("TODO".to_string()),
)],
preset_env_versions: Some(env),
execution_context: Some(execution_context),
custom_ecma_transform_plugins,
Expand Down
26 changes: 5 additions & 21 deletions packages/next-swc/crates/next-core/src/next_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ use turbo_binding::{
ecmascript::{
EcmascriptInputTransformsVc, EcmascriptModuleAssetType, EcmascriptModuleAssetVc,
},
ecmascript_plugin::transform::emotion::EmotionTransformConfig,
ecmascript_plugin::transform::{
emotion::EmotionTransformConfig, relay::RelayConfig,
styled_components::StyledComponentsTransformConfig,
},
node::{
evaluate::evaluate,
execution_context::{ExecutionContext, ExecutionContextVc},
transforms::webpack::{WebpackLoaderConfigItems, WebpackLoaderConfigItemsVc},
},
turbopack::{
evaluate_context::node_evaluate_asset_context,
module_options::StyledComponentsTransformConfig,
},
turbopack::evaluate_context::node_evaluate_asset_context,
},
};
use turbo_tasks::{
Expand Down Expand Up @@ -454,22 +454,6 @@ pub enum ReactRemoveProperties {
Config { properties: Option<Vec<String>> },
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, TraceRawVcs)]
#[serde(rename_all = "camelCase")]
pub struct RelayConfig {
pub src: String,
pub artifact_directory: Option<String>,
pub language: Option<RelayLanguage>,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, TraceRawVcs)]
#[serde(rename_all = "lowercase")]
pub enum RelayLanguage {
TypeScript,
Flow,
JavaScript,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, TraceRawVcs)]
#[serde(untagged)]
pub enum RemoveConsoleConfig {
Expand Down
85 changes: 66 additions & 19 deletions packages/next-swc/crates/next-core/src/next_server/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ use turbo_binding::{
},
free_var_references,
},
ecmascript::EcmascriptInputTransform,
ecmascript::TransformPluginVc,
ecmascript_plugin::transform::directives::{
client::ClientDirectiveTransformer, server::ServerDirectiveTransformer,
},
node::execution_context::ExecutionContextVc,
turbopack::{
condition::ContextCondition,
Expand Down Expand Up @@ -277,6 +280,17 @@ pub async fn get_server_module_options_context(
.clone_if()
};

let client_directive_transform_plugin = Some(TransformPluginVc::cell(Box::new(
ClientDirectiveTransformer::new(&StringVc::cell("server-to-client".to_string())),
)));
let server_directive_transform_plugin = Some(TransformPluginVc::cell(Box::new(
ServerDirectiveTransformer::new(
// ServerDirective is not implemented yet and always reports an issue.
// We don't have to pass a valid transition name yet, but the API is prepared.
&StringVc::cell("TODO".to_string()),
),
)));

let tsconfig = get_typescript_transform_options(project_path);
let decorators_options = get_decorators_transform_options(project_path);
let mdx_rs_options = *next_config.mdx_rs().await?;
Expand All @@ -288,11 +302,12 @@ pub async fn get_server_module_options_context(
if let Some(relay_transform_plugin) = *get_relay_transform_plugin(next_config).await? {
source_transforms.push(relay_transform_plugin);
}
let output_transforms = vec![];

let custom_ecma_transform_plugins = Some(CustomEcmascriptTransformPluginsVc::cell(
CustomEcmascriptTransformPlugins {
source_transforms,
output_transforms: vec![],
source_transforms: source_transforms.clone(),
output_transforms: output_transforms.clone(),
},
));

Expand Down Expand Up @@ -335,12 +350,30 @@ pub async fn get_server_module_options_context(
}
}
ServerContextType::AppSSR { .. } => {
let mut base_source_transforms: Vec<TransformPluginVc> =
vec![server_directive_transform_plugin.clone()]
.into_iter()
.flatten()
.collect();

let base_ecma_transform_plugins = Some(CustomEcmascriptTransformPluginsVc::cell(
CustomEcmascriptTransformPlugins {
source_transforms: base_source_transforms.clone(),
output_transforms: vec![],
},
));

base_source_transforms.extend(source_transforms.clone());

let custom_ecma_transform_plugins = Some(CustomEcmascriptTransformPluginsVc::cell(
CustomEcmascriptTransformPlugins {
source_transforms: base_source_transforms,
output_transforms: output_transforms.clone(),
},
));

let module_options_context = ModuleOptionsContext {
custom_ecmascript_transforms: vec![EcmascriptInputTransform::ServerDirective(
// ServerDirective is not implemented yet and always reports an issue.
// We don't have to pass a valid transition name yet, but the API is prepared.
StringVc::cell("TODO".to_string()),
)],
custom_ecma_transform_plugins: base_ecma_transform_plugins,
execution_context: Some(execution_context),
..Default::default()
};
Expand Down Expand Up @@ -375,18 +408,32 @@ pub async fn get_server_module_options_context(
}
}
ServerContextType::AppRSC { .. } => {
let mut base_source_transforms: Vec<TransformPluginVc> = vec![
client_directive_transform_plugin,
server_directive_transform_plugin,
]
.into_iter()
.flatten()
.collect();

let base_ecma_transform_plugins = Some(CustomEcmascriptTransformPluginsVc::cell(
CustomEcmascriptTransformPlugins {
source_transforms: base_source_transforms.clone(),
output_transforms: vec![],
},
));

base_source_transforms.extend(source_transforms.clone());

let custom_ecma_transform_plugins = Some(CustomEcmascriptTransformPluginsVc::cell(
CustomEcmascriptTransformPlugins {
source_transforms: base_source_transforms,
output_transforms: output_transforms.clone(),
},
));

let module_options_context = ModuleOptionsContext {
custom_ecmascript_transforms: vec![
EcmascriptInputTransform::ClientDirective(StringVc::cell(
"server-to-client".to_string(),
)),
EcmascriptInputTransform::ServerDirective(
// ServerDirective is not implemented yet and always reports an issue.
// We don't have to pass a valid transition name yet, but the API is
// prepared.
StringVc::cell("TODO".to_string()),
),
],
custom_ecma_transform_plugins: base_ecma_transform_plugins,
execution_context: Some(execution_context),
..Default::default()
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
use std::path::PathBuf;

use anyhow::Result;
use turbo_binding::{
swc::custom_transform::relay::{Config, RelayLanguageConfig},
turbopack::{
ecmascript::{OptionTransformPluginVc, TransformPluginVc},
ecmascript_plugin::transform::relay::RelayTransformer,
},
use turbo_binding::turbopack::{
ecmascript::{OptionTransformPluginVc, TransformPluginVc},
ecmascript_plugin::transform::relay::RelayTransformer,
};

use crate::next_config::{NextConfigVc, RelayLanguage};
use crate::next_config::NextConfigVc;

/// Returns a transform plugin for the relay graphql transform.
#[turbo_tasks::function]
Expand All @@ -25,26 +20,13 @@ pub async fn get_relay_transform_plugin(
.relay
.as_ref()
.map(|config| {
// [TODO]: There are config mismatches between RelayConfig to swc_relay::Config
let swc_relay_config = Config {
artifact_directory: config.artifact_directory.as_ref().map(PathBuf::from),
language: config.language.as_ref().map_or(
RelayLanguageConfig::TypeScript,
|v| match v {
RelayLanguage::JavaScript => RelayLanguageConfig::JavaScript,
RelayLanguage::TypeScript => RelayLanguageConfig::TypeScript,
RelayLanguage::Flow => RelayLanguageConfig::Flow,
},
),
..Default::default()
};
OptionTransformPluginVc::cell(Some(TransformPluginVc::cell(Box::new(
RelayTransformer::new(swc_relay_config),
RelayTransformer::new(&config),
))))
})
.unwrap_or(Default::default())
.unwrap_or_default()
})
.unwrap_or(Default::default());
.unwrap_or_default();

Ok(transform_plugin)
}
13 changes: 8 additions & 5 deletions packages/next-swc/crates/next-core/src/transform_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ use turbo_binding::{
source_asset::SourceAssetVc,
},
ecmascript::typescript::resolve::{read_from_tsconfigs, read_tsconfigs, tsconfig},
ecmascript_plugin::transform::emotion::{
EmotionTransformConfig, EmotionTransformConfigVc, OptionEmotionTransformConfigVc,
ecmascript_plugin::transform::{
emotion::{
EmotionTransformConfig, EmotionTransformConfigVc, OptionEmotionTransformConfigVc,
},
styled_components::{
OptionStyledComponentsTransformConfigVc, StyledComponentsTransformConfig,
},
},
turbopack::module_options::{
DecoratorsKind, DecoratorsOptions, DecoratorsOptionsVc, JsxTransformOptions,
JsxTransformOptionsVc, OptionStyledComponentsTransformConfigVc,
StyledComponentsTransformConfig, TypescriptTransformOptions,
TypescriptTransformOptionsVc,
JsxTransformOptionsVc, TypescriptTransformOptions, TypescriptTransformOptionsVc,
},
},
};
Expand Down

0 comments on commit 89f5f22

Please sign in to comment.