chromium/ios/web/public/js_messaging/compile_ts.gni

# Copyright 2022 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import("//tools/typescript/ts_library.gni")

# Defines a target that compiles one or more TypeScript and JavaScript input
# files.
#
# Variables
#   sources:
#     The full list of source scripts including |primary_script| and others
#     referenced from within |primary_script|. Listing all these files here
#     ensures this target is rebuilt if any of the files are changed.
#
#   allow_js (optional):
#     Whether or not JavaScript files are allowed. Defaults to false.
#
#   deps (optional):
#     The dependent targets which produce typescript compiler manifest files.
#     These manifest files will be used to resolve script imports.
#
template("compile_ts") {
  assert(defined(invoker.sources), "sources must be set")

  if (defined(invoker.allow_js) && invoker.allow_js) {
    _is_sources_only_js_and_ts = filter_exclude(invoker.sources,
                                                [
                                                  "*.js",
                                                  "*.ts",
                                                ]) == []
    assert(_is_sources_only_js_and_ts, "all sources must be .ts or .js files")
  } else {
    _is_sources_only_ts = filter_exclude(invoker.sources, [ "*.ts" ]) == []
    assert(_is_sources_only_ts,
           "all sources must be .ts (to allow .js, set allow_js to true)")
  }

  compiled_scripts_dir = "$target_gen_dir" + "/tsc"
  src_dir = rebase_path(root_build_dir + "/../../")

  all_compiled_outputs = []
  foreach(js_file, rebase_path(invoker.sources)) {
    all_compiled_outputs +=
        [ "$compiled_scripts_dir/" +
          rebase_path(get_path_info(js_file, "dir"), src_dir) + "/" +
          get_path_info(js_file, "name") + ".js" ]
  }

  # Compile the scripts and output them into `compiled_scripts_dir` in the same
  # directory structure to maintain path imports specified in the tsc manifest
  # file.
  ts_library(target_name) {
    forward_variables_from(invoker,
                           [
                             "deps",
                             "testonly",
                           ])

    composite = true

    # The root must be the src directory since it must contain all scripts.
    # Ref: TS6059 (Some scripts will be within `root_build_dir`, but others
    # will be directly checked in as source files.)
    root_dir = src_dir

    out_dir = compiled_scripts_dir
    path_mappings = []
    foreach(js_file, invoker.sources) {
      js_import_path = get_path_info(js_file, "dir") + "/" +
                       get_path_info(js_file, "name") + ".js"

      file_from_src = rebase_path(rebase_path(js_import_path), src_dir)

      path_mappings += [ "//" + file_from_src + "|./" +
                         rebase_path(rebase_path(js_file), target_gen_dir) ]
    }
    path_mappings += [ "//*|" + rebase_path("//*", target_gen_dir) ]

    # in_files are defined relative to root_dir as described in ts_library.py
    in_files = rebase_path(rebase_path(invoker.sources), root_dir)
  }
}