Skip to content

Commit 1295ce9

Browse files
authored
Make pack generation import statement commitable (#1610)
* swap functions so the right errors show up
1 parent 87fee8c commit 1295ce9

File tree

6 files changed

+35
-9
lines changed

6 files changed

+35
-9
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ Please follow the recommendations outlined at [keepachangelog.com](http://keepac
1414
## Versions
1515
### [Unreleased]
1616
Changes since the last non-beta release.
17+
#### Added
18+
- Pack Generation: Added functionality that will add an import statement, if missing, to the server bundle entrypoint even if the autobundle generated files still exist [PR 1610](https://github.com/shakacode/react_on_rails/pull/1610) by [judahmeek](https://github.com/judahmeek).
1719

1820
### [14.0.0] - 2024-04-03
1921
_Major bump because dropping support for Ruby 2.7 and deprecated `webpackConfigLoader.js`._

docs/guides/file-system-based-automated-bundle-generation.md

+16-5
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,26 @@ You can change the value in `config/initializers/react_on_rails` by updating it
3636
config.auto_load_bundle = true
3737
```
3838

39+
### Location of generated files
40+
Generated files will go to the following two directories:
41+
* Pack files for entrypoint components will be generated in the `{Shakapacker.config.source_entry_path}/generated` directory.
42+
* The interim server bundle file, which is only generated if you already have a server bundle entrypoint and have not set `make_generated_server_bundle_the_entrypoint` to `true`, will be generated in the `{Pathname(Shakapacker.config.source_entry_path).parent}/generated` directory.
43+
3944
### Update `.gitignore` file
40-
React on Rails automatically generates pack files for components to be registered in the `packs/generated` directory. To avoid committing generated files into the version control system, please update `.gitignore` to have
45+
To avoid committing generated files to your version control system, please update `.gitignore` to include:
4146

4247
```gitignore
4348
# Generated React on Rails packs
44-
app/javascript/packs/generated
49+
**/generated/**
4550
```
4651

47-
*Note: the directory might be different depending on the `source_entry_path` in `config/shakapacker.yml`.*
52+
### Commit changes to server bundle entrypoint
53+
If you already have an existing server bundle entrypoint and have not set `make_generated_server_bundle_the_entrypoint` to `true`, then pack generation will add an import statement to your existing server bundle entrypoint similar to:
54+
```javascript
55+
// import statement added by react_on_rails:generate_packs rake task
56+
import "./../generated/server-bundle-generated.js"
57+
```
58+
We recommend committing this import statement to your version control system.
4859

4960
## Usage
5061

@@ -126,7 +137,7 @@ The tricky part is to figure out which bundles to load on any Rails view. [Shaka
126137

127138
File-system-based automated pack generation simplifies this process with a new option for the view helpers.
128139

129-
For example, if you wanted to utilize our file-system based entrypoint generation for `FooComponentOne` & `BarComponentOne`, but not `BarComponentTwo` (for whatever reason), then...
140+
For example, if you wanted to utilize our file-system based entrypoint generation for `FooComponentOne` and `BarComponentOne`, but not `BarComponentTwo` (for whatever reason), then...
130141

131142
1. Remove generated entrypoints from parameters passed directly to `javascript_pack_tag` and `stylesheet_pack_tag`.
132143
2. Remove generated entrypoints from parameters passed directly to `append_javascript_pack_tag` and `append_stylesheet_pack_tag`.
@@ -186,7 +197,7 @@ For example, if you wanted to utilize our file-system based entrypoint generatio
186197
187198
If server rendering is enabled, the component will be registered for usage both in server and client rendering. In order to have separate definitions for client and server rendering, name the component files as `ComponentName.server.jsx` and `ComponentName.client.jsx`. The `ComponentName.server.jsx` file will be used for server rendering and the `ComponentName.client.jsx` file for client rendering. If you don't want the component rendered on the server, you should only have the `ComponentName.client.jsx` file.
188199
189-
Once generated, all server entrypoints will be imported into a file named `[ReactOnRails.configuration.server_bundle_js_file]-generated.js`, which in turn will be imported into a source file named the same as `ReactOnRails.configuration.server_bundle_js_file`. If your server bundling logic is such that your server bundle source entrypoint is not named the same as your `ReactOnRails.configuration.server_bundle_js_file` & changing it would be difficult, please let us know.
200+
Once generated, all server entrypoints will be imported into a file named `[ReactOnRails.configuration.server_bundle_js_file]-generated.js`, which in turn will be imported into a source file named the same as `ReactOnRails.configuration.server_bundle_js_file`. If your server bundling logic is such that your server bundle source entrypoint is not named the same as your `ReactOnRails.configuration.server_bundle_js_file` and changing it would be difficult, please let us know.
190201
191202
*Note: If specifying separate definitions for client and server rendering, please make sure to delete the generalized `ComponentName.jsx` file.*
192203

lib/react_on_rails/helper.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -441,11 +441,10 @@ def internal_react_component(react_component_name, options = {})
441441
"data-trace" => (render_options.trace ? true : nil),
442442
"data-dom-id" => render_options.dom_id)
443443

444+
load_pack_for_generated_component(react_component_name, render_options)
444445
# Create the HTML rendering part
445446
result = server_rendered_react_component(render_options)
446447

447-
load_pack_for_generated_component(react_component_name, render_options)
448-
449448
{
450449
render_options: render_options,
451450
tag: component_specification_tag,

lib/react_on_rails/packs_generator.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ def self.instance
1515
def generate_packs_if_stale
1616
return unless ReactOnRails.configuration.auto_load_bundle
1717

18+
add_generated_pack_to_server_bundle
1819
are_generated_files_present_and_up_to_date = Dir.exist?(generated_packs_directory_path) &&
1920
File.exist?(generated_server_bundle_file_path) &&
2021
!stale_or_missing_packs?
@@ -99,8 +100,8 @@ def add_generated_pack_to_server_bundle
99100
def generated_server_bundle_file_path
100101
return server_bundle_entrypoint if ReactOnRails.configuration.make_generated_server_bundle_the_entrypoint
101102

102-
generated_server_bundle_file_path = server_bundle_entrypoint.sub(".js", "-generated.js")
103-
generated_server_bundle_file_name = component_name(generated_server_bundle_file_path)
103+
generated_interim_server_bundle_path = server_bundle_entrypoint.sub(".js", "-generated.js")
104+
generated_server_bundle_file_name = component_name(generated_interim_server_bundle_path)
104105
source_entrypoint_parent = Pathname(ReactOnRails::WebpackerUtils.webpacker_source_entry_path).parent
105106
generated_nonentrypoints_path = "#{source_entrypoint_parent}/generated"
106107

spec/dummy/client/app/packs/server-bundle.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// import statement added by react_on_rails:generate_packs rake task
2+
import './../generated/server-bundle-generated.js';
13
// Shows the mapping from the exported object to the name used by the server rendering.
24
import ReactOnRails from 'react-on-rails';
35
// Example of server rendering with no React

spec/dummy/spec/packs_generator_spec.rb

+11
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,17 @@ module ReactOnRails
234234
end.not_to output(GENERATED_PACKS_CONSOLE_OUTPUT_REGEX).to_stdout
235235
end
236236

237+
it "adds a single import statement to the server bundle" do
238+
test_string = "// import statement added by react_on_rails:generate_packs"
239+
same_instance = described_class.instance
240+
File.truncate(server_bundle_js_file_path, 0)
241+
same_instance.generate_packs_if_stale
242+
expect(File.read(server_bundle_js_file_path).scan(/(?=#{test_string})/).count).to equal(1)
243+
# the following expectation checks that an additional import statement is not added if one already exists
244+
same_instance.generate_packs_if_stale
245+
expect(File.read(server_bundle_js_file_path).scan(/(?=#{test_string})/).count).to equal(1)
246+
end
247+
237248
it "generate packs if a new component is added" do
238249
create_new_component("NewComponent")
239250

0 commit comments

Comments
 (0)