Skip to content

Commit 5fea2df

Browse files
committed
Address code review feedback
- Add error handling to swc.config.js for missing shakapacker - Add Prerequisites section documenting Shakapacker 9.0+ requirement - Expand Troubleshooting section with additional common issues: - Build fails with missing @swc/core - Fast Refresh not working - Syntax errors not being caught - TypeScript files not transpiling - Update config examples in documentation to include error handling - Add eslint-disable for global-require (necessary for error handling) These improvements make the migration guide more robust and help users debug common SWC configuration issues.
1 parent 7aaeb26 commit 5fea2df

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

docs/swc-migration.md

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ This document describes the migration from Babel to SWC for JavaScript/TypeScrip
88

99
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.
1010

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+
1119
## Migration Steps
1220

1321
**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.
@@ -32,7 +40,14 @@ default: &default # Using SWC for faster JavaScript transpilation (20x faster th
3240
Create `config/swc.config.js` in your Rails application root with the following content:
3341

3442
```javascript
35-
const { env } = require('shakapacker');
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+
}
3651
3752
const customConfig = {
3853
options: {
@@ -182,6 +197,46 @@ jsc: {
182197

183198
**Solution**: Already configured with `keepClassNames: true` in our SWC config.
184199

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+
185240
## Testing Results
186241

187242
All 305 RSpec tests pass successfully with SWC configuration:

spec/dummy/config/swc.config.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
const { env } = require('shakapacker');
1+
/* eslint-disable global-require */
2+
let env;
3+
try {
4+
({ env } = require('shakapacker'));
5+
} catch (error) {
6+
console.error('Failed to load shakapacker:', error.message);
7+
console.error('Make sure shakapacker is installed: yarn add shakapacker');
8+
process.exit(1);
9+
}
10+
/* eslint-enable global-require */
211

312
const customConfig = {
413
options: {

0 commit comments

Comments
 (0)