Skip to content

Commit

Permalink
Links rules_prerender so it is available to @aspect_rules_js rules.
Browse files Browse the repository at this point in the history
Refs #48.

This exposes `//:node_modules/rules_prerender` which can be used like any other NPM package yet is actually built from `//packages/rules_prerender` at HEAD.

This _should_ be defined in `//packages/rules_prerender/...` but can't because it depends on files outside that Bazel package like `//common/...` which can't be pulled into the NPM package. Easiest solution is to move the `npm_package()` target to the root BUILD file, so _everything_ is a subpackage. However this does result in a weird file layout in the NPM package and clutters the BUILD file. Once the repository is reorganized to use internal NPM packages for common code, this can likely be moved to `//packages/rules_prerender/...`. Left a TODO to follow up with that.
  • Loading branch information
dgp1130 committed Jul 20, 2022
1 parent 9c7fada commit 30efb5b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
23 changes: 23 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
load("@aspect_rules_js//npm:defs.bzl", "npm_link_package", "npm_package")
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("@bazel_skylib//lib:dicts.bzl", "dicts")
load("@bazel_skylib//rules:build_test.bzl", "build_test")
load("@build_bazel_rules_nodejs//:index.bzl", "pkg_npm")
load("@npm//@bazel/typescript:index.bzl", "ts_config")
load("@npm_rules_js//:defs.bzl", "npm_link_all_packages")
load("//tools:publish.bzl", "publish_files")
load("//tools:transitive_sources.bzl", "transitive_sources")
load("//:index.bzl", "prerender_component", "prerender_component_publish_files")

exports_files(["tsconfig.json"], visibility = ["//visibility:public"])
Expand All @@ -18,6 +20,27 @@ ts_config(

npm_link_all_packages(name = "node_modules")

# Makes an internal NPM package for `rules_prerender` and links it.
# TODO(#48): Move to `//packages/rules_prerender` once dependencies outside
# that package are addressed.
npm_package(
name = "rules_prerender_pkg",
srcs = [
":rules_prerender_sources",
":package.json",
],
package = "rules_prerender",
)
transitive_sources(
name = "rules_prerender_sources",
dep = "//packages/rules_prerender",
)
npm_link_package(
name = "node_modules/rules_prerender",
src = ":rules_prerender_pkg",
visibility = ["//visibility:public"],
)

bzl_library(
name = "rules_prerender",
srcs = [
Expand Down
20 changes: 20 additions & 0 deletions tools/transitive_sources.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""Defines `transitive_sources()`."""

load("@build_bazel_rules_nodejs//:providers.bzl", "JSModuleInfo")

def _transitive_sources_impl(ctx):
return DefaultInfo(files = ctx.attr.dep[JSModuleInfo].sources)

transitive_sources = rule(
implementation = _transitive_sources_impl,
attrs = {
"dep": attr.label(
mandatory = True,
providers = [JSModuleInfo],
),
},
doc = """
Collects all the transitive JS sources of the given dependency
and returns them in `DefaultInfo`.
""",
)

0 comments on commit 30efb5b

Please sign in to comment.