@@ -12,6 +12,7 @@ swc(name = "compile")
1212"""
1313
1414load ("//swc/private:swc.bzl" , _swc_lib = "swc" )
15+ load ("//swc/private:swc_plugin.bzl" , _swc_plugin_lib = "swc_plugin" )
1516load ("@aspect_bazel_lib//lib:utils.bzl" , "file_exists" , "to_label" )
1617load ("@bazel_skylib//lib:types.bzl" , "types" )
1718load ("@bazel_skylib//rules:write_file.bzl" , "write_file" )
@@ -30,7 +31,7 @@ for example to set your own output labels for `js_outs`.
3031 toolchains = _swc_lib .toolchains ,
3132)
3233
33- def swc (name , srcs = None , args = [], data = [], output_dir = False , swcrc = None , source_maps = False , out_dir = None , root_dir = None , ** kwargs ):
34+ def swc (name , srcs = None , args = [], data = [], plugins = [], output_dir = False , swcrc = None , source_maps = False , out_dir = None , root_dir = None , ** kwargs ):
3435 """Execute the SWC compiler
3536
3637 Args:
@@ -59,6 +60,8 @@ def swc(name, srcs = None, args = [], data = [], output_dir = False, swcrc = Non
5960 these don't accidentally diverge.
6061 See an example in `examples/paths/BUILD.bazel`.
6162
63+ plugins: Array of plugin labels created with `swc_plugin`.
64+
6265 out_dir: The base directory for output files relative to the output directory for this package
6366
6467 root_dir: A subdirectory under the input package which should be considered the root directory of all the input files
@@ -75,6 +78,10 @@ def swc(name, srcs = None, args = [], data = [], output_dir = False, swcrc = Non
7578 swcrc = ".swcrc"
7679 elif type (swcrc ) == type (dict ()):
7780 swcrc .setdefault ("sourceMaps" , source_maps )
81+ if plugins :
82+ swcrc .setdefault ("jsc" , {})
83+ swcrc ["jsc" ].setdefault ("experimental" , {})
84+ swcrc ["jsc" ]["experimental" ]["plugins" ] = ["__PLUGINS__" ]
7885 rcfile = "{}_swcrc.json" .format (name )
7986 write_file (
8087 name = "_gen_swcrc_" + name ,
@@ -103,6 +110,7 @@ def swc(name, srcs = None, args = [], data = [], output_dir = False, swcrc = Non
103110 swc_compile (
104111 name = name ,
105112 srcs = srcs ,
113+ plugins = plugins ,
106114 js_outs = js_outs ,
107115 map_outs = map_outs ,
108116 output_dir = output_dir ,
@@ -114,3 +122,33 @@ def swc(name, srcs = None, args = [], data = [], output_dir = False, swcrc = Non
114122 root_dir = root_dir ,
115123 ** kwargs
116124 )
125+
126+ _swc_plugin = rule (
127+ doc = "Configure an SWC plugin" ,
128+ implementation = _swc_plugin_lib .implementation ,
129+ attrs = _swc_plugin_lib .attrs ,
130+ provides = _swc_plugin_lib .provides ,
131+ )
132+
133+ def swc_plugin (name , src = None , config = None , ** kwargs ):
134+ """Configure an SWC plugin
135+
136+ Args:
137+ name: A name for this target
138+
139+ src: Label for the plugin, either a directory containing a package.json pointing at a wasm file
140+ as the main entrypoint, or a wasm file.
141+
142+ config: Configuration for the plugin, either a dict or a string containing a serialized JSON object.
143+
144+ **kwargs: additional keyword arguments passed through to underlying rule, eg. `visibility`, `tags`
145+ """
146+ if type (config ) == type (dict ()):
147+ config = json .encode (config )
148+
149+ _swc_plugin (
150+ name = name ,
151+ src = src ,
152+ config = config ,
153+ ** kwargs
154+ )
0 commit comments