|
1 |
| -require 'active_support/deprecation/reporting' |
2 |
| -require 'sass' |
3 |
| -require 'sprockets/sass_importer' |
4 |
| -require 'tilt' |
5 |
| - |
6 |
| -module Sass |
| 1 | +module SassC |
7 | 2 | 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 |
| 3 | + class Importer < SassC::Importer |
| 4 | + def imports(path, parent_path) |
| 5 | + # should return an Import, or array of Imports. |
| 6 | + # Import.new(path) |
43 | 7 |
|
44 |
| - def each_globbed_file(base, glob, context) |
45 |
| - raise ArgumentError unless glob == "*" || glob == "**/*" |
| 8 | + # Imports can usually be passed with only a path. However, |
| 9 | + # when parsing ERB we need to return the import like this: |
| 10 | + # Import.new(path, source: parsed_erb) |
| 11 | + # in this case, the path is just for reference, i don't believe it |
| 12 | + # gets used. |
46 | 13 |
|
47 |
| - exts = extensions.keys.map { |ext| Regexp.escape(".#{ext}") }.join("|") |
48 |
| - sass_re = Regexp.compile("(#{exts})$") |
| 14 | + # the parent path is basically the file that's calling the |
| 15 | + # @import function. This necessary when we need to do an import |
| 16 | + # relative to the folder that the import function is being called from. |
49 | 17 |
|
50 |
| - context.depend_on(base) |
| 18 | + # for the filename, it looks like libsass can properly resolve |
| 19 | + # filenames without extensions provided that the actual file has |
| 20 | + # a .sass or .scss filename. |
51 | 21 |
|
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 |
| 22 | + # it CANNOT resolve a .css file unless you explicitly pass the |
| 23 | + # filename with the .css extension. |
70 | 24 |
|
71 |
| - def erb_extensions |
72 |
| - { |
73 |
| - :scss_erb => :scss, |
74 |
| - :sass_erb => :sass |
75 |
| - } |
76 |
| - end |
| 25 | + # lets ignore globbing for now. |
77 | 26 |
|
78 |
| - def find_relative(*args) |
79 |
| - process_erb_engine(super) |
80 |
| - end |
| 27 | + # for ERB parsing, check out the sass-rails importer below, it looks pretty |
| 28 | + # simple |
81 | 29 |
|
82 |
| - def find(*args) |
83 |
| - process_erb_engine(super) |
84 |
| - end |
| 30 | + # check out |
| 31 | + # https://github.com/sass/sass/blob/stable/lib/sass/importers/filesystem.rb |
| 32 | + # for reference |
85 | 33 |
|
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 |
| 34 | + Import.new(path) |
97 | 35 | end
|
98 | 36 |
|
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 |
| 37 | + private |
112 | 38 |
|
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) |
| 39 | + def load_paths |
| 40 | + options[:load_paths] |
144 | 41 | end
|
145 | 42 | end
|
146 | 43 | end
|
147 | 44 | end
|
| 45 | + |
| 46 | +#require 'active_support/deprecation/reporting' |
| 47 | +#require 'sass' |
| 48 | +#require 'sprockets/sass_importer' |
| 49 | +#require 'tilt' |
| 50 | + |
| 51 | +# module Sass |
| 52 | +# module Rails |
| 53 | +# class SassImporter < Sass::Importers::Filesystem |
| 54 | +# module Globbing |
| 55 | +# GLOB = /(\A|\/)(\*|\*\*\/\*)\z/ |
| 56 | +# |
| 57 | +# def find_relative(name, base, options) |
| 58 | +# if options[:sprockets] && m = name.match(GLOB) |
| 59 | +# path = name.sub(m[0], "") |
| 60 | +# base = File.expand_path(path, File.dirname(base)) |
| 61 | +# glob_imports(base, m[2], options) |
| 62 | +# else |
| 63 | +# super |
| 64 | +# end |
| 65 | +# end |
| 66 | +# |
| 67 | +# def find(name, options) |
| 68 | +# # globs must be relative |
| 69 | +# return if name =~ GLOB |
| 70 | +# super |
| 71 | +# end |
| 72 | +# |
| 73 | +# private |
| 74 | +# def glob_imports(base, glob, options) |
| 75 | +# contents = "" |
| 76 | +# context = options[:sprockets][:context] |
| 77 | +# each_globbed_file(base, glob, context) do |filename| |
| 78 | +# next if filename == options[:filename] |
| 79 | +# contents << "@import #{filename.inspect};\n" |
| 80 | +# end |
| 81 | +# return nil if contents == "" |
| 82 | +# Sass::Engine.new(contents, options.merge( |
| 83 | +# :filename => base, |
| 84 | +# :importer => self, |
| 85 | +# :syntax => :scss |
| 86 | +# )) |
| 87 | +# end |
| 88 | +# |
| 89 | +# def each_globbed_file(base, glob, context) |
| 90 | +# raise ArgumentError unless glob == "*" || glob == "**/*" |
| 91 | +# |
| 92 | +# exts = extensions.keys.map { |ext| Regexp.escape(".#{ext}") }.join("|") |
| 93 | +# sass_re = Regexp.compile("(#{exts})$") |
| 94 | +# |
| 95 | +# context.depend_on(base) |
| 96 | +# |
| 97 | +# Dir["#{base}/#{glob}"].sort.each do |path| |
| 98 | +# if File.directory?(path) |
| 99 | +# context.depend_on(path) |
| 100 | +# elsif sass_re =~ path |
| 101 | +# yield path |
| 102 | +# end |
| 103 | +# end |
| 104 | +# end |
| 105 | +# end |
| 106 | +# |
| 107 | +# module ERB |
| 108 | +# def extensions |
| 109 | +# { |
| 110 | +# 'css.erb' => :scss_erb, |
| 111 | +# 'scss.erb' => :scss_erb, |
| 112 | +# 'sass.erb' => :sass_erb |
| 113 | +# }.merge(super) |
| 114 | +# end |
| 115 | +# |
| 116 | +# def erb_extensions |
| 117 | +# { |
| 118 | +# :scss_erb => :scss, |
| 119 | +# :sass_erb => :sass |
| 120 | +# } |
| 121 | +# end |
| 122 | +# |
| 123 | +# def find_relative(*args) |
| 124 | +# process_erb_engine(super) |
| 125 | +# end |
| 126 | +# |
| 127 | +# def find(*args) |
| 128 | +# process_erb_engine(super) |
| 129 | +# end |
| 130 | +# |
| 131 | +# private |
| 132 | +# def process_erb_engine(engine) |
| 133 | +# if engine && engine.options[:sprockets] && syntax = erb_extensions[engine.options[:syntax]] |
| 134 | +# template = Tilt::ERBTemplate.new(engine.options[:filename]) |
| 135 | +# contents = template.render(engine.options[:sprockets][:context], {}) |
| 136 | +# |
| 137 | +# Sass::Engine.new(contents, engine.options.merge(:syntax => syntax)) |
| 138 | +# else |
| 139 | +# engine |
| 140 | +# end |
| 141 | +# end |
| 142 | +# end |
| 143 | +# |
| 144 | +# module Deprecated |
| 145 | +# def extensions |
| 146 | +# { |
| 147 | +# 'css.scss' => :scss, |
| 148 | +# 'css.sass' => :sass, |
| 149 | +# 'css.scss.erb' => :scss_erb, |
| 150 | +# 'css.sass.erb' => :sass_erb |
| 151 | +# }.merge(super) |
| 152 | +# end |
| 153 | +# |
| 154 | +# def find_relative(*args) |
| 155 | +# deprecate_extra_css_extension(super) |
| 156 | +# end |
| 157 | +# |
| 158 | +# def find(*args) |
| 159 | +# deprecate_extra_css_extension(super) |
| 160 | +# end |
| 161 | +# |
| 162 | +# private |
| 163 | +# def deprecate_extra_css_extension(engine) |
| 164 | +# if engine && filename = engine.options[:filename] |
| 165 | +# if filename.end_with?('.css.scss') |
| 166 | +# msg = "Extra .css in SCSS file is unnecessary. Rename #{filename} to #{filename.sub('.css.scss', '.scss')}." |
| 167 | +# elsif filename.end_with?('.css.sass') |
| 168 | +# msg = "Extra .css in SASS file is unnecessary. Rename #{filename} to #{filename.sub('.css.sass', '.sass')}." |
| 169 | +# elsif filename.end_with?('.css.scss.erb') |
| 170 | +# msg = "Extra .css in SCSS/ERB file is unnecessary. Rename #{filename} to #{filename.sub('.css.scss.erb', '.scss.erb')}." |
| 171 | +# elsif filename.end_with?('.css.sass.erb') |
| 172 | +# msg = "Extra .css in SASS/ERB file is unnecessary. Rename #{filename} to #{filename.sub('.css.sass.erb', '.sass.erb')}." |
| 173 | +# end |
| 174 | +# |
| 175 | +# ActiveSupport::Deprecation.warn(msg) if msg |
| 176 | +# end |
| 177 | +# |
| 178 | +# engine |
| 179 | +# end |
| 180 | +# end |
| 181 | +# |
| 182 | +# include Deprecated |
| 183 | +# include ERB |
| 184 | +# include Globbing |
| 185 | +# |
| 186 | +# # Allow .css files to be @import'd |
| 187 | +# def extensions |
| 188 | +# { 'css' => :scss }.merge(super) |
| 189 | +# end |
| 190 | +# end |
| 191 | +# end |
| 192 | +# end |
0 commit comments