Skip to content

Commit 19c927a

Browse files
committed
get ready for importer to be implemented
1 parent c7f7406 commit 19c927a

26 files changed

+293
-133
lines changed

lib/sassc/rails.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
require_relative "rails/version"
22
require_relative "rails/functions"
3-
#require_relative "rails/importer"
3+
require_relative "rails/importer"
44
require_relative "rails/template"
55
require_relative "rails/railtie"

lib/sassc/rails/importer.rb

Lines changed: 176 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -1,147 +1,192 @@
1-
require 'active_support/deprecation/reporting'
2-
require 'sass'
3-
require 'sprockets/sass_importer'
4-
require 'tilt'
5-
6-
module Sass
1+
module SassC
72
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)
437

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.
4613

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.
4917

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.
5121

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.
7024

71-
def erb_extensions
72-
{
73-
:scss_erb => :scss,
74-
:sass_erb => :sass
75-
}
76-
end
25+
# lets ignore globbing for now.
7726

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
8129

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
8533

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)
9735
end
9836

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
11238

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]
14441
end
14542
end
14643
end
14744
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

lib/sassc/rails/template.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ def call(input)
1111
filename: input[:filename],
1212
syntax: self.class.syntax,
1313
load_paths: input[:environment].paths,
14+
importer: SassC::Rails::Importer,
1415
sprockets: {
1516
context: context,
1617
environment: input[:environment],

sassc-rails.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Gem::Specification.new do |spec|
2828
# reuse portions of the sprockets template
2929
spec.add_dependency 'sass'
3030

31-
spec.add_dependency "sassc", "0.0.8"
31+
spec.add_dependency "sassc", "0.0.9"
3232
spec.add_dependency 'railties'
3333
spec.add_dependency 'sprockets', '3.0.0.beta.6'
3434
end
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.top-level {
2+
font-color: bold;
3+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
*= require partials/_sass_import
3+
*= require partials/_scss_import
4+
*= require_tree ./globbed
5+
*= require subfolder/plain
6+
*= require subfolder/second_level
7+
*/
8+
9+
.css-application {
10+
background: #fff;
11+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.css-erb-handler {
2+
margin: <%= 0 %>;
3+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.css-sass-erb-handler
2+
margin: <%= 0 %>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.css-scss-erb-handler {
2+
margin: <%= 0 %>;
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.globbed {
2+
color: green;
3+
}

0 commit comments

Comments
 (0)