Skip to content

Commit

Permalink
feat: upgrade to rules_js 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
gregmagolan committed May 10, 2024
1 parent 05a7ce6 commit 3c44095
Show file tree
Hide file tree
Showing 13 changed files with 63 additions and 69 deletions.
6 changes: 0 additions & 6 deletions .bazelversion
Original file line number Diff line number Diff line change
@@ -1,7 +1 @@
7.1.1
# The first line of this file is used by Bazelisk and Bazel to be sure
# the right version of Bazel is used to build and test this repo.
# This also defines which version is used on CI.
#
# Note that you should also run integration_tests against other Bazel
# versions you support.
6 changes: 2 additions & 4 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
load("@aspect_bazel_lib//lib:utils.bzl", bazel_lib_utils = "utils")
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("@buildifier_prebuilt//:rules.bzl", "buildifier")

# buildifier: disable=bzl-visibility
load("//swc/private:is_bazel_7_or_greater.bzl", "is_bazel_7_or_greater")

buildifier(
name = "buildifier",
exclude_patterns = ["./.git/*"],
Expand All @@ -27,5 +25,5 @@ bzl_library(
deps = [
"@bazel_tools//tools/build_defs/repo:http.bzl",
"@bazel_tools//tools/build_defs/repo:utils.bzl",
] + (["@bazel_tools//tools/build_defs/repo:cache.bzl"] if is_bazel_7_or_greater() else []),
] + (["@bazel_tools//tools/build_defs/repo:cache.bzl"] if bazel_lib_utils.is_bazel_7_or_greater() else []),
)
6 changes: 3 additions & 3 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ module(
)

# Lower-bounds (minimum) versions for direct runtime dependencies
bazel_dep(name = "aspect_bazel_lib", version = "1.31.2")
bazel_dep(name = "aspect_rules_js", version = "1.31.0") # Note: only used for provider symbols, we don't spawn nodejs actions
bazel_dep(name = "bazel_skylib", version = "1.4.1")
bazel_dep(name = "aspect_bazel_lib", version = "2.7.3")
bazel_dep(name = "aspect_rules_js", version = "2.0.0-alpha.4") # Note: only used for provider symbols, we don't spawn nodejs actions
bazel_dep(name = "bazel_skylib", version = "1.5.0")
bazel_dep(name = "platforms", version = "0.0.7")

swc = use_extension("@aspect_rules_swc//swc:extensions.bzl", "swc")
Expand Down
19 changes: 3 additions & 16 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,11 @@ swc_register_toolchains(
swc_version = LATEST_SWC_VERSION,
)

load("@aspect_bazel_lib//lib:repositories.bzl", "aspect_bazel_lib_dependencies", "register_jq_toolchains")
load("@aspect_rules_js//js:toolchains.bzl", "DEFAULT_NODE_VERSION", "rules_js_register_toolchains")

aspect_bazel_lib_dependencies(override_local_config_platform = True)
rules_js_register_toolchains(node_version = DEFAULT_NODE_VERSION)

register_jq_toolchains()

load("@aspect_rules_js//js:repositories.bzl", "rules_js_dependencies")

rules_js_dependencies()

load("@rules_nodejs//nodejs:repositories.bzl", "DEFAULT_NODE_VERSION", "nodejs_register_toolchains")

nodejs_register_toolchains(
name = "nodejs",
node_version = DEFAULT_NODE_VERSION,
)

load("@aspect_rules_js//npm:npm_import.bzl", "npm_translate_lock")
load("@aspect_rules_js//npm:repositories.bzl", "npm_translate_lock")

npm_translate_lock(
name = "npm",
Expand Down
6 changes: 3 additions & 3 deletions docs/swc.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion e2e/smoke/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ local_path_override(
path = "../..",
)

bazel_dep(name = "bazel_skylib", version = "1.4.1", dev_dependency = True)
bazel_dep(name = "bazel_skylib", version = "1.5.0", dev_dependency = True)

# Optional: specify a custom swc toolchain instead of the default
swc = use_extension("@aspect_rules_swc//swc:extensions.bzl", "swc", dev_dependency = True)
Expand Down
6 changes: 5 additions & 1 deletion examples/plugins/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ npm_link_all_packages(name = "node_modules")
# This requires that the package.json includes a main entrypoint pointing at the plugin wasm file.
swc_plugin(
name = "npm_plugin",
src = ":node_modules/@swc/plugin-transform-imports",
srcs = [
# reference the location where the "@swc/plugin-transform-imports" npm package was linked in our root Bazel package.
":node_modules/@swc/plugin-transform-imports",
":node_modules/@swc/plugin-transform-imports/dir",
],
# optional plugin config, the JSON object for the plugin passed into jsc.experimental.plugins
# https://swc.rs/docs/configuration/compilation#jscexperimentalplugins
config = {
Expand Down
6 changes: 3 additions & 3 deletions swc/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("@aspect_bazel_lib//lib:utils.bzl", bazel_lib_utils = "utils")
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("//swc/private:is_bazel_7_or_greater.bzl", "is_bazel_7_or_greater")
load("//swc/private:resolved_toolchain.bzl", "resolved_toolchain")

# For stardoc to reference the files
Expand Down Expand Up @@ -42,7 +42,7 @@ bzl_library(
deps = [
"@bazel_tools//tools/build_defs/repo:http.bzl",
"@bazel_tools//tools/build_defs/repo:utils.bzl",
] + (["@bazel_tools//tools/build_defs/repo:cache.bzl"] if is_bazel_7_or_greater() else []),
] + (["@bazel_tools//tools/build_defs/repo:cache.bzl"] if bazel_lib_utils.is_bazel_7_or_greater() else []),
)

bzl_library(
Expand All @@ -54,7 +54,7 @@ bzl_library(
"//swc/private:swc_plugin",
"@bazel_skylib//lib:types",
"@bazel_skylib//rules:write_file",
] + (["@bazel_tools//tools/build_defs/repo:cache.bzl"] if is_bazel_7_or_greater() else []),
] + (["@bazel_tools//tools/build_defs/repo:cache.bzl"] if bazel_lib_utils.is_bazel_7_or_greater() else []),
)

bzl_library(
Expand Down
11 changes: 8 additions & 3 deletions swc/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,13 @@ _swc_plugin = rule(
provides = _swc_plugin_lib.provides,
)

def swc_plugin(name, src = None, config = {}, **kwargs):
def swc_plugin(name, srcs = [], config = {}, **kwargs):
"""Configure an SWC plugin
Args:
name: A name for this target
src: Label for the plugin, either a directory containing a package.json pointing at a wasm file
srcs: Plugin files. Either a directory containing a package.json pointing at a wasm file
as the main entrypoint, or a wasm file. Usually a linked npm package target via rules_js.
config: Optional configuration dict for the plugin. This is passed as a JSON object into the
Expand All @@ -146,9 +146,14 @@ def swc_plugin(name, src = None, config = {}, **kwargs):
if not types.is_dict(config):
fail("config must be a dict, not a " + type(config))

# For backward compat
src = kwargs.pop("src", None)
if src:
srcs = srcs[:] + [src]

_swc_plugin(
name = name,
src = src,
srcs = srcs,
config = json.encode(config),
**kwargs
)
23 changes: 15 additions & 8 deletions swc/dependencies.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,27 @@ def http_archive(**kwargs):
def rules_swc_dependencies():
http_archive(
name = "bazel_skylib",
sha256 = "b8a1527901774180afc798aeb28c4634bdccf19c4d98e7bdd1ce79d1fe9aaad7",
urls = ["https://github.com/bazelbuild/bazel-skylib/releases/download/1.4.1/bazel-skylib-1.4.1.tar.gz"],
sha256 = "cd55a062e763b9349921f0f5db8c3933288dc8ba4f76dd9416aac68acee3cb94",
urls = ["https://github.com/bazelbuild/bazel-skylib/releases/download/1.5.0/bazel-skylib-1.5.0.tar.gz"],
)

http_archive(
name = "aspect_bazel_lib",
sha256 = "0da75299c5a52737b2ac39458398b3f256e41a1a6748e5457ceb3a6225269485",
strip_prefix = "bazel-lib-1.31.2",
url = "https://github.com/aspect-build/bazel-lib/releases/download/v1.31.2/bazel-lib-v1.31.2.tar.gz",
sha256 = "87ab4ec479ebeb00d286266aca2068caeef1bb0b1765e8f71c7b6cfee6af4226",
strip_prefix = "bazel-lib-2.7.3",
url = "https://github.com/aspect-build/bazel-lib/releases/download/v2.7.3/bazel-lib-v2.7.3.tar.gz",
)

http_archive(
name = "aspect_rules_js",
sha256 = "7b2a4d1d264e105eae49a27e2e78065b23e2e45724df2251eacdd317e95bfdfd",
strip_prefix = "rules_js-1.31.0",
url = "https://github.com/aspect-build/rules_js/releases/download/v1.31.0/rules_js-v1.31.0.tar.gz",
sha256 = "b2669ce908107570e5f552dbf9f133fd6c81e7a90c6083d8a1e03620657980d1",
strip_prefix = "rules_js-2.0.0-alpha.4",
url = "https://github.com/aspect-build/rules_js/releases/download/v2.0.0-alpha.4/rules_js-v2.0.0-alpha.4.tar.gz",
)

http_archive(
name = "rules_nodejs",
sha256 = "dddd60acc3f2f30359bef502c9d788f67e33814b0ddd99aa27c5a15eb7a41b8c",
strip_prefix = "rules_nodejs-6.1.0",
url = "https://github.com/bazelbuild/rules_nodejs/releases/download/v6.1.0/rules_nodejs-v6.1.0.tar.gz",
)
7 changes: 0 additions & 7 deletions swc/private/is_bazel_7_or_greater.bzl

This file was deleted.

30 changes: 18 additions & 12 deletions swc/private/swc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,14 @@ _attrs = {
If out_dir is also specified, it is used as the name of the output directory.
Otherwise, the directory is named the same as the target.""",
),
"data": js_lib_helpers.JS_LIBRARY_DATA_ATTR,
"data": attr.label_list(
doc = """Runtime dependencies to include in binaries/tests that depend on this target.
Follows the same semantics as `js_library` `data` attribute. See
https://docs.aspect.build/rulesets/aspect_rules_js/docs/js_library#data for more info.
""",
allow_files = True,
),
"swcrc": attr.label(
doc = "label of a configuration file for swc, see https://swc.rs/docs/configuration/swcrc",
allow_single_file = True,
Expand Down Expand Up @@ -326,18 +333,18 @@ def _swc_impl(ctx):
targets = ctx.attr.srcs,
)

transitive_declarations = js_lib_helpers.gather_transitive_declarations(
declarations = [],
transitive_types = js_lib_helpers.gather_transitive_types(
types = [],
targets = ctx.attr.srcs,
)

npm_linked_packages = js_lib_helpers.gather_npm_linked_packages(
npm_sources = js_lib_helpers.gather_npm_sources(
srcs = ctx.attr.srcs,
deps = [],
)

npm_package_store_deps = js_lib_helpers.gather_npm_package_store_deps(
targets = ctx.attr.data,
npm_package_store_infos = js_lib_helpers.gather_npm_package_store_infos(
targets = ctx.attr.srcs + ctx.attr.data,
)

runfiles = js_lib_helpers.gather_runfiles(
Expand All @@ -349,14 +356,13 @@ def _swc_impl(ctx):

return [
js_info(
npm_linked_package_files = npm_linked_packages.direct_files,
npm_linked_packages = npm_linked_packages.direct,
npm_package_store_deps = npm_package_store_deps,
target = ctx.label,
sources = output_sources_depset,
transitive_declarations = transitive_declarations,
transitive_npm_linked_package_files = npm_linked_packages.transitive_files,
transitive_npm_linked_packages = npm_linked_packages.transitive,
types = depset(), # swc does not emit types directly
transitive_sources = transitive_sources,
transitive_types = transitive_types,
npm_sources = npm_sources,
npm_package_store_infos = npm_package_store_infos,
),
DefaultInfo(
files = output_sources_depset,
Expand Down
4 changes: 2 additions & 2 deletions swc/private/swc_plugin.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
load("//swc:providers.bzl", "SwcPluginConfigInfo")

_attrs = {
"src": attr.label(
"srcs": attr.label_list(
doc = "label for the plugin, either a directory containing a package.json pointing at a wasm file as the main entrypoint, or a wasm file",
providers = [DefaultInfo],
mandatory = True,
Expand All @@ -18,7 +18,7 @@ _attrs = {
def _swc_plugin_impl(ctx):
return [
DefaultInfo(
files = ctx.attr.src[DefaultInfo].files,
files = depset(ctx.files.srcs),
),
SwcPluginConfigInfo(
label = ctx.label,
Expand Down

0 comments on commit 3c44095

Please sign in to comment.