-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replaces
@bazel/postcss
usage with lightningcss
.
Refs #46. This adds a new `css_bundle()` target which calls the `css_bundler` binary (using `lightningcss`) to bundle all the given CSS files. This rule is then swapped in for `postcss_multi_binary()` for use across the whole project. This necessitated updating `css_library()` to copy all its sources to the bin directory to be compatible with the `css_bundler` `js_binary()` target downstream. I would have liked to land this in a separate commit, but it actually breaks the `@bazel/postcss` pipeline since that relies on the bundled file having the same name as its entry point. Partially for this reason, I ended up changing the output file paths of the bundled CSS files. Instead of generating the bundled CSS to use the same basename, I rerooted the whole execroot under the target. So if you bundle a file at `@some_wksp//path/to:file.css` at `@some_other_wksp//path/to/pkg:bundle` it will output the bundled version at: ``` .../execroot/some_other_wksp/path/to/pkg/bundle/some_wksp/path/to/file.css ``` Including the workspacename and putting everything under the bundle target means every input file as a 1:1 correlation with its output file, and there is no potential ambiguity or conflict.
- Loading branch information
Showing
21 changed files
with
161 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
load("@aspect_bazel_lib//lib:paths.bzl", "to_output_relative_path") | ||
load("//packages/rules_prerender/css:css_providers.bzl", "CssInfo") | ||
|
||
def _css_bundle_impl(ctx): | ||
# Map each direct dependency source file to an output of the same full file path | ||
# under this target's name. Ideally we would just keep the name the same, but then | ||
# it would conflict with the source file (since that needs to be copied to bin to | ||
# be usable anyways). So instead we "namespace" each file under the target name | ||
# and include the full execroot-relative path to file to be truly unambiguous. | ||
# The output file structure will look like: | ||
# path/to/pkg/css_bundle_target/file_wksp_name/path/to/file.css | ||
sources = ctx.attr.dep[CssInfo].direct_sources | ||
outputs = [ctx.actions.declare_file("%s/%s" % ( | ||
ctx.label.name, | ||
_workspace_short_path(src, ctx.label.workspace_name or ctx.workspace_name), | ||
)) for src in sources] | ||
|
||
# Map each entry point to its associated output. | ||
args = ctx.actions.args() | ||
for (source, output) in zip(sources, outputs): | ||
args.add("--entry-point", "./%s" % to_output_relative_path(source)) | ||
args.add("--output", to_output_relative_path(output)) | ||
|
||
ctx.actions.run( | ||
mnemonic = "BundleCss", | ||
progress_message = "Bundling styles %{label}", | ||
executable = ctx.executable._bundler, | ||
arguments = [args], | ||
inputs = ctx.attr.dep[CssInfo].transitive_sources, | ||
outputs = outputs, | ||
env = { | ||
"BAZEL_BINDIR": ctx.bin_dir.path, | ||
}, | ||
) | ||
|
||
return DefaultInfo(files = depset(outputs)) | ||
|
||
css_bundle = rule( | ||
implementation = _css_bundle_impl, | ||
attrs = { | ||
"dep": attr.label( | ||
mandatory = True, | ||
providers = [CssInfo], | ||
doc = "The `css_library()` target to bundle all its direct sources.", | ||
), | ||
"_bundler": attr.label( | ||
default = "//packages/css_bundler", | ||
executable = True, | ||
cfg = "exec", | ||
), | ||
}, | ||
doc = """ | ||
Bundles the direct sources of the given dependency by treating each file as its | ||
own entry point and resolving `@import` statements to inline the dependencies. | ||
""", | ||
) | ||
|
||
def _workspace_short_path(file, current_wksp): | ||
if file.short_path.startswith("../"): | ||
# File is in an external workspace with path: `../external_wksp/path/to/file`. | ||
# Just drop the `../` and we have the right path. | ||
return file.short_path[len("../"):] | ||
else: | ||
# File is in the main workspace. Use the current workspace name. | ||
return "%s/%s" % (current_wksp, file.short_path) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
load("@aspect_bazel_lib//lib:diff_test.bzl", "diff_test") | ||
load("//packages/css_bundler:css_bundle.bzl", "css_bundle") | ||
load("//packages/rules_prerender/css:css_library.bzl", "css_library") | ||
|
||
css_bundle( | ||
name = "bundle", | ||
dep = ":lib", | ||
) | ||
|
||
css_library( | ||
name = "lib", | ||
srcs = ["lib.css"], | ||
deps = [":transitive"], | ||
) | ||
|
||
css_library( | ||
name = "transitive", | ||
srcs = ["transitive.css"], | ||
) | ||
|
||
diff_test( | ||
name = "test", | ||
file1 = ":bundle", | ||
file2 = "expected.css", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
.transitive { | ||
color: green; | ||
} | ||
|
||
.lib { | ||
color: red; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
@import './transitive.css'; | ||
|
||
.lib { | ||
color: red; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.transitive { | ||
color: green; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
.baz { | ||
color: blue; | ||
color: #00f; | ||
} | ||
|
||
.bar { | ||
color: green; | ||
color: green; | ||
} | ||
|
||
.foo { | ||
color: red; | ||
color: red; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 0 additions & 13 deletions
13
packages/rules_prerender/css/tests/group/subpackage/BUILD.bazel
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.