Skip to content

Commit

Permalink
fix(esbuild): add --preserve-symlinks flag by default
Browse files Browse the repository at this point in the history
  • Loading branch information
mattem authored and alexeagle committed Feb 19, 2021
1 parent ba7e48e commit eb71285
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 13 deletions.
8 changes: 4 additions & 4 deletions examples/esbuild/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions packages/esbuild/esbuild.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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, "."))
Expand All @@ -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

Expand All @@ -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 = "=")
Expand Down
8 changes: 4 additions & 4 deletions packages/esbuild/esbuild_repo.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand All @@ -18,7 +18,7 @@ def esbuild_dependencies():
],
strip_prefix = "package",
build_file_content = """exports_files(["bin/esbuild"])""",
sha256 = "3bf980b5175df873dd84fd614d57722f3b1b9c7e74929504e26192d23075d5c3",
sha256 = "d21a722873ed24586f071973b77223553fca466946f3d7e3976eeaccb14424e6",
)

http_archive(
Expand All @@ -28,7 +28,7 @@ def esbuild_dependencies():
],
strip_prefix = "package",
build_file_content = """exports_files(["esbuild.exe"])""",
sha256 = "826cd58553e7b6910dd22aba001cd72af34e05c9c3e9af567b5b2a6b1c9f3941",
sha256 = "fe5dcb97b4c47f9567012f0a45c19c655f3d2e0d76932f6dd12715dbebbd6eb0",
)

http_archive(
Expand All @@ -38,5 +38,5 @@ def esbuild_dependencies():
],
strip_prefix = "package",
build_file_content = """exports_files(["bin/esbuild"])""",
sha256 = "9dff3f5b06fd964a1cbb6aa9ea5ebf797767f1bd2bac71e084fb0bbefeba24a3",
sha256 = "60dabe141e5dfcf99e7113bded6012868132068a582a102b258fb7b1cfdac14b",
)
2 changes: 2 additions & 0 deletions packages/esbuild/test/typescript/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
)

Expand Down
14 changes: 10 additions & 4 deletions packages/esbuild/test/typescript/bundle.golden.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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);
};


Expand Down Expand Up @@ -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
15 changes: 15 additions & 0 deletions packages/esbuild/test/typescript/generated-module/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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",
)
7 changes: 6 additions & 1 deletion packages/esbuild/test/typescript/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
9 changes: 9 additions & 0 deletions packages/esbuild/test/typescript/relative-module/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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",
)
1 change: 1 addition & 0 deletions packages/esbuild/test/typescript/relative-module/lib.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const getId = () => 'relative-module';

0 comments on commit eb71285

Please sign in to comment.