Skip to content

Commit 872c835

Browse files
author
Andrey Fedoseev
committed
Add load_paths parameter to SASS/SCSS compilers
1 parent 7c2651e commit 872c835

File tree

5 files changed

+41
-13
lines changed

5 files changed

+41
-13
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ Sergey Fedoseev [https://github.com/sir-sigurd]
77
Alexey Kuleshevich [https://github.com/lehins]
88
Andrey Dresvyannikov [https://github.com/sepulchered]
99
Jasper Schulz [https://github.com/jaheba]
10+
Tim Olson [https://github.com/timolson]

CHANGES.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Dev
1313
- Add `Handlebars <http://handlebarsjs.com/>`_ compiler
1414
- Add support for Django 1.9
1515
- Add ``plugins`` parameter to Babel compiler
16+
- Add ``load_paths`` parameter to SASS/SCSS compilers
1617

1718

1819
1.0.1

README.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,13 @@ SASS / SCSS
241241
``compass_enabled``
242242
Boolean. Whether to use compass or not. Compass must be installed in your system. Run "sass --compass" and if no error is shown it means that compass is installed.
243243

244+
``load_paths``
245+
List of import paths (``--load-path`` command line option).
246+
244247
Example::
245248

246249
STATIC_PRECOMPILER_COMPILERS = (
247-
('static_precompiler.compilers.SCSS', {"executable": "/usr/bin/sass", "sourcemap_enabled": True, "compass_enabled": True}),
250+
('static_precompiler.compilers.SCSS', {"executable": "/usr/bin/sass", "sourcemap_enabled": True, "compass_enabled": True, "load_paths": ["path"]}),
248251
)
249252

250253

static_precompiler/compilers/scss.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,29 @@ class SCSS(base.BaseCompiler):
2323
IMPORT_RE = re.compile(r"@import\s+(.+?)\s*;", re.DOTALL)
2424

2525
def __init__(self, executable=settings.SCSS_EXECUTABLE, sourcemap_enabled=False,
26-
compass_enabled=settings.SCSS_USE_COMPASS):
26+
compass_enabled=settings.SCSS_USE_COMPASS, load_paths=None):
2727
self.executable = executable
2828
self.is_sourcemap_enabled = sourcemap_enabled
2929
self.is_compass_enabled = compass_enabled
30+
if load_paths is None:
31+
self.load_paths = []
32+
elif not isinstance(load_paths, (list, tuple)):
33+
raise ValueError("load_paths option must be an iterable object (list, tuple)")
34+
else:
35+
self.load_paths = load_paths
3036
super(SCSS, self).__init__()
3137

38+
def get_extra_args(self):
39+
args = []
40+
41+
for path in self.load_paths:
42+
args += ["-I", path]
43+
44+
if self.is_compass_enabled:
45+
args.append("--compass")
46+
47+
return args
48+
3249
def should_compile(self, source_path, from_management=False):
3350
# Do not compile the files that start with "_" if run from management
3451
if from_management and os.path.basename(source_path).startswith("_"):
@@ -41,10 +58,7 @@ def compile_file(self, source_path):
4158
args = [
4259
self.executable,
4360
"--sourcemap={}".format("auto" if self.is_sourcemap_enabled else "none"),
44-
]
45-
46-
if self.is_compass_enabled:
47-
args.append("--compass")
61+
] + self.get_extra_args()
4862

4963
args.extend([
5064
self.get_full_source_path(source_path),
@@ -73,13 +87,11 @@ def compile_source(self, source):
7387
args = [
7488
self.executable,
7589
"-s",
76-
]
90+
] + self.get_extra_args()
91+
7792
if self.executable.endswith("sass"):
7893
args.append("--scss")
7994

80-
if self.is_compass_enabled:
81-
args.append("--compass")
82-
8395
out, errors = utils.run_command(args, source)
8496
if errors:
8597
raise exceptions.StaticCompilationError(errors)
@@ -250,9 +262,6 @@ def compile_source(self, source):
250262
if self.executable.endswith("scss"):
251263
args.append("--sass")
252264

253-
if self.is_compass_enabled:
254-
args.append("--compass")
255-
256265
out, errors = utils.run_command(args, source)
257266
if errors:
258267
raise exceptions.StaticCompilationError(errors)

static_precompiler/tests/test_scss.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,3 +233,17 @@ def test_compass_import(monkeypatch, tmpdir):
233233
compiler = compilers.SCSS(compass_enabled=False)
234234
with pytest.raises(exceptions.StaticCompilationError):
235235
compiler.compile_file("styles/test-compass-import.scss")
236+
237+
238+
def test_get_extra_args():
239+
240+
assert compilers.SCSS().get_extra_args() == []
241+
242+
assert compilers.SCSS(compass_enabled=True, load_paths=["foo", "bar"]).get_extra_args() == [
243+
"-I", "foo",
244+
"-I", "bar",
245+
"--compass",
246+
]
247+
248+
with pytest.raises(ValueError):
249+
compilers.SCSS(load_paths="foo")

0 commit comments

Comments
 (0)