diff --git a/app/models/site_customization.rb b/app/models/site_customization.rb index 5c3deb9b4..6f6930acb 100644 --- a/app/models/site_customization.rb +++ b/app/models/site_customization.rb @@ -32,7 +32,7 @@ class SiteCustomization < ActiveRecord::Base filesystem_importer: DiscourseSassImporter, sprockets: { context: context, - environment: env + environment: context.environment } }).render diff --git a/config/initializers/sprockets.rb b/config/initializers/sprockets.rb new file mode 100644 index 000000000..b67897963 --- /dev/null +++ b/config/initializers/sprockets.rb @@ -0,0 +1,4 @@ +require_dependency 'discourse_sass_importer' + +Sprockets.send(:remove_const, :SassImporter) +Sprockets::SassImporter = DiscourseSassImporter diff --git a/lib/discourse_sass_importer.rb b/lib/discourse_sass_importer.rb index 4fbbd47ea..857d204ac 100644 --- a/lib/discourse_sass_importer.rb +++ b/lib/discourse_sass_importer.rb @@ -4,7 +4,11 @@ class DiscourseSassImporter < Sass::Importers::Filesystem GLOB = /\*|\[.+\]/ + # Depending upon where this is passed we might either be passed a string as the + # first argument or a sprockets context. If the first argument is a sprockets + # context we store it and use it to mark dependencies. def initialize(*args) + @context = args.first unless args.first.is_a? String @root = Rails.root.join('app', 'assets', 'stylesheets').to_s @same_name_warnings = Set.new end @@ -48,6 +52,7 @@ class DiscourseSassImporter < Sass::Importers::Filesystem def glob_imports(glob, base_pathname, options) contents = "" each_globbed_file(glob, base_pathname.dirname, options) do |filename| + depend_on(filename) unless File.directory?(filename) contents << "@import #{Pathname.new(filename).relative_path_from(base_pathname.dirname).to_s.inspect};\n" end @@ -62,6 +67,21 @@ class DiscourseSassImporter < Sass::Importers::Filesystem private + def depend_on(filename) + if @context + @context.depend_on(filename) + @context.depend_on(globbed_file_parent(filename)) + end + end + + def globbed_file_parent(filename) + if File.directory?(filename) + File.expand_path('..', filename) + else + File.dirname(filename) + end + end + def engine_from_path(name, dir, options) full_filename, syntax = Sass::Util.destructure(find_real_file(dir, name, options)) return unless full_filename && File.readable?(full_filename) diff --git a/lib/tasks/assets.rake b/lib/tasks/assets.rake index 68ccda938..bbc853561 100644 --- a/lib/tasks/assets.rake +++ b/lib/tasks/assets.rake @@ -16,9 +16,6 @@ task 'assets:precompile:before' do # let's make precompile faster using redis magic require 'sprockets' require 'digest/sha1' - require_dependency 'discourse_sass_importer' - - Sprockets::SassImporter = DiscourseSassImporter module ::Sprockets