Skip to content

Commit c383661

Browse files
committed
Add support for plugins
1 parent e2fb7e3 commit c383661

File tree

14 files changed

+248
-7
lines changed

14 files changed

+248
-7
lines changed

examples/plugins/BUILD.bazel

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
load("@aspect_rules_swc//swc:defs.bzl", "swc", "swc_plugin")
2+
3+
swc_plugin(
4+
name = "npm_plugin",
5+
src = "//examples/plugins/plugin-transform-imports:package",
6+
config = {
7+
"lodash": {
8+
"transform": "lodash/{{member}}",
9+
},
10+
},
11+
)
12+
13+
swc_plugin(
14+
name = "file_plugin",
15+
src = "//examples/plugins/plugin-transform-imports:swc_plugin_transform_imports.wasm",
16+
config = json.encode({
17+
"lodash2": {
18+
"transform": "lodash/test/{{member}}",
19+
},
20+
}),
21+
)
22+
23+
swc(
24+
name = "simple",
25+
out_dir = "simple",
26+
plugins = [
27+
":npm_plugin",
28+
":file_plugin",
29+
],
30+
)
31+
32+
swc(
33+
name = "rcdict",
34+
out_dir = "rcdict",
35+
plugins = [
36+
":npm_plugin",
37+
":file_plugin",
38+
],
39+
swcrc = {
40+
"jsc": {
41+
"target": "es5",
42+
},
43+
},
44+
)
45+
46+
swc(
47+
name = "rc",
48+
out_dir = "rc",
49+
plugins = [
50+
":npm_plugin",
51+
":file_plugin",
52+
],
53+
swcrc = "minify.swcrc",
54+
)

examples/plugins/in.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import { map } from "lodash";
2+
import { sort } from "lodash2";

examples/plugins/minify.swcrc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"jsc": {
3+
"minify": true,
4+
"experimental": {
5+
"plugins": [
6+
"__PLUGINS__"
7+
]
8+
}
9+
}
10+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
load("@aspect_rules_js//npm:defs.bzl", "npm_package")
2+
3+
exports_files(
4+
["swc_plugin_transform_imports.wasm"],
5+
visibility = ["//examples/plugins:__pkg__"],
6+
)
7+
8+
npm_package(
9+
name = "package",
10+
srcs = [
11+
"package.json",
12+
"swc_plugin_transform_imports.wasm",
13+
],
14+
visibility = ["//examples/plugins:__pkg__"],
15+
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "@swc/plugin-transform-imports",
3+
"version": "1.5.35",
4+
"index": "swc_plugin_transform_imports.wasm"
5+
}
Binary file not shown.

swc/BUILD.bazel

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ bzl_library(
4141
visibility = ["//visibility:public"],
4242
deps = [
4343
"//swc/private:swc",
44+
"//swc/private:swc_plugin",
4445
"@bazel_skylib//lib:types",
4546
"@bazel_skylib//rules:write_file",
4647
],
@@ -51,3 +52,12 @@ bzl_library(
5152
srcs = ["toolchain.bzl"],
5253
visibility = ["//visibility:public"],
5354
)
55+
56+
bzl_library(
57+
name = "providers",
58+
srcs = ["providers.bzl"],
59+
visibility = ["//visibility:public"],
60+
deps = [
61+
"//swc/private:swc_plugin_config_info",
62+
],
63+
)

swc/defs.bzl

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ swc(name = "compile")
1212
"""
1313

1414
load("//swc/private:swc.bzl", _swc_lib = "swc")
15+
load("//swc/private:swc_plugin.bzl", _swc_plugin_lib = "swc_plugin")
1516
load("@aspect_bazel_lib//lib:utils.bzl", "file_exists", "to_label")
1617
load("@bazel_skylib//lib:types.bzl", "types")
1718
load("@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+
)

swc/private/BUILD.bazel

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
22

3+
exports_files(["plugins.swcrc"])
4+
35
bzl_library(
46
name = "toolchains_repo",
57
srcs = ["toolchains_repo.bzl"],
@@ -12,11 +14,27 @@ bzl_library(
1214
visibility = ["//swc:__subpackages__"],
1315
)
1416

17+
bzl_library(
18+
name = "swc_plugin_config_info",
19+
srcs = ["swc_plugin_config_info.bzl"],
20+
visibility = ["//swc:__subpackages__"],
21+
)
22+
23+
bzl_library(
24+
name = "swc_plugin",
25+
srcs = ["swc_plugin.bzl"],
26+
visibility = ["//swc:__subpackages__"],
27+
deps = [
28+
":swc_plugin_config_info",
29+
],
30+
)
31+
1532
bzl_library(
1633
name = "swc",
1734
srcs = ["swc.bzl"],
1835
visibility = ["//swc:__subpackages__"],
1936
deps = [
37+
":swc_plugin_config_info",
2038
"@aspect_bazel_lib//lib:platform_utils",
2139
"@aspect_rules_js//js:libs",
2240
"@aspect_rules_js//js:providers",

swc/private/plugins.swcrc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"jsc": {
3+
"experimental": {
4+
"plugins": [
5+
"__PLUGINS__"
6+
]
7+
}
8+
}
9+
}

0 commit comments

Comments
 (0)