Skip to content

Commit cfd7ce5

Browse files
committed
Support source map generation with -g option
1 parent fd61bd8 commit cfd7ce5

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,11 @@ In `app2.scss` you could reference app1's and app2's `_colors.scss` import as so
6161
```
6262

6363
Then to compile `app2.scss` and put it in the `css` directory,
64-
run the following management command:
64+
run the following management command (the `-g` will build a source map, which
65+
is helpful for debugging CSS):
6566

6667
```
67-
python manage.py sass app2/static/app2/scss/app2.scss app2/static/app2/css/app2.css
68+
python manage.py sass app2/static/app2/scss/app2.scss app2/static/app2/css/app2.css -g
6869
```
6970

7071
Or, you can compile the entire `scss` directory into
@@ -141,9 +142,7 @@ Limitations
141142

142143
* Only files ending in `.scss` are supported for now.
143144

144-
* Source map generation is not supported yet.
145-
146-
* Only supports `-t` and `-p` options similar to `pysassc`. Ideally `django-sass` will
145+
* Only supports `-g`, `-p`, and `-t` options similar to `pysassc`. Ideally `django-sass` will
147146
be as similar as possible to the `pysassc` command line interface.
148147

149148
Feel free to file an issue or make a pull request to improve any of these limitations. 🐱‍💻
@@ -173,6 +172,9 @@ and Linux), and of course Django (any version).
173172
Changelog
174173
---------
175174

175+
#### 0.2.0
176+
* New feature: `-g` option to build a source map (when input is a file, not a directory).
177+
176178
#### 0.1.2
177179
* Fix: Write compiled CSS files as UTF-8.
178180
* Change: Default `-p` precision from 5 to 8 for better support building Bootstrap CSS.

django_sass/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.1.2"
1+
__version__ = "0.2.0"

django_sass/management/commands/sass.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ def add_arguments(self, parser):
2222
nargs="+",
2323
help="A file or directory in which to output transpiled css",
2424
)
25+
parser.add_argument(
26+
"-g",
27+
dest="g",
28+
action="store_true",
29+
help="Build a sourcemap. Only applicable if input is a file, not a directory.",
30+
)
2531
parser.add_argument(
2632
"-t",
2733
type=str,
@@ -49,7 +55,18 @@ def compile_sass(self, outfile, **kwargs):
4955
# sass.compile() will return None of used with dirname.
5056
# If used with filename, it will return a string of file contents.
5157
if rval and outfile:
52-
# Write the outputted css to file
58+
# If we got a css and sourcemap tuple, write the sourcemap.
59+
if isinstance(rval, tuple):
60+
map_outfile = outfile + ".map"
61+
outfile_dir = os.path.dirname(map_outfile)
62+
if not os.path.exists(outfile_dir):
63+
os.makedirs(outfile_dir, exist_ok=True)
64+
file = open(map_outfile, "w", encoding="utf8")
65+
file.write(rval[1])
66+
file.close()
67+
rval = rval[0]
68+
69+
# Write the outputted css to file.
5370
outfile_dir = os.path.dirname(outfile)
5471
if not os.path.exists(outfile_dir):
5572
os.makedirs(outfile_dir, exist_ok=True)
@@ -79,7 +96,7 @@ def handle(self, *args, **options):
7996
sassargs.update({"include_paths": found_paths})
8097

8198
if os.path.isdir(inpath):
82-
# assume outpath is also a dir, or make it
99+
# Assume outpath is also a dir, or make it.
83100
if not os.path.exists(outpath):
84101
os.makedirs(outpath)
85102
if os.path.isdir(outpath):
@@ -95,8 +112,10 @@ def handle(self, *args, **options):
95112
)
96113
else:
97114
outfile = outpath
115+
if options["g"]:
116+
sassargs.update({"source_map_filename": outfile + ".map"})
98117

99-
# Watch files for changes if specified
118+
# Watch files for changes if specified.
100119
if options["watch"]:
101120
try:
102121
self.stdout.write("Watching...")

0 commit comments

Comments
 (0)