diff --git a/packages/esbuild/esbuild.bzl b/packages/esbuild/esbuild.bzl index bbcf5c792e..5bfb3e4c72 100644 --- a/packages/esbuild/esbuild.bzl +++ b/packages/esbuild/esbuild.bzl @@ -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) @@ -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, diff --git a/packages/esbuild/test/workspace-mapping/BUILD.bazel b/packages/esbuild/test/workspace-mapping/BUILD.bazel new file mode 100644 index 0000000000..1210edcda8 --- /dev/null +++ b/packages/esbuild/test/workspace-mapping/BUILD.bazel @@ -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", +) diff --git a/packages/esbuild/test/workspace-mapping/bundle.golden.txt b/packages/esbuild/test/workspace-mapping/bundle.golden.txt new file mode 100644 index 0000000000..19ed8a39ab --- /dev/null +++ b/packages/esbuild/test/workspace-mapping/bundle.golden.txt @@ -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 diff --git a/packages/esbuild/test/workspace-mapping/main.js b/packages/esbuild/test/workspace-mapping/main.js new file mode 100644 index 0000000000..9aacb9fb5f --- /dev/null +++ b/packages/esbuild/test/workspace-mapping/main.js @@ -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}`; diff --git a/packages/esbuild/test/workspace-mapping/module-one/BUILD.bazel b/packages/esbuild/test/workspace-mapping/module-one/BUILD.bazel new file mode 100644 index 0000000000..b2ce25f4bb --- /dev/null +++ b/packages/esbuild/test/workspace-mapping/module-one/BUILD.bazel @@ -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"], +) diff --git a/packages/esbuild/test/workspace-mapping/module-one/index.js b/packages/esbuild/test/workspace-mapping/module-one/index.js new file mode 100644 index 0000000000..be8c740ce1 --- /dev/null +++ b/packages/esbuild/test/workspace-mapping/module-one/index.js @@ -0,0 +1 @@ +export const getId = () => 'module-one'; diff --git a/packages/esbuild/test/workspace-mapping/module-two/BUILD.bazel b/packages/esbuild/test/workspace-mapping/module-two/BUILD.bazel new file mode 100644 index 0000000000..b5818043c3 --- /dev/null +++ b/packages/esbuild/test/workspace-mapping/module-two/BUILD.bazel @@ -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"], +) diff --git a/packages/esbuild/test/workspace-mapping/module-two/index.js b/packages/esbuild/test/workspace-mapping/module-two/index.js new file mode 100644 index 0000000000..4fa928033d --- /dev/null +++ b/packages/esbuild/test/workspace-mapping/module-two/index.js @@ -0,0 +1 @@ +export const getId = () => 'module-two';