chromium/ui/webui/resources/mojo/BUILD.gn

# 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("//build/config/chromeos/ui_mode.gni")
import("//tools/grit/preprocess_if_expr.gni")
import("//tools/typescript/ts_library.gni")
import("//ui/webui/resources/tools/generate_grd.gni")
import("//ui/webui/resources/tools/minify_js.gni")
import("//ui/webui/webui_features.gni")

tsc_folder = "$root_gen_dir/ui/webui/resources/tsc/mojo"

mojo_ts_files = [
  "skia/public/mojom/skcolor.mojom-webui.ts",
  "skia/public/mojom/bitmap.mojom-webui.ts",
  "skia/public/mojom/image_info.mojom-webui.ts",
  "ui/base/mojom/themes.mojom-webui.ts",
  "ui/base/mojom/window_open_disposition.mojom-webui.ts",
  "ui/gfx/image/mojom/image.mojom-webui.ts",
  "ui/gfx/range/mojom/range.mojom-webui.ts",
  "ui/gfx/geometry/mojom/geometry.mojom-webui.ts",
  "url/mojom/origin.mojom-webui.ts",
]

if (is_ios) {
  mojo_ts_files += [ "ui/base/mojom/ui_base_types.mojom-webui.ts" ]
}

if (is_chromeos_ash) {
  mojo_js_files = [
    "chromeos/ash/components/multidevice/mojom/multidevice_types.mojom-webui.js",
    "chromeos/ash/services/bluetooth_config/public/mojom/cros_bluetooth_config.mojom-webui.js",
    "chromeos/ash/services/cellular_setup/public/mojom/cellular_setup.mojom-webui.js",
    "chromeos/ash/services/cellular_setup/public/mojom/esim_manager.mojom-webui.js",
    "chromeos/ash/services/device_sync/public/mojom/device_sync.mojom-webui.js",
    "chromeos/ash/services/multidevice_setup/public/mojom/multidevice_setup.mojom-webui.js",
    "chromeos/services/network_config/public/mojom/constants.mojom-webui.js",
    "chromeos/services/network_config/public/mojom/cros_network_config.mojom-webui.js",
    "chromeos/services/network_config/public/mojom/network_types.mojom-webui.js",
    "chromeos/services/network_health/public/mojom/network_diagnostics.mojom-webui.js",
    "chromeos/services/network_health/public/mojom/network_health.mojom-webui.js",
    "chromeos/services/network_health/public/mojom/network_health_types.mojom-webui.js",
    "mojo/public/mojom/base/big_string.mojom-webui.js",
    "mojo/public/mojom/base/big_buffer.mojom-webui.js",
    "mojo/public/mojom/base/file.mojom-webui.js",
    "mojo/public/mojom/base/file_path.mojom-webui.js",
    "mojo/public/mojom/base/int128.mojom-webui.js",
    "mojo/public/mojom/base/process_id.mojom-webui.js",
    "mojo/public/mojom/base/read_only_buffer.mojom-webui.js",
    "mojo/public/mojom/base/read_only_file.mojom-webui.js",
    "mojo/public/mojom/base/safe_base_name.mojom-webui.js",
    "mojo/public/mojom/base/string16.mojom-webui.js",
    "mojo/public/mojom/base/text_direction.mojom-webui.js",
    "mojo/public/mojom/base/time.mojom-webui.js",
    "mojo/public/mojom/base/token.mojom-webui.js",
    "mojo/public/mojom/base/unguessable_token.mojom-webui.js",
    "mojo/public/mojom/base/uuid.mojom-webui.js",
    "mojo/public/mojom/base/values.mojom-webui.js",
    "services/network/public/mojom/ip_address.mojom-webui.js",
    "url/mojom/url.mojom-webui.js",
  ]

  mojo_ts_files += [
    "chromeos/ash/services/auth_factor_config/public/mojom/auth_factor_config.mojom-webui.ts",
    "chromeos/components/in_session_auth/mojom/in_session_auth.mojom-webui.ts",
    "chromeos/ash/services/connectivity/public/mojom/passpoint.mojom-webui.ts",
    "chromeos/ash/services/hotspot_config/public/mojom/cros_hotspot_config.mojom-webui.ts",
    "chromeos/ash/services/nearby/public/mojom/nearby_share_settings.mojom-webui.ts",
    "chromeos/ash/services/nearby/public/mojom/nearby_share_target_types.mojom-webui.ts",
    "ui/base/accelerators/mojom/accelerator.mojom-webui.ts",
    "ui/events/mojom/event.mojom-webui.ts",
    "ui/events/mojom/event_constants.mojom-webui.ts",
    "ui/latency/mojom/latency_info.mojom-webui.ts",
  ]
} else {
  mojo_ts_files += [
    "mojo/public/mojom/base/big_string.mojom-webui.ts",
    "mojo/public/mojom/base/big_buffer.mojom-webui.ts",
    "mojo/public/mojom/base/file.mojom-webui.ts",
    "mojo/public/mojom/base/file_path.mojom-webui.ts",
    "mojo/public/mojom/base/int128.mojom-webui.ts",
    "mojo/public/mojom/base/process_id.mojom-webui.ts",
    "mojo/public/mojom/base/read_only_buffer.mojom-webui.ts",
    "mojo/public/mojom/base/read_only_file.mojom-webui.ts",
    "mojo/public/mojom/base/safe_base_name.mojom-webui.ts",
    "mojo/public/mojom/base/string16.mojom-webui.ts",
    "mojo/public/mojom/base/text_direction.mojom-webui.ts",
    "mojo/public/mojom/base/time.mojom-webui.ts",
    "mojo/public/mojom/base/token.mojom-webui.ts",
    "mojo/public/mojom/base/unguessable_token.mojom-webui.ts",
    "mojo/public/mojom/base/uuid.mojom-webui.ts",
    "mojo/public/mojom/base/values.mojom-webui.ts",
    "url/mojom/url.mojom-webui.ts",
  ]
}

preprocess_if_expr("copy_mojo_ts") {
  visibility = [ ":build_ts" ]

  in_folder = root_gen_dir
  out_folder = "$target_gen_dir/preprocessed"
  in_files = mojo_ts_files
  deps = [
    "//skia/public/mojom:mojom_ts__generator",
    "//ui/base/mojom:mojom_ts__generator",
    "//ui/gfx/geometry/mojom:mojom_ts__generator",
    "//ui/gfx/image/mojom:mojom_ts__generator",
    "//ui/gfx/range/mojom:mojom_ts__generator",
    "//url/mojom:url_mojom_origin_ts__generator",
  ]

  if (is_chromeos_ash) {
    deps += [
      "//chromeos/ash/services/auth_factor_config/public/mojom:mojom_ts__generator",
      "//chromeos/ash/services/connectivity/public/mojom:mojom_ts__generator",
      "//chromeos/ash/services/hotspot_config/public/mojom:mojom_ts__generator",
      "//chromeos/ash/services/nearby/public/mojom:nearby_share_settings_ts__generator",
      "//chromeos/components/in_session_auth/mojom:mojom_ts__generator",
      "//ui/base/accelerators/mojom:mojom_ts__generator",
      "//ui/events/mojom:mojom_ts__generator",
      "//ui/latency/mojom:mojom_ts__generator",
    ]
  } else {
    deps += [
      "//mojo/public/mojom/base:base_ts__generator",
      "//url/mojom:url_mojom_gurl_ts__generator",
    ]
  }
}

if (is_chromeos_ash) {
  # Move all the files out of mojom-webui to the expected location.
  preprocess_if_expr("copy_mojo") {
    visibility = [ ":build_ts" ]

    in_folder = "$root_gen_dir/mojom-webui"
    out_folder = "$target_gen_dir/preprocessed"
    in_files = mojo_js_files

    deps = [
      "//chromeos/ash/components/multidevice/mojom:mojom_js__generator",
      "//chromeos/ash/services/bluetooth_config/public/mojom:mojom_js__generator",
      "//chromeos/ash/services/cellular_setup/public/mojom:mojom_js__generator",
      "//chromeos/ash/services/device_sync/public/mojom:mojom_js__generator",
      "//chromeos/ash/services/multidevice_setup/public/mojom:mojom_js__generator",
      "//chromeos/services/network_config/public/mojom:mojom_js__generator",
      "//chromeos/services/network_config/public/mojom:network_types_js__generator",
      "//chromeos/services/network_health/public/mojom:mojom_js__generator",
      "//chromeos/services/network_health/public/mojom:types_js__generator",
      "//mojo/public/mojom/base:base_js__generator",
      "//services/network/public/mojom:mojom_ip_address_js__generator",
      "//url/mojom:url_mojom_gurl_js__generator",
    ]
  }
}

ts_library("build_ts") {
  root_dir = "$target_gen_dir/preprocessed"
  out_dir = tsc_folder
  composite = true

  in_files = mojo_ts_files

  if (is_chromeos_ash) {
    in_files += mojo_js_files
  }

  definitions = [ "$tsc_folder/mojo/public/js/bindings.d.ts" ]

  extra_deps = [
    ":copy_bindings_dts",
    ":copy_mojo_ts",
  ]
  if (is_chromeos_ash) {
    extra_deps += [ ":copy_mojo" ]
  }

  path_mappings = [ "//resources/mojo/*|" +
                    rebase_path("$root_gen_dir/ui/webui/resources/tsc/mojo/*",
                                target_gen_dir) ]
}

if (optimize_webui) {
  minify_js("build_min_js") {
    visibility = [ ":build_grdp" ]
    in_folder = tsc_folder
    out_folder = "$target_gen_dir/minified"
    in_files = []
    js_files = filter_include(get_target_outputs(":build_ts"), [ "*.js" ])
    foreach(_js_file, js_files) {
      in_files += [ string_replace(_js_file, "$tsc_folder/", "") ]
    }
    deps = [ ":build_ts" ]
  }
}

# Copy bindings.d.ts file under the same location where other shared Mojo JS
# generated files reside, so that it can be located by the TS compiler when
# resolving chrome://resources/mojo/mojo/public/js/bindings.js.
copy("copy_bindings_dts") {
  sources = [ "bindings.d.ts" ]
  outputs = [ "$tsc_folder/mojo/public/js/{{source_file_part}}" ]
}

generate_grd("build_grdp") {
  grd_prefix = "webui_mojo"
  input_files_base_dir = rebase_path("$root_gen_dir/", root_build_dir)
  input_files = [ "mojo/public/js/bindings.js" ]
  deps = [ "//mojo/public/js:bindings_module" ]
  if (optimize_webui) {
    deps += [ ":build_min_js" ]
    manifest_files = filter_include(get_target_outputs(":build_min_js"),
                                    [ "*_manifest.json" ])
  } else {
    deps += [ ":build_ts" ]
    manifest_files =
        filter_include(get_target_outputs(":build_ts"), [ "*_manifest.json" ])
  }
  out_grd = "$target_gen_dir/resources.grdp"
  resource_path_prefix = "mojo"
}