Skip to content

Commit

Permalink
fix(npm): expose resolved.json for each alias of a package
Browse files Browse the repository at this point in the history
  • Loading branch information
jbedard committed Jul 20, 2024
1 parent c938130 commit 689a604
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 17 deletions.
1 change: 1 addition & 0 deletions e2e/pnpm_lockfiles/lockfile-test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ BZL_FILES = {

# resolved.json reference
"is-odd_resolved.json": "@REPO_NAME//VERSION:is-odd/resolved.json",
"is-odd-v0_resolved.json": "@REPO_NAME//VERSION:is-odd-v0/resolved.json",

# hasBin, optional deps, deps
"rollup_links_defs.bzl": "@REPO_NAME__rollup__2.14.0__links//:defs.bzl",
Expand Down
1 change: 1 addition & 0 deletions e2e/pnpm_lockfiles/v54/snapshots/is-odd-v0_resolved.json

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

1 change: 1 addition & 0 deletions e2e/pnpm_lockfiles/v60/snapshots/is-odd-v0_resolved.json

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

1 change: 1 addition & 0 deletions e2e/pnpm_lockfiles/v61/snapshots/is-odd-v0_resolved.json

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

1 change: 1 addition & 0 deletions e2e/pnpm_lockfiles/v90/snapshots/is-odd-v0_resolved.json

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

47 changes: 30 additions & 17 deletions npm/private/npm_translate_lock_generate.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -265,15 +265,27 @@ def npm_link_all_packages(name = "node_modules", imported_links = []):
))
for link_package, _link_aliases in _import.link_packages.items():
link_aliases = _link_aliases or [_import.package]

# the build file for the package being linked
build_file = "{}/{}".format(link_package, "BUILD.bazel") if link_package else "BUILD.bazel"
if build_file not in rctx_files:
rctx_files[build_file] = []

# the bzl files for the package being linked
if link_package not in links_bzl:
links_bzl[link_package] = []
if link_package not in links_targets_bzl:
links_targets_bzl[link_package] = []

# for each alias of this package
for link_alias in link_aliases:
if link_package not in links_bzl:
links_bzl[link_package] = []
if link_package not in links_targets_bzl:
links_targets_bzl[link_package] = []
# link the alias to the underlying package
links_bzl[link_package].append(""" link_{i}(name = "{{}}/{name}".format(name))""".format(
i = i,
name = link_alias,
))

# expose the alias if public
if "//visibility:public" in _import.package_visibility:
add_to_link_targets = """ link_targets.append("//{{}}:{{}}/{name}".format(bazel_package, name))""".format(name = link_alias)
links_bzl[link_package].append(add_to_link_targets)
Expand All @@ -282,19 +294,20 @@ def npm_link_all_packages(name = "node_modules", imported_links = []):
package_scope = link_alias.split("/", 1)[0]
add_to_scoped_targets = """ scope_targets["{package_scope}"] = scope_targets["{package_scope}"] + [link_targets[-1]] if "{package_scope}" in scope_targets else [link_targets[-1]]""".format(package_scope = package_scope)
links_bzl[link_package].append(add_to_scoped_targets)
for link_package in _import.link_packages.keys():
build_file = "{}/{}".format(link_package, "BUILD.bazel") if link_package else "BUILD.bazel"
if build_file not in rctx_files:
rctx_files[build_file] = []
resolved_json_rel_path = "{}/{}".format(_import.package, _RESOLVED_JSON_FILENAME) if _import.package else _RESOLVED_JSON_FILENAME
resolved_json_file_path = "{}/{}".format(link_package, resolved_json_rel_path) if link_package else resolved_json_rel_path
rctx.file(resolved_json_file_path, json.encode({
# Allow consumers to auto-detect this filetype
"$schema": "https://docs.aspect.build/rules/aspect_rules_js/docs/npm_translate_lock",
"version": _import.version,
"integrity": _import.integrity,
}))
rctx_files[build_file].append("exports_files([\"{}\"])".format(resolved_json_rel_path))

# the resolved.json for this alias of the package
resolved_json_rel_path = "{}/{}".format(link_alias, _RESOLVED_JSON_FILENAME)
resolved_json_file_path = "{}/{}".format(link_package, resolved_json_rel_path) if link_package else resolved_json_rel_path

rctx.file(resolved_json_file_path, json.encode({
# Allow consumers to auto-detect this filetype
"$schema": "https://docs.aspect.build/rules/aspect_rules_js/docs/npm_translate_lock",
"version": _import.version,
"integrity": _import.integrity,
}))
rctx_files[build_file].append("exports_files([\"{}\"])".format(resolved_json_rel_path))

# the package_json.bzl for this package
if _import.package_info.get("has_bin"):
if rctx.attr.generate_bzl_library_targets:
rctx_files[build_file].append("""load("@bazel_skylib//:bzl_library.bzl", "bzl_library")""")
Expand Down

0 comments on commit 689a604

Please sign in to comment.