Skip to content

Commit 948df6f

Browse files
committed
Revert changes to initializer, webpack files, and rspec config for minimal PR
1 parent 1bb4404 commit 948df6f

File tree

10 files changed

+143
-142
lines changed

10 files changed

+143
-142
lines changed

babel.config.js

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,32 @@
1-
// The source code including full typescript support is available at:
2-
// https://github.com/shakacode/react_on_rails_demo_ssr_hmr/blob/master/babel.config.js
3-
41
module.exports = function (api) {
5-
const defaultConfigFunc = require('shakapacker/package/babel/preset.js')
6-
const resultConfig = defaultConfigFunc(api)
7-
const isProductionEnv = api.env('production')
2+
const defaultConfigFunc = require('shakapacker/package/babel/preset.js');
3+
const resultConfig = defaultConfigFunc(api);
4+
const isProductionEnv = api.env('production');
85

96
const changesOnDefault = {
107
presets: [
118
[
129
'@babel/preset-react',
1310
{
11+
runtime: 'automatic',
1412
development: !isProductionEnv,
1513
useBuiltIns: true,
16-
runtime: 'automatic'
17-
}
18-
]
14+
},
15+
],
1916
].filter(Boolean),
2017
plugins: [
21-
// Enable React Refresh (Fast Refresh) only when webpack-dev-server is running (HMR mode)
22-
// This prevents React Refresh from trying to connect when using static compilation
23-
!isProductionEnv && process.env.WEBPACK_SERVE && 'react-refresh/babel',
24-
isProductionEnv && ['babel-plugin-transform-react-remove-prop-types',
18+
process.env.WEBPACK_SERVE && 'react-refresh/babel',
19+
isProductionEnv && [
20+
'babel-plugin-transform-react-remove-prop-types',
2521
{
26-
removeImport: true
27-
}
28-
]
22+
removeImport: true,
23+
},
24+
],
2925
].filter(Boolean),
30-
}
26+
};
3127

32-
resultConfig.presets = [...resultConfig.presets, ...changesOnDefault.presets]
33-
resultConfig.plugins = [...resultConfig.plugins, ...changesOnDefault.plugins ]
28+
resultConfig.presets = [...resultConfig.presets, ...changesOnDefault.presets];
29+
resultConfig.plugins = [...resultConfig.plugins, ...changesOnDefault.plugins];
3430

35-
return resultConfig
36-
}
31+
return resultConfig;
32+
};
Lines changed: 65 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,84 @@
11
# frozen_string_literal: true
22

3-
# See https://github.com/shakacode/react_on_rails/blob/master/docs/guides/configuration.md
4-
# for many more options.
5-
3+
# Shown below are the defaults for configuration
64
ReactOnRails.configure do |config|
7-
# This configures the script to run to build the production assets by webpack. Set this to nil
8-
# if you don't want react_on_rails building this file for you.
9-
# If nil, then the standard shakacode/shakapacker assets:precompile will run
10-
# config.build_production_command = nil
5+
# Define the files for we need to check for webpack compilation when running tests
6+
config.webpack_generated_files = %w[client-bundle.js server-bundle.js]
117

12-
################################################################################
13-
################################################################################
14-
# TEST CONFIGURATION OPTIONS
15-
# Below options are used with the use of this test helper:
16-
# ReactOnRails::TestHelper.configure_rspec_to_compile_assets(config)
17-
################################################################################
8+
config.build_test_command = "RAILS_ENV=test bin/shakapacker"
9+
config.build_production_command = "RAILS_ENV=production NODE_ENV=production bin/shakapacker"
1810

19-
# If you are using this in your spec_helper.rb (or rails_helper.rb):
20-
#
21-
# ReactOnRails::TestHelper.configure_rspec_to_compile_assets(config)
11+
# This is the file used for server rendering of React when using `(prerender: true)`
12+
# If you are never using server rendering, you may set this to "".
13+
# If you are using the same file for client and server rendering, having this set probably does
14+
# not affect performance.
15+
config.server_bundle_js_file = "server-bundle.js"
16+
17+
# React on Rails 16 compatibility: Workaround for removed error handling
2218
#
23-
# with rspec then this controls what npm command is run
24-
# to automatically refresh your webpack assets on every test run.
19+
# BREAKING CHANGE in v16: React on Rails 14.2.1 had robust error handling that would
20+
# fallback to the Shakapacker output path when bundle lookup failed. This was removed
21+
# in v16.0.1.rc.2, causing it to look in the wrong directory during tests.
2522
#
26-
# Alternately, you can remove the `ReactOnRails::TestHelper.configure_rspec_to_compile_assets`
27-
# and set the config/shakapacker.yml option for test to true.
28-
config.build_test_command = "RAILS_ENV=test bin/shakapacker"
23+
# This configuration tells React on Rails where to find bundles in test environment.
24+
# Without this, it defaults to public/webpack/test/ instead of public/packs/
25+
config.generated_assets_dir = Rails.public_path.join("packs").to_s if Rails.env.test?
2926

3027
################################################################################
28+
# CLIENT RENDERING OPTIONS
29+
# Below options can be overriden by passing options to the react_on_rails
30+
# `render_component` view helper method.
31+
################################################################################
32+
33+
# Default is false. Can be overriden at the component level.
34+
# Set to false for debugging issues before turning on to true.
35+
config.prerender = true
36+
37+
# default is true for development, off otherwise
38+
config.trace = Rails.env.development?
39+
3140
################################################################################
3241
# SERVER RENDERING OPTIONS
42+
# Applicable options can be overriden by passing options to the react_on_rails
43+
# `render_component` view helper method.
3344
################################################################################
34-
# This is the file used for server rendering of React when using `(prerender: true)`
35-
# If you are never using server rendering, you should set this to "".
36-
# Note, there is only one server bundle, unlike JavaScript where you want to minimize the size
37-
# of the JS sent to the client. For the server rendering, React on Rails creates a pool of
38-
# JavaScript execution instances which should handle any component requested.
39-
#
40-
# While you may configure this to be the same as your client bundle file, this file is typically
41-
# different. You should have ONE server bundle which can create all of your server rendered
42-
# React components.
43-
#
44-
config.server_bundle_js_file = "server-bundle.js"
4545

46-
# Configure where server bundles are output. Defaults to "ssr-generated".
47-
# This should match your webpack configuration for server bundles.
48-
config.server_bundle_output_path = "ssr-generated"
46+
# If set to true, this forces Rails to reload the server bundle if it is modified
47+
config.development_mode = Rails.env.development?
48+
49+
# For server rendering. This can be set to false so that server side messages are discarded.
50+
# Default is true. Be cautious about turning this off.
51+
config.replay_console = true
4952

50-
# Enforce that server bundles are only loaded from private (non-public) directories.
51-
# When true, server bundles will only be loaded from the configured server_bundle_output_path.
52-
# This is recommended for production to prevent server-side code from being exposed.
53-
config.enforce_private_server_bundles = true
53+
# Default is true. Logs server rendering messages to Rails.logger.info
54+
config.logging_on_server = true
5455

56+
# Change to true to raise exception on server if the JS code throws. Let's do this only if not
57+
# in production, as the JS code might still work on the client and we don't want to blow up the
58+
# whole Rails page.
59+
config.raise_on_prerender_error = !Rails.env.production?
60+
61+
# Server rendering only (not for render_component helper)
62+
# You can configure your pool of JS virtual machines and specify where it should load code:
63+
# On MRI, use `therubyracer` for the best performance
64+
# (see [discussion](https://github.com/reactjs/react-rails/pull/290))
65+
# On MRI, you'll get a deadlock with `pool_size` > 1
66+
# If you're using JRuby, you can increase `pool_size` to have real multi-threaded rendering.
67+
config.server_renderer_pool_size = 1 # increase if you're on JRuby
68+
config.server_renderer_timeout = 20 # seconds
69+
70+
################################################################################
71+
# I18N OPTIONS
5572
################################################################################
73+
# Replace the following line to the location where you keep translation.js & default.js.
74+
config.i18n_dir = Rails.root.join("client/app/libs/i18n")
75+
5676
################################################################################
57-
# FILE SYSTEM BASED COMPONENT REGISTRY
77+
# MISCELLANEOUS OPTIONS
5878
################################################################################
59-
# `components_subdirectory` is the name of the matching directories that contain automatically registered components
60-
# for use in the Rails views. The default is nil, you can enable the feature by updating it in the next line.
61-
config.components_subdirectory = "ror_components"
62-
#
63-
# For automated component registry, `render_component` view helper method tries to load bundle for component from
64-
# generated directory. default is false, you can pass option at the time of individual usage or update the default
65-
# in the following line
66-
config.auto_load_bundle = true
79+
80+
# This allows you to add additional values to the Rails Context. Implement one static method
81+
# called `custom_context(view_context)` and return a Hash.
82+
config.rendering_extension = nil
83+
config.i18n_output_format = "js"
6784
end

config/webpack/clientWebpackConfig.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
1-
// The source code including full typescript support is available at:
2-
// https://github.com/shakacode/react_on_rails_demo_ssr_hmr/blob/master/config/webpack/clientWebpackConfig.js
1+
// The source code including full typescript support is available at:
2+
// https://github.com/shakacode/react_on_rails_tutorial_with_ssr_and_hmr_fast_refresh/blob/master/config/webpack/clientWebpackConfig.js
33

4+
const webpack = require('webpack');
45
const commonWebpackConfig = require('./commonWebpackConfig');
56

67
const configureClient = () => {
78
const clientConfig = commonWebpackConfig();
89

10+
clientConfig.plugins.push(
11+
new webpack.ProvidePlugin({
12+
$: 'jquery',
13+
jQuery: 'jquery',
14+
ActionCable: '@rails/actioncable',
15+
}),
16+
);
17+
918
// server-bundle is special and should ONLY be built by the serverConfig
1019
// In case this entry is not deleted, a very strange "window" not found
1120
// error shows referring to window["webpackJsonp"]. That is because the
Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,35 @@
1-
// The source code including full typescript support is available at:
2-
// https://github.com/shakacode/react_on_rails_demo_ssr_hmr/blob/master/config/webpack/commonWebpackConfig.js
1+
// The source code including full typescript support is available at:
2+
// https://github.com/shakacode/react_on_rails_tutorial_with_ssr_and_hmr_fast_refresh/blob/master/config/webpack/commonWebpackConfig.js
33

44
// Common configuration applying to client and server configuration
55
const { generateWebpackConfig, merge } = require('shakapacker');
66

77
const baseClientWebpackConfig = generateWebpackConfig();
8-
98
const commonOptions = {
109
resolve: {
1110
extensions: ['.css', '.ts', '.tsx'],
1211
},
1312
};
1413

14+
// add sass resource loader
15+
const sassLoaderConfig = {
16+
loader: 'sass-resources-loader',
17+
options: {
18+
resources: './client/app/assets/styles/app-variables.scss',
19+
},
20+
};
21+
22+
const ignoreWarningsConfig = {
23+
ignoreWarnings: [/Module not found: Error: Can't resolve 'react-dom\/client'/],
24+
};
25+
26+
const scssConfigIndex = baseClientWebpackConfig.module.rules.findIndex((config) =>
27+
'.scss'.match(config.test),
28+
);
29+
30+
baseClientWebpackConfig.module.rules[scssConfigIndex].use.push(sassLoaderConfig);
31+
1532
// Copy the object using merge b/c the baseClientWebpackConfig and commonOptions are mutable globals
16-
const commonWebpackConfig = () => merge({}, baseClientWebpackConfig, commonOptions);
33+
const commonWebpackConfig = () => merge({}, baseClientWebpackConfig, commonOptions, ignoreWarningsConfig);
1734

18-
module.exports = commonWebpackConfig;
35+
module.exports = commonWebpackConfig;

config/webpack/development.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
1-
// The source code including full typescript support is available at:
2-
// https://github.com/shakacode/react_on_rails_demo_ssr_hmr/blob/master/config/webpack/development.js
1+
// The source code including full typescript support is available at:
2+
// https://github.com/shakacode/react_on_rails_tutorial_with_ssr_and_hmr_fast_refresh/blob/master/config/webpack/development.js
3+
4+
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
35

46
const { devServer, inliningCss } = require('shakapacker');
57

6-
const generateWebpackConfigs = require('./generateWebpackConfigs');
8+
const webpackConfig = require('./webpackConfig');
79

810
const developmentEnvOnly = (clientWebpackConfig, _serverWebpackConfig) => {
9-
// React Refresh (Fast Refresh) setup - only when webpack-dev-server is running (HMR mode)
10-
// This matches the condition in generateWebpackConfigs.js and babel.config.js
11-
if (process.env.WEBPACK_SERVE) {
11+
// plugins
12+
if (inliningCss) {
13+
// Note, when this is run, we're building the server and client bundles in separate processes.
14+
// Thus, this plugin is not applied to the server bundle.
15+
1216
// eslint-disable-next-line global-require
1317
const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
1418
clientWebpackConfig.plugins.push(
1519
new ReactRefreshWebpackPlugin({
16-
// Use default overlay configuration for better compatibility
20+
overlay: {
21+
sockPort: devServer.port,
22+
},
1723
}),
1824
);
1925
}
2026
};
2127

22-
module.exports = generateWebpackConfigs(developmentEnvOnly);
28+
module.exports = webpackConfig(developmentEnvOnly);

config/webpack/generateWebpackConfigs.js

Lines changed: 0 additions & 37 deletions
This file was deleted.

config/webpack/production.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
// The source code including full typescript support is available at:
2-
// https://github.com/shakacode/react_on_rails_demo_ssr_hmr/blob/master/config/webpack/production.js
1+
// The source code including full typescript support is available at:
2+
// https://github.com/shakacode/react_on_rails_tutorial_with_ssr_and_hmr_fast_refresh/blob/master/config/webpack/production.js
33

4-
const generateWebpackConfigs = require('./generateWebpackConfigs');
4+
process.env.NODE_ENV = process.env.NODE_ENV || 'production';
5+
6+
const webpackConfig = require('./webpackConfig');
57

68
const productionEnvOnly = (_clientWebpackConfig, _serverWebpackConfig) => {
79
// place any code here that is for production only
810
};
911

10-
module.exports = generateWebpackConfigs(productionEnvOnly);
12+
module.exports = webpackConfig(productionEnvOnly);

config/webpack/serverWebpackConfig.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
// The source code including full typescript support is available at:
2-
// https://github.com/shakacode/react_on_rails_demo_ssr_hmr/blob/master/config/webpack/serverWebpackConfig.js
1+
// The source code including full typescript support is available at:
2+
// https://github.com/shakacode/react_on_rails_tutorial_with_ssr_and_hmr_fast_refresh/blob/master/config/webpack/serverWebpackConfig.js
33

4-
const { merge, config } = require('shakapacker');
4+
const { config } = require('shakapacker');
55
const commonWebpackConfig = require('./commonWebpackConfig');
66

77
const webpack = require('webpack');
@@ -45,14 +45,13 @@ const configureServer = () => {
4545

4646
// Custom output for the server-bundle that matches the config in
4747
// config/initializers/react_on_rails.rb
48-
// Server bundles are output to a private directory (not public) for security
4948
serverWebpackConfig.output = {
5049
filename: 'server-bundle.js',
5150
globalObject: 'this',
5251
// If using the React on Rails Pro node server renderer, uncomment the next line
5352
// libraryTarget: 'commonjs2',
54-
path: require('path').resolve(__dirname, '../../ssr-generated'),
55-
// No publicPath needed since server bundles are not served via web
53+
path: config.outputPath,
54+
publicPath: config.publicPath,
5655
// https://webpack.js.org/configuration/output/#outputglobalobject
5756
};
5857

config/webpack/test.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
// The source code including full typescript support is available at:
2-
// https://github.com/shakacode/react_on_rails_demo_ssr_hmr/blob/master/config/webpack/test.js
3-
4-
const generateWebpackConfigs = require('./generateWebpackConfigs')
1+
const webpackConfig = require('./webpackConfig');
52

63
const testOnly = (_clientWebpackConfig, _serverWebpackConfig) => {
74
// place any code here that is for test only
8-
}
5+
};
96

10-
module.exports = generateWebpackConfigs(testOnly)
7+
module.exports = webpackConfig(testOnly);

spec/rails_helper.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,6 @@
3838
Rails.root.glob("spec/support/**/*.rb").sort.each { |f| require f }
3939

4040
RSpec.configure do |config|
41-
# Ensure that if we are running js tests, we are using latest webpack assets
42-
# This will use the defaults of :js and :server_rendering meta tags
43-
ReactOnRails::TestHelper.configure_rspec_to_compile_assets(config)
44-
end
45-
4641
config.include FactoryBot::Syntax::Methods
4742
# Ensure that if we are running js tests, we are using latest webpack assets
4843
# This will use the defaults of :js and :server_rendering meta tags

0 commit comments

Comments
 (0)