From 406c8bb34083884ec06f4423ce664618b37afb87 Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Tue, 5 May 2015 16:52:03 -0400 Subject: [PATCH] PERF: don't recompile css files that have already been compiled --- lib/sass/discourse_stylesheets.rb | 19 ++++++++++++++++--- lib/tasks/assets.rake | 4 +++- spec/components/discourse_stylesheets_spec.rb | 6 +++--- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/lib/sass/discourse_stylesheets.rb b/lib/sass/discourse_stylesheets.rb index 7b2b59f6ef3..5bae146c527 100644 --- a/lib/sass/discourse_stylesheets.rb +++ b/lib/sass/discourse_stylesheets.rb @@ -33,9 +33,9 @@ class DiscourseStylesheets def self.compile(target = :desktop, opts={}) @lock.synchronize do - FileUtils.rm(MANIFEST_FULL_PATH, force: true) if opts[:force] # Force a recompile, even in production env + FileUtils.rm(MANIFEST_FULL_PATH, force: true) if opts[:force] builder = self.new(target) - builder.compile + builder.compile(opts) builder.stylesheet_filename end end @@ -76,7 +76,20 @@ class DiscourseStylesheets @target = target end - def compile + def compile(opts={}) + unless opts[:force] + if File.exists?(stylesheet_fullpath) + unless StylesheetCache.where(target: @target, digest: digest).exists? + begin + StylesheetCache.add(@target, digest, File.read(stylesheet_fullpath)) + rescue => e + Rails.logger.warn "Completely unexpected error adding contents of '#{stylesheet_fullpath}' to cache #{e}" + end + end + return true + end + end + scss = File.read("#{Rails.root}/app/assets/stylesheets/#{@target}.scss") css = begin DiscourseSassCompiler.compile(scss, @target) diff --git a/lib/tasks/assets.rake b/lib/tasks/assets.rake index aa95714cf9b..efba76eff75 100644 --- a/lib/tasks/assets.rake +++ b/lib/tasks/assets.rake @@ -72,16 +72,18 @@ task 'assets:precompile:before' do end task 'assets:precompile:css' => 'environment' do + puts "Start compiling CSS: #{Time.zone.now}" RailsMultisite::ConnectionManagement.each_connection do |db| # Heroku precompiles assets before db migration, so tables may not exist. # css will get precompiled during first request instead in that case. if ActiveRecord::Base.connection.table_exists?(ColorScheme.table_name) puts "Compiling css for #{db}" [:desktop, :mobile].each do |target| - puts DiscourseStylesheets.compile(target, force: true) + puts DiscourseStylesheets.compile(target) end end end + puts "Done compiling CSS: #{Time.zone.now}" end def assets_path diff --git a/spec/components/discourse_stylesheets_spec.rb b/spec/components/discourse_stylesheets_spec.rb index 79c3aa477df..c7be72e96da 100644 --- a/spec/components/discourse_stylesheets_spec.rb +++ b/spec/components/discourse_stylesheets_spec.rb @@ -7,14 +7,14 @@ describe DiscourseStylesheets do it "can compile desktop bundle" do DiscoursePluginRegistry.stubs(:stylesheets).returns(["#{Rails.root}/spec/fixtures/scss/my_plugin.scss"]) builder = described_class.new(:desktop) - expect(builder.compile).to include('my-plugin-thing') + expect(builder.compile(force: true)).to include('my-plugin-thing') FileUtils.rm builder.stylesheet_fullpath end it "can compile mobile bundle" do DiscoursePluginRegistry.stubs(:mobile_stylesheets).returns(["#{Rails.root}/spec/fixtures/scss/my_plugin.scss"]) builder = described_class.new(:mobile) - expect(builder.compile).to include('my-plugin-thing') + expect(builder.compile(force: true)).to include('my-plugin-thing') FileUtils.rm builder.stylesheet_fullpath end @@ -24,7 +24,7 @@ describe DiscourseStylesheets do "#{Rails.root}/spec/fixtures/scss/broken.scss" ]) builder = described_class.new(:desktop) - expect(builder.compile).not_to include('my-plugin-thing') + expect(builder.compile(force: true)).not_to include('my-plugin-thing') FileUtils.rm builder.stylesheet_fullpath end end