Skip to content

Commit

Permalink
refactor: npm_install and yarn_install no longer create node reposito…
Browse files Browse the repository at this point in the history
…ries

This convenience is now too magical and confusing.
If the user registers nodejs toolchains, this will create an extra toolchain they don't use,
but can be fetched by rules that reference the default.
It also makes a @Yarn workspace available even for users who only need npm.

BREAKING CHANGE:
you now need to explicitly register nodejs toolchains or use the node_repositories helper
  • Loading branch information
alexeagle committed Jan 16, 2022
1 parent 5ad9753 commit ab3fb56
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 11 deletions.
11 changes: 10 additions & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ nodejs_register_toolchains(
node_version = "15.14.0",
)

load("@rules_nodejs//nodejs:yarn_repositories.bzl", "yarn_repositories")

yarn_repositories(
name = "yarn",
node_repository = "node16",
)

load("@build_bazel_rules_nodejs//:npm_deps.bzl", "npm_deps")

npm_deps()
Expand Down Expand Up @@ -131,7 +138,9 @@ browser_repositories(
# Setup esbuild dependencies
load("//toolchains/esbuild:esbuild_repositories.bzl", "esbuild_repositories")

esbuild_repositories()
esbuild_repositories(
node_repository = "node16",
)

#
# Dependencies to run stardoc & generating documentation
Expand Down
8 changes: 7 additions & 1 deletion docs/Toolchains.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ Defaults to `""`
**USAGE**

<pre>
esbuild_repositories(<a href="#esbuild_repositories-name">name</a>, <a href="#esbuild_repositories-npm_repository">npm_repository</a>, <a href="#esbuild_repositories-npm_args">npm_args</a>)
esbuild_repositories(<a href="#esbuild_repositories-name">name</a>, <a href="#esbuild_repositories-npm_repository">npm_repository</a>, <a href="#esbuild_repositories-npm_args">npm_args</a>, <a href="#esbuild_repositories-kwargs">kwargs</a>)
</pre>

Helper for fetching and setting up the esbuild versions and toolchains
Expand Down Expand Up @@ -228,4 +228,10 @@ additional args to pass to the npm install rule

Defaults to `[]`

<h4 id="esbuild_repositories-kwargs">kwargs</h4>

additional named parameters to the npm_install rule




8 changes: 7 additions & 1 deletion docs/esbuild.md
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ Any other common attributes
**USAGE**

<pre>
esbuild_repositories(<a href="#esbuild_repositories-name">name</a>, <a href="#esbuild_repositories-npm_repository">npm_repository</a>, <a href="#esbuild_repositories-npm_args">npm_args</a>)
esbuild_repositories(<a href="#esbuild_repositories-name">name</a>, <a href="#esbuild_repositories-npm_repository">npm_repository</a>, <a href="#esbuild_repositories-npm_args">npm_args</a>, <a href="#esbuild_repositories-kwargs">kwargs</a>)
</pre>

Helper for fetching and setting up the esbuild versions and toolchains
Expand Down Expand Up @@ -476,4 +476,10 @@ additional args to pass to the npm install rule

Defaults to `[]`

<h4 id="esbuild_repositories-kwargs">kwargs</h4>

additional named parameters to the npm_install rule




15 changes: 8 additions & 7 deletions index.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
Users should not load files under "/internal"
"""

load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
load("@rules_nodejs//nodejs:yarn_repositories.bzl", _yarn_repositories = "yarn_repositories")
load("//:version.bzl", "VERSION")
load("//internal/common:check_version.bzl", "check_version")
load("//internal/common:copy_to_bin.bzl", _copy_to_bin = "copy_to_bin")
Expand Down Expand Up @@ -44,6 +46,7 @@ copy_to_bin = _copy_to_bin
params_file = _params_file
generated_file_test = _generated_file_test
js_library = _js_library
npm_install = _npm_install
# ANY RULES ADDED HERE SHOULD BE DOCUMENTED, see index.for_docs.bzl

# @unsorted-dict-items
Expand All @@ -55,14 +58,12 @@ COMMON_REPLACEMENTS = {
"\"@bazel/([a-zA-Z_-]+)\":\\s+\"(file|bazel)[^\"]+\"": "\"@bazel/$1\": \"0.0.0-PLACEHOLDER\"",
}

def npm_install(**kwargs):
# Just in case the user didn't install nodejs, do it now
_node_repositories()
_npm_install(**kwargs)

def yarn_install(**kwargs):
# Just in case the user didn't install nodejs, do it now
_node_repositories()
# Just in case the user didn't install yarn, and if they aren't using a custom yarn binary,
# install the default "@yarn" one for them here.
if "yarn" not in kwargs.keys():
maybe(_yarn_repositories, name = "yarn")

_yarn_install(**kwargs)

# Currently used Bazel version. This version is what the rules here are tested
Expand Down
4 changes: 3 additions & 1 deletion toolchains/esbuild/esbuild_repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
load("@build_bazel_rules_nodejs//:index.bzl", "npm_install")
load(":esbuild_packages.bzl", "ESBUILD_PACKAGES")

def esbuild_repositories(name = "", npm_repository = "npm", npm_args = []):
def esbuild_repositories(name = "", npm_repository = "npm", npm_args = [], **kwargs):
"""Helper for fetching and setting up the esbuild versions and toolchains
This uses Bazel's downloader (via `http_archive`) to fetch the esbuild package
Expand All @@ -29,6 +29,7 @@ def esbuild_repositories(name = "", npm_repository = "npm", npm_args = []):
npm_repository: the name of the repository where the @bazel/esbuild package is installed
by npm_install or yarn_install.
npm_args: additional args to pass to the npm install rule
**kwargs: additional named parameters to the npm_install rule
"""

maybe(
Expand Down Expand Up @@ -78,4 +79,5 @@ def esbuild_repositories(name = "", npm_repository = "npm", npm_args = []):
"--ignore-scripts",
] + npm_args,
package_path = package_path,
**kwargs
)

0 comments on commit ab3fb56

Please sign in to comment.