Skip to content

unresolve sass paths with package_dir #280

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 19 additions & 25 deletions sasstests.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,14 @@ def normalize_path(path):
re_base64_data_uri = re.compile(r'^data:[^;]*?;base64,(.+)$')


def _map_in_output_dir(s):
def cb(match):
filename = os.path.basename(match.group(1))
return '/*# sourceMappingURL={} */'.format(filename)

return re_sourcemap_url.sub(cb, s)


@pytest.fixture(autouse=True)
def no_warnings(recwarn):
yield
Expand Down Expand Up @@ -696,12 +704,6 @@ def test_build_one(self):
with tempdir() as d:
src_path = os.path.join(d, 'test')

def test_source_path(*path):
return normalize_path(os.path.join(d, 'test', *path))

def replace_source_path(s, name):
return s.replace('SOURCE', test_source_path(name))

shutil.copytree('test', src_path)
with pytest.warns(FutureWarning):
m = Manifest(sass_path='test', css_path='css')
Expand All @@ -713,14 +715,11 @@ def replace_source_path(s, name):
with io.open(
os.path.join(d, 'css', 'b.scss.css'), encoding='UTF-8',
) as f:
self.assertEqual(
replace_source_path(B_EXPECTED_CSS_WITH_MAP, 'b.scss'),
f.read(),
)
assert f.read() == _map_in_output_dir(B_EXPECTED_CSS_WITH_MAP)
self.assert_source_map_file(
{
'version': 3,
'file': '../test/b.css',
'file': 'b.scss.css',
'sources': ['../test/b.scss'],
'names': [],
'mappings': (
Expand All @@ -734,14 +733,11 @@ def replace_source_path(s, name):
with io.open(
os.path.join(d, 'css', 'd.scss.css'), encoding='UTF-8',
) as f:
assert (
replace_source_path(D_EXPECTED_CSS_WITH_MAP, 'd.scss') ==
f.read()
)
assert f.read() == _map_in_output_dir(D_EXPECTED_CSS_WITH_MAP)
self.assert_source_map_file(
{
'version': 3,
'file': '../test/d.css',
'file': 'd.scss.css',
'sources': ['../test/d.scss'],
'names': [],
'mappings': (
Expand Down Expand Up @@ -799,7 +795,7 @@ def test_wsgi_sass_middleware(self):
r = client.get('/static/a.scss.css')
assert r.status_code == 200
self.assertEqual(
b(A_EXPECTED_CSS_WITH_MAP),
b(_map_in_output_dir(A_EXPECTED_CSS_WITH_MAP)),
r.data,
)
assert r.mimetype == 'text/css'
Expand All @@ -825,32 +821,30 @@ def test_wsgi_sass_middleware_without_extension(self):
client = Client(app, Response)
r = client.get('/static/a.css')
assert r.status_code == 200
expected = A_EXPECTED_CSS_WITH_MAP.replace('.scss.css', '.css')
expected = A_EXPECTED_CSS_WITH_MAP
expected = expected.replace('.scss.css', '.css')
expected = _map_in_output_dir(expected)
self.assertEqual(expected.encode(), r.data)
assert r.mimetype == 'text/css'

def test_wsgi_sass_middleware_without_extension_sass(self):
with tempdir() as css_dir:
src_dir = os.path.join(css_dir, 'src')
os.makedirs(src_dir)
with open(os.path.join(src_dir, 'a.sass'), 'w') as f:
f.write('a\n\tb\n\t\tcolor: blue;')
app = SassMiddleware(
self.sample_wsgi_app, {
__name__: {
'sass_path': src_dir,
'sass_path': 'test',
'css_path': css_dir,
'wsgi_path': '/static',
'strip_extension': True,
},
},
)
client = Client(app, Response)
r = client.get('/static/a.css')
r = client.get('/static/h.css')
assert r.status_code == 200
expected = (
'a b {\n color: blue; }\n\n'
'/*# sourceMappingURL=../a.css.map */'
'/*# sourceMappingURL=h.css.map */'
)
self.assertEqual(expected.encode(), r.data)
assert r.mimetype == 'text/css'
Expand Down
9 changes: 7 additions & 2 deletions sassutils/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,12 @@ def resolve_filename(self, package_dir, filename):
css_path = os.path.join(package_dir, self.css_path, css_filename)
return sass_path, css_path

def unresolve_filename(self, filename):
def unresolve_filename(self, package_dir, filename):
"""Retrieves the probable source path from the output filename. Pass
in a .css path to get out a .scss path.

:param package_dir: the path of the package directory
:type package_dir: :class:`str`
:param filename: the css filename
:type filename: :class:`str`
:returns: the scss filename
Expand All @@ -200,7 +202,9 @@ def unresolve_filename(self, filename):
filename, _ = os.path.splitext(filename)
if self.strip_extension:
for ext in ('.scss', '.sass'):
test_path = os.path.join(self.sass_path, filename + ext)
test_path = os.path.join(
package_dir, self.sass_path, filename + ext,
)
if os.path.exists(test_path):
return filename + ext
else: # file not found, let it error with `.scss` extension
Expand Down Expand Up @@ -268,6 +272,7 @@ def build_one(self, package_dir, filename, source_map=False):
filename=sass_filename,
include_paths=[root_path],
source_map_filename=source_map_path, # FIXME
output_filename_hint=css_path,
)
else:
css = compile(filename=sass_filename, include_paths=[root_path])
Expand Down
4 changes: 3 additions & 1 deletion sassutils/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ def __call__(self, environ, start_response):
if not path.startswith(prefix):
continue
css_filename = path[len(prefix):]
sass_filename = manifest.unresolve_filename(css_filename)
sass_filename = manifest.unresolve_filename(
package_dir, css_filename,
)
try:
result = manifest.build_one(
package_dir,
Expand Down