@@ -40,7 +40,7 @@ Can be a .js file, a rule producing .js files as its default output, or a rule p
4040
4141Note that you can pass multiple files to terser, which it will bundle together.
4242If you want to do this, you can pass a filegroup here.""" ,
43- allow_files = [".js" ],
43+ allow_files = [".js" , ".map" , ".mjs" ],
4444 mandatory = True ,
4545 ),
4646 "config_file" : attr .label (
@@ -95,6 +95,9 @@ so that it only affects the current build.
9595 ),
9696}
9797
98+ def _filter_js (files ):
99+ return [f for f in files if f .is_directory or f .extension == "js" or f .extension == "mjs" ]
100+
98101def _terser (ctx ):
99102 "Generate actions to create terser config run terser"
100103
@@ -103,17 +106,19 @@ def _terser(ctx):
103106 inputs = ctx .files .src [:]
104107 outputs = []
105108
106- directory_srcs = [s for s in ctx .files .src if s .is_directory ]
109+ sources = _filter_js (inputs )
110+ sourcemaps = [f for f in inputs if f .extension == "map" ]
111+ directory_srcs = [s for s in sources if s .is_directory ]
107112 if len (directory_srcs ) > 0 :
108- if len (ctx . files . src ) > 1 :
113+ if len (sources ) > 1 :
109114 fail ("When directories are passed to terser_minified, there should be only one input" )
110115 outputs .append (ctx .actions .declare_directory (ctx .label .name ))
111116 else :
112117 outputs .append (ctx .actions .declare_file ("%s.js" % ctx .label .name ))
113118 if ctx .attr .sourcemap :
114119 outputs .append (ctx .actions .declare_file ("%s.js.map" % ctx .label .name ))
115120
116- args .add_all ([s .path for s in ctx . files . src ])
121+ args .add_all ([s .path for s in sources ])
117122 args .add_all (["--output" , outputs [0 ].path ])
118123
119124 debug = ctx .attr .debug or "DEBUG" in ctx .var .keys ()
@@ -126,9 +131,12 @@ def _terser(ctx):
126131 # see https://github.com/terser-js/terser#command-line-usage
127132 source_map_opts = ["includeSources" , "base=" + ctx .bin_dir .path ]
128133
129- # We support only inline sourcemaps for now.
130- # It's hard to pair up the .js inputs with corresponding .map files
131- source_map_opts .append ("content=inline" )
134+ if len (sourcemaps ) == 0 :
135+ source_map_opts .append ("content=inline" )
136+ elif len (sourcemaps ) == 1 :
137+ source_map_opts .append ("content='%s'" % sourcemaps [0 ].path )
138+ else :
139+ fail ("When sourcemap is True, there should only be one or none input sourcemaps" )
132140
133141 # This option doesn't work in the config file, only on the CLI
134142 args .add_all (["--source-map" , "," .join (source_map_opts )])
0 commit comments