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

feat(styled-components): Implement pure option #238

Merged
merged 29 commits into from
Nov 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
3100fba
implement `pure` option
ciffelia Nov 25, 2023
64e4a43
enable previously disabled test fixtures
ciffelia Nov 25, 2023
211561b
update test fixtures output
ciffelia Nov 25, 2023
679e587
add comments
ciffelia Nov 25, 2023
97db7a6
Merge branch 'main' into feat-styled-components-pure
ciffelia Nov 26, 2023
d7f3662
fix pure comments not added when expression has a dummy span
ciffelia Nov 26, 2023
49a5855
change the default value of `transpileTemplateLiterals` and `minify` …
ciffelia Nov 26, 2023
7c50cbb
remove redundant config items
ciffelia Nov 26, 2023
4abd5b6
Bump npm package: ./packages/constify
kdy1 Nov 26, 2023
38de554
Bump npm package: ./packages/emotion
kdy1 Nov 26, 2023
eaf3968
Bump npm package: ./packages/jest
kdy1 Nov 26, 2023
2fa2e05
Bump npm package: ./packages/loadable-components
kdy1 Nov 26, 2023
e2954d1
Bump npm package: ./packages/noop
kdy1 Nov 26, 2023
aa91a81
Bump npm package: ./packages/react-remove-properties
kdy1 Nov 26, 2023
54b25a7
Bump npm package: ./packages/relay
kdy1 Nov 26, 2023
0739ca7
Bump npm package: ./packages/remove-console
kdy1 Nov 26, 2023
3a67ce2
Bump npm package: ./packages/styled-components
kdy1 Nov 26, 2023
3ea6190
Bump npm package: ./packages/styled-jsx
kdy1 Nov 26, 2023
8348365
Bump npm package: ./packages/swc-magic
kdy1 Nov 26, 2023
88a47eb
Bump npm package: ./packages/transform-imports
kdy1 Nov 26, 2023
2b0a882
Bump cargo crate: modularize_imports
kdy1 Nov 26, 2023
6d02801
Bump cargo crate: react_remove_properties
kdy1 Nov 26, 2023
6af003d
Bump cargo crate: remove_console
kdy1 Nov 26, 2023
5bc5276
Bump cargo crate: styled_components
kdy1 Nov 26, 2023
f0b1c74
Bump cargo crate: styled_jsx
kdy1 Nov 26, 2023
bff1d0d
Bump cargo crate: swc_constify
kdy1 Nov 26, 2023
fb82f9a
Bump cargo crate: swc_emotion
kdy1 Nov 26, 2023
8b221f5
Bump cargo crate: swc_magic
kdy1 Nov 26, 2023
a26fec5
Bump cargo crate: swc_relay
kdy1 Nov 26, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 9 additions & 9 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 packages/constify/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@swc/plugin-constify",
"version": "0.1.33",
"version": "0.1.34",
"description": "SWC plugin for optimization",
"main": "swc_plugin_constify.wasm",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/constify/transform/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2021"
license = "Apache-2.0"
name = "swc_constify"
repository = "https://github.com/swc-project/plugins.git"
version = "0.28.0"
version = "0.29.0"

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

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": "2.5.103",
"version": "2.5.104",
"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/transform/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2021"
license = "Apache-2.0"
name = "swc_emotion"
repository = "https://github.com/swc-project/plugins.git"
version = "0.64.0"
version = "0.65.0"

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

Expand Down
2 changes: 1 addition & 1 deletion packages/jest/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@swc/plugin-jest",
"version": "1.5.103",
"version": "1.5.104",
"description": "SWC plugin for jest",
"main": "swc_plugin_jest.wasm",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/loadable-components/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@swc/plugin-loadable-components",
"version": "0.3.103",
"version": "0.3.104",
"description": "SWC plugin for `@loadable/components`",
"main": "swc_plugin_loadable_components.wasm",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/noop/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@swc/plugin-noop",
"version": "1.5.101",
"version": "1.5.102",
"description": "Noop SWC plugin, for debugging",
"main": "swc_plugin_noop.wasm",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-remove-properties/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@swc/plugin-react-remove-properties",
"version": "1.5.103",
"version": "1.5.104",
"description": "SWC plugin for https://www.npmjs.com/package/babel-plugin-react-remove-properties",
"main": "swc_plugin_react_remove_properties.wasm",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-remove-properties/transform/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2021"
license = "Apache-2.0"
name = "react_remove_properties"
repository = "https://github.com/swc-project/plugins.git"
version = "0.16.0"
version = "0.17.0"

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

Expand Down
2 changes: 1 addition & 1 deletion packages/relay/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@swc/plugin-relay",
"version": "1.5.103",
"version": "1.5.104",
"description": "SWC plugin for relay",
"main": "swc_plugin_relay.wasm",
"types": "./types.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/relay/transform/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2021"
license = "Apache-2.0"
name = "swc_relay"
repository = "https://github.com/swc-project/plugins.git"
version = "0.36.0"
version = "0.37.0"

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

Expand Down
2 changes: 1 addition & 1 deletion packages/remove-console/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@swc/plugin-remove-console",
"version": "1.5.103",
"version": "1.5.104",
"description": "SWC plugin for https://www.npmjs.com/package/babel-plugin-remove-console",
"main": "swc_plugin_remove_console.wasm",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/remove-console/transform/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2021"
license = "Apache-2.0"
name = "remove_console"
repository = "https://github.com/swc-project/plugins.git"
version = "0.17.0"
version = "0.18.0"

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

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": "1.5.103",
"version": "1.5.104",
"description": "SWC plugin for styled-components",
"main": "swc_plugin_styled_components.wasm",
"scripts": {
Expand Down
8 changes: 5 additions & 3 deletions packages/styled-components/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ use swc_core::{
common::FileName,
ecma::{ast::Program, visit::VisitMutWith},
plugin::{
metadata::TransformPluginMetadataContextKind, plugin_transform,
proxies::TransformPluginProgramMetadata,
metadata::TransformPluginMetadataContextKind,
plugin_transform,
proxies::{PluginCommentsProxy, TransformPluginProgramMetadata},
},
};

Expand All @@ -28,7 +29,8 @@ 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);
let mut pass =
styled_components::styled_components(file_name, hash, config, PluginCommentsProxy);

program.visit_mut_with(&mut pass);

Expand Down
2 changes: 1 addition & 1 deletion packages/styled-components/transform/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ include = ["Cargo.toml", "src/**/*.rs"]
license = "Apache-2.0"
name = "styled_components"
repository = "https://github.com/swc-project/plugins.git"
version = "0.88.0"
version = "0.89.0"

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

Expand Down
28 changes: 17 additions & 11 deletions packages/styled-components/transform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ use std::{cell::RefCell, rc::Rc};

use serde::Deserialize;
use swc_atoms::JsWord;
use swc_common::{chain, pass::Optional, FileName};
use swc_common::{chain, comments::Comments, pass::Optional, FileName};
use swc_ecma_visit::{Fold, VisitMut};

pub use crate::{
utils::{analyze, analyzer, State},
visitors::{
display_name_and_id::display_name_and_id, minify::visitor::minify,
template_literals::template_literals, transpile_css_prop::transpile::transpile_css_prop,
pure_annotation::pure_annotation, template_literals::template_literals,
transpile_css_prop::transpile::transpile_css_prop,
},
};

Expand Down Expand Up @@ -40,10 +41,10 @@ pub struct Config {
#[serde(default)]
pub top_level_import_paths: Vec<JsWord>,

#[serde(default)]
#[serde(default = "true_by_default")]
pub transpile_template_literals: bool,

#[serde(default)]
#[serde(default = "true_by_default")]
pub minify: bool,

#[serde(default)]
Expand All @@ -70,14 +71,15 @@ impl Config {
}
}

/// NOTE: **This is not complete**.
///
/// [pure] is not implemented.
pub fn styled_components(
pub fn styled_components<C>(
file_name: FileName,
src_file_hash: u128,
config: Config,
) -> impl Fold + VisitMut {
comments: C,
) -> impl Fold + VisitMut
where
C: Comments,
{
let state: Rc<RefCell<State>> = Default::default();
let config = Rc::new(config);

Expand All @@ -94,7 +96,11 @@ pub fn styled_components(
display_name_and_id(file_name, src_file_hash, config.clone(), state.clone()),
Optional {
enabled: config.transpile_template_literals,
visitor: template_literals(state)
}
visitor: template_literals(state.clone())
},
Optional {
enabled: config.pure,
visitor: pure_annotation(comments, state)
},
)
}
8 changes: 8 additions & 0 deletions packages/styled-components/transform/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,14 @@ impl State {
|| self.is_with_theme_helper(e)
}

pub(crate) fn is_pure_helper(&self, e: &Expr) -> bool {
self.is_create_global_style_helper(e)
|| self.is_css_helper(e)
|| self.is_use_theme(e)
|| self.is_keyframes_helper(e)
|| self.is_with_theme_helper(e)
}

fn is_css_helper(&self, e: &Expr) -> bool {
match e {
Expr::Ident(e) => Some(e.to_id()) == self.import_local_name("css", None),
Expand Down
1 change: 1 addition & 0 deletions packages/styled-components/transform/src/visitors/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod assign_style_required;
pub mod display_name_and_id;
pub mod minify;
pub mod pure_annotation;
pub mod template_literals;
pub mod transpile_css_prop;
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//! Port of https://github.com/styled-components/babel-plugin-styled-components/blob/4e2eb388d9c90f2921c306c760657d059d01a518/src/visitors/pure.js

use std::{cell::RefCell, rc::Rc};

use swc_common::{comments::Comments, Span};
use swc_ecma_ast::*;
use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith};

use crate::utils::State;

pub fn pure_annotation<C>(comments: C, state: Rc<RefCell<State>>) -> impl Fold + VisitMut
where
C: Comments,
{
as_folder(PureAnnotation { comments, state })
}

#[derive(Debug)]
struct PureAnnotation<C>
where
C: Comments,
{
comments: C,
state: Rc<RefCell<State>>,
}

impl<C> VisitMut for PureAnnotation<C>
where
C: Comments,
{
noop_visit_mut_type!();

fn visit_mut_expr(&mut self, expr: &mut Expr) {
expr.visit_mut_children_with(self);

let (callee_or_tag, span) = match expr {
Expr::Call(CallExpr {
span,
callee: Callee::Expr(callee),
..
}) => (callee, span),
Expr::TaggedTpl(TaggedTpl { span, tag, .. }) => (tag, span),
_ => return,
};
if !self.state.borrow().is_styled(callee_or_tag)
&& !self.state.borrow().is_pure_helper(callee_or_tag)
{
return;
}

if span.is_dummy_ignoring_cmt() {
*span = Span::dummy_with_cmt();
}
if !self.comments.has_flag(span.lo, "PURE") {
self.comments.add_pure_comment(span.lo);
}
}
}
7 changes: 6 additions & 1 deletion packages/styled-components/transform/tests/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ fn fixture(input: PathBuf) {

chain!(
resolver(Mark::new(), Mark::new(), false),
styled_components(fm.name.clone(), fm.src_hash, config.clone())
styled_components(
fm.name.clone(),
fm.src_hash,
config.clone(),
t.comments.clone()
)
)
},
&input,
Expand Down