Skip to content
This repository was archived by the owner on Oct 24, 2025. It is now read-only.

Commit 354e157

Browse files
committed
Add source_map option to Manifest.build_one()
1 parent 7724a49 commit 354e157

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

docs/changes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ To be released.
88

99
- Expose source maps support:
1010

11+
- :meth:`Manifest.build_one() <sassutils.builder.Manifest.build_one>` method
12+
has a new ``source_map`` option. This option builds also a source map
13+
file with the filename followed by :file:`.map` suffix.
1114
- :func:`sass.compile()` has a new optional parameter ``source_comments``.
1215
It can be one of :const:`sass.SOURCE_COMMENTS` keys. It also has
1316
a new parameter ``source_map_filename`` which is required only when

sasstests.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,35 @@ def test_normalize_manifests(self):
233233
assert manifests['package.name2'].sass_path == 'sass/path'
234234
assert manifests['package.name2'].css_path == 'css/path'
235235

236+
def test_build_one(self):
237+
d = tempfile.mkdtemp()
238+
try:
239+
shutil.copytree('test', os.path.join(d, 'test'))
240+
m = Manifest(sass_path='test', css_path='css')
241+
m.build_one(d, 'a.sass')
242+
with open(os.path.join(d, 'css', 'a.sass.css')) as f:
243+
self.assertEqual(A_EXPECTED_CSS, f.read())
244+
m.build_one(d, 'b.sass', source_map=True)
245+
with open(os.path.join(d, 'css', 'b.sass.css')) as f:
246+
self.assertEqual(
247+
B_EXPECTED_CSS +
248+
'\n/*# sourceMappingURL=b.sass.css.map */',
249+
f.read()
250+
)
251+
with open(os.path.join(d, 'css', 'b.sass.css.map')) as f:
252+
self.assertEqual(
253+
{
254+
'version': 3,
255+
'file': 'b.sass',
256+
'sources': ['../test/b.sass'],
257+
'names': [],
258+
'mappings': 'AAAA,EACE;EACE,WAAW'
259+
},
260+
json.load(f)
261+
)
262+
finally:
263+
shutil.rmtree(d)
264+
236265

237266
class WsgiTestCase(unittest.TestCase):
238267

sassutils/builder.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,25 +154,47 @@ def build(self, package_dir):
154154
return frozenset(os.path.join(self.css_path, filename)
155155
for filename in css_files)
156156

157-
def build_one(self, package_dir, filename):
157+
def build_one(self, package_dir, filename, source_map=False):
158158
"""Builds one SASS/SCSS file.
159159
160160
:param package_dir: the path of package directory
161161
:type package_dir: :class:`str`, :class:`basestring`
162162
:param filename: the filename of SASS/SCSS source to compile
163163
:type filename: :class:`str`, :class:`basestring`
164+
:param source_map: whether to use source maps. if :const:`True`
165+
it also write a source map to a ``filename``
166+
followed by :file:`.map` suffix.
167+
default is :const:`False`
168+
:type source_map: :class:`bool`
164169
:returns: the filename of compiled CSS
165170
:rtype: :class:`str`, :class:`basestring`
166171
172+
.. versionadded:: 0.4.0
173+
Added optional ``source_map`` parameter.
174+
167175
"""
168176
sass_filename, css_filename = self.resolve_filename(
169177
package_dir, filename)
170178
root_path = os.path.join(package_dir, self.sass_path)
171-
css = compile(filename=sass_filename, include_paths=[root_path])
172179
css_path = os.path.join(package_dir, self.css_path, css_filename)
180+
if source_map:
181+
source_map_path = css_filename + '.map'
182+
css, source_map = compile(
183+
filename=sass_filename,
184+
include_paths=[root_path],
185+
source_comments='map',
186+
source_map_filename=source_map_path # FIXME
187+
)
188+
else:
189+
css = compile(filename=sass_filename, include_paths=[root_path])
190+
source_map_path = None
191+
source_map = None
173192
css_folder = os.path.dirname(css_path)
174193
if not os.path.exists(css_folder):
175194
os.makedirs(css_folder)
176195
with open(css_path, 'w') as f:
177196
f.write(css)
197+
if source_map:
198+
with open(source_map_path, 'w') as f:
199+
f.write(source_map)
178200
return css_filename

0 commit comments

Comments
 (0)