|
| 1 | +require 'active_support/deprecation/reporting' |
| 2 | +require 'sass' |
| 3 | +require 'sprockets/sass_importer' |
| 4 | +require 'tilt' |
| 5 | + |
| 6 | +module Sass |
| 7 | + module Rails |
| 8 | + class SassImporter < Sass::Importers::Filesystem |
| 9 | + module Globbing |
| 10 | + GLOB = /(\A|\/)(\*|\*\*\/\*)\z/ |
| 11 | + |
| 12 | + def find_relative(name, base, options) |
| 13 | + if options[:sprockets] && m = name.match(GLOB) |
| 14 | + path = name.sub(m[0], "") |
| 15 | + base = File.expand_path(path, File.dirname(base)) |
| 16 | + glob_imports(base, m[2], options) |
| 17 | + else |
| 18 | + super |
| 19 | + end |
| 20 | + end |
| 21 | + |
| 22 | + def find(name, options) |
| 23 | + # globs must be relative |
| 24 | + return if name =~ GLOB |
| 25 | + super |
| 26 | + end |
| 27 | + |
| 28 | + private |
| 29 | + def glob_imports(base, glob, options) |
| 30 | + contents = "" |
| 31 | + context = options[:sprockets][:context] |
| 32 | + each_globbed_file(base, glob, context) do |filename| |
| 33 | + next if filename == options[:filename] |
| 34 | + contents << "@import #{filename.inspect};\n" |
| 35 | + end |
| 36 | + return nil if contents == "" |
| 37 | + Sass::Engine.new(contents, options.merge( |
| 38 | + :filename => base, |
| 39 | + :importer => self, |
| 40 | + :syntax => :scss |
| 41 | + )) |
| 42 | + end |
| 43 | + |
| 44 | + def each_globbed_file(base, glob, context) |
| 45 | + raise ArgumentError unless glob == "*" || glob == "**/*" |
| 46 | + |
| 47 | + exts = extensions.keys.map { |ext| Regexp.escape(".#{ext}") }.join("|") |
| 48 | + sass_re = Regexp.compile("(#{exts})$") |
| 49 | + |
| 50 | + context.depend_on(base) |
| 51 | + |
| 52 | + Dir["#{base}/#{glob}"].sort.each do |path| |
| 53 | + if File.directory?(path) |
| 54 | + context.depend_on(path) |
| 55 | + elsif sass_re =~ path |
| 56 | + yield path |
| 57 | + end |
| 58 | + end |
| 59 | + end |
| 60 | + end |
| 61 | + |
| 62 | + module ERB |
| 63 | + def extensions |
| 64 | + { |
| 65 | + 'css.erb' => :scss_erb, |
| 66 | + 'scss.erb' => :scss_erb, |
| 67 | + 'sass.erb' => :sass_erb |
| 68 | + }.merge(super) |
| 69 | + end |
| 70 | + |
| 71 | + def erb_extensions |
| 72 | + { |
| 73 | + :scss_erb => :scss, |
| 74 | + :sass_erb => :sass |
| 75 | + } |
| 76 | + end |
| 77 | + |
| 78 | + def find_relative(*args) |
| 79 | + process_erb_engine(super) |
| 80 | + end |
| 81 | + |
| 82 | + def find(*args) |
| 83 | + process_erb_engine(super) |
| 84 | + end |
| 85 | + |
| 86 | + private |
| 87 | + def process_erb_engine(engine) |
| 88 | + if engine && engine.options[:sprockets] && syntax = erb_extensions[engine.options[:syntax]] |
| 89 | + template = Tilt::ERBTemplate.new(engine.options[:filename]) |
| 90 | + contents = template.render(engine.options[:sprockets][:context], {}) |
| 91 | + |
| 92 | + Sass::Engine.new(contents, engine.options.merge(:syntax => syntax)) |
| 93 | + else |
| 94 | + engine |
| 95 | + end |
| 96 | + end |
| 97 | + end |
| 98 | + |
| 99 | + module Deprecated |
| 100 | + def extensions |
| 101 | + { |
| 102 | + 'css.scss' => :scss, |
| 103 | + 'css.sass' => :sass, |
| 104 | + 'css.scss.erb' => :scss_erb, |
| 105 | + 'css.sass.erb' => :sass_erb |
| 106 | + }.merge(super) |
| 107 | + end |
| 108 | + |
| 109 | + def find_relative(*args) |
| 110 | + deprecate_extra_css_extension(super) |
| 111 | + end |
| 112 | + |
| 113 | + def find(*args) |
| 114 | + deprecate_extra_css_extension(super) |
| 115 | + end |
| 116 | + |
| 117 | + private |
| 118 | + def deprecate_extra_css_extension(engine) |
| 119 | + if engine && filename = engine.options[:filename] |
| 120 | + if filename.end_with?('.css.scss') |
| 121 | + msg = "Extra .css in SCSS file is unnecessary. Rename #{filename} to #{filename.sub('.css.scss', '.scss')}." |
| 122 | + elsif filename.end_with?('.css.sass') |
| 123 | + msg = "Extra .css in SASS file is unnecessary. Rename #{filename} to #{filename.sub('.css.sass', '.sass')}." |
| 124 | + elsif filename.end_with?('.css.scss.erb') |
| 125 | + msg = "Extra .css in SCSS/ERB file is unnecessary. Rename #{filename} to #{filename.sub('.css.scss.erb', '.scss.erb')}." |
| 126 | + elsif filename.end_with?('.css.sass.erb') |
| 127 | + msg = "Extra .css in SASS/ERB file is unnecessary. Rename #{filename} to #{filename.sub('.css.sass.erb', '.sass.erb')}." |
| 128 | + end |
| 129 | + |
| 130 | + ActiveSupport::Deprecation.warn(msg) if msg |
| 131 | + end |
| 132 | + |
| 133 | + engine |
| 134 | + end |
| 135 | + end |
| 136 | + |
| 137 | + include Deprecated |
| 138 | + include ERB |
| 139 | + include Globbing |
| 140 | + |
| 141 | + # Allow .css files to be @import'd |
| 142 | + def extensions |
| 143 | + { 'css' => :scss }.merge(super) |
| 144 | + end |
| 145 | + end |
| 146 | + end |
| 147 | +end |
0 commit comments