Skip to content

Commit

Permalink
refactor(turbopack): remove deprecated options (vercel/turborepo#7240)
Browse files Browse the repository at this point in the history
### Description

#61481 cleans up the usage of
deprecated options, remove it from turbopack itself.

Closes PACK-2370
  • Loading branch information
kwonoj authored Feb 2, 2024
1 parent 82d926e commit 52ed65a
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 146 deletions.
45 changes: 28 additions & 17 deletions crates/turbopack-cli/src/contexts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ use turbo_tasks::{Value, Vc};
use turbo_tasks_fs::{FileSystem, FileSystemPath};
use turbopack::{
condition::ContextCondition,
ecmascript::TreeShakingMode,
module_options::{CustomEcmascriptTransformPlugins, JsxTransformOptions, ModuleOptionsContext},
ecmascript::{EcmascriptInputTransform, TreeShakingMode},
module_options::{
JsxTransformOptions, ModuleOptionsContext, ModuleRule, ModuleRuleCondition,
ModuleRuleEffect,
},
resolve_options_context::ResolveOptionsContext,
ModuleAssetContext,
};
Expand Down Expand Up @@ -123,24 +126,32 @@ async fn get_client_module_options_context(

let versions = *env.runtime_versions().await?;

let custom_ecma_transform_plugins = Some(CustomEcmascriptTransformPlugins::cell(
CustomEcmascriptTransformPlugins {
source_transforms: vec![
Vc::cell(Box::new(
let conditions = ModuleRuleCondition::any(vec![
ModuleRuleCondition::ResourcePathEndsWith(".js".to_string()),
ModuleRuleCondition::ResourcePathEndsWith(".jsx".to_string()),
ModuleRuleCondition::ResourcePathEndsWith(".ts".to_string()),
ModuleRuleCondition::ResourcePathEndsWith(".tsx".to_string()),
]);

let custom_rules = ModuleRule::new(
conditions,
vec![ModuleRuleEffect::ExtendEcmascriptTransforms {
prepend: Vc::cell(vec![
EcmascriptInputTransform::Plugin(Vc::cell(Box::new(
EmotionTransformer::new(&EmotionTransformConfig::default())
.expect("Should be able to create emotion transformer"),
) as _),
Vc::cell(Box::new(StyledComponentsTransformer::new(
&StyledComponentsTransformConfig::default(),
)) as _),
Vc::cell(Box::new(StyledJsxTransformer::new(
) as _)),
EcmascriptInputTransform::Plugin(Vc::cell(Box::new(
StyledComponentsTransformer::new(&StyledComponentsTransformConfig::default()),
) as _)),
EcmascriptInputTransform::Plugin(Vc::cell(Box::new(StyledJsxTransformer::new(
module_options_context.use_lightningcss,
versions,
)) as _),
],
output_transforms: vec![],
},
));
)) as _)),
]),
append: Vc::cell(vec![]),
}],
);

let module_options_context = ModuleOptionsContext {
enable_jsx,
Expand All @@ -150,7 +161,7 @@ async fn get_client_module_options_context(
foreign_code_context_condition().await?,
module_options_context.clone().cell(),
)],
custom_ecma_transform_plugins,
custom_rules: vec![custom_rules],
..module_options_context
}
.cell();
Expand Down
48 changes: 28 additions & 20 deletions crates/turbopack-tests/tests/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ use turbo_tasks_fs::{
use turbo_tasks_memory::MemoryBackend;
use turbopack::{
condition::ContextCondition,
ecmascript::EcmascriptModuleAsset,
module_options::{CustomEcmascriptTransformPlugins, JsxTransformOptions, ModuleOptionsContext},
ecmascript::{EcmascriptInputTransform, EcmascriptModuleAsset},
module_options::{
JsxTransformOptions, ModuleOptionsContext, ModuleRule, ModuleRuleCondition,
ModuleRuleEffect,
},
resolve_options_context::ResolveOptionsContext,
ModuleAssetContext,
};
Expand Down Expand Up @@ -226,23 +229,28 @@ async fn run_test(resource: String) -> Result<Vc<FileSystemPath>> {
.free_var_references(free_var_references!(..defines.into_iter()).cell())
.cell();

let custom_ecma_transform_plugins = Some(CustomEcmascriptTransformPlugins::cell(
CustomEcmascriptTransformPlugins {
source_transforms: vec![
Vc::cell(Box::new(
EmotionTransformer::new(&EmotionTransformConfig {
sourcemap: Some(false),
..Default::default()
})
.expect("Should be able to create emotion transformer"),
) as _),
Vc::cell(Box::new(StyledComponentsTransformer::new(
&StyledComponentsTransformConfig::default(),
)) as _),
],
output_transforms: vec![],
},
));
let conditions = ModuleRuleCondition::any(vec![
ModuleRuleCondition::ResourcePathEndsWith(".js".to_string()),
ModuleRuleCondition::ResourcePathEndsWith(".jsx".to_string()),
ModuleRuleCondition::ResourcePathEndsWith(".ts".to_string()),
ModuleRuleCondition::ResourcePathEndsWith(".tsx".to_string()),
]);

let custom_rules = ModuleRule::new(
conditions,
vec![ModuleRuleEffect::ExtendEcmascriptTransforms {
prepend: Vc::cell(vec![
EcmascriptInputTransform::Plugin(Vc::cell(Box::new(
EmotionTransformer::new(&EmotionTransformConfig::default())
.expect("Should be able to create emotion transformer"),
) as _)),
EcmascriptInputTransform::Plugin(Vc::cell(Box::new(
StyledComponentsTransformer::new(&StyledComponentsTransformConfig::default()),
) as _)),
]),
append: Vc::cell(vec![]),
}],
);
let asset_context: Vc<Box<dyn AssetContext>> = Vc::upcast(ModuleAssetContext::new(
Vc::cell(HashMap::new()),
compile_time_info,
Expand All @@ -259,7 +267,7 @@ async fn run_test(resource: String) -> Result<Vc<FileSystemPath>> {
}
.cell(),
)],
custom_ecma_transform_plugins,
custom_rules: vec![custom_rules],
..Default::default()
}
.into(),
Expand Down
61 changes: 0 additions & 61 deletions crates/turbopack/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,67 +466,6 @@ async fn process_default_internal(
ModuleRuleEffect::ModuleType(module) => {
current_module_type = Some(*module);
}
ModuleRuleEffect::AddEcmascriptTransforms(additional_transforms) => {
current_module_type = match current_module_type {
Some(ModuleType::Ecmascript {
transforms,
options,
}) => Some(ModuleType::Ecmascript {
transforms: transforms.extend(*additional_transforms),
options,
}),
Some(ModuleType::Typescript {
transforms,
tsx,
analyze_types,
options,
}) => Some(ModuleType::Typescript {
transforms: transforms.extend(*additional_transforms),
tsx,
analyze_types,
options,
}),
Some(ModuleType::Mdx {
transforms,
options,
}) => Some(ModuleType::Mdx {
transforms: transforms.extend(*additional_transforms),
options,
}),
Some(module_type) => {
ModuleIssue {
ident,
title: StyledString::Text("Invalid module type".to_string())
.cell(),
description: StyledString::Text(
"The module type must be Ecmascript or Typescript to add \
Ecmascript transforms"
.to_string(),
)
.cell(),
}
.cell()
.emit();
Some(module_type)
}
None => {
ModuleIssue {
ident,
title: StyledString::Text("Missing module type".to_string())
.cell(),
description: StyledString::Text(
"The module type effect must be applied before adding \
Ecmascript transforms"
.to_string(),
)
.cell(),
}
.cell()
.emit();
None
}
};
}
ModuleRuleEffect::ExtendEcmascriptTransforms { prepend, append } => {
current_module_type = match current_module_type {
Some(ModuleType::Ecmascript {
Expand Down
27 changes: 1 addition & 26 deletions crates/turbopack/src/module_options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ impl ModuleOptions {
ref enable_postcss_transform,
ref enable_webpack_loaders,
preset_env_versions,
ref custom_ecma_transform_plugins,
ref custom_rules,
execution_context,
ref rules,
Expand All @@ -91,28 +90,7 @@ impl ModuleOptions {
}
}

let (before_transform_plugins, after_transform_plugins) =
if let Some(transform_plugins) = custom_ecma_transform_plugins {
let transform_plugins = transform_plugins.await?;
(
transform_plugins
.source_transforms
.iter()
.cloned()
.map(EcmascriptInputTransform::Plugin)
.collect(),
transform_plugins
.output_transforms
.iter()
.cloned()
.map(EcmascriptInputTransform::Plugin)
.collect(),
)
} else {
(vec![], vec![])
};

let mut transforms = before_transform_plugins;
let mut transforms = vec![];

// Order of transforms is important. e.g. if the React transform occurs before
// Styled JSX, there won't be JSX nodes for Styled JSX to transform.
Expand Down Expand Up @@ -176,7 +154,6 @@ impl ModuleOptions {
.iter()
.cloned()
.chain(transforms.iter().cloned())
.chain(after_transform_plugins.iter().cloned())
.collect(),
)
} else {
Expand All @@ -196,7 +173,6 @@ impl ModuleOptions {
.iter()
.cloned()
.chain(transforms.iter().cloned())
.chain(after_transform_plugins.iter().cloned())
.collect(),
);

Expand All @@ -217,7 +193,6 @@ impl ModuleOptions {
.iter()
.cloned()
.chain(transforms.iter().cloned())
.chain(after_transform_plugins.iter().cloned())
.collect(),
);

Expand Down
18 changes: 1 addition & 17 deletions crates/turbopack/src/module_options/module_options_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use indexmap::IndexMap;
use serde::{Deserialize, Serialize};
use turbo_tasks::{trace::TraceRawVcs, ValueDefault, Vc};
use turbopack_core::{environment::Environment, resolve::options::ImportMapping};
use turbopack_ecmascript::{references::esm::UrlRewriteBehavior, TransformPlugin, TreeShakingMode};
use turbopack_ecmascript::{references::esm::UrlRewriteBehavior, TreeShakingMode};
use turbopack_node::{
execution_context::ExecutionContext,
transforms::{postcss::PostCssTransformOptions, webpack::WebpackLoaderItems},
Expand Down Expand Up @@ -99,18 +99,6 @@ pub struct JsxTransformOptions {
pub runtime: Option<String>,
}

/// Configuration options for the custom ecma transform to be applied.
#[turbo_tasks::value(shared)]
#[derive(Default, Clone)]
pub struct CustomEcmascriptTransformPlugins {
/// List of plugins to be applied before the main transform.
/// Transform will be applied in the order of the list.
pub source_transforms: Vec<Vc<TransformPlugin>>,
/// List of plugins to be applied after the main transform.
/// Transform will be applied in the order of the list.
pub output_transforms: Vec<Vc<TransformPlugin>>,
}

#[turbo_tasks::value(shared)]
#[derive(Default, Clone)]
#[serde(default)]
Expand Down Expand Up @@ -152,10 +140,6 @@ pub struct ModuleOptionsContext {
// however we might want to unify them in the future.
pub enable_mdx_rs: Option<Vc<MdxTransformModuleOptions>>,
pub preset_env_versions: Option<Vc<Environment>>,
#[deprecated(
note = "Use custom_rules with ModuleRuleEffect::ExtendEcmascriptTransforms instead"
)]
pub custom_ecma_transform_plugins: Option<Vc<CustomEcmascriptTransformPlugins>>,
/// Custom rules to be applied after all default rules.
pub custom_rules: Vec<ModuleRule>,
pub execution_context: Option<Vc<ExecutionContext>>,
Expand Down
5 changes: 0 additions & 5 deletions crates/turbopack/src/module_options/module_rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,6 @@ impl ModuleRule {
#[derive(Debug, Clone)]
pub enum ModuleRuleEffect {
ModuleType(ModuleType),
#[deprecated(
note = "ExtendEcmascriptTransforms provides equivalent features, as well as supporting an \
additional way to prepend transforms."
)]
AddEcmascriptTransforms(Vc<EcmascriptInputTransforms>),
/// Allow to extend an existing Ecmascript module rules for the additional
/// transforms. First argument will prepend the existing transforms, and
/// the second argument will append the new transforms.
Expand Down

0 comments on commit 52ed65a

Please sign in to comment.