From 8e7c731d3fcbe7b97638b5e701c1837f353e6252 Mon Sep 17 00:00:00 2001 From: Matt Mackay Date: Sat, 6 Mar 2021 09:19:33 -0500 Subject: [PATCH] feat(esbuild): add max_threads setting to limit number of threads used --- packages/esbuild/esbuild.bzl | 13 ++++++++ packages/esbuild/test/node/BUILD.bazel | 39 +++++++++++++++++++++++ packages/esbuild/test/node/env.js | 3 ++ packages/esbuild/test/node/main.js | 6 ++++ packages/esbuild/test/node/out.golden.txt | 1 + 5 files changed, 62 insertions(+) create mode 100644 packages/esbuild/test/node/BUILD.bazel create mode 100644 packages/esbuild/test/node/env.js create mode 100644 packages/esbuild/test/node/main.js create mode 100644 packages/esbuild/test/node/out.golden.txt diff --git a/packages/esbuild/esbuild.bzl b/packages/esbuild/esbuild.bzl index 312d7cf889..f5d9aa5911 100644 --- a/packages/esbuild/esbuild.bzl +++ b/packages/esbuild/esbuild.bzl @@ -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, @@ -110,6 +114,8 @@ def _esbuild_impl(ctx): execution_requirements = { "no-remote-exec": "1", }, + mnemonic = "esbuild", + env = env, ) return [ @@ -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. diff --git a/packages/esbuild/test/node/BUILD.bazel b/packages/esbuild/test/node/BUILD.bazel new file mode 100644 index 0000000000..c7c3aced4c --- /dev/null +++ b/packages/esbuild/test/node/BUILD.bazel @@ -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", +) diff --git a/packages/esbuild/test/node/env.js b/packages/esbuild/test/node/env.js new file mode 100644 index 0000000000..bb1a68edd9 --- /dev/null +++ b/packages/esbuild/test/node/env.js @@ -0,0 +1,3 @@ +export function getEnvVars() { + return process.env; +} diff --git a/packages/esbuild/test/node/main.js b/packages/esbuild/test/node/main.js new file mode 100644 index 0000000000..6960524834 --- /dev/null +++ b/packages/esbuild/test/node/main.js @@ -0,0 +1,6 @@ +const {get} = require('https'); +const {getEnvVars} = require('./env'); + +const ESBUILD_TEST = getEnvVars().ESBUILD_TEST; + +console.log(`ESBUILD_TEST=${ESBUILD_TEST}`); \ No newline at end of file diff --git a/packages/esbuild/test/node/out.golden.txt b/packages/esbuild/test/node/out.golden.txt new file mode 100644 index 0000000000..4814c8a5c5 --- /dev/null +++ b/packages/esbuild/test/node/out.golden.txt @@ -0,0 +1 @@ +ESBUILD_TEST=YES