|
| 1 | +# SWC Migration Guide |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This document describes the migration from Babel to SWC for JavaScript/TypeScript transpilation in React on Rails projects using Shakapacker 9.0+. |
| 6 | + |
| 7 | +## What is SWC? |
| 8 | + |
| 9 | +SWC (Speedy Web Compiler) is a Rust-based JavaScript/TypeScript compiler that is approximately 20x faster than Babel. Shakapacker 9.0+ uses SWC as the default transpiler. |
| 10 | + |
| 11 | +## Prerequisites |
| 12 | + |
| 13 | +- **Shakapacker 9.0+** - SWC support requires Shakapacker version 9.0 or higher |
| 14 | +- **Node.js 18+** - Recommended for best compatibility |
| 15 | +- **Yarn or npm** - For package management |
| 16 | + |
| 17 | +This guide assumes you're already using Shakapacker 9.0+. If you need to upgrade from an earlier version, see the [Shakapacker upgrade guide](https://github.com/shakacode/shakapacker/blob/main/docs/v8_to_v9_upgrade.md). |
| 18 | + |
| 19 | +## Migration Steps |
| 20 | + |
| 21 | +**Note**: This migration has been successfully implemented in the React on Rails standard dummy app (`spec/dummy`). The Pro dummy app (`react_on_rails_pro/spec/dummy`) continues using Babel for RSC stability. |
| 22 | + |
| 23 | +### 1. Install Required Dependencies |
| 24 | + |
| 25 | +```bash |
| 26 | +yarn add -D @swc/core swc-loader |
| 27 | +``` |
| 28 | + |
| 29 | +### 2. Update shakapacker.yml |
| 30 | + |
| 31 | +Change the `javascript_transpiler` setting from `babel` to `swc`: |
| 32 | + |
| 33 | +```yaml |
| 34 | +default: &default # Using SWC for faster JavaScript transpilation (20x faster than Babel) |
| 35 | + javascript_transpiler: swc |
| 36 | +``` |
| 37 | +
|
| 38 | +### 3. Create SWC Configuration File |
| 39 | +
|
| 40 | +Create `config/swc.config.js` in your Rails application root with the following content: |
| 41 | + |
| 42 | +```javascript |
| 43 | +let env; |
| 44 | +try { |
| 45 | + ({ env } = require('shakapacker')); |
| 46 | +} catch (error) { |
| 47 | + console.error('Failed to load shakapacker:', error.message); |
| 48 | + console.error('Make sure shakapacker is installed: yarn add shakapacker'); |
| 49 | + process.exit(1); |
| 50 | +} |
| 51 | +
|
| 52 | +const customConfig = { |
| 53 | + options: { |
| 54 | + jsc: { |
| 55 | + parser: { |
| 56 | + syntax: 'ecmascript', |
| 57 | + jsx: true, |
| 58 | + dynamicImport: true, |
| 59 | + }, |
| 60 | + transform: { |
| 61 | + react: { |
| 62 | + runtime: 'automatic', |
| 63 | + development: env.isDevelopment, |
| 64 | + refresh: env.isDevelopment && env.runningWebpackDevServer, |
| 65 | + useBuiltins: true, |
| 66 | + }, |
| 67 | + }, |
| 68 | + // Keep class names for better debugging and compatibility |
| 69 | + keepClassNames: true, |
| 70 | + }, |
| 71 | + env: { |
| 72 | + targets: '> 0.25%, not dead', |
| 73 | + }, |
| 74 | + }, |
| 75 | +}; |
| 76 | +
|
| 77 | +module.exports = customConfig; |
| 78 | +``` |
| 79 | + |
| 80 | +### 4. Test the Migration |
| 81 | + |
| 82 | +After configuring SWC, test your build process: |
| 83 | + |
| 84 | +```bash |
| 85 | +# Compile assets |
| 86 | +bundle exec rake shakapacker:compile |
| 87 | +
|
| 88 | +# Run tests |
| 89 | +bundle exec rspec |
| 90 | +``` |
| 91 | + |
| 92 | +## React Server Components (RSC) Compatibility |
| 93 | + |
| 94 | +### Current Status (2025) |
| 95 | + |
| 96 | +Based on research and testing, here are the key findings regarding SWC and React Server Components compatibility: |
| 97 | + |
| 98 | +#### ⚠️ Experimental Status |
| 99 | + |
| 100 | +- **SWC support for React Server Components is EXPERIMENTAL and UNSTABLE** |
| 101 | +- The React Compiler's SWC plugin is still experimental as of 2025 |
| 102 | +- SWC plugins in general do not follow semver for compatibility |
| 103 | +- Next.js recommends version 15.3.1+ for optimal SWC-based build performance with RSC |
| 104 | + |
| 105 | +#### Known Issues |
| 106 | + |
| 107 | +1. **Plugin Instability**: All SWC plugins, including React-related ones, are considered experimental and may have breaking changes without semver guarantees |
| 108 | + |
| 109 | +2. **Framework Dependencies**: React Server Components work best with frameworks that have explicit RSC support (like Next.js), as they require build-time infrastructure |
| 110 | + |
| 111 | +3. **Hydration Challenges**: When using RSC with SWC, hydration mismatches can occur and are difficult to debug |
| 112 | + |
| 113 | +4. **Library Compatibility**: Many popular React libraries are client-centric and may throw hydration errors when used in server components |
| 114 | + |
| 115 | +### Recommendations |
| 116 | + |
| 117 | +#### For Standard React Applications |
| 118 | + |
| 119 | +- ✅ **SWC is fully compatible** with standard React applications (client-side only) |
| 120 | +- ✅ All 305 React on Rails tests pass with SWC transpilation |
| 121 | +- ✅ Significant performance improvements (20x faster than Babel) |
| 122 | + |
| 123 | +#### For React Server Components |
| 124 | + |
| 125 | +- ⚠️ **Use with caution** - RSC support in SWC is experimental |
| 126 | +- 📝 **Document your configuration** carefully if using RSC with SWC |
| 127 | +- 🧪 **Extensive testing required** before production deployment |
| 128 | +- 🔄 **Monitor updates** to SWC and React Compiler for stability improvements |
| 129 | + |
| 130 | +### Alternative: Continue Using Babel for RSC |
| 131 | + |
| 132 | +If you need stable React Server Components support today: |
| 133 | + |
| 134 | +1. Keep `javascript_transpiler: babel` in shakapacker.yml |
| 135 | +2. Use the existing Babel configuration with RSC-specific plugins |
| 136 | +3. Wait for SWC RSC support to stabilize before migrating |
| 137 | + |
| 138 | +## Migration from Babel to SWC: Feature Comparison |
| 139 | + |
| 140 | +### Features Migrated Successfully |
| 141 | + |
| 142 | +| Babel Feature | SWC Equivalent | Notes | |
| 143 | +| ------------------ | --------------------------------- | --------------------------- | |
| 144 | +| JSX Transform | `jsc.transform.react` | Automatic runtime supported | |
| 145 | +| React Fast Refresh | `jsc.transform.react.refresh` | Works in development mode | |
| 146 | +| Dynamic Imports | `jsc.parser.dynamicImport` | Fully supported | |
| 147 | +| Class Properties | Built-in | No config needed | |
| 148 | +| TypeScript | `jsc.parser.syntax: 'typescript'` | Native support | |
| 149 | +
|
| 150 | +### Features Requiring Different Approach |
| 151 | +
|
| 152 | +| Babel Feature | SWC Approach | Migration Notes | |
| 153 | +| ------------------------------------------------ | ------------------------------ | ----------------------------------- | |
| 154 | +| `babel-plugin-transform-react-remove-prop-types` | Built-in optimization | Handled automatically in production | |
| 155 | +| `@babel/plugin-proposal-export-default-from` | `jsc.parser.exportDefaultFrom` | Parser option instead of plugin | |
| 156 | +| Babel macros | Not supported | Requires alternative implementation | |
| 157 | +| `@loadable/babel-plugin` | Manual code splitting | Use React.lazy() instead | |
| 158 | + |
| 159 | +### Features Not Supported by SWC |
| 160 | + |
| 161 | +1. **Babel Macros** - No equivalent, requires code refactoring |
| 162 | +2. **Some Babel Plugins** - Custom Babel plugins won't work, need alternatives |
| 163 | +3. **`.swcrc` files** - Not recommended with webpack; use `config/swc.config.js` instead |
| 164 | + |
| 165 | +## Performance Benefits |
| 166 | + |
| 167 | +Based on testing with React on Rails: |
| 168 | + |
| 169 | +- **Compilation Speed**: ~20x faster than Babel |
| 170 | +- **Development Experience**: Significantly faster HMR (Hot Module Replacement) |
| 171 | +- **Build Times**: Reduced from minutes to seconds for large applications |
| 172 | +- **Memory Usage**: Lower memory footprint during builds |
| 173 | + |
| 174 | +## Troubleshooting |
| 175 | + |
| 176 | +### Issue: PropTypes Not Being Stripped |
| 177 | + |
| 178 | +**Solution**: SWC automatically strips PropTypes in production mode. Ensure `NODE_ENV=production` is set. |
| 179 | + |
| 180 | +### Issue: CSS Modules Not Working |
| 181 | + |
| 182 | +**Solution**: CSS Modules handling is done by webpack, not by the transpiler. This should work the same with both Babel and SWC. |
| 183 | + |
| 184 | +### Issue: Decorators Not Working |
| 185 | + |
| 186 | +**Solution**: Enable decorators in SWC config: |
| 187 | + |
| 188 | +```javascript |
| 189 | +jsc: { |
| 190 | + parser: { |
| 191 | + decorators: true; |
| 192 | + } |
| 193 | +} |
| 194 | +``` |
| 195 | + |
| 196 | +### Issue: Class Names Being Mangled (Stimulus) |
| 197 | + |
| 198 | +**Solution**: Already configured with `keepClassNames: true` in our SWC config. |
| 199 | + |
| 200 | +### Issue: Build Fails with "Cannot find module '@swc/core'" |
| 201 | + |
| 202 | +**Solution**: Clear node_modules and reinstall: |
| 203 | + |
| 204 | +```bash |
| 205 | +rm -rf node_modules yarn.lock |
| 206 | +yarn install |
| 207 | +``` |
| 208 | + |
| 209 | +### Issue: Fast Refresh Not Working |
| 210 | + |
| 211 | +**Solution**: Ensure webpack-dev-server is running and check that: |
| 212 | + |
| 213 | +- `env.runningWebpackDevServer` is true in development |
| 214 | +- No syntax errors in components |
| 215 | +- Components follow Fast Refresh rules (no anonymous exports, must export React components) |
| 216 | + |
| 217 | +### Issue: Syntax Errors Not Being Caught |
| 218 | + |
| 219 | +**Solution**: SWC parser is more permissive than Babel. Add TypeScript or stricter ESLint configuration for better error catching: |
| 220 | + |
| 221 | +```bash |
| 222 | +yarn add -D @typescript-eslint/parser @typescript-eslint/eslint-plugin |
| 223 | +``` |
| 224 | + |
| 225 | +### Issue: TypeScript Files Not Transpiling |
| 226 | + |
| 227 | +**Solution**: For TypeScript files, update your SWC config to use TypeScript parser: |
| 228 | + |
| 229 | +```javascript |
| 230 | +jsc: { |
| 231 | + parser: { |
| 232 | + syntax: 'typescript', |
| 233 | + tsx: true, |
| 234 | + dynamicImport: true, |
| 235 | + }, |
| 236 | + // ... rest of config |
| 237 | +} |
| 238 | +``` |
| 239 | + |
| 240 | +## Testing Results |
| 241 | + |
| 242 | +All 305 RSpec tests pass successfully with SWC configuration: |
| 243 | + |
| 244 | +``` |
| 245 | +305 examples, 0 failures |
| 246 | +``` |
| 247 | + |
| 248 | +Test coverage includes: |
| 249 | + |
| 250 | +- Client-side rendering |
| 251 | +- Server-side rendering |
| 252 | +- Redux integration |
| 253 | +- React Router |
| 254 | +- CSS Modules |
| 255 | +- Image loading |
| 256 | +- Manual rendering |
| 257 | +- Shared stores |
| 258 | + |
| 259 | +## Conclusion |
| 260 | + |
| 261 | +**For React on Rails projects without React Server Components**: ✅ **Migration to SWC is recommended** |
| 262 | + |
| 263 | +The standard React on Rails dummy app (`spec/dummy`) successfully uses SWC, demonstrating its compatibility with core React on Rails features. |
| 264 | + |
| 265 | +**For projects using React Server Components**: ⚠️ **Stay with Babel for now** - The React on Rails Pro dummy app continues using Babel due to RSC's experimental status with SWC. Consider staying with Babel until SWC RSC support stabilizes, or conduct extensive testing before production deployment. |
| 266 | + |
| 267 | +## References |
| 268 | + |
| 269 | +- [Shakapacker SWC Documentation](https://github.com/shakacode/shakapacker/blob/main/docs/using_swc_loader.md) |
| 270 | +- [SWC Official Documentation](https://swc.rs/) |
| 271 | +- [React Compiler Documentation](https://react.dev/learn/react-compiler) |
| 272 | +- [React Server Components RFC](https://github.com/reactjs/rfcs/blob/main/text/0188-server-components.md) |
0 commit comments