blob: 8ee2b2c699ba3e02395974fd4c09f821c7975953 [file] [edit]
# Copyright 2021 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import("../../../scripts/build/typescript/typescript.gni")
import("./node.gni")
import("./vars.gni")
template("bundle") {
assert(defined(invoker.entrypoint),
"You must define the 'entrypoint' for a bundle target")
_bundle_all = false
if (defined(invoker.bundle_all)) {
_bundle_all = invoker.bundle_all
}
_sourcemap = true
if (defined(invoker.sourcemap)) {
_sourcemap = invoker.sourcemap
} else if (_bundle_all) {
_sourcemap = false
}
node_action(target_name) {
script = "scripts/build/esbuild.js"
deps = []
if (defined(invoker.deps)) {
deps += invoker.deps
}
# esbuild resolves the module type and package configuration from
# package.json in the build root, which //tools/typescript:copy_package_json
# produces. Depend on it so the copy runs first; otherwise a concurrent copy
# can fail the read on Windows with a sharing violation. The target only
# exists in a Chromium checkout.
if (build_with_chromium) {
deps += [ "//tools/typescript:copy_package_json" ]
}
inputs = [
invoker.entrypoint,
devtools_location_prepend + "scripts/build/devtools_plugin.js",
devtools_location_prepend + "scripts/devtools_paths.js",
]
if (defined(invoker.inputs)) {
inputs += invoker.inputs
}
if (!_bundle_all) {
_bundled_entrypoints_target =
devtools_location_prepend + ":bundled_entrypoints"
deps += [ _bundled_entrypoints_target ]
_entrypoints_file =
get_label_info(_bundled_entrypoints_target, "target_gen_dir") +
"/bundled_entrypoints.json"
inputs += [ _entrypoints_file ]
}
_esbuild = devtools_location_prepend + "third_party/esbuild/esbuild"
if (host_os == "win") {
inputs += [ _esbuild + ".exe" ]
} else {
inputs += [ _esbuild ]
}
_depfile = "$target_gen_dir/$target_name.d"
depfile = _depfile
args = [
"--entrypoint",
rebase_path(invoker.entrypoint, root_build_dir),
"--outfile",
rebase_path(invoker.output_file_location, root_build_dir),
"--root-dir",
rebase_path("//", root_build_dir),
"--root-gen-dir",
rebase_path(root_gen_dir, root_build_dir),
"--depfile",
rebase_path(_depfile, root_build_dir),
]
if (_sourcemap) {
args += [ "--sourcemap" ]
}
if (!_bundle_all) {
args += [
"--entrypoints-file",
rebase_path(_entrypoints_file, root_build_dir),
]
} else {
args += [ "--bundle-all" ]
}
if (defined(invoker.format)) {
args += [
"--format",
invoker.format,
]
}
if (defined(invoker.platform)) {
assert(invoker.platform == "browser" || invoker.platform == "node",
"platform must be \"browser\" or \"node\"")
# Without bundle_all, every bare specifier is external, so npm packages
# would silently stay unbundled.
assert(_bundle_all || invoker.platform != "node",
"platform = \"node\" requires bundle_all = true")
args += [
"--platform",
invoker.platform,
]
}
# Imports to leave out of the bundle. Each entry is one of:
# - A bare package specifier (e.g. "yargs"), which is emitted as written.
# - A path to a single file that starts with "./" or "../" and is relative
# to the directory of `entrypoint` (e.g. "./cli.js"). Every import that
# resolves to that file is external, including imports inside npm
# packages. Name the file as the imports do, extension included. An
# entry that no import resolves to fails the build. Each entry must be
# imported by code that is in the bundle: esbuild does not follow the
# imports of an external file, so a file that only another external
# imports matches nothing.
# The import is emitted as the path from the output file to the file's
# location in the gen directory. Add the `deps` or `data` that put the
# file there. Nothing puts node_modules there, so only list a file of an
# npm package if it is loaded through a dynamic `import()` that is never
# called or a `require()` in a dead branch. A static `import` is
# resolved when Node loads the bundle, and fails with
# ERR_MODULE_NOT_FOUND at startup even if nothing uses the module.
if (defined(invoker.external)) {
# Without bundle_all, every bare specifier is already external.
assert(_bundle_all, "external requires bundle_all = true")
foreach(_external, invoker.external) {
args += [ "--external=$_external" ]
}
}
# Defines `require` for platform = "node" ESM bundles that contain
# CommonJS dependencies. Opt-in because it clashes with bundled modules
# that declare their own top-level `require`.
if (defined(invoker.node_require_shim) && invoker.node_require_shim) {
assert(defined(invoker.platform) && invoker.platform == "node",
"node_require_shim requires platform = \"node\"")
assert(!defined(invoker.format) || invoker.format == "esm",
"node_require_shim requires format = \"esm\"")
args += [ "--node-require-shim" ]
}
if (defined(invoker.global_name)) {
args += [
"--global-name",
invoker.global_name,
]
}
if (defined(invoker.css_style_sheet) && invoker.css_style_sheet) {
args += [ "--css-style-sheet" ]
}
if (defined(invoker.max_size)) {
if (!is_debug) {
args += [
"--max-size",
"${invoker.max_size}",
]
} else {
not_needed(invoker, [ "max_size" ])
}
}
if (!is_debug) {
args += [ "--minify" ]
}
outputs = [ invoker.output_file_location ]
if (defined(invoker.metafile)) {
args += [
"--metafile",
rebase_path(invoker.metafile, root_build_dir),
]
outputs += [ invoker.metafile ]
}
forward_variables_from(invoker,
[
"data",
"metadata",
"public_deps",
"testonly",
"visibility",
])
}
}