|
1 | 1 | """Defines functionality to generate entry points for JS and CSS.""" |
2 | 2 |
|
| 3 | +load("@aspect_bazel_lib//lib:paths.bzl", "to_output_relative_path") |
3 | 4 | load("@aspect_rules_js//js:defs.bzl", "js_run_binary") |
4 | 5 | load("//common:label.bzl", "absolute", "file_path_of") |
5 | 6 |
|
6 | 7 | visibility(["//"]) |
7 | 8 |
|
8 | | -def script_entry_point( |
9 | | - name, |
10 | | - metadata, |
11 | | - output_entry_point, |
12 | | - testonly = None, |
13 | | - visibility = None, |
14 | | -): |
15 | | - """Generates an entry point for scripts with the given metadata. |
16 | | - |
17 | | - Args: |
18 | | - name: Name of this target. |
19 | | - metadata: A metadata JSON file containing information about the scripts |
20 | | - to bundle. |
21 | | - output_entry_point: Location to write the generated script entry point. |
22 | | - testonly: https://bazel.build/reference/be/common-definitions#common-attributes |
23 | | - visibility: https://bazel.build/reference/be/common-definitions#common-attributes |
24 | | - """ |
25 | | - package_depth = len(native.package_name().split("/")) if native.package_name() != "" else 0 |
26 | | - js_run_binary( |
27 | | - name = name, |
28 | | - mnemonic = "GenerateScriptEntryPoint", |
29 | | - progress_message = "Generating script entry point %{label}", |
30 | | - srcs = [metadata], |
31 | | - outs = [output_entry_point], |
32 | | - tool = Label("//tools/binaries/script_entry_generator"), |
33 | | - args = [ |
34 | | - "--metadata", file_path_of(absolute(metadata)), |
35 | | - "--import-depth", str(package_depth), |
36 | | - "--output", file_path_of(absolute(output_entry_point)), |
37 | | - ], |
38 | | - testonly = testonly, |
39 | | - visibility = visibility, |
| 9 | +def _script_entry_points_impl(ctx): |
| 10 | + output = ctx.actions.declare_directory(ctx.label.name) |
| 11 | + |
| 12 | + args = ctx.actions.args() |
| 13 | + args.add("--metadata", to_output_relative_path(ctx.file.metadata)) |
| 14 | + args.add("--output-dir", to_output_relative_path(output)) |
| 15 | + |
| 16 | + ctx.actions.run( |
| 17 | + mnemonic = "GenerateScriptEntryPoints", |
| 18 | + progress_message = "Generating script entry points %{label}", |
| 19 | + executable = ctx.executable._script_entry_generator, |
| 20 | + arguments = [args], |
| 21 | + inputs = [ctx.file.metadata], |
| 22 | + outputs = [output], |
| 23 | + env = {"BAZEL_BINDIR": ctx.bin_dir.path}, |
40 | 24 | ) |
| 25 | + |
| 26 | + return DefaultInfo(files = depset([output])) |
| 27 | + |
| 28 | +script_entry_points = rule( |
| 29 | + implementation = _script_entry_points_impl, |
| 30 | + attrs = { |
| 31 | + "metadata": attr.label( |
| 32 | + mandatory = True, |
| 33 | + allow_single_file = True, |
| 34 | + doc = """ |
| 35 | + A metadata JSON file containing information about the scripts |
| 36 | + to bundle. |
| 37 | + """ |
| 38 | + ), |
| 39 | + "_script_entry_generator": attr.label( |
| 40 | + default = "//tools/binaries/script_entry_generator", |
| 41 | + executable = True, |
| 42 | + cfg = "exec", |
| 43 | + ), |
| 44 | + }, |
| 45 | + doc = """ |
| 46 | + Generates a `TreeArtifact` at `%{name}` with a `.js` file generated for |
| 47 | + every HTML file with included scripts as defined in the given `metadata` |
| 48 | + file. For example, if the user generates the following HTML files and |
| 49 | + includes the associated JavaScript files: |
| 50 | +
|
| 51 | + ``` |
| 52 | + * /index.html - [ path/to/pkg/foo.js, path/to/pkg/baz.js ] |
| 53 | + * /dir/other.html - [ path/to/pkg/bar.js, path/to/pkg/baz.js ] |
| 54 | + ``` |
| 55 | +
|
| 56 | + Then `script_entry_points` will generate a `TreeArtifact` with: |
| 57 | +
|
| 58 | + ``` |
| 59 | + * /index.js - `import './path/to/pkg/foo.js'; import './path/to/pkg/baz.js';` |
| 60 | + * /dir/other.js - `import './path/to/pkg/bar.js'; import './path/to/pkg/baz.js';` |
| 61 | + ``` |
| 62 | +
|
| 63 | + These can serve as entry points for the JavaScript bundles necessary for |
| 64 | + each HTML page. |
| 65 | + """, |
| 66 | +) |
0 commit comments