| # 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("../typescript/typescript.gni") | |
| import("./bundle.gni") | |
| import("./copy.gni") | |
| import("./node.gni") | |
| import("./vars.gni") | |
| template("devtools_entrypoint") { | |
| assert(defined(invoker.entrypoint), | |
| "You must define the 'entrypoint' for an entrypoint target") | |
| if (!devtools_bundle) { | |
| ts_library(target_name) { | |
| sources = [ invoker.entrypoint ] | |
| forward_variables_from(invoker, | |
| "*", | |
| [ | |
| "entrypoint", | |
| "sources", | |
| ]) | |
| } | |
| } else { | |
| _entrypoint_name = target_name | |
| _file_name = get_path_info(invoker.entrypoint, "name") | |
| _ts_target = "${target_name}_typescript" | |
| _bundle_target = "${target_name}_bundle" | |
| # 1. Type-check entrypoint and generate .d.ts in out/<config>/gen (no JS emit). | |
| ts_library(_ts_target) { | |
| tsconfig_name = _entrypoint_name | |
| sources = [ invoker.entrypoint ] | |
| no_js_emit = true | |
| forward_variables_from(invoker, | |
| "*", | |
| [ | |
| "entrypoint", | |
| "sources", | |
| "visibility", | |
| ]) | |
| # Reset default visibility from set_defaults("ts_library") before | |
| # overriding. | |
| visibility = [] | |
| visibility = [ ":*" ] | |
| } | |
| # Sub-target: Terminal barrier for transitive source propagation. Because | |
| # cross-module entrypoints are marked external by esbuild, bundling will not | |
| # traverse further into dependencies when consuming this entrypoint. | |
| # Therefore, this group exposes no public_inputs and forwards no | |
| # public_deps. | |
| _sources_target = "${target_name}_sources" | |
| group(_sources_target) { | |
| forward_variables_from(invoker, [ "testonly" ]) | |
| } | |
| _bundle_deps = [ ":$_sources_target" ] | |
| if (defined(invoker.deps)) { | |
| _bundle_deps += invoker.deps | |
| } | |
| if (defined(invoker.public_deps)) { | |
| _bundle_deps += invoker.public_deps | |
| } | |
| if (defined(invoker.ts_deps)) { | |
| foreach(dep, invoker.ts_deps) { | |
| _dep_dir = get_label_info(dep, "dir") | |
| _dep_name = get_label_info(dep, "name") | |
| _bundle_deps += [ "$_dep_dir:${_dep_name}_sources" ] | |
| } | |
| } | |
| # 2. Bundle from the source TS file to out/<config>/gen. | |
| bundle(_bundle_target) { | |
| entrypoint = invoker.entrypoint | |
| output_file_location = "$target_gen_dir/$_file_name.js" | |
| deps = _bundle_deps | |
| forward_variables_from(invoker, [ "testonly" ]) | |
| visibility = [ ":*" ] | |
| } | |
| # 3. Main target exposing d.ts files via public_deps and runtime bundled outputs | |
| # via data_deps. | |
| # | |
| # Architecture Note: | |
| # ------------------ | |
| # Downstream TypeScript targets depend on this entrypoint target to resolve types | |
| # (.d.ts) at compile time. Placing `_bundle_target` in `data_deps` (instead of | |
| # `public_deps`) is critical for two reasons: | |
| # | |
| # 1. Parallelism: Decouples compile-time TypeScript type checking from Rollup/esbuild | |
| # bundling. Downstream `ts_library` and `devtools_entrypoint` targets can begin | |
| # type checking immediately once `_ts_target` emits declarations (.d.ts), without | |
| # being blocked waiting for the slower bundling step to complete. | |
| # 2. Ninja Restat Pruning: When implementation code changes without modifying | |
| # public type signatures, `--isolatedDeclarations` produces identical .d.ts files. | |
| # `run_with_restat.py` preserves the .d.ts timestamp, allowing Ninja to prune | |
| # downstream recompilation. If `_bundle_target` were in `public_deps`, its output | |
| # (.js) would be updated on every code edit, invalidating downstream targets and | |
| # defeating Ninja's restat pruning. | |
| # | |
| # Resource packaging (action("generate_devtools_grd") and | |
| # node_action("compress") in root BUILD.gn) uses dynamic depfiles to track | |
| # generated bundle JS files in Ninja's dependency database, allowing runtime | |
| # outputs to remain in data_deps without requiring manual bundle target | |
| # registration in packaging groups. | |
| group(target_name) { | |
| public_deps = [ ":$_ts_target" ] | |
| if (defined(invoker.public_deps)) { | |
| public_deps += invoker.public_deps | |
| } | |
| data = [ "$target_gen_dir/$_file_name.js" ] | |
| data_deps = [ ":$_bundle_target" ] | |
| if (defined(invoker.data_deps)) { | |
| data_deps += invoker.data_deps | |
| } | |
| metadata = { | |
| grd_files = [ "$target_gen_dir/$_file_name.js" ] | |
| } | |
| forward_variables_from(invoker, | |
| [ | |
| "testonly", | |
| "visibility", | |
| ]) | |
| } | |
| } | |
| } | |
| set_defaults("devtools_entrypoint") { | |
| inputs = [] | |
| } |