PERF: don't recompile css files that have already been compiled

This commit is contained in:
Neil Lalonde
2015-05-05 16:52:03 -04:00
parent d7b3f9bfe2
commit 406c8bb340
3 changed files with 22 additions and 7 deletions

View File

@@ -33,9 +33,9 @@ class DiscourseStylesheets
def self.compile(target = :desktop, opts={}) def self.compile(target = :desktop, opts={})
@lock.synchronize do @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 = self.new(target)
builder.compile builder.compile(opts)
builder.stylesheet_filename builder.stylesheet_filename
end end
end end
@@ -76,7 +76,20 @@ class DiscourseStylesheets
@target = target @target = target
end 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") scss = File.read("#{Rails.root}/app/assets/stylesheets/#{@target}.scss")
css = begin css = begin
DiscourseSassCompiler.compile(scss, @target) DiscourseSassCompiler.compile(scss, @target)

View File

@@ -72,16 +72,18 @@ task 'assets:precompile:before' do
end end
task 'assets:precompile:css' => 'environment' do task 'assets:precompile:css' => 'environment' do
puts "Start compiling CSS: #{Time.zone.now}"
RailsMultisite::ConnectionManagement.each_connection do |db| RailsMultisite::ConnectionManagement.each_connection do |db|
# Heroku precompiles assets before db migration, so tables may not exist. # Heroku precompiles assets before db migration, so tables may not exist.
# css will get precompiled during first request instead in that case. # css will get precompiled during first request instead in that case.
if ActiveRecord::Base.connection.table_exists?(ColorScheme.table_name) if ActiveRecord::Base.connection.table_exists?(ColorScheme.table_name)
puts "Compiling css for #{db}" puts "Compiling css for #{db}"
[:desktop, :mobile].each do |target| [:desktop, :mobile].each do |target|
puts DiscourseStylesheets.compile(target, force: true) puts DiscourseStylesheets.compile(target)
end end
end end
end end
puts "Done compiling CSS: #{Time.zone.now}"
end end
def assets_path def assets_path

View File

@@ -7,14 +7,14 @@ describe DiscourseStylesheets do
it "can compile desktop bundle" do it "can compile desktop bundle" do
DiscoursePluginRegistry.stubs(:stylesheets).returns(["#{Rails.root}/spec/fixtures/scss/my_plugin.scss"]) DiscoursePluginRegistry.stubs(:stylesheets).returns(["#{Rails.root}/spec/fixtures/scss/my_plugin.scss"])
builder = described_class.new(:desktop) 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 FileUtils.rm builder.stylesheet_fullpath
end end
it "can compile mobile bundle" do it "can compile mobile bundle" do
DiscoursePluginRegistry.stubs(:mobile_stylesheets).returns(["#{Rails.root}/spec/fixtures/scss/my_plugin.scss"]) DiscoursePluginRegistry.stubs(:mobile_stylesheets).returns(["#{Rails.root}/spec/fixtures/scss/my_plugin.scss"])
builder = described_class.new(:mobile) 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 FileUtils.rm builder.stylesheet_fullpath
end end
@@ -24,7 +24,7 @@ describe DiscourseStylesheets do
"#{Rails.root}/spec/fixtures/scss/broken.scss" "#{Rails.root}/spec/fixtures/scss/broken.scss"
]) ])
builder = described_class.new(:desktop) 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 FileUtils.rm builder.stylesheet_fullpath
end end
end end