|
| 1 | +# Rspack Generator Option Implementation |
| 2 | + |
| 3 | +This document summarizes the implementation of the `--rspack` option for the React on Rails generator, based on the patterns from [PR #20 in react_on_rails-demos](https://github.com/shakacode/react_on_rails-demos/pull/20). |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The `--rspack` flag allows users to generate a React on Rails application using Rspack instead of Webpack as the bundler. Rspack provides significantly faster build times (~53-270ms vs typical webpack builds). |
| 8 | + |
| 9 | +## Changes Made |
| 10 | + |
| 11 | +### 1. Install Generator (`lib/generators/react_on_rails/install_generator.rb`) |
| 12 | + |
| 13 | +- **Added `--rspack` class option** (line 31-35): Boolean flag to enable Rspack bundler |
| 14 | +- **Updated `invoke_generators`** (line 82-83): Pass rspack option to base generator |
| 15 | +- **Added `add_rspack_dependencies` method** (line 499-513): Installs Rspack core packages: |
| 16 | + - `@rspack/core` |
| 17 | + - `rspack-manifest-plugin` |
| 18 | +- **Updated `add_dev_dependencies`** (line 515-534): Conditionally installs rspack or webpack refresh plugins: |
| 19 | + - Rspack: `@rspack/cli`, `@rspack/plugin-react-refresh`, `react-refresh` |
| 20 | + - Webpack: `@pmmmwh/react-refresh-webpack-plugin`, `react-refresh` |
| 21 | +- **Updated `add_js_dependencies`** (line 433): Calls `add_rspack_dependencies` when rspack flag is set |
| 22 | + |
| 23 | +### 2. Base Generator (`lib/generators/react_on_rails/base_generator.rb`) |
| 24 | + |
| 25 | +- **Added `--rspack` class option** (line 22-26): Boolean flag (passed from install generator) |
| 26 | +- **Updated `copy_packer_config`** (line 85-100): Calls `configure_rspack_in_shakapacker` after copying config |
| 27 | +- **Added `configure_rspack_in_shakapacker` method** (line 404-426): |
| 28 | + - Adds `assets_bundler: 'rspack'` to shakapacker.yml default section |
| 29 | + - Changes `webpack_loader` to `'swc'` (Rspack works best with SWC transpiler) |
| 30 | + |
| 31 | +### 3. Webpack Configuration Templates |
| 32 | + |
| 33 | +Updated webpack configuration templates to support both webpack and rspack bundlers with unified config approach: |
| 34 | + |
| 35 | +**development.js.tt**: |
| 36 | + |
| 37 | +- Added `config` to shakapacker require to access `assets_bundler` setting |
| 38 | +- Conditional React Refresh plugin loading based on `config.assets_bundler`: |
| 39 | + - Rspack: Uses `@rspack/plugin-react-refresh` |
| 40 | + - Webpack: Uses `@pmmmwh/react-refresh-webpack-plugin` |
| 41 | +- Prevents "window not found" errors when using rspack |
| 42 | + |
| 43 | +**serverWebpackConfig.js.tt**: |
| 44 | + |
| 45 | +- Added `bundler` variable that conditionally requires `@rspack/core` or `webpack` |
| 46 | +- Changed `webpack.optimize.LimitChunkCountPlugin` to `bundler.optimize.LimitChunkCountPlugin` |
| 47 | +- Enables same config to work with both bundlers without warnings |
| 48 | +- Avoids hardcoding webpack-specific imports |
| 49 | + |
| 50 | +### 4. Bundler Switching Script (`lib/generators/react_on_rails/templates/base/base/bin/switch-bundler`) |
| 51 | + |
| 52 | +Created a new executable script that allows switching between webpack and rspack after installation: |
| 53 | + |
| 54 | +**Features:** |
| 55 | + |
| 56 | +- Updates `shakapacker.yml` with correct `assets_bundler` setting |
| 57 | +- Switches `webpack_loader` between 'swc' (rspack) and 'babel' (webpack) |
| 58 | +- Removes old bundler dependencies from package.json |
| 59 | +- Installs new bundler dependencies |
| 60 | +- Supports npm, yarn, and pnpm package managers |
| 61 | +- Auto-detects package manager from lock files |
| 62 | + |
| 63 | +**Usage:** |
| 64 | + |
| 65 | +```bash |
| 66 | +bin/switch-bundler rspack # Switch to Rspack |
| 67 | +bin/switch-bundler webpack # Switch to Webpack |
| 68 | +``` |
| 69 | + |
| 70 | +**Dependencies managed:** |
| 71 | + |
| 72 | +- **Webpack**: webpack, webpack-cli, webpack-dev-server, webpack-assets-manifest, webpack-merge, @pmmmwh/react-refresh-webpack-plugin |
| 73 | +- **Rspack**: @rspack/core, @rspack/cli, @rspack/plugin-react-refresh, rspack-manifest-plugin |
| 74 | + |
| 75 | +## Usage |
| 76 | + |
| 77 | +### Generate new app with Rspack: |
| 78 | + |
| 79 | +```bash |
| 80 | +rails generate react_on_rails:install --rspack |
| 81 | +``` |
| 82 | + |
| 83 | +### Generate with Rspack and TypeScript: |
| 84 | + |
| 85 | +```bash |
| 86 | +rails generate react_on_rails:install --rspack --typescript |
| 87 | +``` |
| 88 | + |
| 89 | +### Generate with Rspack and Redux: |
| 90 | + |
| 91 | +```bash |
| 92 | +rails generate react_on_rails:install --rspack --redux |
| 93 | +``` |
| 94 | + |
| 95 | +### Switch existing app to Rspack: |
| 96 | + |
| 97 | +```bash |
| 98 | +bin/switch-bundler rspack |
| 99 | +``` |
| 100 | + |
| 101 | +## Configuration Changes |
| 102 | + |
| 103 | +When `--rspack` is used, the following configuration changes are applied to `config/shakapacker.yml`: |
| 104 | + |
| 105 | +```yaml |
| 106 | +default: &default |
| 107 | + source_path: app/javascript |
| 108 | + assets_bundler: 'rspack' # Added |
| 109 | + # ... other settings |
| 110 | + webpack_loader: 'swc' # Changed from 'babel' |
| 111 | +``` |
| 112 | +
|
| 113 | +## Dependencies |
| 114 | +
|
| 115 | +### Rspack-specific packages installed: |
| 116 | +
|
| 117 | +**Production:** |
| 118 | +
|
| 119 | +- `@rspack/core` - Core Rspack bundler |
| 120 | +- `rspack-manifest-plugin` - Manifest generation for Rspack |
| 121 | + |
| 122 | +**Development:** |
| 123 | + |
| 124 | +- `@rspack/cli` - Rspack CLI tools |
| 125 | +- `@rspack/plugin-react-refresh` - React Fast Refresh for Rspack |
| 126 | +- `react-refresh` - React Fast Refresh runtime |
| 127 | + |
| 128 | +### Webpack packages NOT installed with --rspack: |
| 129 | + |
| 130 | +**Production:** |
| 131 | + |
| 132 | +- `webpack` |
| 133 | +- `webpack-assets-manifest` |
| 134 | +- `webpack-merge` |
| 135 | + |
| 136 | +**Development:** |
| 137 | + |
| 138 | +- `webpack-cli` |
| 139 | +- `webpack-dev-server` |
| 140 | +- `@pmmmwh/react-refresh-webpack-plugin` |
| 141 | + |
| 142 | +## Performance Benefits |
| 143 | + |
| 144 | +According to PR #20: |
| 145 | + |
| 146 | +- Build times: ~53-270ms with Rspack vs typical webpack builds |
| 147 | +- Approximately 20x faster transpilation with SWC (used by Rspack) |
| 148 | +- Faster development builds and CI runs |
| 149 | + |
| 150 | +## Testing |
| 151 | + |
| 152 | +The implementation follows existing generator patterns and passes RuboCop checks with zero offenses. |
| 153 | + |
| 154 | +## Compatibility |
| 155 | + |
| 156 | +- Works with existing webpack configuration files (unified config approach) |
| 157 | +- Compatible with TypeScript option (`--typescript`) |
| 158 | +- Compatible with Redux option (`--redux`) |
| 159 | +- Supports all package managers (npm, yarn, pnpm) |
| 160 | +- Reversible via `bin/switch-bundler` script |
0 commit comments