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={})
@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)

View File

@ -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

View File

@ -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