|
| 1 | +load("@build_bazel_rules_nodejs//internal/common:sources_aspect.bzl", "sources_aspect") |
| 2 | + |
| 3 | +"""Gets the workspace name of the given rule context.""" |
| 4 | + |
| 5 | +def _get_workspace_name(ctx): |
| 6 | + if ctx.label.workspace_root: |
| 7 | + # We need the workspace_name for the target being visited. |
| 8 | + # Starlark doesn't have this - instead they have a workspace_root |
| 9 | + # which looks like "external/repo_name" - so grab the second path segment. |
| 10 | + return ctx.label.workspace_root.split("/")[1] |
| 11 | + else: |
| 12 | + return ctx.workspace_name |
| 13 | + |
| 14 | +"""Implementation of the dev server rule.""" |
| 15 | + |
| 16 | +def _dev_server_rule_impl(ctx): |
| 17 | + files = depset(ctx.files.srcs) |
| 18 | + |
| 19 | + # List of files which are required for the devserver to run. This includes the |
| 20 | + # bazel runfile helpers (to resolve runfiles in bash) and the devserver binary |
| 21 | + # with its transitive runfiles (in order to be able to run the devserver). |
| 22 | + required_tools = ctx.files._bash_runfile_helpers + \ |
| 23 | + ctx.files._dev_server_bin + \ |
| 24 | + ctx.attr._dev_server_bin[DefaultInfo].files.to_list() + \ |
| 25 | + ctx.attr._dev_server_bin[DefaultInfo].data_runfiles.files.to_list() |
| 26 | + |
| 27 | + # Walk through all dependencies specified in the "deps" attribute. These labels need to be |
| 28 | + # unwrapped in case there are built using TypeScript-specific rules. This is because targets |
| 29 | + # built using "ts_library" or "ng_module" do not declare the generated JS files as default |
| 30 | + # rule output. The output aspect that is applied to the "deps" attribute, provides two struct |
| 31 | + # fields which resolve to the unwrapped JS output files. |
| 32 | + # https://github.com/bazelbuild/rules_nodejs/blob/e04c8c31f3cb859754ea5c5e97f331a3932b725d/internal/common/sources_aspect.bzl#L53-L55 |
| 33 | + for d in ctx.attr.deps: |
| 34 | + if hasattr(d, "node_sources"): |
| 35 | + files = depset(transitive = [files, d.node_sources]) |
| 36 | + elif hasattr(d, "files"): |
| 37 | + files = depset(transitive = [files, d.files]) |
| 38 | + if hasattr(d, "dev_scripts"): |
| 39 | + files = depset(transitive = [files, d.dev_scripts]) |
| 40 | + |
| 41 | + workspace_name = _get_workspace_name(ctx) |
| 42 | + root_paths = ["", "/".join([workspace_name, ctx.label.package])] + ctx.attr.additional_root_paths |
| 43 | + |
| 44 | + # We can't use "ctx.actions.args()" because there is no way to convert the args object |
| 45 | + # into a string representing the command line arguments. It looks like bazel has some |
| 46 | + # internal logic to compute the string representation of "ctx.actions.args()". |
| 47 | + args = '--root_paths="%s" ' % ",".join(root_paths) |
| 48 | + args += "--port=%s " % ctx.attr.port |
| 49 | + |
| 50 | + if ctx.attr.historyApiFallback: |
| 51 | + args += "--historyApiFallback " |
| 52 | + |
| 53 | + ctx.actions.expand_template( |
| 54 | + template = ctx.file._launcher_template, |
| 55 | + output = ctx.outputs.launcher, |
| 56 | + substitutions = { |
| 57 | + "TEMPLATED_args": args, |
| 58 | + }, |
| 59 | + is_executable = True, |
| 60 | + ) |
| 61 | + |
| 62 | + return [ |
| 63 | + DefaultInfo(runfiles = ctx.runfiles( |
| 64 | + files = files.to_list() + required_tools, |
| 65 | + collect_data = True, |
| 66 | + collect_default = True, |
| 67 | + )), |
| 68 | + ] |
| 69 | + |
| 70 | +dev_server_rule = rule( |
| 71 | + implementation = _dev_server_rule_impl, |
| 72 | + outputs = { |
| 73 | + "launcher": "%{name}.sh", |
| 74 | + }, |
| 75 | + attrs = { |
| 76 | + "srcs": attr.label_list(allow_files = True, doc = """ |
| 77 | + Sources that should be available to the dev-server. This attribute can be |
| 78 | + used for explicit files. This attribute only uses the files exposed by the |
| 79 | + DefaultInfo provider (i.e. TypeScript targets should be added to "deps"). |
| 80 | + """), |
| 81 | + "additional_root_paths": attr.string_list(doc = """ |
| 82 | + Additionally paths to serve files from. The paths should be formatted |
| 83 | + as manifest paths (e.g. "my_workspace/src") |
| 84 | + """), |
| 85 | + "historyApiFallback": attr.bool( |
| 86 | + default = True, |
| 87 | + doc = """ |
| 88 | + Whether the devserver should fallback to "/index.html" for non-file requests. |
| 89 | + This is helpful for single page applications using the HTML history API. |
| 90 | + """, |
| 91 | + ), |
| 92 | + "port": attr.int( |
| 93 | + default = 4200, |
| 94 | + doc = """The port that the devserver will listen on.""", |
| 95 | + ), |
| 96 | + "deps": attr.label_list( |
| 97 | + allow_files = True, |
| 98 | + aspects = [sources_aspect], |
| 99 | + doc = """ |
| 100 | + Dependencies that need to be available to the dev-server. This attribute can be |
| 101 | + used for TypeScript targets which provide multiple flavors of output. |
| 102 | + """, |
| 103 | + ), |
| 104 | + "_bash_runfile_helpers": attr.label(default = Label("@bazel_tools//tools/bash/runfiles")), |
| 105 | + "_dev_server_bin": attr.label( |
| 106 | + default = Label("//tools/dev-server:dev-server_bin"), |
| 107 | + ), |
| 108 | + "_launcher_template": attr.label(allow_single_file = True, default = Label("//tools/dev-server:launcher_template.sh")), |
| 109 | + }, |
| 110 | +) |
| 111 | + |
| 112 | +""" |
| 113 | + Creates a dev server that can depend on individual bazel targets. The server uses |
| 114 | + bazel runfile resolution in order to work with Bazel package paths. e.g. developers can |
| 115 | + request files through their manifest path: "my_workspace/src/dev-app/my-genfile". |
| 116 | +""" |
| 117 | + |
| 118 | +def dev_server(name, testonly = False, tags = [], **kwargs): |
| 119 | + dev_server_rule( |
| 120 | + name = "%s_launcher" % name, |
| 121 | + visibility = ["//visibility:private"], |
| 122 | + tags = tags, |
| 123 | + **kwargs |
| 124 | + ) |
| 125 | + |
| 126 | + native.sh_binary( |
| 127 | + name = name, |
| 128 | + # The "ibazel_notify_changes" tag tells ibazel to not relaunch the executable on file |
| 129 | + # changes. Rather it will communicate with the server implementation through "stdin". |
| 130 | + tags = tags + ["ibazel_notify_changes"], |
| 131 | + srcs = ["%s_launcher.sh" % name], |
| 132 | + data = [":%s_launcher" % name], |
| 133 | + testonly = testonly, |
| 134 | + ) |
0 commit comments