| load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_binary") | |
| load("@io_bazel_rules_docker//contrib:test.bzl", "container_test") | |
| load("@io_bazel_rules_docker//nodejs:image.bzl", "nodejs_image") | |
| load("@bazel_skylib//rules:write_file.bzl", "write_file") | |
| SHARP = select({ | |
| # When the target platform is Linux, request the copy of the package | |
| # which downloaded linux executables in its post-install | |
| "@bazel_tools//src/conditions:linux_x86_64": ["@npm_linux//sharp"], | |
| # Otherwise just use it for the host platform. | |
| "//conditions:default": ["@npm//sharp"], | |
| }) | |
| nodejs_binary( | |
| name = "main", | |
| data = [ | |
| "//foolib", | |
| ] + SHARP, | |
| entry_point = "main.js", | |
| ) | |
| # bazel run --platforms=@rules_nodejs//:linux_amd64 //:nodejs_image | |
| nodejs_image( | |
| name = "nodejs_image", | |
| binary = ":main", | |
| include_node_repo_args = False, | |
| node_repository_name = "node16_linux_amd64", | |
| ) | |
| container_test( | |
| name = "nodejs_image_test", | |
| configs = [":nodejs_image.yaml"], | |
| image = ":nodejs_image", | |
| ) | |
| # this creates: | |
| # bazel run main_node15_linux_amd64 | |
| # bazel run main_node16_linux_amd64 | |
| # bazel run nodejs_image_node15_linux_amd64 | |
| # bazel run nodejs_image_node16_linux_amd64 | |
| # bazel test nodejs_image_test_node15_linux_amd64 | |
| # bazel test nodejs_image_test_node16_linux_amd64 | |
| [ | |
| [ | |
| # Trivial test fixture: a nodejs program that writes to a file | |
| write_file( | |
| name = "test_" + id, | |
| out = "image_%s.yaml" % id, | |
| content = [ | |
| "schemaVersion: 2.0.0", | |
| "metadataTest:", | |
| " entrypoint: ['/app//main_%s']" % id, | |
| """ workdir: "/app//main_%s.runfiles/e2e_nodejs_image" """ % id, | |
| ], | |
| ), | |
| nodejs_binary( | |
| name = "main_" + id, | |
| data = ["//foolib"], | |
| entry_point = "binary_version.js", | |
| toolchain = toolchain, | |
| ), | |
| nodejs_image( | |
| name = "nodejs_image_" + id, | |
| binary = ":main_" + id, | |
| include_node_repo_args = False, | |
| node_repository_name = id, | |
| ), | |
| container_test( | |
| name = "nodejs_image_test_" + id, | |
| configs = ["test_" + id], | |
| image = ":nodejs_image_" + id, | |
| ), | |
| ] | |
| for id, toolchain in zip( | |
| [ | |
| "node15_linux_amd64", | |
| "node16_linux_amd64", | |
| ], | |
| [ | |
| "@node15_linux_amd64//:node_toolchain", | |
| "@node16_linux_amd64//:node_toolchain", | |
| ], | |
| ) | |
| ] |