Update sprockets. (#4167)

* Update sass-rails.

* FIX: Tilt dependency has been removed from Ember::Handlebars::Template.

* Update `DiscourseIIFE` to new Sprockets API.

* `Rails.application.assets` returns `nil` in production.

* Move sprockets-rails out of the assets group.

* Pin ember-rails to 0.18.5 which works with Sprockets 3.x.

* Update sprockets to 3.6.0.

* Make `DiscourseSassCompiler` work with Sprockets 3.

* Use `Sass::Rails::SassImporterGlobbing` instead of haxxing our own.

* Moneky patch so that we don't add dependencies for our custom css.

* FIX: Missing class.

* Upgrade ember-handlebars-template.

* FIX: require path needs to share the same root as the folder's path.

* Bump discourse-qunit-rails.

* Update ember-template-compiler.js to 1.12.2.

* `prepend` is private in Ruby 2.0.0.
This commit is contained in:
Guo Xiang Tan
2016-04-18 10:47:52 +08:00
parent 36e3f1f5e4
commit 256d7a00e9
17 changed files with 673 additions and 618 deletions

View File

@@ -1,9 +1,19 @@
class DiscourseIIFE < Sprockets::Processor
class DiscourseIIFE
def initialize(options = {}, &block)
end
def self.instance
@instance ||= new
end
def self.call(input)
instance.call(input)
end
# Add a IIFE around our javascript
def evaluate(context, locals)
path = context.pathname.to_s
def call(input)
path = input[:environment].context_class.new(input).pathname.to_s
data = input[:data]
# Only discourse or admin paths
return data unless (path =~ /\/javascripts\/discourse/ || path =~ /\/javascripts\/admin/ || path =~ /\/test\/javascripts/)

View File

@@ -1,13 +1,13 @@
module Ember
module Handlebars
class Template < Tilt::Template
class Template
# Wrap in an IIFE in development mode to get the correct filename
def compile_ember_handlebars(string, ember_template = 'Handlebars')
def compile_ember_handlebars(string, ember_template = 'Handlebars', options = nil)
if ::Rails.env.development?
"(function() { try { return Ember.#{ember_template}.compile(#{indent(string).inspect}); } catch(err) { throw err; } })()"
else
"Ember.#{ember_template}.compile(#{indent(string).inspect});"
"Ember.#{ember_template}.compile(#{indent(string).inspect}, #{options.to_json});"
end
end
end

View File

@@ -1,32 +1,47 @@
# barber patches to re-route raw compilation via ember compat handlebars
#
module Barber
class EmberCompatPrecompiler < Barber::Precompiler
class Barber::Precompiler
def sources
[File.open("#{Rails.root}/vendor/assets/javascripts/handlebars.js"), precompiler]
end
def sources
[File.open("#{Rails.root}/vendor/assets/javascripts/handlebars.js"), precompiler]
end
def precompiler
@precompiler ||= StringIO.new <<END
var Discourse = {};
#{File.read(Rails.root + "app/assets/javascripts/discourse/lib/ember_compat_handlebars.js")}
var Barber = {
precompile: function(string) {
return Discourse.EmberCompatHandlebars.precompile(string,false).toString();
}
};
def precompiler
@precompiler ||= StringIO.new <<END
var Discourse = {};
#{File.read(Rails.root + "app/assets/javascripts/discourse/lib/ember_compat_handlebars.js")}
var Barber = {
precompile: function(string) {
return Discourse.EmberCompatHandlebars.precompile(string,false).toString();
}
};
END
end
end
module Discourse
module Ember
module Handlebars
module Helper
def precompile_handlebars(string)
"Discourse.EmberCompatHandlebars.template(#{Barber::Precompiler.compile(string)});"
end
def compile_handlebars(string)
"Discourse.EmberCompatHandlebars.compile(#{indent(string).inspect});"
end
end
end
end
end
class Ember::Handlebars::Template
def precompile_handlebars(string)
"Discourse.EmberCompatHandlebars.template(#{Barber::EmberCompatPrecompiler.compile(string)});"
end
def compile_handlebars(string)
"Discourse.EmberCompatHandlebars.compile(#{indent(string).inspect});"
include Discourse::Ember::Handlebars::Helper
# FIXME: Previously, ember-handlebars-templates uses the logical path which incorrectly
# returned paths with the `.raw` extension and our code is depending on the `.raw`
# to find the right template to use.
def actual_name(input)
actual_name = input[:name]
input[:filename].include?('.raw') ? "#{actual_name}.raw" : actual_name
end
end

View File

@@ -0,0 +1,19 @@
# sass-rails expects an actual file to exists when calling `@import`. However,
# we don't actually create the files for our special imports but rather inject
# them dynamically.
module Discourse
module Sprockets
module Resolve
def resolve(path, options = {})
return [path, []] if DiscourseSassImporter.special_imports.has_key?(File.basename(path, '.scss'))
super
end
end
end
end
module Sprockets
class Base
prepend Discourse::Sprockets::Resolve
end
end

View File

@@ -0,0 +1,16 @@
# sass-rails expects an actual file to exists when calling `@import`. However,
# we don't actually create the files for our special imports but rather inject
# them dynamically.
module Discourse
module Sprockets
module Resolve
def resolve(path, options = {})
return [path, []] if DiscourseSassImporter.special_imports.has_key?(File.basename(path, '.scss'))
super
end
end
end
end
# Call `prepend` directly once we drop support for Ruby 2.0.0.
Sprockets::Base.send(:prepend, Discourse::Sprockets::Resolve)

View File

@@ -20,7 +20,7 @@ class DiscourseSafeSassImporter < DiscourseSassImporter
special_imports[name].each do |css_file|
contents << File.read(css_file)
end
Sass::Engine.new(contents, options.merge(
::Sass::Engine.new(contents, options.merge(
filename: "#{name}.scss",
importer: self,
syntax: :scss

View File

@@ -39,17 +39,17 @@ class DiscourseSassCompiler
# Options:
# safe: (boolean) if true, theme and plugin stylesheets will not be included. Default is false.
def compile(opts={})
env = Rails.application.assets
# In production Rails.application.assets is a Sprockets::Index
# instead of Sprockets::Environment, there is no cleaner way
# to get the environment from the index.
if env.is_a?(Sprockets::Index)
env = env.instance_variable_get('@environment')
end
app = Rails.application
env = app.assets || Sprockets::Railtie.build_environment(app)
pathname = Pathname.new("app/assets/stylesheets/#{@target}.scss")
context = env.context_class.new(env, "#{@target}.scss", pathname)
context = env.context_class.new(
environment: env,
filename: "#{@target}.scss",
pathname: pathname,
metadata: {}
)
debug_opts = Rails.env.production? ? {} : {
line_numbers: true,
@@ -57,12 +57,15 @@ class DiscourseSassCompiler
style: :expanded
}
importer_class = opts[:safe] ? DiscourseSafeSassImporter : DiscourseSassImporter
css = ::Sass::Engine.new(@scss, {
syntax: :scss,
cache: false,
read_cache: false,
style: :compressed,
filesystem_importer: opts[:safe] ? DiscourseSafeSassImporter : DiscourseSassImporter,
filesystem_importer: importer_class,
load_paths: context.environment.paths.map { |path| importer_class.new(path.to_s) },
sprockets: {
context: context,
environment: context.environment

View File

@@ -2,156 +2,118 @@
# Sprockets::SassImporter implementation provided in sass-rails since that is used
# during asset precompilation.
class DiscourseSassImporter < Sass::Importers::Filesystem
GLOB = /\*|\[.+\]/ unless defined? GLOB
module Sass
def extensions
{
'css' => :scss,
'css.scss' => :scss,
'css.sass' => :sass,
'css.erb' => :scss,
'scss.erb' => :scss,
'sass.erb' => :sass,
'css.scss.erb' => :scss,
'css.sass.erb' => :sass
}.merge!(super)
end
# 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
def special_imports
{
"plugins" => DiscoursePluginRegistry.stylesheets,
"plugins_mobile" => DiscoursePluginRegistry.mobile_stylesheets,
"plugins_desktop" => DiscoursePluginRegistry.desktop_stylesheets,
"plugins_variables" => DiscoursePluginRegistry.sass_variables,
"theme_variables" => [ColorScheme::BASE_COLORS_FILE],
"category_backgrounds" => Proc.new { |c| "body.category-#{c.full_slug} { background-image: url(#{apply_cdn(c.background_url)}) }\n" }
}
end
def extensions
{
'css' => :scss,
'css.scss' => :scss,
'css.sass' => :sass,
'css.erb' => :scss,
'scss.erb' => :scss,
'sass.erb' => :sass,
'css.scss.erb' => :scss,
'css.sass.erb' => :sass
}.merge!(super)
end
def special_imports
{
"plugins" => DiscoursePluginRegistry.stylesheets,
"plugins_mobile" => DiscoursePluginRegistry.mobile_stylesheets,
"plugins_desktop" => DiscoursePluginRegistry.desktop_stylesheets,
"plugins_variables" => DiscoursePluginRegistry.sass_variables,
"theme_variables" => [ColorScheme::BASE_COLORS_FILE]
}
end
def find_relative(name, base, options)
if name =~ GLOB
glob_imports(name, Pathname.new(base), options)
else
def find_relative(name, base, options)
engine_from_path(name, File.dirname(base), options)
end
end
def apply_cdn(url)
"#{GlobalSetting.cdn_url}#{url}"
end
def find(name, options)
if name == "category_backgrounds"
contents = ""
Category.where('background_url IS NOT NULL').each do |c|
if c.background_url.present?
contents << "body.category-#{c.full_slug} { background-image: url(#{apply_cdn(c.background_url)}) }\n"
end
end
return Sass::Engine.new(contents, options.merge(
filename: "#{name}.scss",
importer: self,
syntax: :scss
))
def apply_cdn(url)
"#{GlobalSetting.cdn_url}#{url}"
end
if special_imports.has_key? name
if name == "theme_variables"
def find(name, options)
if name == "category_backgrounds"
contents = ""
if color_scheme = ColorScheme.enabled
ColorScheme.base_colors.each do |n, base_hex|
override = color_scheme.colors_by_name[n]
contents << "$#{n}: ##{override ? override.hex : base_hex} !default;\n"
end
else
special_imports[name].each do |css_file|
contents << File.read(css_file)
end
Category.where('background_url IS NOT NULL').each do |c|
contents << special_imports[name].call(c) if c.background_url.present?
end
Sass::Engine.new(contents, options.merge(
return ::Sass::Engine.new(contents, options.merge(
filename: "#{name}.scss",
importer: self,
syntax: :scss
))
else
stylesheets = special_imports[name]
contents = ""
stylesheets.each do |css_file|
if css_file =~ /\.scss$/
contents << "@import '#{css_file}';"
end
if special_imports.has_key? name
if name == "theme_variables"
contents = ""
if color_scheme = ColorScheme.enabled
ColorScheme.base_colors.each do |n, base_hex|
override = color_scheme.colors_by_name[n]
contents << "$#{n}: ##{override ? override.hex : base_hex} !default;\n"
end
else
contents << File.read(css_file)
special_imports[name].each do |css_file|
contents << File.read(css_file)
end
end
depend_on(css_file)
::Sass::Engine.new(contents, options.merge(
filename: "#{name}.scss",
importer: self,
syntax: :scss
))
else
stylesheets = special_imports[name]
contents = ""
stylesheets.each do |css_file|
if css_file =~ /\.scss$/
contents << "@import '#{css_file}';"
else
contents << File.read(css_file)
end
depend_on(css_file)
end
::Sass::Engine.new(contents, options.merge(
filename: "#{name}.scss",
importer: self,
syntax: :scss
))
end
Sass::Engine.new(contents, options.merge(
filename: "#{name}.scss",
importer: self,
syntax: :scss
))
end
elsif name =~ GLOB
nil # globs must be relative
else
engine_from_path(name, root, options)
end
end
def each_globbed_file(glob, base_pathname, options)
Dir["#{base_pathname}/#{glob}"].sort.each do |filename|
next if filename == options[:filename]
yield filename # assume all matching files are requirable
end
end
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
end
return nil if contents.empty?
Sass::Engine.new(contents, options.merge(
filename: base_pathname.to_s,
importer: self,
syntax: :scss
))
end
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)
engine_from_path(name, root, options)
end
end
def engine_from_path(name, dir, options)
full_filename, _ = Sass::Util.destructure(find_real_file(dir, name, options))
return unless full_filename && File.readable?(full_filename)
private
depend_on(full_filename)
Sass::Engine.for_file(full_filename, options)
end
def depend_on(filename)
if @context
@context.depend_on(filename)
@context.depend_on(globbed_file_parent(filename))
end
end
def engine_from_path(name, dir, options)
full_filename, _ = ::Sass::Util.destructure(find_real_file(dir, name, options))
return unless full_filename && File.readable?(full_filename)
depend_on(full_filename)
::Sass::Engine.for_file(full_filename, options)
end
end
include Sass
include ::Sass::Rails::SassImporter::Globbing
def self.special_imports
self.new('').special_imports
end
end