Skip to content

Commit

Permalink
feat: add public_hoist_packages attribute to npm_translate_lock to em…
Browse files Browse the repository at this point in the history
…ulate .npmrc public-hoist-pattern[] (#222)
  • Loading branch information
gregmagolan authored Jun 17, 2022
1 parent 3eb2fb8 commit db88926
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 5 deletions.
7 changes: 7 additions & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# Set a custom registry for a scope that is picked up by pnpm when resolving packages;
# This affects the lockfile format and is here to cover this case.
@types:registry=https://registry.yarnpkg.com

# The rules_js linker won't see this public-hoist-pattern[] since hoisting is _not_ encoded
# in the pnpm lockfile. In pnpm it is a linking time consideration when laying out the node_modules
# tree. In rules_js we currently have a `public_hoist_packages` attribute on `npm_translate_lock`
# the emulates this behavior (see example in WORKSPACE). In the future we'll add support to
# parse the .npmrc file so these directives are used to direct hoisting in the rules_js linker as well.
public-hoist-pattern[]=ms
7 changes: 7 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ npm_translate_lock(
"@gregmagolan/[email protected]": ["//examples/npm_deps:patches/[email protected]"],
},
pnpm_lock = "//:pnpm-lock.yaml",
public_hoist_packages = {
# Instructs the linker to hoist the [email protected] npm package to `node_modules/ms` in the `examples/npm_deps` package.
# Similar to adding `public-hoist-pattern[]=ms` in .npmrc but with control over which version to hoist and where
# to hoist it. This hoisted package can be referenced by the label `//examples/npm_deps:node_modules/ms` same as
# other direct dependencies in the `examples/npm_deps/package.json`.
"[email protected]": ["examples/npm_deps"],
},
verify_node_modules_ignored = "//:.bazelignore",
warn_on_unqualified_tarball_url = False,
)
Expand Down
3 changes: 2 additions & 1 deletion docs/npm_import.md

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

25 changes: 25 additions & 0 deletions examples/npm_deps/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,28 @@ uvu_test(
":uvu_spec",
],
)

#######################################
# Use case 6: depend on a hoisted npm package, ms, that isn't a direct dependency
# in package.json but is a transitive dep of [email protected]. Hoisted packages are
# currently specified in the translate_package_lock rule. In the future we'll
# add support for parsing the .npmrc and hoisting via public-hoist-pattern[]
# directives.

write_file(
name = "write6",
out = "case6.js",
content = ["""
const ms = require('ms')
const assert = require('assert')
assert.ok(ms('2 days') == 172800000)
"""],
)

js_test(
name = "test6",
data = [
":node_modules/ms",
],
entry_point = "case6.js",
)
1 change: 1 addition & 0 deletions examples/npm_deps/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"@aspect-test/c": "2.0.2",
"@gregmagolan/test-b": "0.0.2",
"@rollup/plugin-commonjs": "21.1.0",
"debug": "3.2.7",
"mobx-react": "7.3.0",
"mobx": "6.3.0",
"react": "17.0.2",
Expand Down
20 changes: 20 additions & 0 deletions npm/private/npm_translate_lock.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,14 @@ _ATTRS = {
"prod": attr.bool(
doc = """If true, only install dependencies""",
),
"public_hoist_packages": attr.string_list_dict(
doc = """A map of package names or package names with their version (e.g., "my-package" or "[email protected]")
to a list of Bazel packages in which to hoist the package to the top-level of the node_modules tree when linking.
This is similar to setting https://pnpm.io/npmrc#public-hoist-pattern in an .npmrc file outside of Bazel, however,
wild-cards are not yet supported and translate_pnpm_lock will fail if there are multiple versions of a package that
are to be hoisted.""",
),
"dev": attr.bool(
doc = """If true, only install devDependencies""",
),
Expand Down Expand Up @@ -331,6 +339,18 @@ def _gen_npm_imports(lockfile, attr):
else:
link_packages[link_package].append(dep_package)

# check if this package should be hoisted via public_hoist_packages
public_hoist_packages = []
public_hoist_packages = attr.public_hoist_packages.get(name, [])[:]
public_hoist_packages.extend(attr.public_hoist_packages.get(friendly_name, []))
if unfriendly_name:
public_hoist_packages.extend(attr.patches.get(unfriendly_name, []))
for public_hoist_package in public_hoist_packages:
if public_hoist_package not in link_packages:
link_packages[public_hoist_package] = [name]
elif name not in link_packages[public_hoist_package]:
link_packages[public_hoist_package].append(name)

run_lifecycle_hooks = (
requires_build and
attr.run_lifecycle_hooks and
Expand Down
6 changes: 4 additions & 2 deletions npm/private/test/defs_checked.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ load("@npm__d3-time-format__2.3.0__links//:defs.bzl", link_107_store = "npm_link
load("@npm__d3-time__1.1.0__links//:defs.bzl", link_108_store = "npm_link_imported_package_store")
load("@npm__d3-timer__1.0.10__links//:defs.bzl", link_109_store = "npm_link_imported_package_store")
load("@npm__debug__2.6.9__links//:defs.bzl", link_110_store = "npm_link_imported_package_store")
load("@npm__debug__3.2.7__links//:defs.bzl", link_111_store = "npm_link_imported_package_store")
load("@npm__debug__3.2.7__links//:defs.bzl", link_111_direct = "npm_link_imported_package_direct", link_111_store = "npm_link_imported_package_store")
load("@npm__debug__4.3.4__links//:defs.bzl", link_112_store = "npm_link_imported_package_store")
load("@npm__debug__4.3.4__supports-color_8.1.1__links//:defs.bzl", link_113_store = "npm_link_imported_package_store")
load("@npm__decamelize__4.0.0__links//:defs.bzl", link_114_store = "npm_link_imported_package_store")
Expand Down Expand Up @@ -286,7 +286,7 @@ load("@npm__mrmime__1.0.1__links//:defs.bzl", link_280_store = "npm_link_importe
load("@npm__ms__0.7.3__links//:defs.bzl", link_281_store = "npm_link_imported_package_store")
load("@npm__ms__2.0.0__links//:defs.bzl", link_282_store = "npm_link_imported_package_store")
load("@npm__ms__2.1.2__links//:defs.bzl", link_283_store = "npm_link_imported_package_store")
load("@npm__ms__2.1.3__links//:defs.bzl", link_284_store = "npm_link_imported_package_store")
load("@npm__ms__2.1.3__links//:defs.bzl", link_284_direct = "npm_link_imported_package_direct", link_284_store = "npm_link_imported_package_store")
load("@npm__mumath__3.3.4__links//:defs.bzl", link_285_store = "npm_link_imported_package_store")
load("@npm__murmurhash-js__1.0.0__links//:defs.bzl", link_286_store = "npm_link_imported_package_store")
load("@npm__nanoid__3.3.3__links//:defs.bzl", link_287_store = "npm_link_imported_package_store")
Expand Down Expand Up @@ -938,8 +938,10 @@ def npm_link_all_packages(name = "node_modules", imported_links = []):
scoped_direct_targets["@gregmagolan"] = scoped_direct_targets["@gregmagolan"] + [direct_targets[-1]] if "@gregmagolan" in scoped_direct_targets else [direct_targets[-1]]
direct_targets.append(link_22_direct(name = "{}/@rollup/plugin-commonjs".format(name)))
scoped_direct_targets["@rollup"] = scoped_direct_targets["@rollup"] + [direct_targets[-1]] if "@rollup" in scoped_direct_targets else [direct_targets[-1]]
direct_targets.append(link_111_direct(name = "{}/debug".format(name)))
direct_targets.append(link_270_direct(name = "{}/mobx-react".format(name)))
direct_targets.append(link_271_direct(name = "{}/mobx".format(name)))
direct_targets.append(link_284_direct(name = "{}/ms".format(name)))
direct_targets.append(link_330_direct(name = "{}/react".format(name)))
direct_targets.append(link_345_direct(name = "{}/rollup".format(name)))
direct_targets.append(link_407_direct(name = "{}/uvu".format(name)))
Expand Down
8 changes: 6 additions & 2 deletions npm/private/test/repositories_checked.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -1930,7 +1930,9 @@ def npm_repositories():
name = "npm__debug__3.2.7",
root_package = "",
link_workspace = "",
link_packages = {},
link_packages = {
"examples/npm_deps": ["debug"],
},
package = "debug",
version = "3.2.7",
integrity = "sha512-CFjzYYAi4ThfiQvizrFQevTTXHtnCqWfe7x1AhgEscTz6ZbLbfoLRLPugTQyBth6f8ZERVUSyWHFD/7Wu4t1XQ==",
Expand Down Expand Up @@ -5289,7 +5291,9 @@ def npm_repositories():
name = "npm__ms__2.1.3",
root_package = "",
link_workspace = "",
link_packages = {},
link_packages = {
"examples/npm_deps": ["ms"],
},
package = "ms",
version = "2.1.3",
integrity = "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==",
Expand Down
2 changes: 2 additions & 0 deletions pnpm-lock.yaml

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

0 comments on commit db88926

Please sign in to comment.