-
-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add public_hoist_packages attribute to npm_translate_lock to em…
…ulate .npmrc public-hoist-pattern[] (#222)
- Loading branch information
1 parent
3eb2fb8
commit db88926
Showing
9 changed files
with
74 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
) | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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""", | ||
), | ||
|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.