@@ -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