Skip to content

Commit e51e92d

Browse files
committed
unresolve sass paths with package_dir
1 parent f3d7571 commit e51e92d

File tree

3 files changed

+29
-28
lines changed

3 files changed

+29
-28
lines changed

sasstests.py

+19-25
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,14 @@ def normalize_path(path):
143143
re_base64_data_uri = re.compile(r'^data:[^;]*?;base64,(.+)$')
144144

145145

146+
def _map_in_output_dir(s):
147+
def cb(match):
148+
filename = os.path.basename(match.group(1))
149+
return '/*# sourceMappingURL={} */'.format(filename)
150+
151+
return re_sourcemap_url.sub(cb, s)
152+
153+
146154
@pytest.fixture(autouse=True)
147155
def no_warnings(recwarn):
148156
yield
@@ -696,12 +704,6 @@ def test_build_one(self):
696704
with tempdir() as d:
697705
src_path = os.path.join(d, 'test')
698706

699-
def test_source_path(*path):
700-
return normalize_path(os.path.join(d, 'test', *path))
701-
702-
def replace_source_path(s, name):
703-
return s.replace('SOURCE', test_source_path(name))
704-
705707
shutil.copytree('test', src_path)
706708
with pytest.warns(FutureWarning):
707709
m = Manifest(sass_path='test', css_path='css')
@@ -713,14 +715,11 @@ def replace_source_path(s, name):
713715
with io.open(
714716
os.path.join(d, 'css', 'b.scss.css'), encoding='UTF-8',
715717
) as f:
716-
self.assertEqual(
717-
replace_source_path(B_EXPECTED_CSS_WITH_MAP, 'b.scss'),
718-
f.read(),
719-
)
718+
assert f.read() == _map_in_output_dir(B_EXPECTED_CSS_WITH_MAP)
720719
self.assert_source_map_file(
721720
{
722721
'version': 3,
723-
'file': '../test/b.css',
722+
'file': 'b.scss.css',
724723
'sources': ['../test/b.scss'],
725724
'names': [],
726725
'mappings': (
@@ -734,14 +733,11 @@ def replace_source_path(s, name):
734733
with io.open(
735734
os.path.join(d, 'css', 'd.scss.css'), encoding='UTF-8',
736735
) as f:
737-
assert (
738-
replace_source_path(D_EXPECTED_CSS_WITH_MAP, 'd.scss') ==
739-
f.read()
740-
)
736+
assert f.read() == _map_in_output_dir(D_EXPECTED_CSS_WITH_MAP)
741737
self.assert_source_map_file(
742738
{
743739
'version': 3,
744-
'file': '../test/d.css',
740+
'file': 'd.scss.css',
745741
'sources': ['../test/d.scss'],
746742
'names': [],
747743
'mappings': (
@@ -799,7 +795,7 @@ def test_wsgi_sass_middleware(self):
799795
r = client.get('/static/a.scss.css')
800796
assert r.status_code == 200
801797
self.assertEqual(
802-
b(A_EXPECTED_CSS_WITH_MAP),
798+
b(_map_in_output_dir(A_EXPECTED_CSS_WITH_MAP)),
803799
r.data,
804800
)
805801
assert r.mimetype == 'text/css'
@@ -825,32 +821,30 @@ def test_wsgi_sass_middleware_without_extension(self):
825821
client = Client(app, Response)
826822
r = client.get('/static/a.css')
827823
assert r.status_code == 200
828-
expected = A_EXPECTED_CSS_WITH_MAP.replace('.scss.css', '.css')
824+
expected = A_EXPECTED_CSS_WITH_MAP
825+
expected = expected.replace('.scss.css', '.css')
826+
expected = _map_in_output_dir(expected)
829827
self.assertEqual(expected.encode(), r.data)
830828
assert r.mimetype == 'text/css'
831829

832830
def test_wsgi_sass_middleware_without_extension_sass(self):
833831
with tempdir() as css_dir:
834-
src_dir = os.path.join(css_dir, 'src')
835-
os.makedirs(src_dir)
836-
with open(os.path.join(src_dir, 'a.sass'), 'w') as f:
837-
f.write('a\n\tb\n\t\tcolor: blue;')
838832
app = SassMiddleware(
839833
self.sample_wsgi_app, {
840834
__name__: {
841-
'sass_path': src_dir,
835+
'sass_path': 'test',
842836
'css_path': css_dir,
843837
'wsgi_path': '/static',
844838
'strip_extension': True,
845839
},
846840
},
847841
)
848842
client = Client(app, Response)
849-
r = client.get('/static/a.css')
843+
r = client.get('/static/h.css')
850844
assert r.status_code == 200
851845
expected = (
852846
'a b {\n color: blue; }\n\n'
853-
'/*# sourceMappingURL=../a.css.map */'
847+
'/*# sourceMappingURL=h.css.map */'
854848
)
855849
self.assertEqual(expected.encode(), r.data)
856850
assert r.mimetype == 'text/css'

sassutils/builder.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,12 @@ def resolve_filename(self, package_dir, filename):
188188
css_path = os.path.join(package_dir, self.css_path, css_filename)
189189
return sass_path, css_path
190190

191-
def unresolve_filename(self, filename):
191+
def unresolve_filename(self, package_dir, filename):
192192
"""Retrieves the probable source path from the output filename. Pass
193193
in a .css path to get out a .scss path.
194194
195+
:param package_dir: the path of the package directory
196+
:type package_dir: :class:`str`
195197
:param filename: the css filename
196198
:type filename: :class:`str`
197199
:returns: the scss filename
@@ -200,7 +202,9 @@ def unresolve_filename(self, filename):
200202
filename, _ = os.path.splitext(filename)
201203
if self.strip_extension:
202204
for ext in ('.scss', '.sass'):
203-
test_path = os.path.join(self.sass_path, filename + ext)
205+
test_path = os.path.join(
206+
package_dir, self.sass_path, filename + ext,
207+
)
204208
if os.path.exists(test_path):
205209
return filename + ext
206210
else: # file not found, let it error with `.scss` extension
@@ -268,6 +272,7 @@ def build_one(self, package_dir, filename, source_map=False):
268272
filename=sass_filename,
269273
include_paths=[root_path],
270274
source_map_filename=source_map_path, # FIXME
275+
output_filename_hint=css_path,
271276
)
272277
else:
273278
css = compile(filename=sass_filename, include_paths=[root_path])

sassutils/wsgi.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,9 @@ def __call__(self, environ, start_response):
125125
if not path.startswith(prefix):
126126
continue
127127
css_filename = path[len(prefix):]
128-
sass_filename = manifest.unresolve_filename(css_filename)
128+
sass_filename = manifest.unresolve_filename(
129+
package_dir, css_filename,
130+
)
129131
try:
130132
result = manifest.build_one(
131133
package_dir,

0 commit comments

Comments
 (0)