Skip to content

Commit

Permalink
perf(styled-components): Do not allocate if visitors are not used (#392)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 authored Jan 24, 2025
1 parent 5886faf commit 53bd1e6
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

6 changes: 6 additions & 0 deletions packages/styled-components/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @swc/plugin-styled-components

## 6.1.0

### Minor Changes

- d532813: Avoid allocation when not used

## 6.0.4

### Patch Changes
Expand Down
6 changes: 6 additions & 0 deletions packages/styled-components/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ Then update your `.swcrc` file like below:

# @swc/plugin-styled-components

## 6.1.0

### Minor Changes

- d532813: Avoid allocation when not used

## 6.0.4

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/styled-components/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@swc/plugin-styled-components",
"version": "6.0.4",
"version": "6.1.0",
"description": "SWC plugin for styled-components",
"main": "swc_plugin_styled_components.wasm",
"scripts": {
Expand Down
7 changes: 3 additions & 4 deletions packages/styled-components/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use styled_components::Config;
use swc_common::{SourceMapper, Spanned};
use swc_core::{
common::FileName,
ecma::{ast::Program, visit::VisitMutWith},
ecma::ast::Program,
plugin::{
metadata::TransformPluginMetadataContextKind,
plugin_transform,
Expand All @@ -33,10 +33,9 @@ fn styled_components(mut program: Program, data: TransformPluginProgramMetadata)
let pos = data.source_map.lookup_char_pos(program.span().lo);
let hash = pos.file.src_hash;

let mut pass =
styled_components::styled_components(file_name, hash, config, PluginCommentsProxy);
let pass = styled_components::styled_components(file_name, hash, config, PluginCommentsProxy);

program.visit_mut_with(&mut pass);
program.mutate(pass);

program
}
2 changes: 1 addition & 1 deletion packages/styled-components/transform/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ homepage = { workspace = true }
license = { workspace = true }
repository = { workspace = true }
rust-version = { workspace = true }
version = "0.98.1"
version = "0.98.2"


# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
58 changes: 43 additions & 15 deletions packages/styled-components/transform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ use std::{cell::RefCell, rc::Rc, sync::Arc};
use serde::Deserialize;
use swc_atoms::JsWord;
use swc_common::{comments::Comments, pass::Optional, FileName};
use swc_ecma_ast::Pass;
use swc_ecma_visit::VisitMut;
use swc_ecma_ast::{Pass, Program};

pub use crate::{
utils::{analyze, analyzer, State},
Expand Down Expand Up @@ -77,7 +76,7 @@ pub fn styled_components<C>(
src_file_hash: u128,
config: Config,
comments: C,
) -> impl Pass + VisitMut
) -> impl Pass
where
C: Comments,
{
Expand All @@ -90,18 +89,47 @@ where
enabled: config.css_prop,
visitor: transpile_css_prop(state.clone()),
},
Optional {
enabled: config.minify,
visitor: minify(state.clone()),
},
display_name_and_id(file_name, src_file_hash, config.clone(), state.clone()),
Optional {
enabled: config.transpile_template_literals,
visitor: template_literals(state.clone()),
},
Optional {
enabled: config.pure,
visitor: pure_annotation(comments, state),
MayWork {
pass: (
Optional {
enabled: config.minify,
visitor: minify(state.clone()),
},
display_name_and_id(file_name, src_file_hash, config.clone(), state.clone()),
Optional {
enabled: config.transpile_template_literals,
visitor: template_literals(state.clone()),
},
Optional {
enabled: config.pure,
visitor: pure_annotation(comments, state.clone()),
},
),
state,
},
)
}

struct MayWork<P>
where
P: Pass,
{
pass: P,
state: Rc<RefCell<State>>,
}

impl<P> Pass for MayWork<P>
where
P: Pass,
{
fn process(&mut self, p: &mut Program) {
{
let state = self.state.borrow();
if !state.need_work() {
return;
}
}

self.pass.process(p);
}
}
7 changes: 7 additions & 0 deletions packages/styled-components/transform/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ pub struct State {
}

impl State {
pub(crate) fn need_work(&self) -> bool {
self.styled_required.is_some()
|| self.imported_local_name.is_some()
|| !self.imported_local_named.is_empty()
|| self.imported_local_ns.is_some()
}

pub(crate) fn is_styled(&self, tag: &Expr) -> bool {
if let Expr::Call(CallExpr {
callee: Callee::Expr(callee),
Expand Down

0 comments on commit 53bd1e6

Please sign in to comment.