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 4 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
28 changes: 19 additions & 9 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,9 +41,13 @@ pub struct Config {
#[serde(default)]
pub top_level_import_paths: Vec<JsWord>,

/// NOTE: This option is enabled by default in
/// `babel-plugin-styled-components` but not in this plugin.
ciffelia marked this conversation as resolved.
Show resolved Hide resolved
#[serde(default)]
pub transpile_template_literals: bool,

/// NOTE: This option is enabled by default in
/// `babel-plugin-styled-components` but not in this plugin.
ciffelia marked this conversation as resolved.
Show resolved Hide resolved
#[serde(default)]
pub minify: bool,

Expand Down Expand Up @@ -70,14 +75,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 +100,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 @@ -253,6 +253,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,63 @@
//! 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, Spanned, DUMMY_SP};
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 is_styled_or_pure_helper = match expr {
ciffelia marked this conversation as resolved.
Show resolved Hide resolved
Expr::Call(CallExpr {
callee: Callee::Expr(callee),
..
}) => {
self.state.borrow().is_styled(callee) || self.state.borrow().is_pure_helper(callee)
}
Expr::TaggedTpl(tagged) => {
self.state.borrow().is_styled(&tagged.tag)
|| self.state.borrow().is_pure_helper(&tagged.tag)
}
_ => false,
};
if !is_styled_or_pure_helper {
return;
}

let span = expr.span();
if span == DUMMY_SP {
// NOTE: Styled components generated by transpile_css_prop don't have span.
ciffelia marked this conversation as resolved.
Show resolved Hide resolved
// Currently there is no way to add comments to them.
return;
}
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

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"transpileTemplateLiterals": true,
"minify": true,
"pure": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { createGlobalStyle } from 'styled-components';
const GlobalStyle = /*#__PURE__*/ createGlobalStyle([
"body{color:red;}"
]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"transpileTemplateLiterals": true,
"minify": true,
"pure": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import styled, { css } from 'styled-components';
const partial = /*#__PURE__*/ css([
"color:red;"
]);
const Component = /*#__PURE__*/ styled.div.withConfig({
displayName: "code__Component",
componentId: "sc-c31dfdff-0"
})([
"",
";background:blue;"
], partial);
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"transpileTemplateLiterals": true,
"minify": true,
"pure": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { keyframes } from 'styled-components';
const Animation = /*#__PURE__*/ keyframes([
"0%{opacity:0;}100%{opacity:1;}"
]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"transpileTemplateLiterals": true,
"minify": true,
"pure": true
}
Loading
Loading