Skip to content

Latest commit

 

History

History
executable file
·
415 lines (235 loc) · 13.2 KB

esbuild.md

File metadata and controls

executable file
·
415 lines (235 loc) · 13.2 KB

esbuild rules for Bazel

The esbuild rules runs the esbuild bundler tool with Bazel. esbuild is an extremely fast JavaScript bundler written in Go, its current benchmarks show it can be 320x faster that other bundlers

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

The esbuild binary is fetched automatically for your platform and is exposed via Bazel toolchains. To do this, add the esbuild_repositories rule to your WORKSPACE. You'll need to point it to the repository created by npm_install or yarn_install where the @bazel/esbuild package is fetched. (Typically, this is npm). Set the npm_repository attribute to the name of that repository.

npm_install(
    name = "npm",
    # @bazel/esbuild is a dependency in this package.json
    package_json = "//:package.json",
    package_lock_json = "//:package-lock.json",
)

load("@build_bazel_rules_nodejs//toolchains/esbuild:esbuild_repositories.bzl", "esbuild_repositories")

esbuild_repositories(npm_repository = "npm")  # Note, npm is the default value for npm_repository

To avoid eagerly fetching all the npm dependencies, this load statement comes from the "Built-in" @build_bazel_rules_nodejs repository rather than from @npm. In rules_nodejs 5.0 we intend to fix this layering violation by having the whole esbuild support distributed independently of rules_nodejs, and not require any package to be installed from npm.

Overview

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.

load("@npm//@bazel/esbuild:index.bzl", "esbuild")
load("@npm//@bazel/typescript:index.bzl", "ts_project")

ts_project(
    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.

To create a code split bundle, set splitting = True on the esbuild rule.

load("@npm//@bazel/esbuild:index.bzl", "esbuild")
load("@npm//@bazel/typescript:index.bzl", "ts_project")

ts_project(
    name = "lib",
    srcs = ["a.ts"],
    deps = [
        "@npm//foo",
    ],
)

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

This will create an output directory containing all the code split chunks, along with their sourcemaps files

esbuild

USAGE

esbuild(name, args, args_file, config, define, deps, entry_point, entry_points, external, format,
        launcher, link_workspace_root, max_threads, minify, node_context_data, output, output_css,
        output_dir, output_map, platform, sourcemap, sources_content, splitting, srcs, target)

Runs the esbuild bundler under Bazel

For further information about esbuild, see https://esbuild.github.io/

ATTRIBUTES

name

(Name, mandatory): A unique name for this target.

args

(Dictionary: String -> String): A dict of extra arguments that are included in the call to esbuild, where the key is the argument name. Values are subject to $(location ...) expansion

Defaults to {}

args_file

(Label): Internal use only

Defaults to None

config

(Label): Configuration file used for esbuild, from the esbuild_config macro. Note that options set in this file may get overwritten. See https://github.com/bazelbuild/rules_nodejs/tree/stable/packages/esbuild/test/plugins/BUILD.bazel for examples of using esbuild_config and plugins. The dependencies of this attribute must provide: Unknown Provider

Defaults to None

define

(Dictionary: String -> String): A dict of global identifier replacements. Values are subject to $(location ...) expansion. Example:

esbuild(
    name = "bundle",
    define = {
        "process.env.NODE_ENV": "production"
    },
)

See https://esbuild.github.io/api/#define for more details

Defaults to {}

deps

(List of labels): A list of direct dependencies that are required to build the bundle

Defaults to []

entry_point

(Label): The bundle's entry point (e.g. your main.js or app.js or index.js)

This is a shortcut for the entry_points attribute with a single entry. Specify either this attribute or entry_point, but not both.

Defaults to None

entry_points

(List of labels): The bundle's entry points (e.g. your main.js or app.js or index.js)

Specify either this attribute or entry_point, but not both.

Defaults to []

external

(List of strings): A list of module names that are treated as external and not included in the resulting bundle

See https://esbuild.github.io/api/#external for more details

Defaults to []

format

(String): The output format of the bundle, defaults to iife when platform is browser and cjs when platform is node. If performing code splitting or multiple entry_points are specified, defaults to esm.

See https://esbuild.github.io/api/#format for more details

Defaults to ""

launcher

(Label, mandatory): Internal use only

(Boolean): 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.

Defaults to False

max_threads

(Integer): Sets the GOMAXPROCS variable to limit the number of threads that esbuild can run with. This can be useful if running many esbuild rule invocations in parallel, which has the potential to cause slowdown. For general use, leave this attribute unset.

Defaults to 0

minify

(Boolean): Minifies the bundle with the built in minification. Removes whitespace, shortens identifieres and uses equivalent but shorter syntax.

Sets all --minify-* flags

See https://esbuild.github.io/api/#minify for more details

Defaults to False

node_context_data

(Label): Provides info about the build context, such as stamping.

By default it reads from the bazel command line, such as the --stamp argument. Use this to override values for this target, such as enabling or disabling stamping. You can use the node_context_data rule in @build_bazel_rules_nodejs//internal/node:context.bzl to create a NodeContextInfo. The dependencies of this attribute must provide: NodeContextInfo

Defaults to @build_bazel_rules_nodejs//internal:node_context_data

output

(Label): Name of the output file when bundling

output_css

(Label): Declare a .css file will be output next to output bundle.

If your JS code contains import statements that import .css files, esbuild will place the content in a file next to the main output file, which you'll need to declare. If your output file is named 'foo.js', you should set this to 'foo.css'.

output_dir

(Boolean): If true, esbuild produces an output directory containing all output files

Defaults to False

output_map

(Label): Name of the output source map when bundling

platform

(String): The platform to bundle for.

See https://esbuild.github.io/api/#platform for more details

Defaults to "browser"

sourcemap

(String): Defines where sourcemaps are output and how they are included in the bundle. By default, a separate .js.map file is generated and referenced by the bundle. If 'external', a separate .js.map file is generated but not referenced by the bundle. If 'inline', a sourcemap is generated and its contents are inlined into the bundle (and no external sourcemap file is created). If 'both', a sourcemap is inlined and a .js.map file is created.

See https://esbuild.github.io/api/#sourcemap for more details

Defaults to ""

sources_content

(Boolean): If False, omits the sourcesContent field from generated source maps

See https://esbuild.github.io/api/#sources-content for more details

Defaults to False

splitting

(Boolean): If true, esbuild produces an output directory containing all the output files from code splitting for multiple entry points

See https://esbuild.github.io/api/#splitting and https://esbuild.github.io/api/#entry-points for more details

Defaults to False

srcs

(List of labels): Source files to be made available to esbuild

Defaults to []

target

(String): Environment target (e.g. es2017, chrome58, firefox57, safari11, edge16, node10, esnext). Default es2015.

See https://esbuild.github.io/api/#target for more details

Defaults to "es2015"

configure_esbuild_toolchain

USAGE

configure_esbuild_toolchain(name, binary, exec_compatible_with)

Defines a toolchain for esbuild given the binary path and platform constraints

PARAMETERS

name

unique name for this toolchain, generally in the form "esbuild_platform_arch"

binary

label for the esbuild binary

exec_compatible_with

list of platform constraints

esbuild_config

USAGE

esbuild_config(name, config_file, srcs, deps, kwargs)

Macro for an esbuild configuration file and its assoicated dependencies

PARAMETERS

name

Unique name for this rule

config_file

The configuration file / entrypoint

srcs

List of source files referenced by the configuration

Defaults to []

deps

List of dependencies required for this configuration

Defaults to []

kwargs

Any other common attributes

esbuild_repositories

USAGE

esbuild_repositories(name, npm_repository)

Helper for fetching and setting up the esbuild versions and toolchains

PARAMETERS

name

currently unused

Defaults to ""

npm_repository

the name of the repository where the @bazel/esbuild package is installed by npm_install or yarn_install.

Defaults to "npm"