Skip to content

Commit

Permalink
feat(esbuild): add max_threads setting to limit number of threads used
Browse files Browse the repository at this point in the history
  • Loading branch information
mattem authored and alexeagle committed Mar 7, 2021
1 parent 92e8169 commit 8e7c731
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 0 deletions.
13 changes: 13 additions & 0 deletions packages/esbuild/esbuild.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ def _esbuild_impl(ctx):

args.add_all(ctx.attr.args)

env = {}
if ctx.attr.max_threads > 0:
env["GOMAXPROCS"] = str(ctx.attr.max_threads)

ctx.actions.run(
inputs = inputs,
outputs = outputs,
Expand All @@ -110,6 +114,8 @@ def _esbuild_impl(ctx):
execution_requirements = {
"no-remote-exec": "1",
},
mnemonic = "esbuild",
env = env,
)

return [
Expand Down Expand Up @@ -168,6 +174,13 @@ See https://esbuild.github.io/api/#format for more details
doc = """Link the workspace root to the bin_dir to support absolute requires like 'my_wksp/path/to/file'.
If source files need to be required then they can be copied to the bin_dir with copy_to_bin.""",
),
"max_threads": attr.int(
mandatory = False,
doc = """Sets the `GOMAXPROCS` variable to limit the number of threads that esbuild can run with.
This can be useful if running many esbuild rule invocations in parallel, which has the potential to cause slowdown.
For general use, leave this attribute unset.
""",
),
"minify": attr.bool(
default = False,
doc = """Minifies the bundle with the built in minification.
Expand Down
39 changes: 39 additions & 0 deletions packages/esbuild/test/node/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
load("//:index.bzl", "generated_file_test", "nodejs_binary", "npm_package_bin")
load("//packages/esbuild/test:tests.bzl", "esbuild")

esbuild(
name = "bundle",
# JS sources can be set directly on the esbuild rule
srcs = [
"env.js",
"main.js",
],
entry_point = "main.js",
# Setting this to test the code path
# This isn't needed for this bundle to run
max_threads = 1,
platform = "node",
)

nodejs_binary(
name = "bin",
data = [
":bundle",
],
entry_point = "bundle.js",
)

npm_package_bin(
name = "runner",
env = {
"ESBUILD_TEST": "YES",
},
stdout = "out.txt",
tool = ":bin",
)

generated_file_test(
name = "test",
src = "out.golden.txt",
generated = "out.txt",
)
3 changes: 3 additions & 0 deletions packages/esbuild/test/node/env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function getEnvVars() {
return process.env;
}
6 changes: 6 additions & 0 deletions packages/esbuild/test/node/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const {get} = require('https');
const {getEnvVars} = require('./env');

const ESBUILD_TEST = getEnvVars().ESBUILD_TEST;

console.log(`ESBUILD_TEST=${ESBUILD_TEST}`);
1 change: 1 addition & 0 deletions packages/esbuild/test/node/out.golden.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ESBUILD_TEST=YES

0 comments on commit 8e7c731

Please sign in to comment.