From 7f8108e4ac3fa7960b64f4d08374aab699ebe4a5 Mon Sep 17 00:00:00 2001 From: Benjamin Neff <benjamin@coding4coffee.ch> Date: Mon, 31 Jul 2017 22:43:28 +0200 Subject: [PATCH] Precompile bookmarklet to use in production `Rails.application.assets` is only available when `config.assets.compile` is true (which is false in production). So the old way with a separate rake task doesn't work in production. But we can get the filename of the precompiled file from `Rails.application.assets_manifest.assets`. --- config/application.rb | 1 + lib/bookmarklet_renderer.rb | 26 +++++++++++++++++--------- lib/tasks/assets.rake | 6 ------ 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/config/application.rb b/config/application.rb index dd9ca5adfd..619b66070c 100644 --- a/config/application.rb +++ b/config/application.rb @@ -72,6 +72,7 @@ module Diaspora jquery_ujs.js main.js jsxc.js + bookmarklet.js mobile/bookmarklet.js mobile/mobile.js templates.js diff --git a/lib/bookmarklet_renderer.rb b/lib/bookmarklet_renderer.rb index 3114d5a807..0ecc9cf48a 100644 --- a/lib/bookmarklet_renderer.rb +++ b/lib/bookmarklet_renderer.rb @@ -2,27 +2,35 @@ class BookmarkletRenderer class << self def cached_name - @cached ||= Rails.root.join("public", "assets", "bookmarklet.js") + @cached_name ||= if Rails.application.config.assets.compile + "bookmarklet.js" + else + Rails.application.assets_manifest.assets["bookmarklet.js"] + end end - def source_name + def cached_path + @cached_path ||= Rails.root.join("public", "assets", cached_name) + end + + def source @source ||= Rails.application.assets["bookmarklet.js"].pathname.to_s end def body - if !File.exist?(cached_name) && Rails.env.production? - raise "please run the Rake task to compile the bookmarklet: `bundle exec rake assets:uglify_bookmarklet`" + unless File.exist?(cached_path) || Rails.application.config.assets.compile + raise "Please run the rake task to compile the bookmarklet: `bin/rake assets:precompile`" end - compile unless Rails.env.production? # don't make me re-run rake in development - @body ||= File.read(cached_name) + compile if Rails.application.config.assets.compile + @body ||= File.read(cached_path) end def compile - src = File.read(source_name) + src = File.read(source) @body = Uglifier.compile(src) - FileUtils.mkdir_p cached_name.dirname - File.open(cached_name, "w") {|f| f.write(@body) } + FileUtils.mkdir_p cached_path.dirname + File.open(cached_path, "w") {|f| f.write(@body) } end end end diff --git a/lib/tasks/assets.rake b/lib/tasks/assets.rake index 0bf591cfb3..917630f62e 100644 --- a/lib/tasks/assets.rake +++ b/lib/tasks/assets.rake @@ -5,11 +5,6 @@ namespace :assets do renderer.render end - desc "Uglify bookmarklet snippet" - task :uglify_bookmarklet => :environment do - BookmarkletRenderer.compile - end - desc "Create non digest assets" task non_digest_assets: :environment do logger = ::Logging::Logger["assets:non_digest_assets"] @@ -35,7 +30,6 @@ namespace :assets do # Augment precompile with error page generation task :precompile do Rake::Task["assets:generate_error_pages"].invoke - Rake::Task["assets:uglify_bookmarklet"].invoke Rake::Task["assets:non_digest_assets"].invoke end end -- GitLab