Skip to content

Commit

Permalink
fix(esbuild): add link_workspace_root for workspace absolute imports
Browse files Browse the repository at this point in the history
  • Loading branch information
jbedard committed Feb 18, 2021
1 parent 9f9bb15 commit 72bcfde
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/esbuild/esbuild.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ def _esbuild_impl(ctx):
# how to resolve custom package or module names
path_alias_mappings = dict()

if (ctx.attr.link_workspace_root):
path_alias_mappings.update(generate_path_mapping(ctx.workspace_name, "/".join(ctx.build_file_path.split("/")[:-1])))

for dep in ctx.attr.deps:
if JSEcmaScriptModuleInfo in dep:
deps_depsets.append(dep[JSEcmaScriptModuleInfo].sources)
Expand Down Expand Up @@ -152,6 +155,10 @@ and cjs when platform is node. If performing code splitting, defaults to esm.
See https://esbuild.github.io/api/#format for more details
""",
),
"link_workspace_root": attr.bool(
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.""",
),
"minify": attr.bool(
default = False,
Expand Down
30 changes: 30 additions & 0 deletions packages/esbuild/test/workspace-mapping/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
load("//:index.bzl", "generated_file_test")
load("//packages/esbuild/test:tests.bzl", "esbuild")

esbuild(
name = "bundle",
entry_point = "main.js",
format = "esm",
link_workspace_root = True,
deps = [
"//packages/esbuild/test/workspace-mapping/module-one",
"//packages/esbuild/test/workspace-mapping/module-two",
],
)

# esbuild will put the filepath in a comment within the non-minified file,
# on different platforms this will cause the golden test to differ
# strip them here
# note that this regex doesn't strip the sourcemap URI comment
genrule(
name = "strip_bundle_comments",
srcs = ["bundle.js"],
outs = ["bundle.stripped.js"],
cmd = "cat $(location :bundle.js) | sed \"s#// .*##\" > $@",
)

generated_file_test(
name = "bundle_test",
src = "bundle.golden.txt",
generated = "bundle.stripped.js",
)
15 changes: 15 additions & 0 deletions packages/esbuild/test/workspace-mapping/bundle.golden.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", {value, configurable: true});


var getId = /* @__PURE__ */ __name(() => "module-one", "getId");


var getId2 = /* @__PURE__ */ __name(() => "module-two", "getId");


var main_default = `Full ID: ${getId} - ${getId2}`;
export {
main_default as default
};
//# sourceMappingURL=bundle.js.map
4 changes: 4 additions & 0 deletions packages/esbuild/test/workspace-mapping/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import {getId as m1Id} from 'build_bazel_rules_nodejs/module-one';
import {getId as m2Id} from 'build_bazel_rules_nodejs/module-two';

export default `Full ID: ${m1Id} - ${m2Id}`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
load("//internal/js_library:js_library.bzl", "js_library")

package(default_visibility = ["//packages/esbuild/test:__subpackages__"])

js_library(
name = "module-one",
srcs = [":index.js"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const getId = () => 'module-one';
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
load("//internal/js_library:js_library.bzl", "js_library")

package(default_visibility = ["//packages/esbuild/test:__subpackages__"])

js_library(
name = "module-two",
srcs = [":index.js"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const getId = () => 'module-two';

0 comments on commit 72bcfde

Please sign in to comment.