blob: aa772f007b796b30f2d1cc3326d85e322c7d8994 [file] [log] [blame]
# Demonstrate and test the workaround for running build/test actions in the output directory
# Needed for tools like react-scripts and maybe vue-cli which don't allow specifying a path to
# the project when you call them.
# See https://github.com/bazelbuild/rules_nodejs/issues/1840
load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_binary", "nodejs_test", "npm_package_bin")
# Trivial tool that expects to run in source directory under the package
nodejs_binary(
name = "tool_cp",
entry_point = "cp.js",
)
# A tool like react-scripts needs to run in the output directory since it writes outputs
# to $pwd/build
# That means it also needs to find inputs in that directory.
# So we copy all the inputs it needs.
_package_segments = len(package_name().split("/"))
npm_package_bin(
name = "do_copy",
outs = ["package.json"],
# We have to compensate for the changed directory, adapting other arguments
# to reach back to parent directory
args = ["/".join([".."] * _package_segments + ["$@"])],
chdir = package_name(),
data = ["_package.json"],
stdout = "do_copy.log",
tool = ":tool_cp",
)
# Trivial tool to mock react-scripts
nodejs_binary(
name = "tool_bin",
entry_point = "tool.js",
)
# This tool is like react-scripts and wants to run in our directory
# with our package.json, and always writes to "build/app.js
npm_package_bin(
name = "call_tool",
outs = ["build/app.js"],
chdir = "$(RULEDIR)",
data = ["package.json"],
tool = ":tool_bin",
)
nodejs_test(
name = "test",
# Also run a test in the output directory
chdir = package_name() + "/build",
data = ["build/app.js"],
entry_point = "test.js",
)