Skip to content

Commit be184c2

Browse files
mattemalexeagle
authored andcommitted
fix(esbuild): use run_node to invoke linker before running esuild
1 parent 1c50e96 commit be184c2

File tree

9 files changed

+78
-29
lines changed

9 files changed

+78
-29
lines changed

examples/esbuild/WORKSPACE

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ http_archive(
2525
urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/3.3.0/rules_nodejs-3.3.0.tar.gz"],
2626
)
2727

28-
_ESBUILD_VERSION = "0.11.5"
28+
_ESBUILD_VERSION = "0.11.6"
2929

3030
http_archive(
3131
name = "esbuild_darwin",
3232
build_file_content = """exports_files(["bin/esbuild"])""",
33-
sha256 = "98436890727bdb0d4beddd9c9e07d0aeff0e8dfe0169f85e568eca0dd43f665e",
33+
sha256 = "2b06365b075b854654fc9ed26fcd48a0c38947e1c8d5151ce400cd1e173bb138",
3434
strip_prefix = "package",
3535
urls = [
3636
"https://registry.npmjs.org/esbuild-darwin-64/-/esbuild-darwin-64-%s.tgz" % _ESBUILD_VERSION,
@@ -40,7 +40,7 @@ http_archive(
4040
http_archive(
4141
name = "esbuild_windows",
4242
build_file_content = """exports_files(["esbuild.exe"])""",
43-
sha256 = "589c8ff97210bd41de106e6317ce88f9e88d2cacfd8178ae1217f2b857ff6c3a",
43+
sha256 = "ddab1121833f0a12ca4fb3e288231e058f5526310671e84c0a9aa575340bb20b",
4444
strip_prefix = "package",
4545
urls = [
4646
"https://registry.npmjs.org/esbuild-windows-64/-/esbuild-windows-64-%s.tgz" % _ESBUILD_VERSION,
@@ -50,7 +50,7 @@ http_archive(
5050
http_archive(
5151
name = "esbuild_linux",
5252
build_file_content = """exports_files(["bin/esbuild"])""",
53-
sha256 = "113c2e84895f4422a3676db4e15d9f01b2b4fac041edab25284fdb9574ba58a0",
53+
sha256 = "34612e3e15e6c31d9d742d3fd677bd5208b7e5c0ee9c93809999138c6c5c1039",
5454
strip_prefix = "package",
5555
urls = [
5656
"https://registry.npmjs.org/esbuild-linux-64/-/esbuild-linux-64-%s.tgz" % _ESBUILD_VERSION,

packages/esbuild/BUILD.bazel

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ load("//third_party/github.com/bazelbuild/bazel-skylib:rules/copy_file.bzl", "co
1919

2020
package(default_visibility = ["//visibility:public"])
2121

22+
exports_files(["launcher.js"])
23+
2224
codeowners(
2325
teams = ["@mattem"],
2426
)
@@ -67,11 +69,14 @@ pkg_npm(
6769
"esbuild.bzl",
6870
"helpers.bzl",
6971
"index.bzl",
72+
"launcher.js",
7073
"package.json",
7174
],
72-
build_file_content = " ",
75+
build_file_content = """exports_files(["launcher.js"])
76+
""",
7377
substitutions = dict({
7478
"@build_bazel_rules_nodejs//packages/esbuild:esbuild.bzl": "//@bazel/esbuild:esbuild.bzl",
79+
"@build_bazel_rules_nodejs//packages/esbuild:launcher.js": "//@bazel/esbuild:launcher.js",
7580
}),
7681
deps = [
7782
":npm_version_check",

packages/esbuild/_README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ yarn add -D @bazel/esbuild
1818
Add an `http_archive` fetching the esbuild binary for each platform that you need to support.
1919

2020
```python
21-
_ESBUILD_VERSION = "0.11.5" # reminder: update SHAs below when changing this value
21+
_ESBUILD_VERSION = "0.11.6" # reminder: update SHAs below when changing this value
2222
http_archive(
2323
name = "esbuild_darwin",
2424
urls = [
2525
"https://registry.npmjs.org/esbuild-darwin-64/-/esbuild-darwin-64-%s.tgz" % _ESBUILD_VERSION,
2626
],
2727
strip_prefix = "package",
2828
build_file_content = """exports_files(["bin/esbuild"])""",
29-
sha256 = "98436890727bdb0d4beddd9c9e07d0aeff0e8dfe0169f85e568eca0dd43f665e",
29+
sha256 = "2b06365b075b854654fc9ed26fcd48a0c38947e1c8d5151ce400cd1e173bb138",
3030
)
3131

3232
http_archive(
@@ -36,7 +36,7 @@ http_archive(
3636
],
3737
strip_prefix = "package",
3838
build_file_content = """exports_files(["esbuild.exe"])""",
39-
sha256 = "589c8ff97210bd41de106e6317ce88f9e88d2cacfd8178ae1217f2b857ff6c3a",
39+
sha256 = "ddab1121833f0a12ca4fb3e288231e058f5526310671e84c0a9aa575340bb20b",
4040
)
4141

4242
http_archive(
@@ -46,7 +46,7 @@ http_archive(
4646
],
4747
strip_prefix = "package",
4848
build_file_content = """exports_files(["bin/esbuild"])""",
49-
sha256 = "113c2e84895f4422a3676db4e15d9f01b2b4fac041edab25284fdb9574ba58a0",
49+
sha256 = "34612e3e15e6c31d9d742d3fd677bd5208b7e5c0ee9c93809999138c6c5c1039",
5050
)
5151
```
5252

packages/esbuild/esbuild.bzl

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
esbuild rule
33
"""
44

5-
load("@build_bazel_rules_nodejs//:providers.bzl", "JSEcmaScriptModuleInfo", "JSModuleInfo", "NpmPackageInfo", "node_modules_aspect")
5+
load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_binary")
6+
load("@build_bazel_rules_nodejs//:providers.bzl", "JSEcmaScriptModuleInfo", "JSModuleInfo", "NpmPackageInfo", "node_modules_aspect", "run_node")
67
load("@build_bazel_rules_nodejs//internal/linker:link_node_modules.bzl", "MODULE_MAPPINGS_ASPECT_RESULTS_NAME", "module_mappings_aspect")
78
load(":helpers.bzl", "filter_files", "generate_path_mapping", "resolve_entry_point", "write_jsconfig_file")
89

@@ -14,10 +15,6 @@ def _esbuild_impl(ctx):
1415
# Path alias mapings are used to create a jsconfig with mappings so that esbuild
1516
# how to resolve custom package or module names
1617
path_alias_mappings = dict()
17-
npm_workspaces = []
18-
19-
if (ctx.attr.link_workspace_root):
20-
path_alias_mappings.update(generate_path_mapping(ctx.workspace_name, "."))
2118

2219
for dep in ctx.attr.deps:
2320
if JSEcmaScriptModuleInfo in dep:
@@ -33,19 +30,12 @@ def _esbuild_impl(ctx):
3330

3431
if NpmPackageInfo in dep:
3532
deps_depsets.append(dep[NpmPackageInfo].sources)
36-
npm_workspaces.append(dep[NpmPackageInfo].workspace)
3733

3834
# Collect the path alias mapping to resolve packages correctly
3935
if hasattr(dep, MODULE_MAPPINGS_ASPECT_RESULTS_NAME):
4036
for key, value in getattr(dep, MODULE_MAPPINGS_ASPECT_RESULTS_NAME).items():
4137
path_alias_mappings.update(generate_path_mapping(key, value[1].replace(ctx.bin_dir.path + "/", "")))
4238

43-
node_modules_mappings = [
44-
"../../../external/%s/node_modules/*" % workspace
45-
for workspace in depset(npm_workspaces).to_list()
46-
]
47-
path_alias_mappings.update({"*": node_modules_mappings})
48-
4939
deps_inputs = depset(transitive = deps_depsets).to_list()
5040
inputs = filter_files(ctx.files.entry_point) + ctx.files.srcs + deps_inputs
5141

@@ -55,6 +45,7 @@ def _esbuild_impl(ctx):
5545
entry_point = resolve_entry_point(ctx.file.entry_point, inputs, ctx.files.srcs)
5646

5747
args = ctx.actions.args()
48+
args.use_param_file(param_file_arg = "--esbuild_flags=%s", use_always = True)
5849

5950
args.add("--bundle", entry_point.path)
6051

@@ -125,19 +116,25 @@ def _esbuild_impl(ctx):
125116
if "no-remote-exec" in ctx.attr.tags:
126117
execution_requirements = {"no-remote-exec": "1"}
127118

128-
ctx.actions.run(
119+
launcher_args = ctx.actions.args()
120+
launcher_args.add("--esbuild=%s" % ctx.executable.tool.path)
121+
122+
run_node(
123+
ctx = ctx,
129124
inputs = depset(inputs),
130125
outputs = outputs,
131-
executable = ctx.executable.tool,
132-
arguments = [args],
126+
arguments = [launcher_args, args],
133127
progress_message = "%s Javascript %s [esbuild]" % ("Bundling" if not ctx.attr.output_dir else "Splitting", entry_point.short_path),
134128
execution_requirements = execution_requirements,
135129
mnemonic = "esbuild",
136130
env = env,
131+
executable = "launcher",
132+
link_workspace_root = ctx.attr.link_workspace_root,
133+
tools = [ctx.executable.tool],
137134
)
138135

139136
return [
140-
DefaultInfo(files = depset(outputs + [jsconfig_file])),
137+
DefaultInfo(files = depset(outputs)),
141138
]
142139

143140
esbuild = rule(
@@ -189,6 +186,12 @@ and cjs when platform is node. If performing code splitting, defaults to esm.
189186
See https://esbuild.github.io/api/#format for more details
190187
""",
191188
),
189+
"launcher": attr.label(
190+
mandatory = True,
191+
executable = True,
192+
doc = "Internal use only",
193+
cfg = "exec",
194+
),
192195
"link_workspace_root": attr.bool(
193196
doc = """Link the workspace root to the bin_dir to support absolute requires like 'my_wksp/path/to/file'.
194197
If source files need to be required then they can be copied to the bin_dir with copy_to_bin.""",
@@ -296,10 +299,18 @@ def esbuild_macro(name, output_dir = False, **kwargs):
296299
**kwargs: All other args from `esbuild_bundle`
297300
"""
298301

302+
kwargs.pop("launcher", None)
303+
_launcher = "_%s_esbuild_launcher" % name
304+
nodejs_binary(
305+
name = _launcher,
306+
entry_point = Label("@build_bazel_rules_nodejs//packages/esbuild:launcher.js"),
307+
)
308+
299309
if output_dir == True:
300310
esbuild(
301311
name = name,
302312
output_dir = True,
313+
launcher = _launcher,
303314
**kwargs
304315
)
305316
else:
@@ -316,5 +327,6 @@ def esbuild_macro(name, output_dir = False, **kwargs):
316327
name = name,
317328
output = output,
318329
output_map = output_map,
330+
launcher = _launcher,
319331
**kwargs
320332
)

packages/esbuild/esbuild_repo.bzl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Helper macro for fetching esbuild versions for internal tests and examples in ru
66

77
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
88

9-
_VERSION = "0.11.5"
9+
_VERSION = "0.11.6"
1010

1111
def esbuild_dependencies():
1212
"""Helper to install required dependencies for the esbuild rules"""
@@ -20,7 +20,7 @@ def esbuild_dependencies():
2020
],
2121
strip_prefix = "package",
2222
build_file_content = """exports_files(["bin/esbuild"])""",
23-
sha256 = "98436890727bdb0d4beddd9c9e07d0aeff0e8dfe0169f85e568eca0dd43f665e",
23+
sha256 = "2b06365b075b854654fc9ed26fcd48a0c38947e1c8d5151ce400cd1e173bb138",
2424
)
2525
http_archive(
2626
name = "esbuild_windows",
@@ -29,7 +29,7 @@ def esbuild_dependencies():
2929
],
3030
strip_prefix = "package",
3131
build_file_content = """exports_files(["esbuild.exe"])""",
32-
sha256 = "589c8ff97210bd41de106e6317ce88f9e88d2cacfd8178ae1217f2b857ff6c3a",
32+
sha256 = "ddab1121833f0a12ca4fb3e288231e058f5526310671e84c0a9aa575340bb20b",
3333
)
3434
http_archive(
3535
name = "esbuild_linux",
@@ -38,5 +38,5 @@ def esbuild_dependencies():
3838
],
3939
strip_prefix = "package",
4040
build_file_content = """exports_files(["bin/esbuild"])""",
41-
sha256 = "113c2e84895f4422a3676db4e15d9f01b2b4fac041edab25284fdb9574ba58a0",
41+
sha256 = "34612e3e15e6c31d9d742d3fd677bd5208b7e5c0ee9c93809999138c6c5c1039",
4242
)

packages/esbuild/launcher.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const {spawn} = require('child_process');
2+
const {readFileSync} = require('fs');
3+
4+
function getFlag(flag, required = true) {
5+
const argvFlag = process.argv.find(arg => arg.startsWith(`${flag}=`));
6+
if (required && !argvFlag) {
7+
console.error(`Expected flag '${flag}' passed to launcher, but not found`);
8+
process.exit(1);
9+
}
10+
11+
return argvFlag.split('=')[1];
12+
}
13+
14+
const esbuild = getFlag('--esbuild');
15+
const params_file_path = getFlag('--esbuild_flags');
16+
17+
let flags = [];
18+
try {
19+
flags = readFileSync(params_file_path, {encoding: 'utf8'}).trim().replace(/'/gm, '').split('\n');
20+
} catch (e) {
21+
console.error('Error while reading esbuild flags param file', e);
22+
process.exit(1);
23+
}
24+
25+
spawn(esbuild, flags, {detached: true, stdio: 'inherit'});

packages/esbuild/test/vanilla_js/BUILD.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ esbuild(
88
"name.js",
99
],
1010
entry_point = "main.js",
11+
deps = [
12+
"@npm//date-fns",
13+
],
1114
)
1215

1316
nodejs_binary(
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
const NAME = require('./name').NAME;
2+
const {isDate} = require('date-fns');
3+
4+
console.log(isDate(NAME));
25
console.log(NAME);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
false
12
rules_nodejs

0 commit comments

Comments
 (0)