Commit d433348
Add automatic precompile hook coordination in bin/dev (#2092)
## Summary
Adds automatic precompile hook coordination to `bin/dev`, eliminating
the need for manual coordination, sleep hacks, and duplicate task calls
in `Procfile.dev`.
**This PR is focused solely on the bin/dev coordination feature.** The
complementary idempotent locale generation feature is in PR #2093.
## How It Works
When you configure a `precompile_hook` in `config/shakapacker.yml`:
```yaml
default: &default
precompile_hook: 'bundle exec rake react_on_rails:locale'
```
`bin/dev` will now:
1. ✅ Run the hook **once** before starting development processes
2. ✅ Set `SHAKAPACKER_SKIP_PRECOMPILE_HOOK=true` environment variable
3. ✅ Pass the env var to all spawned processes (Rails, webpack, etc.)
4. ✅ Prevent webpack processes from re-running the hook independently
## Before & After
**Before (manual coordination with sleep hacks):**
```procfile
# Procfile.dev
wp-server: sleep 15 && bundle exec rake react_on_rails:locale && bin/shakapacker --watch
```
**After (automatic coordination via bin/dev):**
```procfile
# Procfile.dev
wp-server: bin/shakapacker --watch
```
```yaml
# config/shakapacker.yml
default: &default
precompile_hook: 'bundle exec rake react_on_rails:locale'
```
Clean, simple, reliable - no sleep hacks required!
## Key Features
**Shakapacker Version Detection**
- Checks if you're using Shakapacker < 9.4.0
- Displays friendly warning about `SHAKAPACKER_SKIP_PRECOMPILE_HOOK`
support
- Recommends upgrading to 9.4.0+ to avoid duplicate hook execution
**Error Handling**
- Exits immediately if precompile hook fails
- Shows clear error message with the failed command
- Suggests fixing or removing the hook from config
**Smart Skipping**
- Skips hook execution for `bin/dev kill` and `bin/dev help` commands
- Only runs hook for actual development modes (hmr, static, prod)
**Help Flag Handling**
- Detects `-h`/`--help` flags early
- Prevents hook execution when user just wants help
- Maintains clean separation between help and runtime logic
## Use Cases
**Locale generation (with PR #2093):**
```yaml
precompile_hook: 'bundle exec rake react_on_rails:locale'
```
**ReScript compilation:**
```yaml
precompile_hook: 'yarn rescript'
```
**Multiple tasks:**
```yaml
precompile_hook: 'bundle exec rake react_on_rails:locale && yarn rescript'
```
Any expensive build task that needs to run before webpack starts!
## Implementation Details
**Modified Files:**
- `lib/react_on_rails/dev/server_manager.rb` - Core coordination logic
- `spec/react_on_rails/dev/server_manager_spec.rb` - Comprehensive test
coverage (161 lines)
- `docs/building-features/process-managers.md` - Feature documentation
- `docs/building-features/i18n.md` - Usage example with locale
generation
**Test Coverage:**
- ✅ Hook execution for all modes (development, static, prod)
- ✅ Environment variable setting across all modes
- ✅ Skipping for kill/help commands
- ✅ Help flag handling (-h/--help)
- ✅ Shakapacker version warning (< 9.4.0)
- ✅ Error handling when hook fails
- ✅ No-hook configuration scenario
## Shakapacker Version Requirements
- **9.3.0+**: `precompile_hook` configuration support
- **9.4.0+**: `SHAKAPACKER_SKIP_PRECOMPILE_HOOK` env var support
(prevents duplicate execution)
If you're on Shakapacker < 9.4.0, `bin/dev` will warn you that the hook
may run multiple times.
## Related PRs
- PR #2093 - Idempotent locale generation (makes `react_on_rails:locale`
safe to call multiple times)
- Issue #2091 - Original feature request
## Breaking Changes
None - this is purely additive functionality.
## Testing
Run the test suite:
```bash
bundle exec rspec spec/react_on_rails/dev/server_manager_spec.rb
```
Manual testing:
```bash
# Configure a precompile_hook in config/shakapacker.yml
echo 'default: &default\n precompile_hook: "echo Running hook..."' >> config/shakapacker.yml
# Run bin/dev and verify:
bin/dev
# Should see: "🔧 Running Shakapacker precompile hook..."
# Should see: "Running hook..."
# Should see: "✅ Precompile hook completed successfully"
```
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent 2306825 commit d433348
File tree
5 files changed
+342
-11
lines changed- docs/building-features
- lib/react_on_rails/dev
- spec/react_on_rails/dev
5 files changed
+342
-11
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
26 | 35 | | |
27 | 36 | | |
28 | 37 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
24 | | - | |
| 24 | + | |
25 | 25 | | |
26 | | - | |
27 | | - | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
28 | 49 | | |
29 | 50 | | |
30 | 51 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
19 | | - | |
20 | | - | |
21 | | - | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
22 | 59 | | |
23 | 60 | | |
24 | 61 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
| 12 | + | |
11 | 13 | | |
12 | 14 | | |
13 | 15 | | |
| |||
145 | 147 | | |
146 | 148 | | |
147 | 149 | | |
148 | | - | |
| 150 | + | |
149 | 151 | | |
150 | 152 | | |
151 | 153 | | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
152 | 161 | | |
153 | 162 | | |
154 | 163 | | |
| |||
172 | 181 | | |
173 | 182 | | |
174 | 183 | | |
175 | | - | |
176 | | - | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
177 | 193 | | |
178 | 194 | | |
179 | 195 | | |
| |||
184 | 200 | | |
185 | 201 | | |
186 | 202 | | |
187 | | - | |
| 203 | + | |
188 | 204 | | |
189 | 205 | | |
190 | 206 | | |
| |||
194 | 210 | | |
195 | 211 | | |
196 | 212 | | |
197 | | - | |
| 213 | + | |
198 | 214 | | |
199 | 215 | | |
200 | 216 | | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
201 | 293 | | |
202 | 294 | | |
203 | 295 | | |
| |||
0 commit comments