Skip to content

Commit b1edb19

Browse files
DunqingCopilot
authored andcommitted
fix(transformer/styled-components): named styled import doesn't work (#16573)
close: rolldown/rolldown#7292
1 parent 6d3a2e8 commit b1edb19

File tree

5 files changed

+49
-4
lines changed

5 files changed

+49
-4
lines changed

crates/oxc_transformer/src/plugins/styled_components.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,9 @@ impl Default for StyledComponentsOptions {
234234
struct StyledComponentsBinding {
235235
/// `import * as styled from 'styled-components'`
236236
namespace: Option<SymbolId>,
237-
/// `import styled from 'styled-components'` or `import { default as styled } from 'styled-components'`
237+
/// `import styled from 'styled-components';`
238+
/// `import { default as styled } from 'styled-components';`
239+
/// `import { styled } from 'styled-components';`
238240
styled: Option<SymbolId>,
239241
/// Named imports like `import { createGlobalStyle, css, keyframes } from 'styled-components'`
240242
helpers: [Option<SymbolId>; 6],
@@ -478,7 +480,8 @@ impl<'a> StyledComponents<'a, '_> {
478480
let symbol_id = specifier.local.symbol_id();
479481
let imported_name = specifier.imported.name();
480482
match imported_name.as_str() {
481-
"default" => {
483+
// Handle `import { default as styled }` and `import { styled }`
484+
"default" | "styled" => {
482485
self.styled_bindings.styled = Some(symbol_id);
483486
}
484487
name => {

tasks/transform_conformance/snapshots/oxc.snap.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
commit: 79586034
22

3-
Passed: 200/331
3+
Passed: 201/332
44

55
# All Passed:
66
* babel-plugin-transform-class-static-block
@@ -1770,7 +1770,7 @@ after transform: ["Function", "babelHelpers"]
17701770
rebuilt : ["babelHelpers", "dec"]
17711771

17721772

1773-
# plugin-styled-components (24/39)
1773+
# plugin-styled-components (25/40)
17741774
* minify-comments/input.js
17751775
Unresolved references mismatch:
17761776
after transform: ["x", "y", "z"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { styled } from "styled-components";
2+
3+
const Test = styled.div`width: 100%;`;
4+
const Test2 = true ? styled.div`` : styled.div``;
5+
const styles = { One: styled.div`` }
6+
let Component;
7+
Component = styled.div``;
8+
const WrappedComponent = styled(Inner)``;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"plugins": [
3+
["styled-components", {
4+
"fileName": false,
5+
"transpileTemplateLiterals": false,
6+
"ssr": true
7+
}]
8+
]
9+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { styled } from "styled-components";
2+
const Test = styled.div.withConfig({
3+
displayName: "Test",
4+
componentId: "sc-yes67e-0"
5+
})`width:100%;`;
6+
const Test2 = true ? styled.div.withConfig({
7+
displayName: "Test2",
8+
componentId: "sc-yes67e-1"
9+
})`` : styled.div.withConfig({
10+
displayName: "Test2",
11+
componentId: "sc-yes67e-2"
12+
})``;
13+
const styles = { One: styled.div.withConfig({
14+
displayName: "One",
15+
componentId: "sc-yes67e-3"
16+
})`` };
17+
let Component;
18+
Component = styled.div.withConfig({
19+
displayName: "Component",
20+
componentId: "sc-yes67e-4"
21+
})``;
22+
const WrappedComponent = styled(Inner).withConfig({
23+
displayName: "WrappedComponent",
24+
componentId: "sc-yes67e-5"
25+
})``;

0 commit comments

Comments
 (0)