Skip to content

Commit 0cee9aa

Browse files
justin808claude
andcommitted
Update webpack config templates for unified rspack/webpack support
Implements unified configuration approach where the same webpack config files work with both webpack and rspack bundlers, based on the assets_bundler setting in shakapacker.yml. ## Changes **development.js.tt**: - Add config import from shakapacker to access assets_bundler setting - Conditional React Refresh plugin loading: - Rspack: @rspack/plugin-react-refresh - Webpack: @pmmmwh/react-refresh-webpack-plugin - Prevents "window not found" errors when using rspack **serverWebpackConfig.js.tt**: - Replace hardcoded webpack require with bundler variable - Bundler conditionally requires @rspack/core or webpack - Use bundler.optimize.LimitChunkCountPlugin instead of webpack-specific - Eliminates warnings about webpack when using rspack **RSPACK_IMPLEMENTATION.md**: - Document webpack config template changes - Explain unified configuration approach ## Benefits - Single set of config files works for both bundlers - No warnings when using rspack about webpack.config.js - Seamless switching between bundlers via bin/switch-bundler - Follows pattern from react_on_rails-demos PR #20 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 52c85c6 commit 0cee9aa

File tree

3 files changed

+39
-12
lines changed

3 files changed

+39
-12
lines changed

RSPACK_IMPLEMENTATION.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,26 @@ The `--rspack` flag allows users to generate a React on Rails application using
2828
- Adds `assets_bundler: 'rspack'` to shakapacker.yml default section
2929
- Changes `webpack_loader` to `'swc'` (Rspack works best with SWC transpiler)
3030

31-
### 3. Bundler Switching Script (`lib/generators/react_on_rails/templates/base/base/bin/switch-bundler`)
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`)
3251

3352
Created a new executable script that allows switching between webpack and rspack after installation:
3453

lib/generators/react_on_rails/templates/base/base/config/webpack/development.js.tt

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
<%= add_documentation_reference(config[:message], "// https://github.com/shakacode/react_on_rails_demo_ssr_hmr/blob/master/config/webpack/development.js") %>
22

3-
const { devServer, inliningCss } = require('shakapacker');
3+
const { devServer, inliningCss, config } = require('shakapacker');
44

55
const generateWebpackConfigs = require('./generateWebpackConfigs');
66

77
const developmentEnvOnly = (clientWebpackConfig, _serverWebpackConfig) => {
8-
// React Refresh (Fast Refresh) setup - only when webpack-dev-server is running (HMR mode)
9-
// This matches the condition in generateWebpackConfigs.js and babel.config.js
8+
// React Refresh (Fast Refresh) setup - only when dev server is running (HMR mode)
109
if (process.env.WEBPACK_SERVE) {
1110
// eslint-disable-next-line global-require
12-
const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
13-
clientWebpackConfig.plugins.push(
14-
new ReactRefreshWebpackPlugin({
15-
// Use default overlay configuration for better compatibility
16-
}),
17-
);
11+
if (config.assets_bundler === 'rspack') {
12+
// Rspack uses @rspack/plugin-react-refresh for React Fast Refresh
13+
const ReactRefreshPlugin = require('@rspack/plugin-react-refresh');
14+
clientWebpackConfig.plugins.push(new ReactRefreshPlugin());
15+
} else {
16+
// Webpack uses @pmmmwh/react-refresh-webpack-plugin
17+
const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
18+
clientWebpackConfig.plugins.push(
19+
new ReactRefreshWebpackPlugin({
20+
// Use default overlay configuration for better compatibility
21+
}),
22+
);
23+
}
1824
}
1925
};
2026

lib/generators/react_on_rails/templates/base/base/config/webpack/serverWebpackConfig.js.tt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
const { merge, config } = require('shakapacker');
44
const commonWebpackConfig = require('./commonWebpackConfig');
55

6-
const webpack = require('webpack');
6+
const bundler = config.assets_bundler === 'rspack'
7+
? require('@rspack/core')
8+
: require('webpack');
79

810
const configureServer = () => {
911
// We need to use "merge" because the clientConfigObject, EVEN after running
@@ -40,7 +42,7 @@ const configureServer = () => {
4042
serverWebpackConfig.optimization = {
4143
minimize: false,
4244
};
43-
serverWebpackConfig.plugins.unshift(new webpack.optimize.LimitChunkCountPlugin({ maxChunks: 1 }));
45+
serverWebpackConfig.plugins.unshift(new bundler.optimize.LimitChunkCountPlugin({ maxChunks: 1 }));
4446

4547
// Custom output for the server-bundle that matches the config in
4648
// config/initializers/react_on_rails.rb

0 commit comments

Comments
 (0)