|
| 1 | +load("@aspect_bazel_lib//lib:copy_to_bin.bzl", "copy_to_bin") |
| 2 | +load("@aspect_bazel_lib//lib:expand_template.bzl", "expand_template") |
| 3 | +load("@aspect_bazel_lib//lib:jq.bzl", "jq") |
| 4 | +load("@aspect_bazel_lib//lib:utils.bzl", "to_label") |
| 5 | +load("@aspect_rules_js//npm:defs.bzl", _npm_package = "npm_package") |
| 6 | +load("@rules_pkg//:pkg.bzl", "pkg_tar") |
| 7 | +load("//tools:link_package_json_to_tarballs.bzl", "link_package_json_to_tarballs") |
| 8 | +load("//tools:snapshot_repo_filter.bzl", "SNAPSHOT_REPO_JQ_FILTER") |
| 9 | +load("//tools:substitutions.bzl", "NO_STAMP_PACKAGE_SUBSTITUTIONS", "get_npm_package_substitutions_for_rjs") |
| 10 | + |
| 11 | +def npm_package( |
| 12 | + name, |
| 13 | + deps = [], |
| 14 | + visibility = None, |
| 15 | + pkg_deps = [], |
| 16 | + pkg_json = "package.json", |
| 17 | + **kwargs): |
| 18 | + if name != "pkg": |
| 19 | + fail("Expected npm_package to be named `pkg`. " + |
| 20 | + "This is needed for pnpm workspace integration.") |
| 21 | + |
| 22 | + # Merge package.json with root package.json and perform various substitutions to |
| 23 | + # prepare it for release. For jq docs, see https://stedolan.github.io/jq/manual/. |
| 24 | + jq( |
| 25 | + name = "basic_substitutions", |
| 26 | + # Note: this jq filter relies on the order of the inputs |
| 27 | + # buildifier: do not sort |
| 28 | + srcs = ["//:package.json", pkg_json], |
| 29 | + filter_file = "//tools:package_json_release_filter.jq", |
| 30 | + args = ["--slurp"], |
| 31 | + out = "substituted/package.json", |
| 32 | + ) |
| 33 | + |
| 34 | + # Copy package.json files to bazel-out so we can use their bazel-out paths to determine |
| 35 | + # the corresponding package npm package tgz path for substitutions. |
| 36 | + copy_to_bin( |
| 37 | + name = "package_json_copy", |
| 38 | + srcs = [pkg_json], |
| 39 | + ) |
| 40 | + pkg_deps_copies = [] |
| 41 | + for pkg_dep in pkg_deps: |
| 42 | + pkg_label = to_label(pkg_dep) |
| 43 | + if pkg_label.name != "package.json": |
| 44 | + fail("ERROR: only package.json files allowed in pkg_deps of pkg_npm macro") |
| 45 | + pkg_deps_copies.append("@%s//%s:package_json_copy" % (pkg_label.workspace_name, pkg_label.package)) |
| 46 | + |
| 47 | + # Substitute dependencies on other packages in this repo with tarballs. |
| 48 | + link_package_json_to_tarballs( |
| 49 | + name = "tar_substitutions", |
| 50 | + src = "substituted/package.json", |
| 51 | + pkg_deps = [":package_json_copy"] + pkg_deps_copies, |
| 52 | + out = "substituted_with_tars/package.json", |
| 53 | + ) |
| 54 | + |
| 55 | + # Substitute dependencies on other packages in this repo with snapshot repos. |
| 56 | + jq( |
| 57 | + name = "snapshot_repo_substitutions", |
| 58 | + srcs = ["substituted/package.json"], |
| 59 | + filter = SNAPSHOT_REPO_JQ_FILTER, |
| 60 | + out = "substituted_with_snapshot_repos/package.json", |
| 61 | + ) |
| 62 | + |
| 63 | + expand_template( |
| 64 | + name = "final_package_json", |
| 65 | + template = select({ |
| 66 | + # Do local tar substitution if config_setting is true. |
| 67 | + "//:package_json_use_tar_deps": "substituted_with_tars/package.json", |
| 68 | + # Do snapshot repo substitution if config_setting is true. |
| 69 | + "//:package_json_use_snapshot_repo_deps": "substituted_with_snapshot_repos/package.json", |
| 70 | + "//conditions:default": "substituted/package.json", |
| 71 | + }), |
| 72 | + out = "substituted_final/package.json", |
| 73 | + substitutions = NO_STAMP_PACKAGE_SUBSTITUTIONS, |
| 74 | + stamp_substitutions = get_npm_package_substitutions_for_rjs(), |
| 75 | + ) |
| 76 | + |
| 77 | + _npm_package( |
| 78 | + name = "npm_package", |
| 79 | + visibility = visibility, |
| 80 | + srcs = [":final_package_json"] + deps, |
| 81 | + replace_prefixes = { |
| 82 | + "substituted_final/": "", |
| 83 | + "substituted_with_tars/": "", |
| 84 | + "substituted_with_snapshot_repos/": "", |
| 85 | + "substituted/": "", |
| 86 | + }, |
| 87 | + exclude_srcs_patterns = [ |
| 88 | + # Exclude `node_modules` which may be pulled by the `js_module_output` runfiles. |
| 89 | + "node_modules/**/*", |
| 90 | + ], |
| 91 | + allow_overwrites = True, |
| 92 | + **kwargs |
| 93 | + ) |
| 94 | + |
| 95 | + # Note: For now, in hybrid mode with RNJS and RJS, we ensure |
| 96 | + # both `:pkg` and `:npm_package` work. |
| 97 | + native.alias( |
| 98 | + name = "pkg", |
| 99 | + actual = ":npm_package", |
| 100 | + ) |
| 101 | + |
| 102 | + if pkg_json: |
| 103 | + pkg_tar( |
| 104 | + name = "npm_package_archive", |
| 105 | + srcs = [":pkg"], |
| 106 | + extension = "tgz", |
| 107 | + strip_prefix = "./npm_package", |
| 108 | + visibility = visibility, |
| 109 | + ) |
0 commit comments