angular/tools/esm-interop/nodejs-rules.bzl
Paul Gschwendtner 8cd0e3f24c build: update legacy ESM interop rules to support explicit .js entrypoints (#61370)
As we migrate more and more code to `ts_project`, we are ending up with
`.js` file extensions (ts_library did generate `.mjs` magically).

Since we don't want to get into the business of migrating
`nodejs_binary` targets at this point (in some cases we might)— we
should support pointing to such files.

PR Close #61370
2025-05-16 11:02:07 +00:00

52 lines
1.6 KiB
Python

"""Macros for extending the NodeJS Bazel rules with ESM support."""
load("@build_bazel_rules_nodejs//:index.bzl", _nodejs_binary = "nodejs_binary", _nodejs_test = "nodejs_test")
load("//tools/esm-interop:esm-node-module-loader.bzl", "enable_esm_node_module_loader")
def nodejs_binary(
name,
linker_enabled = False,
npm_workspace = "npm",
**kwargs):
env = kwargs.pop("env", {})
testonly = kwargs.pop("testonly", False)
entry_point = kwargs.pop("entry_point", None)
# Ensure ESM entry-points are not resolved to their link target.
templated_args = kwargs.pop("templated_args", [])
templated_args = templated_args + ["--node_options=--preserve-symlinks-main"]
if not linker_enabled:
env = enable_esm_node_module_loader(npm_workspace, env)
_nodejs_binary(
name = name,
testonly = testonly,
entry_point = entry_point,
env = env,
templated_args = templated_args,
use_esm = True,
**kwargs
)
def nodejs_test(
name,
linker_enabled = False,
npm_workspace = "npm",
**kwargs):
env = kwargs.pop("env", {})
# Ensure ESM entry-points are not resolved to their link target.
templated_args = kwargs.pop("templated_args", [])
templated_args = templated_args + ["--node_options=--preserve-symlinks-main"]
if not linker_enabled:
env = enable_esm_node_module_loader(npm_workspace, env)
_nodejs_test(
name = name,
env = env,
templated_args = templated_args,
use_esm = True,
**kwargs
)