Skip to content

Commit beb27f7

Browse files
authored
Add documentation for React Refresh and Styled Components transformer plugins (#429)
1 parent a6ea645 commit beb27f7

File tree

1 file changed

+154
-0
lines changed

1 file changed

+154
-0
lines changed

src/docs/guide/usage/transformer.md

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- Transforming TypeScript to ESNext.
66
- Transforming React JSX to ESNext, with built-in React Refresh.
77
- [TypeScript Isolated Declarations Emit](https://devblogs.microsoft.com/typescript/announcing-typescript-5-5-beta/#isolated-declarations) without using the TypeScript compiler.
8+
- Built-in support for popular plugins like styled-components.
89

910
## Installation
1011

@@ -23,6 +24,159 @@ Rust usage example can be found [here](https://github.com/oxc-project/oxc/blob/m
2324

2425
- [`unplugin-isolated-decl`](https://github.com/unplugin/unplugin-isolated-decl)
2526

27+
## Built-in Plugins
28+
29+
Oxc transformer includes built-in support for popular transformation plugins to improve developer experience and build performance.
30+
31+
### React Refresh
32+
33+
React Refresh (also known as React Fast Refresh) provides hot reloading capabilities for React components during development. This plugin automatically instruments React components to preserve state during development.
34+
35+
#### Usage
36+
37+
React Refresh is automatically enabled when transforming JSX code in development mode. To configure it explicitly:
38+
39+
```javascript
40+
import { transform } from "oxc-transform";
41+
42+
const result = transform("App.jsx", sourceCode, {
43+
jsx: {
44+
plugin: "react",
45+
development: true, // Enables React Refresh
46+
refresh: {
47+
refreshReg: "$RefreshReg$",
48+
refreshSig: "$RefreshSig$",
49+
emitFullSignatures: true,
50+
},
51+
},
52+
});
53+
```
54+
55+
#### Configuration Options
56+
57+
| Option | Type | Default | Description |
58+
| -------------------- | --------- | ---------------- | ----------------------------------------------------------- |
59+
| `refreshReg` | `string` | `"$RefreshReg$"` | The name of the function to register components for refresh |
60+
| `refreshSig` | `string` | `"$RefreshSig$"` | The name of the function to create signatures for refresh |
61+
| `emitFullSignatures` | `boolean` | `false` | Whether to emit full signatures for better debugging |
62+
63+
### Styled Components
64+
65+
The styled-components plugin adds comprehensive support for styled-components with server-side rendering, style minification, and enhanced debugging capabilities.
66+
67+
#### Basic Usage
68+
69+
```javascript
70+
import { transform } from "oxc-transform";
71+
72+
const result = transform("Component.jsx", sourceCode, {
73+
plugins: {
74+
styledComponents: {
75+
displayName: true,
76+
ssr: true,
77+
fileName: true,
78+
minify: true,
79+
},
80+
},
81+
});
82+
```
83+
84+
#### Example
85+
86+
**Input:**
87+
88+
```jsx
89+
import styled from "styled-components";
90+
91+
const Button = styled.div`
92+
color: blue;
93+
padding: 10px;
94+
`;
95+
```
96+
97+
**Output (with default options):**
98+
99+
```jsx
100+
import styled from "styled-components";
101+
102+
const Button = styled.div.withConfig({
103+
displayName: "Button",
104+
componentId: "sc-1234567-0",
105+
})(["color:blue;padding:10px;"]);
106+
```
107+
108+
#### Configuration Options
109+
110+
##### Core Options
111+
112+
| Option | Type | Default | Description |
113+
| ------------- | --------- | ------- | ----------------------------------------------------------------------------------- |
114+
| `displayName` | `boolean` | `true` | Enhances the attached CSS class name with component names for easier debugging |
115+
| `ssr` | `boolean` | `true` | Adds unique component IDs to avoid checksum mismatches during server-side rendering |
116+
| `fileName` | `boolean` | `true` | Controls whether the displayName is prefixed with the filename |
117+
118+
##### Template Literal Options
119+
120+
| Option | Type | Default | Description |
121+
| --------------------------- | --------- | ------- | ------------------------------------------------------------------------------ |
122+
| `transpileTemplateLiterals` | `boolean` | `true` | Converts template literals to a smaller representation for reduced bundle size |
123+
| `minify` | `boolean` | `true` | Minifies CSS content by removing whitespace and comments |
124+
125+
##### Advanced Options
126+
127+
| Option | Type | Default | Description |
128+
| ---------------------- | ---------- | ----------- | ------------------------------------------------------------- |
129+
| `pure` | `boolean` | `false` | Adds `/*#__PURE__*/` comments for better tree-shaking |
130+
| `namespace` | `string` | `undefined` | Adds a namespace prefix to component IDs |
131+
| `meaninglessFileNames` | `string[]` | `["index"]` | List of filenames considered meaningless for component naming |
132+
133+
##### Not Yet Implemented
134+
135+
| Option | Type | Default | Description |
136+
| --------------------- | ---------- | ------- | ------------------------------------- |
137+
| `cssProp` | `boolean` | `true` | JSX css prop transformation (planned) |
138+
| `topLevelImportPaths` | `string[]` | `[]` | Custom import path handling (planned) |
139+
140+
#### Supported Import Patterns
141+
142+
The plugin works with various styled-components import patterns:
143+
144+
```javascript
145+
// Default import
146+
import styled from "styled-components";
147+
148+
// Namespace import
149+
import * as styled from "styled-components";
150+
151+
// Named imports
152+
import { createGlobalStyle, css, keyframes } from "styled-components";
153+
154+
// Native and primitives
155+
import styled from "styled-components/native";
156+
import styled from "styled-components/primitives";
157+
```
158+
159+
#### Features
160+
161+
**✅ Fully Supported:**
162+
163+
- Display names for debugging
164+
- Filename prefixing in display names
165+
- Server-side rendering support
166+
- Template literal transpilation
167+
- CSS minification
168+
- Namespace prefixes
169+
- Pure annotations for call expressions
170+
171+
**⚠️ Partially Supported:**
172+
173+
- Pure annotations (call expressions only, not tagged templates due to bundler limitations)
174+
175+
**❌ Not Yet Implemented:**
176+
177+
- JSX css prop transformation
178+
- Custom import path handling
179+
26180
<!-- Links -->
27181

28182
[url-oxc-crate]: https://docs.rs/oxc

0 commit comments

Comments
 (0)