Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: Use reference-based APIs and use Pass APIs #393

Merged
merged 38 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Cargo.lock

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

12 changes: 12 additions & 0 deletions packages/emotion/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# @swc/plugin-emotion

## 8.3.0

### Minor Changes

- 4e7336c: Remove needless allocations

## 8.2.0

### Minor Changes

- 54b4a1a: Remove needless allocations

## 8.1.0

### Minor Changes
Expand Down
12 changes: 12 additions & 0 deletions packages/emotion/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ Source code for plugin itself (not transforms) are copied from https://github.co

# @swc/plugin-emotion

## 8.3.0

### Minor Changes

- 4e7336c: Remove needless allocations

## 8.2.0

### Minor Changes

- 54b4a1a: Remove needless allocations

## 8.1.0

### Minor Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/emotion/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@swc/plugin-emotion",
"version": "8.1.0",
"version": "8.3.0",
"description": "SWC plugin for emotion css-in-js library",
"main": "swc_plugin_emotion.wasm",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/emotion/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub fn process_transform(program: Program, data: TransformPluginProgramMetadata)
let pos = source_map.lookup_char_pos(program.span().lo);
let hash = pos.file.src_hash as u32;
program.apply(swc_emotion::emotion(
config,
&config,
path,
hash,
source_map,
Expand Down
2 changes: 1 addition & 1 deletion packages/emotion/transform/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ license = { workspace = true }
name = "swc_emotion"
repository = { workspace = true }
rust-version = { workspace = true }
version = "0.74.2"
version = "0.75.0"


# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
37 changes: 18 additions & 19 deletions packages/emotion/transform/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
use std::{
borrow::Cow,
path::{Path, PathBuf},
sync::Arc,
};
use std::{borrow::Cow, path::Path, sync::Arc};

use base64::Engine;
use once_cell::sync::Lazy;
Expand Down Expand Up @@ -165,21 +161,24 @@ enum PackageMeta {
Namespace(EmotionModuleConfig),
}

pub fn emotion<C: Comments>(
emotion_options: EmotionOptions,
path: &Path,
pub fn emotion<'a, C>(
emotion_options: &'a EmotionOptions,
path: &'a Path,
src_file_hash: u32,
cm: Arc<SourceMapperDyn>,
comments: C,
) -> impl Pass {
) -> impl 'a + Pass
where
C: 'a + Comments,
{
EmotionTransformer::new(emotion_options, path, src_file_hash, cm, comments)
}

pub struct EmotionTransformer<C: Comments> {
pub options: EmotionOptions,
pub struct EmotionTransformer<'a, C: Comments> {
pub options: &'a EmotionOptions,
#[allow(unused)]
filepath_hash: Option<u32>,
filepath: PathBuf,
filepath: &'a Path,
dirname: Option<String>,
filename: Option<String>,
src_file_hash: u32,
Expand All @@ -196,10 +195,10 @@ pub struct EmotionTransformer<C: Comments> {
}

#[swc_trace]
impl<C: Comments> EmotionTransformer<C> {
pub fn new(
options: EmotionOptions,
path: &Path,
impl<'a, C: Comments> EmotionTransformer<'a, C> {
fn new(
options: &'a EmotionOptions,
path: &'a Path,
src_file_hash: u32,
cm: Arc<SourceMapperDyn>,
comments: C,
Expand All @@ -212,7 +211,7 @@ impl<C: Comments> EmotionTransformer<C> {
EmotionTransformer {
options,
filepath_hash: None,
filepath: path.to_owned(),
filepath: path,
src_file_hash,
dirname: path
.parent()
Expand Down Expand Up @@ -455,7 +454,7 @@ impl<C: Comments> EmotionTransformer<C> {
}
}

impl<C: Comments> Fold for EmotionTransformer<C> {
impl<C: Comments> Fold for EmotionTransformer<'_, C> {
fn fold_call_expr(&mut self, mut expr: CallExpr) -> CallExpr {
// If no package that we care about is imported, skip the following
// transformation logic.
Expand Down Expand Up @@ -987,7 +986,7 @@ fn remove_space_around_colon(input: &str, is_first_item: bool, is_last_item: boo
)
}

impl<C> Pass for EmotionTransformer<C>
impl<C> Pass for EmotionTransformer<'_, C>
where
C: Comments,
{
Expand Down
64 changes: 38 additions & 26 deletions packages/emotion/transform/tests/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ fn ts_syntax() -> Syntax {
#[fixture("tests/fixture/**/input.tsx")]
fn next_emotion_fixture(input: PathBuf) {
let output = input.parent().unwrap().join("output.ts");

let test_import_map = serde_json::from_str(include_str!("./testImportMap.json")).unwrap();

let options = EmotionOptions {
enabled: Some(true),
sourcemap: Some(true),
auto_label: Some(true),
import_map: Some(test_import_map),
..Default::default()
};

let path = PathBuf::from("input.ts");

test_fixture(
ts_syntax(),
&|tr| {
Expand All @@ -37,19 +50,11 @@ fn next_emotion_fixture(input: PathBuf) {
unresolved_mark,
);

let test_import_map =
serde_json::from_str(include_str!("./testImportMap.json")).unwrap();
let fm = tr.cm.load_file(&input).unwrap();
(
swc_emotion::emotion(
EmotionOptions {
enabled: Some(true),
sourcemap: Some(true),
auto_label: Some(true),
import_map: Some(test_import_map),
..Default::default()
},
&PathBuf::from("input.ts"),
&options,
&path,
fm.src_hash as u32,
tr.cm.clone(),
tr.comments.as_ref().clone(),
Expand Down Expand Up @@ -96,6 +101,15 @@ fn emotion_label_option_fixture(output: PathBuf) {
format!("[{output_folder_name}]").into()
};

let options = EmotionOptions {
enabled: Some(true),
sourcemap: Some(true),
auto_label: Some(true),
label_format: Some(label_option.clone()),
..Default::default()
};
let file_name = PathBuf::from(format!("{output_folder_name}/index.tsx"));

test_fixture(
ts_syntax(),
&|tr| {
Expand All @@ -117,14 +131,8 @@ fn emotion_label_option_fixture(output: PathBuf) {
let fm: std::sync::Arc<swc_common::SourceFile> = tr.cm.load_file(&input).unwrap();
(
swc_emotion::emotion(
EmotionOptions {
enabled: Some(true),
sourcemap: Some(true),
auto_label: Some(true),
label_format: Some(label_option.clone()),
..Default::default()
},
&PathBuf::from(format!("{output_folder_name}/index.tsx")),
&options,
&file_name,
fm.src_hash as u32,
tr.cm.clone(),
tr.comments.as_ref().clone(),
Expand Down Expand Up @@ -161,6 +169,16 @@ fn emotion_label(input: PathBuf, label: String) {
let mut output = PathBuf::from(&input);
output.set_extension("js");

let options = EmotionOptions {
enabled: Some(true),
sourcemap: Some(true),
auto_label: Some(true),
label_format: Some(label.clone().into()),
..Default::default()
};

let file_name = PathBuf::from(format!("{output_folder_name}/{input_file_name}"));

test_fixture(
ts_syntax(),
&|tr| {
Expand All @@ -182,14 +200,8 @@ fn emotion_label(input: PathBuf, label: String) {
let fm = tr.cm.load_file(&input).unwrap();
(
swc_emotion::emotion(
EmotionOptions {
enabled: Some(true),
sourcemap: Some(true),
auto_label: Some(true),
label_format: Some(label.clone().into()),
..Default::default()
},
&PathBuf::from(format!("{output_folder_name}/{input_file_name}")),
&options,
&file_name,
fm.src_hash as u32,
tr.cm.clone(),
tr.comments.as_ref().clone(),
Expand Down
12 changes: 12 additions & 0 deletions packages/styled-components/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# @swc/plugin-styled-components

## 6.3.0

### Minor Changes

- 4e7336c: Remove needless allocations

## 6.2.0

### Minor Changes

- 54b4a1a: Remove needless allocations

## 6.1.0

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

# @swc/plugin-styled-components

## 6.3.0

### Minor Changes

- 4e7336c: Remove needless allocations

## 6.2.0

### Minor Changes

- 54b4a1a: Remove needless allocations

## 6.1.0

### Minor 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.1.0",
"version": "6.3.0",
"description": "SWC plugin for styled-components",
"main": "swc_plugin_styled_components.wasm",
"scripts": {
Expand Down
9 changes: 6 additions & 3 deletions packages/styled-components/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,12 @@ 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 pass = styled_components::styled_components(file_name, hash, config, PluginCommentsProxy);

program.mutate(pass);
program.mutate(styled_components::styled_components(
&file_name,
hash,
&config,
PluginCommentsProxy,
));

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.2"
version = "0.99.0"


# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
Loading
Loading