Skip to content

Commit ba7e48e

Browse files
authored
fix(esbuild): add link_workspace_root for workspace absolute imports (#2476)
Fixes #2474
1 parent b39669f commit ba7e48e

File tree

9 files changed

+75
-0
lines changed

9 files changed

+75
-0
lines changed

commitlint.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module.exports = {
1111
'concatjs',
1212
'create',
1313
'cypress',
14+
'esbuild',
1415
'examples',
1516
'jasmine',
1617
'labs',

packages/esbuild/esbuild.bzl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ def _esbuild_impl(ctx):
1515
# how to resolve custom package or module names
1616
path_alias_mappings = dict()
1717

18+
if (ctx.attr.link_workspace_root):
19+
path_alias_mappings.update(generate_path_mapping(ctx.workspace_name, "."))
20+
1821
for dep in ctx.attr.deps:
1922
if JSEcmaScriptModuleInfo in dep:
2023
deps_depsets.append(dep[JSEcmaScriptModuleInfo].sources)
@@ -152,6 +155,10 @@ and cjs when platform is node. If performing code splitting, defaults to esm.
152155
153156
See https://esbuild.github.io/api/#format for more details
154157
""",
158+
),
159+
"link_workspace_root": attr.bool(
160+
doc = """Link the workspace root to the bin_dir to support absolute requires like 'my_wksp/path/to/file'.
161+
If source files need to be required then they can be copied to the bin_dir with copy_to_bin.""",
155162
),
156163
"minify": attr.bool(
157164
default = False,
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
load("//:index.bzl", "generated_file_test")
2+
load("//packages/esbuild/test:tests.bzl", "esbuild")
3+
4+
esbuild(
5+
name = "bundle",
6+
entry_point = "main.js",
7+
format = "esm",
8+
link_workspace_root = True,
9+
deps = [
10+
"//packages/esbuild/test/workspace-mapping/module-one",
11+
"//packages/esbuild/test/workspace-mapping/module-two",
12+
],
13+
)
14+
15+
# esbuild will put the filepath in a comment within the non-minified file,
16+
# on different platforms this will cause the golden test to differ
17+
# strip them here
18+
# note that this regex doesn't strip the sourcemap URI comment
19+
genrule(
20+
name = "strip_bundle_comments",
21+
srcs = ["bundle.js"],
22+
outs = ["bundle.stripped.js"],
23+
cmd = "cat $(location :bundle.js) | sed \"s#// .*##\" > $@",
24+
)
25+
26+
generated_file_test(
27+
name = "bundle_test",
28+
src = "bundle.golden.txt",
29+
generated = "bundle.stripped.js",
30+
)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var __defProp = Object.defineProperty;
2+
var __name = (target, value) => __defProp(target, "name", {value, configurable: true});
3+
4+
5+
var getId = /* @__PURE__ */ __name(() => "module-one", "getId");
6+
7+
8+
var getId2 = /* @__PURE__ */ __name(() => "module-two", "getId");
9+
10+
11+
var main_default = `Full ID: ${getId} - ${getId2}`;
12+
export {
13+
main_default as default
14+
};
15+
//# sourceMappingURL=bundle.js.map
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import {getId as m1Id} from 'build_bazel_rules_nodejs/packages/esbuild/test/workspace-mapping/module-one';
2+
import {getId as m2Id} from 'build_bazel_rules_nodejs/packages/esbuild/test/workspace-mapping/module-two';
3+
4+
export default `Full ID: ${m1Id} - ${m2Id}`;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
load("//internal/js_library:js_library.bzl", "js_library")
2+
3+
package(default_visibility = ["//packages/esbuild/test:__subpackages__"])
4+
5+
js_library(
6+
name = "module-one",
7+
srcs = [":index.js"],
8+
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const getId = () => 'module-one';
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
load("//internal/js_library:js_library.bzl", "js_library")
2+
3+
package(default_visibility = ["//packages/esbuild/test:__subpackages__"])
4+
5+
js_library(
6+
name = "module-two",
7+
srcs = [":index.js"],
8+
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const getId = () => 'module-two';

0 commit comments

Comments
 (0)