Skip to content

Commit

Permalink
Allow setting REACT_ON_RAILS_PRECOMPILE to false to skip building (#1371
Browse files Browse the repository at this point in the history
)

* Update assets.rake

Co-authored-by: Judah Meek
  • Loading branch information
justin808 authored Aug 28, 2021
1 parent 27921ec commit 1a7626f
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 23 deletions.
20 changes: 10 additions & 10 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,25 +69,25 @@ aliases:
- &restore-package-node-modules-cache
name: Restore cached node_modules directory
keys:
- v4-package-node-modules-cache-{{ checksum "yarn.lock" }}
- v5-package-node-modules-cache-{{ checksum "yarn.lock" }}

# Restore spec/dummy/node_modules dir from cache using yarn.lock checksum as a key.
- &restore-dummy-app-node-modules-cache
name: Restore cached spec/dummy/node_modules directory
keys:
- v4-dummy-app-node-modules-cache-{{ checksum "spec/dummy/yarn.lock" }}
- v5-dummy-app-node-modules-cache-{{ checksum "spec/dummy/yarn.lock" }}

# Restore vendor/bundle dir from cache using Gemfile.lock checksum as a key.
- &restore-dummy-app-gem-cache
name: Restore cached Ruby Gems for dummy app
keys:
- v4-dummy-app-gem-cache-{{ checksum "spec/dummy/Gemfile.lock" }}
- v5-dummy-app-gem-cache-{{ checksum "spec/dummy/Gemfile.lock" }}

# Restore vendor/bundle dir from cache using react_on_rails.gemspec checksum as a key.
- &restore-package-gem-cache
name: Restore cached Ruby Gems for package
keys:
- v4-package-app-gem-cache-{{ checksum "react_on_rails.gemspec" }}
- v5-package-app-gem-cache-{{ checksum "react_on_rails.gemspec" }}

# NOTE: Sometimes CI generated docker images are not updated in time to keep up with the minimum required
# by chromedriver versions of Chrome. Just bump here Chrome version if chromedriver raises errors
Expand Down Expand Up @@ -139,7 +139,7 @@ jobs:
- run: *install-package-node-modules
- save_cache:
name: Save root node_modules to cache
key: v4-package-node-modules-cache-{{ checksum "yarn.lock" }}
key: v5-package-node-modules-cache-{{ checksum "yarn.lock" }}
paths:
- node_modules

Expand All @@ -157,7 +157,7 @@ jobs:
- run: *install-dummy-app-node-modules
- save_cache:
name: Save spec/dummy/node_modules to cache
key: v4-dummy-app-node-modules-cache-{{ checksum "spec/dummy/yarn.lock" }}
key: v5-dummy-app-node-modules-cache-{{ checksum "spec/dummy/yarn.lock" }}
paths:
- spec/dummy/node_modules
- spec/dummy/node_modules
Expand All @@ -172,7 +172,7 @@ jobs:
- run: *install-package-ruby-gems
- save_cache:
name: Save dummy app ruby gems to cache
key: v4-package-app-gem-cache-{{ checksum "react_on_rails.gemspec" }}
key: v5-package-app-gem-cache-{{ checksum "react_on_rails.gemspec" }}
paths:
- vendor/bundle

Expand All @@ -186,7 +186,7 @@ jobs:
- run: *install-dummy-app-ruby-gems
- save_cache:
name: Save dummy app ruby gems to cache
key: v4-dummy-app-gem-cache-{{ checksum "spec/dummy/Gemfile.lock" }}
key: v5-dummy-app-gem-cache-{{ checksum "spec/dummy/Gemfile.lock" }}
paths:
- spec/dummy/vendor/bundle

Expand All @@ -209,7 +209,7 @@ jobs:
command: cd spec/dummy && yarn run build:test
- save_cache:
name: Save test webpack bundles to cache (for build number checksum used by rspec job)
key: v4-dummy-app-webpack-bundle-{{ .Revision }}
key: v5-dummy-app-webpack-bundle-{{ .Revision }}
paths:
- spec/dummy/public/webpack
# Run JS unit tests for Renderer package.
Expand Down Expand Up @@ -249,7 +249,7 @@ jobs:
- restore_cache: *restore-dummy-app-gem-cache
- restore_cache:
name: Restore cached webpack bundles for dummy app
key: v4-dummy-app-webpack-bundle-{{ .Revision }}
key: v5-dummy-app-webpack-bundle-{{ .Revision }}
- restore_cache: *restore-dummy-app-node-modules-cache
- restore_cache: *restore-package-node-modules-cache
- run: *install-package-node-modules
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ Changes since last non-beta release.

*Please add entries here for your pull requests that are not yet released.*

#### Added

- Ability to stop React on Rails from modifying or creating the `assets:precompile` task. [PR 1371](https://github.com/shakacode/react_on_rails/pull/1371) by [justin808](https://github.com/justin808). Thanks to [elstgav](https://github.com/elstgav) for [the suggestion](https://github.com/shakacode/react_on_rails/issues/1368)!

### [12.3.0] - 2021-07-26
#### Added

Expand Down
2 changes: 2 additions & 0 deletions docs/guides/how-react-on-rails-works.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,5 @@ However, if you want to run a custom command to run webpack to build your bundle
Then React on Rails modifies the `assets:precompile` task to run your `build_production_command`.

If you have used the provided generator, these bundles will automatically be added to your `.gitignore` to prevent extraneous noise from re-generated code in your pull requests. You will want to do this manually if you do not use the provided generator.

You can stop React on Rails from modifying or creating the `assets:precompile` task, by setting a `REACT_ON_RAILS_PRECOMPILE` environment variable to `no`, `false`, `n` or `f`.
15 changes: 3 additions & 12 deletions docs/rails/rails-engine-integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,15 @@ s.add_dependency 'react_on_rails', '~> 6'
```ruby
require "react_on_rails"
```
+ In your `lib/tasks/<engine_name>_tasks.rake`:
```ruby
Rake.application.remove_task('react_on_rails:assets:compile_environment')

task 'react_on_rails:assets:compile_environment' do
path = File.join(YourEngineName::Engine.root, 'client')
sh "cd #{path} && #{ReactOnRails.configuration.build_production_command}"
end
```
## In the project including your engine

Place `gem 'react_on_rails', '~> 6'` before the gem pointing at your engine in your gemfile.

This is necessary because React on Rails attaches itself to the rake assets:precompile task. It then uses a direct path to cd into client, which will not exist in the main app that includes your engine. Since you'll always be precompiling assets in the parent app, this will always fail. The workaround then, is to remove the task and replace it with one that goes into your Engine's root. The reason you have to include the react on rails gem before your engine is so that the `react_on_rails:assets:compile_environment` task is defined by the time your engine gets loaded to remove it.

Requiring `react_on_rails` and including the helper will get rid of any issues where react on rails or react_component is undefined.

As far as solving the assets issue, `lib/tasks/assets.rake` in `react_on_rails` would somehow have to know that `react_on_rails` was included in an engine, and decide the path accordingly. This might be impossible, especially in the case of multiple engines using `react_on_rails` in a single application. Another solution would be to detach this rake task from the rails assets:precompile task, and let people use it separately.
As far as solving the assets issue, `lib/tasks/assets.rake` in `react_on_rails` would somehow have to know that `react_on_rails` was included in an engine, and decide the path accordingly. This might be impossible, especially in the case of multiple engines using `react_on_rails` in a single application.

Another solution would be to detach this rake task from the `rails assets:precompile` task. This can be done by adding `REACT_ON_RAILS_PRECOMPILE=false` to your environment. If you do so, then react assets will have to be bundled separately from `rails assets:precompile`.

# Github Issues

Expand Down
4 changes: 3 additions & 1 deletion lib/tasks/assets.rake
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ require "active_support"

ENV["RAILS_ENV"] ||= ENV["RACK_ENV"] || "development"

unless ReactOnRails::WebpackerUtils.webpacker_webpack_production_config_exists?
skip_react_on_rails_precompile = %w[no false n f].include?(ENV["REACT_ON_RAILS_PRECOMPILE"])

if !skip_react_on_rails_precompile && !ReactOnRails::WebpackerUtils.webpacker_webpack_production_config_exists?
# Ensure that rails/webpacker does not call bin/webpack if we're providing
# the build command.
ENV["WEBPACKER_PRECOMPILE"] = "false"
Expand Down

0 comments on commit 1a7626f

Please sign in to comment.