Skip to content

Commit c164c6d

Browse files
committed
refactor: distribute esbuild_repositories function in the built-in, not the @bazel/esbuild package
Avoids eager fetching of the npm_install/yarn_install by not load'ing from the @npm package
1 parent b4b588a commit c164c6d

File tree

16 files changed

+92
-92
lines changed

16 files changed

+92
-92
lines changed

BUILD.bazel

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ bzl_library(
5353
"//internal/pkg_npm:bzl",
5454
"//internal/pkg_web:bzl",
5555
"//internal/providers:bzl",
56+
"//toolchains/esbuild:bzl",
5657
"//toolchains/node:bzl",
5758
],
5859
)
@@ -69,9 +70,13 @@ codeowners(
6970

7071
pkg_npm(
7172
name = "rules_nodejs_package",
72-
srcs = glob(["*.bzl"]) + [
73+
srcs = [
7374
"BUILD.bazel",
7475
"LICENSE",
76+
"index.bzl",
77+
"package.bzl",
78+
"providers.bzl",
79+
"version.bzl",
7580
],
7681
substitutions = COMMON_REPLACEMENTS,
7782
deps = [
@@ -104,6 +109,7 @@ pkg_npm(
104109
"//third_party/npm/node_modules/browserify:package_contents",
105110
"//third_party/npm/node_modules/ieee754:package_contents",
106111
"//third_party/npm/node_modules/named-amd:package_contents",
112+
"//toolchains/esbuild:package_contents",
107113
"//toolchains/node:package_contents",
108114
],
109115
)

WORKSPACE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ browser_repositories(
131131
)
132132

133133
# Setup esbuild dependencies
134-
load("//packages/esbuild:esbuild_repositories.bzl", "esbuild_repositories")
134+
load("//toolchains/esbuild:esbuild_repositories.bzl", "esbuild_repositories")
135135

136136
esbuild_repositories()
137137

docs/esbuild.md

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,30 @@ or using yarn
2222
yarn add -D @bazel/esbuild
2323
```
2424

25-
The esbuild binary is fetched from npm automatically and exposed via toolchains. Add the `esbuild_repositories` rule to the `WORKSPACE`:
25+
The esbuild binary is fetched automatically for your platform and is exposed via Bazel toolchains.
26+
To do this, add the `esbuild_repositories` rule to your `WORKSPACE`.
27+
You'll need to point it to the repository created by npm_install or yarn_install where the `@bazel/esbuild`
28+
package is fetched. (Typically, this is `npm`).
29+
Set the `npm_repository` attribute to the name of that repository.
2630

2731
```python
28-
load("@npm//@bazel/esbuild:esbuild_repositories.bzl", "esbuild_repositories")
29-
30-
esbuild_repositories()
31-
```
32-
33-
As esbuild is being fetched from `npm`, the load statement above can cause eager fetches of the `@npm` external repository.
34-
To work around this, it's possible to fetch the `@bazel/esbuild` package via an `http_archive`
35-
36-
```python
37-
http_archive(
38-
name = "bazel_esbuild",
39-
urls = [
40-
"https://registry.npmjs.org/@bazel/esbuild/-/esbuild-4.0.0.tgz",
41-
],
42-
strip_prefix = "package",
32+
npm_install(
33+
name = "npm",
34+
# @bazel/esbuild is a dependency in this package.json
35+
package_json = "//:package.json",
36+
package_lock_json = "//:package-lock.json",
4337
)
4438

45-
load("@bazel_esbuild//:esbuild_repositories.bzl", "esbuild_repositories")
39+
load("@build_bazel_rules_nodejs//toolchains/esbuild:esbuild_repositories.bzl", "esbuild_repositories")
4640

47-
esbuild_repositories()
41+
esbuild_repositories(npm_repository = "npm") # Note, npm is the default value for npm_repository
4842
```
4943

44+
> To avoid eagerly fetching all the npm dependencies, this load statement comes from the "Built-in"
45+
> `@build_bazel_rules_nodejs` repository rather than from `@npm`.
46+
> In rules_nodejs 5.0 we intend to fix this layering violation by having the whole esbuild support
47+
> distributed independently of rules_nodejs, and not require any package to be installed from npm.
48+
5049
## Overview
5150

5251
The `esbuild` rule can take a JS or TS dependency tree and bundle it to a single file, or split across multiple files, outputting a directory.
@@ -392,7 +391,7 @@ Any other common attributes
392391
**USAGE**
393392

394393
<pre>
395-
esbuild_repositories(<a href="#esbuild_repositories-name">name</a>)
394+
esbuild_repositories(<a href="#esbuild_repositories-name">name</a>, <a href="#esbuild_repositories-npm_repository">npm_repository</a>)
396395
</pre>
397396

398397
Helper for fetching and setting up the esbuild versions and toolchains
@@ -406,4 +405,11 @@ currently unused
406405

407406
Defaults to `""`
408407

408+
<h4 id="esbuild_repositories-npm_repository">npm_repository</h4>
409+
410+
the name of the repository where the @bazel/esbuild package is installed
411+
by npm_install or yarn_install.
412+
413+
Defaults to `"npm"`
414+
409415

examples/esbuild/WORKSPACE

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,6 @@ npm_install(
3333
package_lock_json = "//:package-lock.json",
3434
)
3535

36-
# This is the simpler way to load, however it eagerly fetches all of the npm
37-
# dependencies for every build due to this load statement, whether the build
38-
# needs them or not.
39-
# load("@npm//@bazel/esbuild:esbuild_repositories.bzl", "esbuild_repositories")
40-
41-
# We can avoid that eager fetch by loading esbuild_repositories
42-
# from a separately-fetched npm package.
43-
http_archive(
44-
name = "bazel_esbuild",
45-
strip_prefix = "package",
46-
urls = [
47-
# TODO(alexeagle): how to keep this version up-to-date as we cut releases?
48-
"https://registry.npmjs.org/@bazel/esbuild/-/esbuild-4.0.0-rc.0.tgz",
49-
],
50-
)
51-
52-
load("@bazel_esbuild//:esbuild_repositories.bzl", "esbuild_repositories")
36+
load("@build_bazel_rules_nodejs//toolchains/esbuild:esbuild_repositories.bzl", "esbuild_repositories")
5337

5438
esbuild_repositories()

internal/node/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ filegroup(
5252
visibility = ["//:__pkg__"],
5353
)
5454

55+
# BEGIN-INTERNAL
5556
# To update node_patches.js run:
5657
# bazel run //internal/node:checked_in_node_patches.update
5758
generated_file_test(
@@ -61,3 +62,4 @@ generated_file_test(
6162
# For some reason rollup produces different, non-matching output with --config=no-runfiles
6263
tags = ["fix-windows"],
6364
)
65+
# END-INTERNAL

packages/esbuild/BUILD.bazel

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ bzl_library(
3131
"@bazel_tools//tools:bzl_srcs",
3232
],
3333
deps = [
34-
"//packages/esbuild/toolchain:bzl",
3534
"@bazel_skylib//lib:paths",
3635
"@build_bazel_rules_nodejs//:bzl",
3736
"@build_bazel_rules_nodejs//internal/common:bzl",
3837
"@build_bazel_rules_nodejs//internal/node:bzl",
38+
"@build_bazel_rules_nodejs//toolchains/esbuild:bzl",
3939
],
4040
)
4141

@@ -59,8 +59,6 @@ filegroup(
5959
srcs = [
6060
"esbuild.bzl",
6161
"esbuild_config.bzl",
62-
"esbuild_packages.bzl",
63-
"esbuild_repositories.bzl",
6462
"helpers.bzl",
6563
"index.bzl",
6664
"launcher.js",
@@ -71,15 +69,11 @@ filegroup(
7169
pkg_npm(
7270
name = "npm_package",
7371
package_name = "@bazel/esbuild",
74-
srcs = [
75-
":srcs",
76-
"//packages/esbuild/toolchain:srcs",
77-
],
72+
srcs = [":srcs"],
7873
build_file_content = """exports_files(["launcher.js"])""",
79-
substitutions = dict({
74+
substitutions = {
8075
"@build_bazel_rules_nodejs//packages/esbuild": "//@bazel/esbuild",
81-
"package_path = \"packages/esbuild\",": "package_path = \"external/\" + pkg_label.workspace_name + \"/@bazel/esbuild\",",
82-
}),
76+
},
8377
deps = [
8478
":README.md",
8579
":npm_version_check",

packages/esbuild/esbuild.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_binary", "params_file")
66
load("@build_bazel_rules_nodejs//:providers.bzl", "ExternalNpmPackageInfo", "JSEcmaScriptModuleInfo", "JSModuleInfo", "NODE_CONTEXT_ATTRS", "NodeContextInfo", "node_modules_aspect", "run_node")
77
load("@build_bazel_rules_nodejs//internal/linker:link_node_modules.bzl", "MODULE_MAPPINGS_ASPECT_RESULTS_NAME", "module_mappings_aspect")
88
load("@build_bazel_rules_nodejs//internal/common:expand_variables.bzl", "expand_variables")
9-
load("@build_bazel_rules_nodejs//packages/esbuild/toolchain:toolchain.bzl", "TOOLCHAIN")
9+
load("@build_bazel_rules_nodejs//toolchains/esbuild:toolchain.bzl", "TOOLCHAIN")
1010
load(":helpers.bzl", "desugar_entry_point_names", "filter_files", "generate_path_mapping", "resolve_entry_point", "write_args_file", "write_jsconfig_file")
1111

1212
def _esbuild_impl(ctx):

packages/esbuild/index.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ load(
2424
_esbuild_config = "esbuild_config",
2525
)
2626
load(
27-
"@build_bazel_rules_nodejs//packages/esbuild/toolchain:toolchain.bzl",
27+
"@build_bazel_rules_nodejs//toolchains/esbuild:toolchain.bzl",
2828
_configure_esbuild_toolchain = "configure_esbuild_toolchain",
2929
)
3030

packages/esbuild/index.docs.bzl

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,30 @@ or using yarn
1616
yarn add -D @bazel/esbuild
1717
```
1818
19-
The esbuild binary is fetched from npm automatically and exposed via toolchains. Add the `esbuild_repositories` rule to the `WORKSPACE`:
19+
The esbuild binary is fetched automatically for your platform and is exposed via Bazel toolchains.
20+
To do this, add the `esbuild_repositories` rule to your `WORKSPACE`.
21+
You'll need to point it to the repository created by npm_install or yarn_install where the `@bazel/esbuild`
22+
package is fetched. (Typically, this is `npm`).
23+
Set the `npm_repository` attribute to the name of that repository.
2024
2125
```python
22-
load("@npm//@bazel/esbuild:esbuild_repositories.bzl", "esbuild_repositories")
23-
24-
esbuild_repositories()
25-
```
26-
27-
As esbuild is being fetched from `npm`, the load statement above can cause eager fetches of the `@npm` external repository.
28-
To work around this, it's possible to fetch the `@bazel/esbuild` package via an `http_archive`
29-
30-
```python
31-
http_archive(
32-
name = "bazel_esbuild",
33-
urls = [
34-
"https://registry.npmjs.org/@bazel/esbuild/-/esbuild-4.0.0.tgz",
35-
],
36-
strip_prefix = "package",
26+
npm_install(
27+
name = "npm",
28+
# @bazel/esbuild is a dependency in this package.json
29+
package_json = "//:package.json",
30+
package_lock_json = "//:package-lock.json",
3731
)
3832
39-
load("@bazel_esbuild//:esbuild_repositories.bzl", "esbuild_repositories")
33+
load("@build_bazel_rules_nodejs//toolchains/esbuild:esbuild_repositories.bzl", "esbuild_repositories")
4034
41-
esbuild_repositories()
35+
esbuild_repositories(npm_repository = "npm") # Note, npm is the default value for npm_repository
4236
```
4337
38+
> To avoid eagerly fetching all the npm dependencies, this load statement comes from the "Built-in"
39+
> `@build_bazel_rules_nodejs` repository rather than from `@npm`.
40+
> In rules_nodejs 5.0 we intend to fix this layering violation by having the whole esbuild support
41+
> distributed independently of rules_nodejs, and not require any package to be installed from npm.
42+
4443
## Overview
4544
4645
The `esbuild` rule can take a JS or TS dependency tree and bundle it to a single file, or split across multiple files, outputting a directory.
@@ -97,11 +96,11 @@ load(
9796
_esbuild_config = "esbuild_config",
9897
)
9998
load(
100-
"@build_bazel_rules_nodejs//packages/esbuild:esbuild_repositories.bzl",
99+
"@build_bazel_rules_nodejs//toolchains/esbuild:esbuild_repositories.bzl",
101100
_esbuild_repositories = "esbuild_repositories",
102101
)
103102
load(
104-
"@build_bazel_rules_nodejs//packages/esbuild/toolchain:toolchain.bzl",
103+
"@build_bazel_rules_nodejs//toolchains/esbuild:toolchain.bzl",
105104
_configure_esbuild_toolchain = "configure_esbuild_toolchain",
106105
)
107106

scripts/update-esbuild-versions.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ async function main() {
8282
replaceFileContent('packages/esbuild/esbuild_packages.bzl', replacements);
8383

8484
// update package.json used for API wrapper
85-
replaceFileContent('packages/esbuild/toolchain/package.json', [[/"esbuild": "(.+?)"/, version]]);
86-
exec(`npm i --package-lock-only`, {silent: true, cwd: 'packages/esbuild/toolchain'});
85+
replaceFileContent('toolchains/esbuild/package.json', [[/"esbuild": "(.+?)"/, version]]);
86+
exec(`npm i --package-lock-only`, {silent: true, cwd: 'toolchains/esbuild'});
8787
}
8888

8989
main();

0 commit comments

Comments
 (0)