Skip to content

Commit 7a68d13

Browse files
authored
fix(styled-jsx): Allow specifying multiple selectors in :global (#476)
1 parent 31810e3 commit 7a68d13

14 files changed

Lines changed: 102 additions & 6 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/styled-jsx/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# @swc/plugin-styled-jsx
22

3+
## 8.0.3
4+
5+
### Patch Changes
6+
7+
- ae8b938: Allow specifying multiple selectors in :global
8+
39
## 8.0.2
410

511
### Patch Changes

packages/styled-jsx/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
# @swc/plugin-styled-jsx
44

5+
## 8.0.3
6+
7+
### Patch Changes
8+
9+
- ae8b938: Allow specifying multiple selectors in :global
10+
511
## 8.0.2
612

713
### Patch Changes

packages/styled-jsx/__tests__/__snapshots__/wasm.test.ts.snap

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,30 @@ const Test = ()=>/*#__PURE__*/ React.createElement("div", {
10161016
"
10171017
`;
10181018
1019+
exports[`Should load swc-confidential wasm plugin correctly > Should transform global-multiple correctly 1`] = `
1020+
"import _JSXStyle from "styled-jsx/style";
1021+
const Test = ()=>/*#__PURE__*/ React.createElement("div", {
1022+
className: "jsx-3d9ecfaa448ef714"
1023+
}, /*#__PURE__*/ React.createElement("span", {
1024+
className: "jsx-3d9ecfaa448ef714"
1025+
}, "test"), /*#__PURE__*/ React.createElement(_JSXStyle, {
1026+
id: "3d9ecfaa448ef714"
1027+
}, ".c1{background:orange!important}"));
1028+
"
1029+
`;
1030+
1031+
exports[`Should load swc-confidential wasm plugin correctly > Should transform global-nested correctly 1`] = `
1032+
"import _JSXStyle from "styled-jsx/style";
1033+
const Test = ()=>/*#__PURE__*/ React.createElement("div", {
1034+
className: "jsx-258c8cca0b71d485"
1035+
}, /*#__PURE__*/ React.createElement("span", {
1036+
className: "jsx-258c8cca0b71d485"
1037+
}, "test"), /*#__PURE__*/ React.createElement(_JSXStyle, {
1038+
id: "258c8cca0b71d485"
1039+
}, ".p1.jsx-258c8cca0b71d485{background:purple!important}.p1.jsx-258c8cca0b71d485 .c1,.p1.jsx-258c8cca0b71d485 .c2.jsx-258c8cca0b71d485{background:orange!important}"));
1040+
"
1041+
`;
1042+
10191043
exports[`Should load swc-confidential wasm plugin correctly > Should transform global-redundant correctly 1`] = `
10201044
"import _JSXStyle from "styled-jsx/style";
10211045
export default function IndexPage() {

packages/styled-jsx/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@swc/plugin-styled-jsx",
3-
"version": "8.0.2",
3+
"version": "8.0.3",
44
"description": "SWC plugin for styled-jsx",
55
"main": "swc_plugin_styled_jsx.wasm",
66
"scripts": {

packages/styled-jsx/transform/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ license = { workspace = true }
1010
name = "styled_jsx"
1111
repository = { workspace = true }
1212
rust-version = { workspace = true }
13-
version = "0.91.2"
13+
version = "0.91.3"
1414

1515

1616
[features]

packages/styled-jsx/transform/src/transform_css_lightningcss.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use anyhow::{bail, Context, Error};
1010
use lightningcss::{
1111
error::ParserError,
1212
properties::custom::{TokenList, TokenOrValue},
13-
selector::{Combinator, Component, PseudoClass, Selector},
13+
selector::{Combinator, Component, PseudoClass, Selector, SelectorList},
1414
stylesheet::{MinifyOptions, ParserFlags, ParserOptions, PrinterOptions, StyleSheet},
1515
targets::{Browsers, Features, Targets},
1616
traits::{IntoOwned, ParseWithOptions, ToCss},
@@ -126,7 +126,7 @@ pub fn transform_css(
126126

127127
let targets = Targets {
128128
browsers: Some(convert_browsers(browsers)),
129-
include: Features::Nesting,
129+
include: Features::Nesting | Features::IsSelector,
130130
..Default::default()
131131
};
132132

@@ -574,7 +574,7 @@ impl CssNamespace {
574574
/// specification), but it is popular usage, so we just add `a ` at top and
575575
/// then remove it
576576
fn parse_token_list<'i>(tokens: &TokenList<'i>) -> Vec<Component<'i>> {
577-
let mut buf = "a ".to_string();
577+
let mut buf = "".to_string();
578578

579579
for t in tokens.0.iter() {
580580
match t {
@@ -624,6 +624,14 @@ fn parse_token_list<'i>(tokens: &TokenList<'i>) -> Vec<Component<'i>> {
624624
debug!("Parsing: {:?}", buf)
625625
}
626626

627+
if let Ok(s) = SelectorList::parse_string_with_options(&buf, Default::default()) {
628+
if s.0.len() != 1 {
629+
return vec![Component::Is(s.0.into_owned().into_boxed_slice())];
630+
}
631+
}
632+
633+
buf = format!("a {buf}");
634+
627635
let mut result: Vec<Component<'i>> = vec![];
628636

629637
let selector = Selector::parse_string_with_options(&buf, Default::default())
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const Test = () => (
2+
<div>
3+
<span>test</span>
4+
<style jsx>{`
5+
:global(.c1, .c2) {
6+
background: orange !important;
7+
}
8+
`}</style>
9+
</div>
10+
);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import _JSXStyle from "styled-jsx/style";
2+
const Test = ()=><div className={"jsx-3d9ecfaa448ef714"}>
3+
<span className={"jsx-3d9ecfaa448ef714"}>test</span>
4+
<_JSXStyle id={"3d9ecfaa448ef714"}>{":is(.c1,.c2){background:orange!important}"}</_JSXStyle>
5+
</div>;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import _JSXStyle from "styled-jsx/style";
2+
const Test = ()=><div className={"jsx-3d9ecfaa448ef714"}>
3+
<span className={"jsx-3d9ecfaa448ef714"}>test</span>
4+
<_JSXStyle id={"3d9ecfaa448ef714"}>{".c1{background:orange!important}"}</_JSXStyle>
5+
</div>;

0 commit comments

Comments
 (0)