chromium/ios/web/public/js_messaging/optimize_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("//ios/web/public/js_messaging/compile_ts.gni")
import("//ios/web/public/js_messaging/optimize_js.gni")

# Defines a target that compiles a single TypeScript or JavaScript file,
# and copies it to the application resources directory.
# This template is shorthand for passing a compiled script from
# //ios/web/public/js_messaging/compile_ts.gni to
# //ios/web/public/js_messaging/optimize_js.gni
#
# Variables
#   sources:
#     A single TypeScript (or JavaScript) file to compile and bundle into the
#     application.
#
template("optimize_ts") {
  assert(defined(invoker.sources), "sources must be set")
  _sources_count = 0
  foreach(_, invoker.sources) {
    _sources_count += 1
  }
  assert(_sources_count == 1,
         "only a single source may be provided to optimize_ts")

  _compile_ts_target_name = target_name + "_compile_ts"
  compile_ts(_compile_ts_target_name) {
    forward_variables_from(invoker,
                           [
                             "deps",
                             "sources",
                             "testonly",
                           ])
  }

  optimize_js(target_name) {
    forward_variables_from(invoker,
                           [
                             "testonly",
                             "visibility",
                           ])

    deps = [ ":$_compile_ts_target_name" ]
    if (defined(invoker.deps)) {
      deps += invoker.deps
    }

    _invoker_sources = invoker.sources
    _script_name = get_path_info(_invoker_sources[0], "name") + ".js"
    _script = filter_include(get_target_outputs(":$_compile_ts_target_name"),
                             [ "*/$_script_name" ])
    primary_script = _script[0]
    sources = _script
  }
}