Skip to content

Commit

Permalink
feat: add support for Label in nodejs_binary#entry_point
Browse files Browse the repository at this point in the history
  • Loading branch information
manekinekko authored and gregmagolan committed Mar 21, 2019
1 parent c254866 commit 6027973
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 16 deletions.
2 changes: 1 addition & 1 deletion internal/jasmine_node_test/jasmine_node_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def jasmine_node_test(
all_data += [Label("//internal/jasmine_node_test:jasmine_runner.js")]
all_data += [":%s_devmode_srcs.MF" % name]
all_data += [Label("@bazel_tools//tools/bash/runfiles")]
entry_point = "build_bazel_rules_nodejs/internal/jasmine_node_test/jasmine_runner.js"
entry_point = "//internal/jasmine_node_test:jasmine_runner.js"

nodejs_test(
name = name,
Expand Down
42 changes: 36 additions & 6 deletions internal/node/node.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ They support module mapping: any targets in the transitive dependencies with
a `module_name` attribute can be `require`d by that name.
"""

load("//internal/common:expand_into_runfiles.bzl", "expand_location_into_runfiles")
load("//internal/common:expand_into_runfiles.bzl", "expand_location_into_runfiles", "expand_path_into_runfiles")
load("//internal/common:module_mappings.bzl", "module_mappings_runtime_aspect")
load("//internal/common:node_module_info.bzl", "NodeModuleInfo", "collect_node_modules_aspect")
load("//internal/common:sources_aspect.bzl", "sources_aspect")
Expand Down Expand Up @@ -74,6 +74,11 @@ def _write_loader_script(ctx):
"node_modules",
] if f])

if len(ctx.attr.entry_point.files) != 1:
fail("labels in entry_point must contain exactly one file")

file_path = expand_path_into_runfiles(ctx, ctx.file.entry_point.short_path)

ctx.actions.expand_template(
template = ctx.file._loader_template,
output = ctx.outputs.loader,
Expand All @@ -82,7 +87,7 @@ def _write_loader_script(ctx):
"TEMPLATED_bootstrap": "\n " + ",\n ".join(
["\"" + d + "\"" for d in ctx.attr.bootstrap],
),
"TEMPLATED_entry_point": ctx.attr.entry_point,
"TEMPLATED_entry_point": file_path,
"TEMPLATED_gen_dir": ctx.genfiles_dir.path,
"TEMPLATED_install_source_map_support": str(ctx.attr.install_source_map_support).lower(),
"TEMPLATED_module_roots": "\n " + ",\n ".join(module_mappings),
Expand Down Expand Up @@ -191,12 +196,37 @@ _NODEJS_EXECUTABLE_ATTRS = {
allow_files = True,
aspects = [sources_aspect, module_mappings_runtime_aspect, collect_node_modules_aspect],
),
"entry_point": attr.string(
"entry_point": attr.label(
doc = """The script which should be executed first, usually containing a main function.
This attribute expects a string starting with the workspace name, so that it's not ambiguous
in cases where a script with the same name appears in another directory or external workspace.
The `entry_point` accepts a target's name as an entry point.
If the target is a rule, it should produce the JavaScript entry file that will be passed to the nodejs_binary rule).
For example:
```
filegroup(
name = "entry_file",
srcs = ["workspace/path/to/entry/file"]
)
nodejs_binary(
name = "my_binary",
...
entry_point = ":entry_file",
)
```
If the entry JavaScript file belongs to the same package (as the BUILD file),
you can simply reference it by its relative name to the package directory:
```
nodejs_binary(
name = "my_binary",
...
entry_point = ":file.js",
)
```
""",
mandatory = True,
allow_single_file = True,
),
"install_source_map_support": attr.bool(
doc = """Install the source-map-support package.
Expand Down
8 changes: 4 additions & 4 deletions internal/node/test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ load("//:defs.bzl", "nodejs_binary")
nodejs_binary(
name = "no_deps",
data = ["no-deps.js"],
entry_point = "build_bazel_rules_nodejs/internal/node/test/no-deps",
entry_point = ":no-deps.js",
)

# You can have a nodejs_binary with a node_modules attribute
# and no fine grained deps
nodejs_binary(
name = "has_deps_legacy",
data = ["has-deps.js"],
entry_point = "build_bazel_rules_nodejs/internal/node/test/has-deps",
entry_point = ":has-deps",
node_modules = "@fine_grained_deps_yarn//:node_modules",
)

Expand All @@ -25,7 +25,7 @@ nodejs_binary(
"has-deps.js",
"@fine_grained_deps_yarn//typescript",
],
entry_point = "build_bazel_rules_nodejs/internal/node/test/has-deps",
entry_point = ":has-deps",
)

# You can have a nodejs_binary with both a node_modules attribute
Expand All @@ -36,6 +36,6 @@ nodejs_binary(
"has-deps.js",
"@fine_grained_deps_yarn//typescript",
],
entry_point = "build_bazel_rules_nodejs/internal/node/test/has-deps",
entry_point = ":has-deps",
node_modules = "@fine_grained_deps_yarn//:node_modules",
)
2 changes: 1 addition & 1 deletion internal/npm_install/generate_build_file.js
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ filegroup(
result += `# Wire up the \`bin\` entry \`${name}\`
nodejs_binary(
name = "${name}__bin",
entry_point = "${pkg._dir}/${path}",
entry_point = "//node_modules/${pkg._dir}:${name}",
install_source_map_support = False,
data = [":${pkg._name}__pkg"],
)
Expand Down
2 changes: 1 addition & 1 deletion internal/npm_install/test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ nodejs_binary(
"@npm//jasmine",
"@npm//unidiff",
],
entry_point = "build_bazel_rules_nodejs/internal/npm_install/test/update_golden.js",
entry_point = "//internal/npm_install/test:update_golden.js",
install_source_map_support = False,
)

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/web_package/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ filegroup(
nodejs_binary(
name = "assembler",
data = ["assembler.js"],
entry_point = "build_bazel_rules_nodejs/internal/web_package/assembler.js",
entry_point = "//internal/web_package:assembler.js",
install_source_map_support = False,
node_modules = ":node_modules_none",
)
Expand Down

0 comments on commit 6027973

Please sign in to comment.