Skip to content

Commit 8e7c731

Browse files
mattemalexeagle
authored andcommitted
feat(esbuild): add max_threads setting to limit number of threads used
1 parent 92e8169 commit 8e7c731

File tree

5 files changed

+62
-0
lines changed

5 files changed

+62
-0
lines changed

packages/esbuild/esbuild.bzl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ def _esbuild_impl(ctx):
101101

102102
args.add_all(ctx.attr.args)
103103

104+
env = {}
105+
if ctx.attr.max_threads > 0:
106+
env["GOMAXPROCS"] = str(ctx.attr.max_threads)
107+
104108
ctx.actions.run(
105109
inputs = inputs,
106110
outputs = outputs,
@@ -110,6 +114,8 @@ def _esbuild_impl(ctx):
110114
execution_requirements = {
111115
"no-remote-exec": "1",
112116
},
117+
mnemonic = "esbuild",
118+
env = env,
113119
)
114120

115121
return [
@@ -168,6 +174,13 @@ See https://esbuild.github.io/api/#format for more details
168174
doc = """Link the workspace root to the bin_dir to support absolute requires like 'my_wksp/path/to/file'.
169175
If source files need to be required then they can be copied to the bin_dir with copy_to_bin.""",
170176
),
177+
"max_threads": attr.int(
178+
mandatory = False,
179+
doc = """Sets the `GOMAXPROCS` variable to limit the number of threads that esbuild can run with.
180+
This can be useful if running many esbuild rule invocations in parallel, which has the potential to cause slowdown.
181+
For general use, leave this attribute unset.
182+
""",
183+
),
171184
"minify": attr.bool(
172185
default = False,
173186
doc = """Minifies the bundle with the built in minification.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
load("//:index.bzl", "generated_file_test", "nodejs_binary", "npm_package_bin")
2+
load("//packages/esbuild/test:tests.bzl", "esbuild")
3+
4+
esbuild(
5+
name = "bundle",
6+
# JS sources can be set directly on the esbuild rule
7+
srcs = [
8+
"env.js",
9+
"main.js",
10+
],
11+
entry_point = "main.js",
12+
# Setting this to test the code path
13+
# This isn't needed for this bundle to run
14+
max_threads = 1,
15+
platform = "node",
16+
)
17+
18+
nodejs_binary(
19+
name = "bin",
20+
data = [
21+
":bundle",
22+
],
23+
entry_point = "bundle.js",
24+
)
25+
26+
npm_package_bin(
27+
name = "runner",
28+
env = {
29+
"ESBUILD_TEST": "YES",
30+
},
31+
stdout = "out.txt",
32+
tool = ":bin",
33+
)
34+
35+
generated_file_test(
36+
name = "test",
37+
src = "out.golden.txt",
38+
generated = "out.txt",
39+
)

packages/esbuild/test/node/env.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function getEnvVars() {
2+
return process.env;
3+
}

packages/esbuild/test/node/main.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const {get} = require('https');
2+
const {getEnvVars} = require('./env');
3+
4+
const ESBUILD_TEST = getEnvVars().ESBUILD_TEST;
5+
6+
console.log(`ESBUILD_TEST=${ESBUILD_TEST}`);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ESBUILD_TEST=YES

0 commit comments

Comments
 (0)