From eb712851105fc146ff852d5e159603d4225f9806 Mon Sep 17 00:00:00 2001 From: Matt Mackay Date: Thu, 18 Feb 2021 10:15:29 -0500 Subject: [PATCH] fix(esbuild): add --preserve-symlinks flag by default --- examples/esbuild/WORKSPACE | 8 ++++---- packages/esbuild/esbuild.bzl | 9 +++++++++ packages/esbuild/esbuild_repo.bzl | 8 ++++---- packages/esbuild/test/typescript/BUILD.bazel | 2 ++ .../esbuild/test/typescript/bundle.golden.txt | 14 ++++++++++---- .../test/typescript/generated-module/BUILD.bazel | 15 +++++++++++++++ packages/esbuild/test/typescript/main.ts | 7 ++++++- .../test/typescript/relative-module/BUILD.bazel | 9 +++++++++ .../test/typescript/relative-module/lib.ts | 1 + 9 files changed, 60 insertions(+), 13 deletions(-) create mode 100644 packages/esbuild/test/typescript/generated-module/BUILD.bazel create mode 100644 packages/esbuild/test/typescript/relative-module/BUILD.bazel create mode 100644 packages/esbuild/test/typescript/relative-module/lib.ts diff --git a/examples/esbuild/WORKSPACE b/examples/esbuild/WORKSPACE index 1aa1b4ef9a..bd06cc0311 100644 --- a/examples/esbuild/WORKSPACE +++ b/examples/esbuild/WORKSPACE @@ -25,12 +25,12 @@ http_archive( urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/3.2.0/rules_nodejs-3.2.0.tar.gz"], ) -_ESBUILD_VERSION = "0.8.34" +_ESBUILD_VERSION = "0.8.48" http_archive( name = "esbuild_darwin", build_file_content = """exports_files(["bin/esbuild"])""", - sha256 = "3bf980b5175df873dd84fd614d57722f3b1b9c7e74929504e26192d23075d5c3", + sha256 = "d21a722873ed24586f071973b77223553fca466946f3d7e3976eeaccb14424e6", strip_prefix = "package", urls = [ "https://registry.npmjs.org/esbuild-darwin-64/-/esbuild-darwin-64-%s.tgz" % _ESBUILD_VERSION, @@ -40,7 +40,7 @@ http_archive( http_archive( name = "esbuild_windows", build_file_content = """exports_files(["esbuild.exe"])""", - sha256 = "826cd58553e7b6910dd22aba001cd72af34e05c9c3e9af567b5b2a6b1c9f3941", + sha256 = "fe5dcb97b4c47f9567012f0a45c19c655f3d2e0d76932f6dd12715dbebbd6eb0", strip_prefix = "package", urls = [ "https://registry.npmjs.org/esbuild-windows-64/-/esbuild-windows-64-%s.tgz" % _ESBUILD_VERSION, @@ -50,7 +50,7 @@ http_archive( http_archive( name = "esbuild_linux", build_file_content = """exports_files(["bin/esbuild"])""", - sha256 = "9dff3f5b06fd964a1cbb6aa9ea5ebf797767f1bd2bac71e084fb0bbefeba24a3", + sha256 = "60dabe141e5dfcf99e7113bded6012868132068a582a102b258fb7b1cfdac14b", strip_prefix = "package", urls = [ "https://registry.npmjs.org/esbuild-linux-64/-/esbuild-linux-64-%s.tgz" % _ESBUILD_VERSION, diff --git a/packages/esbuild/esbuild.bzl b/packages/esbuild/esbuild.bzl index 92238c116f..a17f1b9991 100644 --- a/packages/esbuild/esbuild.bzl +++ b/packages/esbuild/esbuild.bzl @@ -14,6 +14,7 @@ def _esbuild_impl(ctx): # Path alias mapings are used to create a jsconfig with mappings so that esbuild # how to resolve custom package or module names path_alias_mappings = dict() + npm_workspaces = [] if (ctx.attr.link_workspace_root): path_alias_mappings.update(generate_path_mapping(ctx.workspace_name, ".")) @@ -29,12 +30,19 @@ def _esbuild_impl(ctx): if NpmPackageInfo in dep: deps_depsets.append(dep[NpmPackageInfo].sources) + npm_workspaces.append(dep[NpmPackageInfo].workspace) # Collect the path alias mapping to resolve packages correctly if hasattr(dep, MODULE_MAPPINGS_ASPECT_RESULTS_NAME): for key, value in getattr(dep, MODULE_MAPPINGS_ASPECT_RESULTS_NAME).items(): path_alias_mappings.update(generate_path_mapping(key, value[1].replace(ctx.bin_dir.path + "/", ""))) + node_modules_mappings = [ + "../../../external/%s/node_modules/*" % workspace + for workspace in depset(npm_workspaces).to_list() + ] + path_alias_mappings.update({"*": node_modules_mappings}) + deps_inputs = depset(transitive = deps_depsets).to_list() inputs = filter_files(ctx.files.entry_point, [".mjs", ".js"]) + ctx.files.srcs + deps_inputs @@ -48,6 +56,7 @@ def _esbuild_impl(ctx): args.add("--bundle", entry_point.path) args.add("--sourcemap") args.add("--keep-names") + args.add("--preserve-symlinks") args.add_joined(["--platform", ctx.attr.platform], join_with = "=") args.add_joined(["--target", ctx.attr.target], join_with = "=") args.add_joined(["--log-level", "info"], join_with = "=") diff --git a/packages/esbuild/esbuild_repo.bzl b/packages/esbuild/esbuild_repo.bzl index 99a3fbc660..869d0f93fd 100644 --- a/packages/esbuild/esbuild_repo.bzl +++ b/packages/esbuild/esbuild_repo.bzl @@ -4,7 +4,7 @@ Helper macro for fetching esbuild versions for internal tests and examples in ru load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") -_VERSION = "0.8.34" +_VERSION = "0.8.48" def esbuild_dependencies(): """Helper to install required dependencies for the esbuild rules""" @@ -18,7 +18,7 @@ def esbuild_dependencies(): ], strip_prefix = "package", build_file_content = """exports_files(["bin/esbuild"])""", - sha256 = "3bf980b5175df873dd84fd614d57722f3b1b9c7e74929504e26192d23075d5c3", + sha256 = "d21a722873ed24586f071973b77223553fca466946f3d7e3976eeaccb14424e6", ) http_archive( @@ -28,7 +28,7 @@ def esbuild_dependencies(): ], strip_prefix = "package", build_file_content = """exports_files(["esbuild.exe"])""", - sha256 = "826cd58553e7b6910dd22aba001cd72af34e05c9c3e9af567b5b2a6b1c9f3941", + sha256 = "fe5dcb97b4c47f9567012f0a45c19c655f3d2e0d76932f6dd12715dbebbd6eb0", ) http_archive( @@ -38,5 +38,5 @@ def esbuild_dependencies(): ], strip_prefix = "package", build_file_content = """exports_files(["bin/esbuild"])""", - sha256 = "9dff3f5b06fd964a1cbb6aa9ea5ebf797767f1bd2bac71e084fb0bbefeba24a3", + sha256 = "60dabe141e5dfcf99e7113bded6012868132068a582a102b258fb7b1cfdac14b", ) diff --git a/packages/esbuild/test/typescript/BUILD.bazel b/packages/esbuild/test/typescript/BUILD.bazel index b48a33f09f..f5e4efae23 100644 --- a/packages/esbuild/test/typescript/BUILD.bazel +++ b/packages/esbuild/test/typescript/BUILD.bazel @@ -9,9 +9,11 @@ ts_library( srcs = [":main.ts"], tsconfig = ":tsconfig.json", deps = [ + "//packages/esbuild/test/typescript/generated-module", "//packages/esbuild/test/typescript/module-dynamic", "//packages/esbuild/test/typescript/module-one", "//packages/esbuild/test/typescript/module-two", + "//packages/esbuild/test/typescript/relative-module", ], ) diff --git a/packages/esbuild/test/typescript/bundle.golden.txt b/packages/esbuild/test/typescript/bundle.golden.txt index 80f6bfdc08..9039c1c4c2 100644 --- a/packages/esbuild/test/typescript/bundle.golden.txt +++ b/packages/esbuild/test/typescript/bundle.golden.txt @@ -14,7 +14,6 @@ var __commonJS = (callback, module) => () => { return module.exports; }; var __exportStar = (target, module, desc) => { - __markAsModule(target); if (module && typeof module === "object" || typeof module === "function") { for (let key of __getOwnPropNames(module)) if (!__hasOwnProp.call(target, key) && key !== "default") @@ -25,7 +24,7 @@ var __exportStar = (target, module, desc) => { var __toModule = (module) => { if (module && module.__esModule) return module; - return __exportStar(__defProp(module != null ? __create(__getProtoOf(module)) : {}, "default", {value: module, enumerable: true}), module); + return __exportStar(__markAsModule(__defProp(module != null ? __create(__getProtoOf(module)) : {}, "default", {value: module, enumerable: true})), module); }; @@ -55,8 +54,15 @@ var getId = /* @__PURE__ */ __name(() => "module-one", "getId"); var getId2 = /* @__PURE__ */ __name(() => "module-two", "getId"); -var main_default = `Full ID: ${getId} - ${getId2} - ${import_module_dynamic.getId}`; +var getId3 = /* @__PURE__ */ __name(() => `generated-module`, "getId"); + + +var getId4 = /* @__PURE__ */ __name(() => "relative-module", "getId"); + + +var ID = `Full ID: ${getId()} - ${getId2()} - ${import_module_dynamic.getId()} - ${getId4()} - ${getId3()}`; +console.log(ID); export { - main_default as default + ID }; //# sourceMappingURL=bundle.js.map diff --git a/packages/esbuild/test/typescript/generated-module/BUILD.bazel b/packages/esbuild/test/typescript/generated-module/BUILD.bazel new file mode 100644 index 0000000000..273c043c94 --- /dev/null +++ b/packages/esbuild/test/typescript/generated-module/BUILD.bazel @@ -0,0 +1,15 @@ +load("//packages/typescript:index.bzl", "ts_library") + +package(default_visibility = ["//packages/esbuild/test:__subpackages__"]) + +genrule( + name = "lib", + outs = ["lib.ts"], + cmd = "echo 'export const getId = () => `generated-module`;' > $@", +) + +ts_library( + name = "generated-module", + srcs = ["lib.ts"], + tsconfig = "//packages/esbuild/test/typescript:tsconfig.json", +) diff --git a/packages/esbuild/test/typescript/main.ts b/packages/esbuild/test/typescript/main.ts index e2359f95d6..ca2a26d62b 100644 --- a/packages/esbuild/test/typescript/main.ts +++ b/packages/esbuild/test/typescript/main.ts @@ -2,4 +2,9 @@ import {getId as mdyn} from '@typescript/module-dynamic'; import {getId as m1Id} from '@typescript/module-one'; import {getId as m2Id} from '@typescript/module-two'; -export default `Full ID: ${m1Id} - ${m2Id} - ${mdyn}`; +import {getId as mGenId} from './generated-module/lib'; +import {getId as mRelId} from './relative-module/lib'; + +export const ID = `Full ID: ${m1Id()} - ${m2Id()} - ${mdyn()} - ${mRelId()} - ${mGenId()}`; + +console.log(ID); diff --git a/packages/esbuild/test/typescript/relative-module/BUILD.bazel b/packages/esbuild/test/typescript/relative-module/BUILD.bazel new file mode 100644 index 0000000000..1fdf5c2114 --- /dev/null +++ b/packages/esbuild/test/typescript/relative-module/BUILD.bazel @@ -0,0 +1,9 @@ +load("//packages/typescript:index.bzl", "ts_library") + +package(default_visibility = ["//packages/esbuild/test:__subpackages__"]) + +ts_library( + name = "relative-module", + srcs = ["lib.ts"], + tsconfig = "//packages/esbuild/test/typescript:tsconfig.json", +) diff --git a/packages/esbuild/test/typescript/relative-module/lib.ts b/packages/esbuild/test/typescript/relative-module/lib.ts new file mode 100644 index 0000000000..61fbbeef52 --- /dev/null +++ b/packages/esbuild/test/typescript/relative-module/lib.ts @@ -0,0 +1 @@ +export const getId = () => 'relative-module';