forked from swc-project/plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(styled-components): Implement
pure
option (swc-project#238)
This implements `pure` option in styled-components plugin. cf. vercel/next.js#30802
- Loading branch information
Showing
82 changed files
with
298 additions
and
259 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
58 changes: 58 additions & 0 deletions
58
packages/styled-components/transform/src/visitors/pure_annotation.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.