Skip to content

Commit

Permalink
Refactors CSS for prerender_component rewrite.
Browse files Browse the repository at this point in the history
This makes three changes:
1. Adds `CssInfo` to a couple required places.
2. Makes `merge_import_maps` public.
3. Updates `link_prerender_component` to use `css_group` to provide `CssInfo` and `CssImportMapInfo`.

All of these changes will be needed in the upcoming change to `prerender_component` which requires CSS providers to be more consistently passed around.
  • Loading branch information
dgp1130 committed Jul 22, 2023
1 parent aa0ee03 commit 3e66fa9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
29 changes: 22 additions & 7 deletions packages/rules_prerender/css/css_group.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Defines `css_group()` to group multiple `css_library()` targets like `filegroup()`."""

load(":css_providers.bzl", "CssImportMapInfo")
load(":css_providers.bzl", "CssImportMapInfo", "CssInfo")

visibility(["//packages/rules_prerender/..."])

Expand All @@ -10,22 +10,37 @@ def _css_group_impl(ctx):
files = depset([], transitive = [dep[DefaultInfo].files
for dep in ctx.attr.deps]),
),
CssInfo(
direct_sources = depset([]),
transitive_sources = depset(
direct = [],
transitive = [dep[CssInfo].transitive_sources
for dep in ctx.attr.deps
if CssInfo in dep],
),
),
CssImportMapInfo(
import_map = _merge_import_maps([dep[CssImportMapInfo]
for dep in ctx.attr.deps
if CssImportMapInfo in dep]),
import_map = merge_import_maps([dep[CssImportMapInfo]
for dep in ctx.attr.deps
if CssImportMapInfo in dep]),
),
]

css_group = rule(
implementation = _css_group_impl,
attrs = {
"deps": attr.label_list(mandatory = True),
"deps": attr.label_list(
mandatory = True,
providers = [CssInfo, CssImportMapInfo],
),
},
doc = "Like a `filegroup()`, but for `css_library()` targets.",
doc = """
Merges `CssInfo` and `CssImportMapInfo` providers from multiple targets.
Like a `filegroup()`, but for `css_library()` targets.
""",
)

def _merge_import_maps(css_import_maps):
def merge_import_maps(css_import_maps):
"""Merges a list of `CssImportMapInfo` into a single `CssImportMapInfo`.
Fails the build if the same import path appears as a key in two maps.
Expand Down
5 changes: 3 additions & 2 deletions packages/rules_prerender/link_prerender_component.bzl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("@aspect_rules_js//js:providers.bzl", "JsInfo")
load("//packages/rules_prerender/css:css_group.bzl", "css_group")
load("//tools/typescript:defs.bzl", "ts_project")
load("//packages/rules_prerender:web_resources.bzl", "WebResourceInfo")

Expand Down Expand Up @@ -57,11 +58,11 @@ def link_prerender_component(name, package, visibility = None, testonly = None):
testonly = testonly,
)

native.filegroup(
css_group(
name = "%s_styles" % name,
srcs = [], # Empty.
visibility = visibility,
testonly = testonly,
deps = [], # Empty for now, styles aren't supported.
)

_empty_web_resources(
Expand Down
4 changes: 4 additions & 0 deletions tools/binaries/css_bundler/css_bundle.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ def _css_bundle_impl(ctx):

return [
DefaultInfo(files = depset(outputs)),
CssInfo(
direct_sources = depset(outputs),
transitive_sources = depset(outputs),
),
CssImportMapInfo(import_map = _make_import_map(ctx, sources, outputs)),
]

Expand Down

0 comments on commit 3e66fa9

Please sign in to comment.