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

fix(styled-components): fix displayName and ssr feature not working in some cases #240

Merged
merged 26 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
f18cf35
remove unused file
ciffelia Nov 26, 2023
53c9ba5
stop ignoring some tests
ciffelia Nov 26, 2023
a85b623
add test for `meaninglessFileNames` option
ciffelia Nov 26, 2023
4ec2c01
fix `display_name_and_id` not working in some cases
ciffelia Nov 26, 2023
77d84f7
update snapshots
ciffelia Nov 26, 2023
af92d10
Bump npm package: ./packages/constify
kdy1 Nov 28, 2023
f85295c
Bump npm package: ./packages/emotion
kdy1 Nov 28, 2023
e395f12
Bump npm package: ./packages/jest
kdy1 Nov 28, 2023
58d5cdc
Bump npm package: ./packages/loadable-components
kdy1 Nov 28, 2023
2f7a62f
Bump npm package: ./packages/noop
kdy1 Nov 28, 2023
071b2e7
Bump npm package: ./packages/react-remove-properties
kdy1 Nov 28, 2023
b35e9d8
Bump npm package: ./packages/relay
kdy1 Nov 28, 2023
33d1b19
Bump npm package: ./packages/remove-console
kdy1 Nov 28, 2023
f149733
Bump npm package: ./packages/styled-components
kdy1 Nov 28, 2023
6cb0751
Bump npm package: ./packages/styled-jsx
kdy1 Nov 28, 2023
e5c02cf
Bump npm package: ./packages/swc-magic
kdy1 Nov 28, 2023
ae09814
Bump npm package: ./packages/transform-imports
kdy1 Nov 28, 2023
abf9261
Bump cargo crate: modularize_imports
kdy1 Nov 28, 2023
325a8b2
Bump cargo crate: react_remove_properties
kdy1 Nov 28, 2023
7c224a2
Bump cargo crate: remove_console
kdy1 Nov 28, 2023
aa222ed
Bump cargo crate: styled_components
kdy1 Nov 28, 2023
61de1ea
Bump cargo crate: styled_jsx
kdy1 Nov 28, 2023
ace65a7
Bump cargo crate: swc_constify
kdy1 Nov 28, 2023
61a519c
Bump cargo crate: swc_emotion
kdy1 Nov 28, 2023
6bf1317
Bump cargo crate: swc_magic
kdy1 Nov 28, 2023
dfd4810
Bump cargo crate: swc_relay
kdy1 Nov 28, 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

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -266,63 +266,40 @@ impl VisitMut for DisplayNameAndId {

Expr::Call(CallExpr {
callee: Callee::Expr(callee),
args,
..
}) => {
(
// styled()
self.state.borrow().is_styled(&*callee)
// callee is styled.div
self.state.borrow().is_styled(callee)
&& get_property_as_ident(callee)
.map(|v| v == "withConfig")
.map(|v| v != "withConfig")
.unwrap_or(false)
) || (
// styled(x)({})
self.state.borrow().is_styled(&*callee)
// callee is styled(MyComponent)
self.state.borrow().is_styled(callee)
&& !get_callee(callee)
.map(|callee| callee.is_member())
.unwrap_or(false)
) || (
// styled(x).attrs()({})
// callee is styled(MyComponent).attrs(...)
self.state.borrow().is_styled(callee)
&& get_callee(callee)
.map(|callee| {
callee.is_member()
&& get_property_as_ident(callee)
.map(|v| v == "withConfig")
.unwrap_or(false)
})
.and_then(get_property_as_ident)
.map(|v| v != "withConfig")
.unwrap_or(false)
) || (
// styled(x).withConfig({})
self.state.borrow().is_styled(&*callee)
// callee is styled(MyComponent).withConfig({ ... }), and componentId or
// displayName is not set
self.state.borrow().is_styled(callee)
&& get_callee(callee)
.map(|callee| {
callee.is_member()
&& get_property_as_ident(callee)
.map(|v| v == "withConfig")
.unwrap_or(false)
&& !args.is_empty()
&& args[0].spread.is_none()
&& match &*args[0].expr {
Expr::Object(first_arg) => {
!first_arg.props.iter().any(|prop| match prop {
PropOrSpread::Prop(prop) => {
match get_prop_name(prop) {
Some(PropName::Ident(prop_name)) => {
matches!(
&*prop_name.sym,
"componentId" | "displayName"
)
}
_ => false,
}
}
_ => false,
})
}
_ => false,
}
})
.and_then(get_property_as_ident)
.map(|v| v == "withConfig")
.unwrap_or(false)
&& Expr::as_call(callee)
.and_then(|with_config_call| with_config_call.args.get(0))
.filter(|first_arg| first_arg.spread.is_none())
.and_then(|first_arg| first_arg.expr.as_object())
.map(|first_arg_obj| !already_has(first_arg_obj))
.unwrap_or(false)
)
}
Expand Down
1 change: 0 additions & 1 deletion packages/styled-components/transform/src/visitors/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pub mod assign_style_required;
pub mod display_name_and_id;
pub mod minify;
pub mod pure_annotation;
Expand Down

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
Expand Up @@ -35,23 +35,31 @@ const WrappedComponent3 = styled(Inner).withConfig({
})({});
const WrappedComponent4 = styled(Inner).attrs(()=>({
something: 'else'
}))({});
})).withConfig({
displayName: "WrappedComponent4",
componentId: "sc-e0d5b7ad-8"
})({});
const WrappedComponent5 = styled.div.attrs(()=>({
something: 'else'
}))({});
})).withConfig({
displayName: "WrappedComponent5",
componentId: "sc-e0d5b7ad-9"
})({});
const WrappedComponent6 = styled.div.attrs(()=>({
something: 'else'
})).withConfig({
displayName: "WrappedComponent6",
componentId: "sc-e0d5b7ad-8"
componentId: "sc-e0d5b7ad-10"
})``;
const WrappedComponent7 = styled.div.withConfig({
shouldForwardProp: ()=>{},
displayName: "WrappedComponent7",
componentId: "sc-e0d5b7ad-9"
componentId: "sc-e0d5b7ad-11"
})({});
const WrappedComponent8 = styled.div.withConfig({
shouldForwardProp: ()=>{}
shouldForwardProp: ()=>{},
displayName: "WrappedComponent8",
componentId: "sc-e0d5b7ad-12"
}).attrs(()=>({
something: 'else'
}))({});
Expand All @@ -60,5 +68,5 @@ const WrappedComponent9 = styled.div.attrs(()=>({
})).withConfig({
shouldForwardProp: ()=>{},
displayName: "WrappedComponent9",
componentId: "sc-e0d5b7ad-10"
componentId: "sc-e0d5b7ad-13"
})({});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"ssr": false,
"fileName": false,
"transpileTemplateLiterals": false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ import _styled from "styled-components";
import React from "react";
import { css } from "styled-components";
export default function Example() {
return <_StyledDiv>oops</_StyledDiv>;
return <_StyledDiv>oops</_StyledDiv>;
}
const someCss = css(["color:red;"]);

const someCss = css([
"color:red;"
]);
var _StyledDiv = _styled("div").withConfig({
displayName: "code___StyledDiv",
componentId: "sc-7mydya-0"
})(["", ""], someCss);
displayName: "code___StyledDiv",
componentId: "sc-216c6228-0"
})([
"",
""
], someCss);
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"ssr": false,
"displayName": false,
"minify": false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import styled from 'styled-components';
const Named = styled.div([
"\n width: 100%;\n"
]);
const NamedWithInterpolation = styled.div([
"\n color: ",
";\n"
], (color)=>props.color);
const Wrapped = styled(Inner)([
"color: red;"
]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import styled from "styled-components";

const Test = styled.div`color: red;`;
const before = styled.div`color: blue;`;
styled.div``;
export default styled.button``;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"meaninglessFileNames": ["code"],
"transpileTemplateLiterals": false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import styled from "styled-components";
const Test = styled.div.withConfig({
displayName: "use-directory-name__Test",
componentId: "sc-a2611da4-0"
})`color:red;`;
const before = styled.div.withConfig({
displayName: "use-directory-name__before",
componentId: "sc-a2611da4-1"
})`color:blue;`;
styled.div.withConfig({
displayName: "use-directory-name",
componentId: "sc-a2611da4-2"
})``;
export default styled.button.withConfig({
displayName: "use-directory-name",
componentId: "sc-a2611da4-3"
})``;
Loading