Skip to content

Commit

Permalink
refactor: Do not duplicate SWC transforms in the final binary (#5200)
Browse files Browse the repository at this point in the history
### Description

This PR modifies generic instantiation to use `&dyn Comments` instead of
`SwcComments`, to reduce binary size along with
swc-project/swc#7489.

### Testing Instructions

I'll post the binary size once
swc-project/swc#7489 is merged.

---

 - Previous binary size: `133.4MiB`
 - New binary size: `132.4MiB`


---


Closes WEB-1148
  • Loading branch information
kdy1 authored and mehulkar committed Jun 5, 2023
1 parent e603df8 commit 9060b38
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 32 deletions.
48 changes: 24 additions & 24 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ mdxjs = { version = "0.1.12" }
modularize_imports = { version = "0.30.0" }
styled_components = { version = "0.57.0" }
styled_jsx = { version = "0.34.0" }
swc_core = { version = "0.76.37" }
swc_core = { version = "0.76.41" }
swc_emotion = { version = "0.33.0" }
swc_relay = { version = "0.5.0" }
testing = { version = "0.33.13" }
Expand Down
22 changes: 15 additions & 7 deletions crates/turbopack-ecmascript/src/transform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use anyhow::Result;
use async_trait::async_trait;
use swc_core::{
base::SwcComments,
common::{chain, util::take::Take, Mark, SourceMap},
common::{chain, comments::Comments, util::take::Take, Mark, SourceMap},
ecma::{
ast::{Module, ModuleItem, Program, Script},
preset_env::{self, Targets},
Expand Down Expand Up @@ -169,16 +169,22 @@ impl EcmascriptInputTransform {
..Default::default()
};

program.visit_mut_with(&mut react(
// Explicit type annotation to ensure that we don't duplicate transforms in the
// final binary
program.visit_mut_with(&mut react::<&dyn Comments>(
source_map.clone(),
Some(comments.clone()),
Some(&comments),
config,
top_level_mark,
unresolved_mark,
));
}
EcmascriptInputTransform::CommonJs => {
program.visit_mut_with(&mut swc_core::ecma::transforms::module::common_js(
// Explicit type annotation to ensure that we don't duplicate transforms in the
// final binary
program.visit_mut_with(&mut swc_core::ecma::transforms::module::common_js::<
&dyn Comments,
>(
unresolved_mark,
swc_core::ecma::transforms::module::util::Config {
allow_top_level_this: true,
Expand All @@ -188,7 +194,7 @@ impl EcmascriptInputTransform {
..Default::default()
},
swc_core::ecma::transforms::base::feature::FeatureFlag::all(),
Some(comments.clone()),
Some(&comments),
));
}
EcmascriptInputTransform::PresetEnv(env) => {
Expand Down Expand Up @@ -216,10 +222,12 @@ impl EcmascriptInputTransform {
module_program
};

// Explicit type annotation to ensure that we don't duplicate transforms in the
// final binary
*program = module_program.fold_with(&mut chain!(
preset_env::preset_env(
preset_env::preset_env::<&'_ dyn Comments>(
top_level_mark,
Some(comments.clone()),
Some(&comments),
config,
Assumptions::default(),
&mut FeatureFlag::empty(),
Expand Down

0 comments on commit 9060b38

Please sign in to comment.