Skip to content

Commit

Permalink
Ability to work without sprockets (#671)
Browse files Browse the repository at this point in the history
This enables omitting Sprockets.

Sprockets'  tasks (assets:*)  not available if you do not require 'sprockets/railtie' and when you try run some rake task got a message: 
```bash
ReactOnRails: Set generated_assets_dir to default: app/assets/webpack
Running via Spring preloader in process 18586
rake aborted!
Don't know how to build task 'assets:precompile' (see --tasks)
/Users/arthur/.rvm/gems/ruby-2.3.3/gems/react_on_rails-6.3.4/lib/tasks/assets.rake:35:in `<top (required)>'
...

```
My application config

```ruby
# frozen_string_literal: true
require_relative 'boot'

require 'rails'

# Pick the frameworks you want:
require 'active_model/railtie'
require 'active_job/railtie'
require 'active_record/railtie'
require 'action_controller/railtie'
require 'action_mailer/railtie'
require 'action_view/railtie'
# require 'sprockets/railtie'

Bundler.require(*Rails.groups)

module My
  class Application < Rails::Application
    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration should go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded.

    config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}').to_s]
    config.i18n.default_locale = :ru
    config.i18n.enforce_available_locales = true

    config.time_zone = 'Moscow'

    config.middleware.use I18n::JS::Middleware
  end
end
```
  • Loading branch information
fc-arny authored and justin808 committed Feb 11, 2017
1 parent 7ad5c13 commit 85c6b08
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 31 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ Contributors: please follow the recommendations outlined at [keepachangelog.com]

## [Unreleased]
*Please add entries here for your pull requests.*
### Changed
- Add ability use gem without sprockets. [#671](https://github.com/shakacode/react_on_rails/pull/671) by [fc-arny](https://github.com/fc-arny)

### Fixed
- Fixed issue [#706](https://github.com/shakacode/react_on_rails/issues/706) with "flickering" components when they are unmounted too early [#709](https://github.com/shakacode/react_on_rails/pull/709) by [szyablitsky](https://github.com/szyablitsky)

Expand Down
68 changes: 37 additions & 31 deletions lib/tasks/assets.rake
Original file line number Diff line number Diff line change
@@ -1,28 +1,50 @@
require "react_on_rails/assets_precompile"

namespace :react_on_rails do
namespace :assets do
desc "Creates non-digested symlinks for the assets in the public asset dir"
task symlink_non_digested_assets: :"assets:environment" do
ReactOnRails::AssetsPrecompile.new.symlink_non_digested_assets
end
if defined?(Sprockets)
namespace :react_on_rails do
namespace :assets do
desc "Creates non-digested symlinks for the assets in the public asset dir"
task symlink_non_digested_assets: :"assets:environment" do
ReactOnRails::AssetsPrecompile.new.symlink_non_digested_assets
end

desc "Cleans all broken symlinks for the assets in the public asset dir"
task delete_broken_symlinks: :"assets:environment" do
ReactOnRails::AssetsPrecompile.new.delete_broken_symlinks
end
desc "Cleans all broken symlinks for the assets in the public asset dir"
task delete_broken_symlinks: :"assets:environment" do
ReactOnRails::AssetsPrecompile.new.delete_broken_symlinks
end

# In this task, set prerequisites for the assets:precompile task
desc <<-DESC
# In this task, set prerequisites for the assets:precompile task
desc <<-DESC
Create webpack assets before calling assets:environment
The webpack task must run before assets:environment task.
Otherwise Sprockets cannot find the files that webpack produces.
This is the secret sauce for how a Heroku deployment knows to create the webpack generated JavaScript files.
DESC
task compile_environment: :webpack do
Rake::Task["assets:environment"].invoke
DESC
task compile_environment: :webpack do
Rake::Task["assets:environment"].invoke
end

desc "Delete assets created with webpack, in the generated assetst directory (/app/assets/webpack)"
task clobber: :environment do
ReactOnRails::AssetsPrecompile.new.clobber
end
end
end

# These tasks run as pre-requisites of assets:precompile.
# Note, it's not possible to refer to ReactOnRails configuration values at this point.
Rake::Task["assets:precompile"]
.clear_prerequisites
.enhance([:environment, "react_on_rails:assets:compile_environment"])
.enhance do
Rake::Task["react_on_rails:assets:symlink_non_digested_assets"].invoke
Rake::Task["react_on_rails:assets:delete_broken_symlinks"].invoke
end
end

# Sprockets independent tasks
namespace :react_on_rails do
namespace :assets do
desc <<-DESC
Compile assets with webpack
Uses command defined with ReactOnRails.configuration.npm_build_production_command
Expand All @@ -33,21 +55,5 @@ sh "cd client && `ReactOnRails.configuration.npm_build_production_command`"
sh "cd client && #{ReactOnRails.configuration.npm_build_production_command}"
end
end

desc "Delete assets created with webpack, in the generated assetst directory (/app/assets/webpack)"
task clobber: :environment do
ReactOnRails::AssetsPrecompile.new.clobber
end
end
end

# These tasks run as pre-requisites of assets:precompile.
# Note, it's not possible to refer to ReactOnRails configuration values at this point.
Rake::Task["assets:precompile"]
.clear_prerequisites
.enhance([:environment, "react_on_rails:assets:compile_environment"])
.enhance do
Rake::Task["react_on_rails:assets:symlink_non_digested_assets"].invoke
Rake::Task["react_on_rails:assets:delete_broken_symlinks"].invoke
end

0 comments on commit 85c6b08

Please sign in to comment.