| # 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", "generated_file_test", "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", |
| ) |
| |
| # TODO: figure out why this suddenly stopped producing package.json output |
| # https://github.com/bazelbuild/rules_nodejs/issues/3328 |
| i3328 = ["fix-windows"] |
| |
| # 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"], |
| exit_code_out = "exit.code", |
| stdout = "do_copy.log", |
| tags = i3328, |
| tool = ":tool_cp", |
| ) |
| |
| generated_file_test( |
| name = "exit_code_test", |
| src = "exit_code_test.golden", |
| generated = ":exit.code", |
| tags = i3328, |
| ) |
| |
| # 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"], |
| tags = i3328, |
| 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", |
| tags = i3328, |
| ) |
| |
| nodejs_test( |
| name = "test_multithread", |
| chdir = package_name(), |
| data = ["worker.js"], |
| entry_point = "multithread.js", |
| tags = i3328, |
| ) |