lib/tasks/assets.rake in react_on_rails-6.0.0.beta.1 vs lib/tasks/assets.rake in react_on_rails-6.0.0.beta.2

- old
+ new

@@ -19,14 +19,13 @@ end end namespace :react_on_rails do namespace :assets do - - if ReactOnRails.configuration.symlink_non_digested_assets_regex - desc "Creates non-digested symlinks for the assets in the public asset dir" - task symlink_non_digested_assets: :"assets:environment" do + desc "Creates non-digested symlinks for the assets in the public asset dir" + task symlink_non_digested_assets: :"assets:environment" do + if ReactOnRails.configuration.symlink_non_digested_assets_regex manifest_path = Dir.glob(ReactOnRails::assets_path.join(".sprockets-manifest-*.json")) .first manifest_data = JSON.load(File.new(manifest_path)) manifest_data["assets"].each do |logical_path, digested_path| @@ -40,76 +39,68 @@ ReactOnRails::symlink_file(full_digested_path, full_nondigested_path) ReactOnRails::symlink_file(full_digested_gz_path, full_nondigested_gz_path) end end end + end - desc "Cleans all broken symlinks for the assets in the public asset dir" - task delete_broken_symlinks: :"assets:environment" do - Dir.glob(ReactOnRails::assets_path.join("*")).each do |filename| - if File.lstat(filename).symlink? - begin - target = File.readlink(filename) - rescue - puts "React on Rails: Warning: your platform doesn't support File::readlink method."/ - "Skipping broken link check." - return - end - path = Pathname.new(File.dirname(filename)) - target_path = path.join(target) - unless File.exist?(target_path) - puts "React on Rails: Deleting broken link: #{filename}" - File.delete(filename) - end + desc "Cleans all broken symlinks for the assets in the public asset dir" + task delete_broken_symlinks: :"assets:environment" do + Dir.glob(ReactOnRails::assets_path.join("*")).each do |filename| + if File.lstat(filename).symlink? + begin + target = File.readlink(filename) + rescue + puts "React on Rails: Warning: your platform doesn't support File::readlink method."/ + "Skipping broken link check." + return end + path = Pathname.new(File.dirname(filename)) + target_path = path.join(target) + unless File.exist?(target_path) + puts "React on Rails: Deleting broken link: #{filename}" + File.delete(filename) + end end end end - - # We might need the clear_prerequisites (delete after testing without) - # Rake::Task["assets:precompile"] - # .clear_prerequisites - # .enhance(["assets:compile_environment"]) - - if ReactOnRails.configuration.npm_build_production_command.present? - # 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 - end + DESC + task compile_environment: :webpack do + Rake::Task["assets:environment"].invoke + end - desc <<-DESC + desc <<-DESC Compile assets with webpack Uses command defined with ReactOnRails.configuration.npm_build_production_command -sh "cd client && #{ReactOnRails.configuration.npm_build_production_command}" - DESC - task :webpack do +sh "cd client && `ReactOnRails.configuration.npm_build_production_command`" + DESC + task webpack: :environment do + if ReactOnRails.configuration.npm_build_production_command.present? sh "cd client && #{ReactOnRails.configuration.npm_build_production_command}" end + end - desc "Delete assets created with webpack, in #{ReactOnRails.configuration.generated_assets_dir}" - task :clobber do + desc "Delete assets created with webpack, in the generated assetst directory (/app/assets/webpack)" + task clobber: :environment do + dir = Rails.root.join(ReactOnRails.configuration.generated_assets_dir) + if dir.present? && File.directory?(dir) + puts "Deleting files in directory #{dir}" rm_r Dir.glob(Rails.root.join("#{ReactOnRails.configuration.generated_assets_dir}/*")) + else + puts "Could not find dir #{dir}" end end end end -if ReactOnRails.configuration.symlink_non_digested_assets_regex - Rake::Task["assets:precompile"].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 - -if ReactOnRails.configuration.npm_build_production_command.present? - Rake::Task["assets:precompile"].enhance do - Rake::Task["react_on_rails:assets:compile_environment"].invoke - 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"].enhance(["react_on_rails:assets:compile_environment", + "react_on_rails:assets:symlink_non_digested_assets", + "react_on_rails:assets:delete_broken_symlinks"])