Skip to content

Commit f17b4a0

Browse files
ihabadhamjustin808
andauthored
Docs/auto bundle rename and dummy reference (#1758)
* rename BarComponentTwo to SpecialComponentNotToAutoLoadBundle in the Solution section * Revise documentation for React component bundle loading --------- Co-authored-by: Justin Gordon <[email protected]>
1 parent afe2306 commit f17b4a0

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

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

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ default:
1717
1818
For more details, see [Configuration and Code](https://github.com/shakacode/shakapacker#configuration-and-code) section in [shakapacker](https://github.com/shakacode/shakapacker/).
1919
20+
> Example (dummy app): `nested_entries: true` with a different `source_path: client/app`. See `config/shakapacker.yml` in the dummy app.
21+
> [Dummy shakapacker.yml](https://github.com/shakacode/react_on_rails/blob/master/spec/dummy/config/shakapacker.yml)
22+
2023
### Configure Components Subdirectory
2124

2225
`components_subdirectory` is the name of the matched directories containing components that will be automatically registered for use by the view helpers.
@@ -28,6 +31,9 @@ config.components_subdirectory = "ror_components"
2831

2932
Now all React components inside the directories called `ror_components` will automatically be registered for usage with [`react_component`](../api/view-helpers-api.md#react_component) and [`react_component_hash`](../api/view-helpers-api.md#react_component_hash) helper methods provided by React on Rails.
3033

34+
> Example (dummy app): the configured components subdirectory is named `startup` instead of `ror_components`.
35+
> [Dummy initializer](https://github.com/shakacode/react_on_rails/blob/master/spec/dummy/config/initializers/react_on_rails.rb)
36+
3137
### Configure `auto_load_bundle` Option
3238

3339
For automated component registry, [`react_component`](../api/view-helpers-api.md#react_component) and [`react_component_hash`](../api/view-helpers-api.md#react_component_hash) view helper method tries to load generated bundle for component from the generated directory automatically per `auto_load_bundle` option. `auto_load_bundle` option in `config/initializers/react_on_rails` configures the default value that will be passed to component helpers. The default is `false`, and the parameter can be passed explicitly for each call.
@@ -38,6 +44,9 @@ You can change the value in `config/initializers/react_on_rails` by updating it
3844
config.auto_load_bundle = true
3945
```
4046

47+
> Example (dummy app): `auto_load_bundle` is set to `true` in the same initializer.
48+
> [Dummy initializer](https://github.com/shakacode/react_on_rails/blob/master/spec/dummy/config/initializers/react_on_rails.rb)
49+
4150
### Location of generated files
4251

4352
Generated files will go to the following two directories:
@@ -65,6 +74,9 @@ import './../generated/server-bundle-generated.js';
6574

6675
We recommend committing this import statement to your version control system.
6776

77+
> Example (dummy app): see the server bundle entrypoint import.
78+
> [Dummy server-bundle.js](https://github.com/shakacode/react_on_rails/blob/master/spec/dummy/client/app/packs/server-bundle.js)
79+
6880
## Usage
6981

7082
### Basic usage
@@ -146,24 +158,26 @@ The tricky part is to figure out which bundles to load on any Rails view. [Shaka
146158

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

149-
For example, if you wanted to utilize our file-system based entrypoint generation for `FooComponentOne` and `BarComponentOne`, but not `BarComponentTwo` (for whatever reason), then...
161+
> Note: In the Background examples above, we used `BarComponentTwo`. In the Solution below, we refer to the same component as `SpecialComponentNotToAutoLoadBundle` to emphasize that it is excluded from auto-loading. You do not need to rename your files.
162+
163+
For example, if you wanted to utilize our file-system based entrypoint generation for `FooComponentOne` and `BarComponentOne`, but not `SpecialComponentNotToAutoLoadBundle` (formerly `BarComponentTwo`) (for whatever reason), then...
150164

151165
1. Remove generated entrypoints from parameters passed directly to `javascript_pack_tag` and `stylesheet_pack_tag`.
152166
2. Remove generated entrypoints from parameters passed directly to `append_javascript_pack_tag` and `append_stylesheet_pack_tag`.
153167

154168
Your layout would now contain:
155169

156170
```erb
157-
<%= javascript_pack_tag('BarComponentTwo') %>
158-
<%= stylesheet_pack_tag('BarComponentTwo') %>
171+
<%= javascript_pack_tag('SpecialComponentNotToAutoLoadBundle') %>
172+
<%= stylesheet_pack_tag('SpecialComponentNotToAutoLoadBundle') %>
159173
```
160174

161175
3. Create a directory structure where the components that you want to be auto-generated are within `ReactOnRails.configuration.components_subdirectory`, which should be a subdirectory of `Shakapacker.config.source_path`:
162176

163177
```text
164178
app/javascript:
165179
└── packs:
166-
│ └── BarComponentTwo.jsx # Internally uses ReactOnRails.register
180+
│ └── SpecialComponentNotToAutoLoadBundle.jsx # Internally uses ReactOnRails.register
167181
└── src:
168182
│ └── Foo
169183
│ │ └── ...
@@ -174,16 +188,17 @@ For example, if you wanted to utilize our file-system based entrypoint generatio
174188
│ │ └── ror_components # configured as `components_subdirectory`
175189
│ │ │ └── BarComponentOne.jsx
176190
│ │ └── something_else
177-
│ │ │ └── BarComponentTwo.jsx
191+
│ │ │ └── SpecialComponentNotToAutoLoadBundle.jsx
178192
```
179193

180194
4. You no longer need to register the React components within the `ReactOnRails.configuration.components_subdirectory` nor directly add their bundles. For example, you can have a Rails view using three components:
181195

182196
```erb
183-
<% append_javascript_pack('BarComponentTwo') %>
184197
<%= react_component("FooComponentOne", {}, auto_load_bundle: true) %>
185198
<%= react_component("BarComponentOne", {}, auto_load_bundle: true) %>
186-
<%= react_component("BarComponentTwo", {}) %>
199+
200+
<% append_javascript_pack_tag('SpecialComponentNotToAutoLoadBundle') %>
201+
<%= react_component("SpecialComponentNotToAutoLoadBundle", {}) %>
187202
```
188203
189204
If a component uses multiple HTML strings for server rendering, the [`react_component_hash`](../api/view-helpers-api.md#react_component_hash) view helper can be used on the Rails view, as illustrated below.
@@ -208,6 +223,8 @@ For example, if you wanted to utilize our file-system based entrypoint generatio
208223
209224
If server rendering is enabled, the component will be registered for usage both in server and client rendering. To have separate definitions for client and server rendering, name the component files `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.
210225
226+
> Example (dummy app): paired files such as [`ReduxApp.client.jsx`](https://github.com/shakacode/react_on_rails/blob/master/spec/dummy/client/app/startup/ReduxApp.client.jsx) and [`ReduxApp.server.jsx`](https://github.com/shakacode/react_on_rails/blob/master/spec/dummy/client/app/startup/ReduxApp.server.jsx), and [`RouterApp.client.jsx`](https://github.com/shakacode/react_on_rails/blob/master/spec/dummy/client/app/startup/RouterApp.client.jsx) and [`RouterApp.server.jsx`](https://github.com/shakacode/react_on_rails/blob/master/spec/dummy/client/app/startup/RouterApp.server.jsx).
227+
211228
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.
212229
213230
> [!IMPORTANT]

0 commit comments

Comments
 (0)