diff --git a/WORKSPACE b/WORKSPACE index 23c896a..eb010a4 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -15,7 +15,7 @@ rules_swc_dependencies() load("//swc:repositories.bzl", "swc_register_toolchains") swc_register_toolchains( - name = "swc", + name = "default_swc", swc_version = "v1.2.119", ) diff --git a/examples/macro/ts_project.bzl b/examples/macro/ts_project.bzl index 000e848..865514c 100644 --- a/examples/macro/ts_project.bzl +++ b/examples/macro/ts_project.bzl @@ -23,9 +23,9 @@ def my_ts_project(name, srcs = []): $(execpath @aspect_rules_swc//swc:cli) \\ $(location in.ts) \\ -o $@""", - toolchains = ["@swc_toolchains//:resolved_toolchain"], + toolchains = ["@default_swc_toolchains//:resolved_toolchain"], tools = [ "@aspect_rules_swc//swc:cli", - "@swc_toolchains//:resolved_toolchain", + "@default_swc_toolchains//:resolved_toolchain", ], ) diff --git a/swc/BUILD.bazel b/swc/BUILD.bazel index 9ca4fd4..df8cbed 100644 --- a/swc/BUILD.bazel +++ b/swc/BUILD.bazel @@ -35,8 +35,12 @@ write_file( nodejs_binary( name = "cli", data = [ - "@npm__swc_cli-0.1.52", - "@npm__swc_core-1.2.119", + # Don't use the shorter syntax here because the user or bzlmod + # might use repository mapping to give a different name + # to the repository, e.g. + # @aspect_rules_swc.0.2.0.swc.npm__swc_core-1.2.119 + "@npm__swc_cli-0.1.52//:pkg", + "@npm__swc_core-1.2.119//:pkg", ], entry_point = "main.js", visibility = ["//visibility:public"], diff --git a/swc/dependencies.bzl b/swc/dependencies.bzl index f27394b..73ae9b8 100644 --- a/swc/dependencies.bzl +++ b/swc/dependencies.bzl @@ -41,7 +41,7 @@ def rules_swc_dependencies(): maybe( http_archive, name = "aspect_rules_js", - sha256 = "e8576a74a7e80b873179514cf1ad48b62b18ae024e74200ecd40ae6dc00c515a", - strip_prefix = "rules_js-0.3.0", - url = "https://github.com/aspect-build/rules_js/archive/v0.3.0.tar.gz", + sha256 = "dd78b4911b7c2e6c6e919b85cd31572cc15e5baa62b9e7354d8a1065c67136e3", + strip_prefix = "rules_js-0.3.1", + url = "https://github.com/aspect-build/rules_js/archive/v0.3.1.tar.gz", ) diff --git a/swc/extensions.bzl b/swc/extensions.bzl new file mode 100644 index 0000000..d6db15d --- /dev/null +++ b/swc/extensions.bzl @@ -0,0 +1,35 @@ +"extensions for bzlmod" + +load(":repositories.bzl", "swc_register_toolchains") + +swc_toolchain = tag_class(attrs = { + "name": attr.string(doc = "Base name for generated repositories"), + "swc_version": attr.string(doc = "Version of the swc rust binding"), +}) + +def _toolchain_extension(module_ctx): + registrations = {} + for mod in module_ctx.modules: + for toolchain in mod.tags.toolchain: + if toolchain.name in registrations.keys(): + if toolchain.swc_version == registrations[toolchain.name]: + # No problem to register a matching toolchain twice + continue + fail("Multiple conflicting toolchains declared for name {} ({} and {}".format( + toolchain.name, + toolchain.swc_version, + registrations[toolchain.name], + )) + else: + registrations[toolchain.name] = toolchain.swc_version + for name, swc_version in registrations.items(): + swc_register_toolchains( + name = name, + swc_version = swc_version, + register = False, + ) + +swc = module_extension( + implementation = _toolchain_extension, + tag_classes = {"toolchain": swc_toolchain}, +) diff --git a/swc/repositories.bzl b/swc/repositories.bzl index ab0c1cc..4e2acb8 100644 --- a/swc/repositories.bzl +++ b/swc/repositories.bzl @@ -41,7 +41,7 @@ swc_repositories = repository_rule( ) # Wrapper macro around everything above, this is the primary API -def swc_register_toolchains(name, **kwargs): +def swc_register_toolchains(name, register = True, **kwargs): """Convenience macro for users which does typical setup. - create a repository for each built-in platform like "swc_linux_amd64" - @@ -52,6 +52,8 @@ def swc_register_toolchains(name, **kwargs): Users can avoid this macro and do these steps themselves, if they want more control. Args: name: base name for all created repos, like "swc" + register: whether to call through to native.register_toolchains. + Should be True for WORKSPACE users, but false when used under bzlmod extension **kwargs: passed to each node_repositories call """ for platform in PLATFORMS.keys(): @@ -60,7 +62,8 @@ def swc_register_toolchains(name, **kwargs): platform = platform, **kwargs ) - native.register_toolchains("@%s_toolchains//:%s_toolchain" % (name, platform)) + if register: + native.register_toolchains("@%s_toolchains//:%s_toolchain" % (name, platform)) toolchains_repo( name = name + "_toolchains",