Skip to content

Commit

Permalink
feat: add esbuild package
Browse files Browse the repository at this point in the history
  • Loading branch information
mattem committed Jan 24, 2021
1 parent 66697f5 commit db35bf6
Show file tree
Hide file tree
Showing 37 changed files with 939 additions and 5 deletions.
5 changes: 5 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ browser_repositories(
firefox = True,
)

# Setup esbuild dependencies
load("//packages/esbuild:index.bzl", "esbuild_repository")

esbuild_repository(name = "esbuild")

#
# Dependencies to run stardoc & generating documentation
#
Expand Down
1 change: 1 addition & 0 deletions docs/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ rules_nodejs_docs(
"Rollup": "//packages/rollup:README.md",
"Terser": "//packages/terser:README.md",
"TypeScript": "//packages/typescript:README.md",
"esbuild": "//packages/esbuild:README.md",
},
tags = [
"fix-windows",
Expand Down
10 changes: 5 additions & 5 deletions internal/linker/link_node_modules.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def _debug(vars, *args):
print("[link_node_modules.bzl]", *args)

# Arbitrary name; must be chosen to globally avoid conflicts with any other aspect
_ASPECT_RESULT_NAME = "link_node_modules__aspect_result"
ASPECT_RESULT_NAME = "link_node_modules__aspect_result"

# Traverse 'srcs' in addition so that we can go across a genrule
_MODULE_MAPPINGS_DEPS_NAMES = ["data", "deps", "srcs"]
Expand Down Expand Up @@ -81,7 +81,7 @@ def write_node_modules_manifest(ctx, extra_data = [], mnemonic = None, link_work
fail("All npm dependencies need to come from a single workspace. Found '%s' and '%s'." % (node_modules_root, possible_root))

# ...first-party packages to be linked into the node_modules tree
for k, v in getattr(dep, _ASPECT_RESULT_NAME, {}).items():
for k, v in getattr(dep, ASPECT_RESULT_NAME, {}).items():
if _link_mapping(dep.label, mappings, k, v):
# Special case for ts_library module_name for legacy behavior and for AMD name
# work-around. Do not propagate tslibrary root type to runtime as it is not
Expand Down Expand Up @@ -126,7 +126,7 @@ def _get_module_mappings(target, ctx):
# Propogate transitive mappings
for name in _MODULE_MAPPINGS_DEPS_NAMES:
for dep in getattr(ctx.rule.attr, name, []):
for k, v in getattr(dep, _ASPECT_RESULT_NAME, {}).items():
for k, v in getattr(dep, ASPECT_RESULT_NAME, {}).items():
# A package which was reachable transitively via a *_binary or *_test
# rule is assumed to be in the runfiles of that binary,
# so we switch the linker target root.
Expand Down Expand Up @@ -172,9 +172,9 @@ def _get_module_mappings(target, ctx):

def _module_mappings_aspect_impl(target, ctx):
# Use a dictionary to construct the result struct
# so that we can reference the _ASPECT_RESULT_NAME variable
# so that we can reference the ASPECT_RESULT_NAME variable
return struct(**{
_ASPECT_RESULT_NAME: _get_module_mappings(target, ctx),
ASPECT_RESULT_NAME: _get_module_mappings(target, ctx),
})

module_mappings_aspect = aspect(
Expand Down
83 changes: 83 additions & 0 deletions packages/esbuild/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Copyright 2020 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("@build_bazel_rules_nodejs//:tools/defaults.bzl", "codeowners", "pkg_npm")
load("@build_bazel_rules_nodejs//tools/stardoc:index.bzl", "stardoc")
load("//third_party/github.com/bazelbuild/bazel-skylib:rules/copy_file.bzl", "copy_file")

package(default_visibility = ["//visibility:public"])

codeowners(
teams = ["@mattem"],
)

bzl_library(
name = "bzl",
srcs = glob(["**/*.bzl"]),
deps = [
"@bazel_skylib//lib:paths",
"@build_bazel_rules_nodejs//:bzl",
"@build_bazel_rules_nodejs//internal/common:bzl",
"@build_bazel_rules_nodejs//internal/node:bzl",
],
)

stardoc(
name = "docs",
testonly = True,
out = "index.md",
input = "index.bzl",
tags = ["fix-windows"],
deps = [":bzl"],
)

genrule(
name = "generate_README",
srcs = [
"_README.md",
"index.md",
],
outs = ["README.md"],
cmd = """cat $(execpath _README.md) $(execpath index.md) | sed 's/^##/\\\n##/' > $@""",
tags = ["fix-windows"],
visibility = ["//docs:__pkg__"],
)

copy_file(
name = "npm_version_check",
src = "//internal:npm_version_check.js",
out = ":npm_version_check.js",
)

pkg_npm(
name = "npm_package",
srcs = [
"esbuild.bzl",
"esbuild_repo.bzl",
"helpers.bzl",
"index.bzl",
"package.json",
],
build_file_content = " ",
deps = [
":npm_version_check",
] + select({
# FIXME: fix stardoc on Windows; //packages/karma:index.md generation fails with:
# ERROR: D:/b/62unjjin/external/npm_bazel_karma/BUILD.bazel:65:1: Couldn't build file
# external/npm_bazel_karma/docs.raw: Generating proto for Starlark doc for docs failed (Exit 1)
"@bazel_tools//src/conditions:windows": [],
"//conditions:default": [":generate_README"],
}),
)
60 changes: 60 additions & 0 deletions packages/esbuild/_README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# esbuild rules for Bazel

The esbuild rules runs the [esbuild](https://github.com/evanw/esbuild) bundler tool with Bazel.

## Installation

Add the `@bazel/esbuild` npm packages to your `devDependencies` in `package.json`.

```
npm install --save-dev @bazel/esbuild
```
or using yarn
```
yarn add -D @bazel/esbuild
```

Add the esbuild repository to the `WORKSPACE` file

```python
load("@npm//@bazel/esbuild:index.bzl", "esbuild_repository")

esbuild_repository(name = "esbuild")
```

The version of `esbuild` can be specified by passing the `version` and `platform_sha` attributes

```python
load("@npm//@bazel/esbuild:index.bzl", "esbuild_repository")

esbuild_repository(
name = "esbuild",
version = "0.7.19",
platform_sha = {
"darwin_64": "deadf43c0868430983234f90781e1b542975a2bc3549b2353303fac236816149",
"linux_64": "2d25ad82dba8f565e8766b838acd3b966f9a2417499105ec10afb01611594ef1",
"windows_64": "135b2ff549d4b1cfa4f8e2226f85ee97641b468aaced7585112ebe8c0af2d766",
}
)
```

## Example use of esbuild

The `esbuild` rule can take a JS or TS dependency tree and bundle it to a single file, or split across multiple files, outputting a directory.

```python
ts_library(
name = "lib",
srcs = ["a.ts"],
)

esbuild(
name = "bundle",
entry_point = "a.ts",
deps = [":lib"],
)
```

The above will create three output files, `bundle.js`, `bundle.js.map` and `bundle_metadata.json` which contains the bundle metadata to aid in debugging and resoloution tracing.

Further options for minifying and splitting are available.
Loading

0 comments on commit db35bf6

Please sign in to comment.