chromium/third_party/cros-components/BUILD.gn

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

import("//ash/webui/tools/mwc_path_mappings.gni")
import("//build/config/chromeos/ui_mode.gni")
import("//third_party/material_web_components/rewrite_imports.gni")
import("//tools/typescript/ts_library.gni")
import("//ui/webui/resources/tools/generate_grd.gni")

_cros_components_ts_files = [
  "accordion/accordion.ts",
  "accordion/accordion_item.ts",
  "async_helpers/async_helpers.ts",
  "badge/badge.ts",
  "button/button.ts",
  "card/card.ts",
  "checkbox/checkbox.ts",
  "chip/chip.ts",
  "dropdown/dropdown.ts",
  "dropdown/dropdown_option.ts",
  "helpers/helpers.ts",
  "icon_button/icon-button.ts",
  "icon_dropdown/icon-dropdown.ts",
  "icon_dropdown/icon-dropdown-option.ts",
  "lottie_renderer/event_binders.ts",
  "lottie_renderer/lottie-renderer.ts",
  "lottie_renderer/worker.ts",
  "lottie_renderer/lottie_processing_helpers.ts",
  "menu/menu.ts",
  "menu/menu_item.ts",
  "menu/menu_separator.ts",
  "menu/menu_util.ts",
  "menu/sub_menu_item.ts",
  "orca_feedback/maybe_safe_html.ts",
  "orca_feedback/orca-feedback.ts",
  "radio/radio.ts",
  "sidenav/sidenav.ts",
  "sidenav/sidenav_item.ts",
  "sidenav/sidenav_util.ts",
  "slider/slider.ts",
  "snackbar/snackbar.ts",
  "snackbar/show_snackbar.ts",
  "snackbar/snackbar-item.ts",
  "snackbar/snackbar-manager.ts",
  "switch/switch.ts",
  "tab_slider/tab-slider-item.ts",
  "tab_slider/tab-slider.ts",
  "tabs/tab.ts",
  "tabs/tabs.ts",
  "tag/tag.ts",
  "textfield/textfield.ts",
  "tooltip/tooltip.ts",
]

ts_library("cros_components__ts_library") {
  # Downstream ts_library targets should depend on `cros_components_ts` below.
  visibility = [ ":*" ]

  root_dir = "src/"
  out_dir =
      "$root_gen_dir/ui/webui/resources/tsc/cros_components/to_be_rewritten"
  tsconfig_base = "tsconfig_base.json"

  in_files = _cros_components_ts_files

  root_src_dir = rebase_path("//", target_gen_dir)
  resources_dir =
      rebase_path("$root_gen_dir/ui/webui/resources/tsc", target_gen_dir)

  path_mappings = [
    "chrome://resources/cr_components/color_change_listener/*|$resources_dir/cr_components/color_change_listener/*",
    "@material/*|$root_src_dir/third_party/material_web_components/components-chromium/node_modules/@material/*",
  ]

  foreach(_mwc_mapping, mwc_path_mappings) {
    path_mappings +=
        [ _mwc_mapping[0] + "|" + rebase_path(_mwc_mapping[1], target_gen_dir) ]
  }

  composite = true
  deps = [
    "//third_party/material_web_components:library",
    "//ui/webui/resources/cr_components/color_change_listener:build_ts",
  ]
}

# Downstream ts_library targets that depend on `cros_components_ts` expect
# tsconfig_cros_components_ts.json to exist so copy the tsconfig of
# `:cros_components__ts_library`.
copy("rename_tsconfig") {
  visibility = [ ":*" ]
  sources = [ "$target_gen_dir/tsconfig_cros_components__ts_library.json" ]
  outputs = [ "$target_gen_dir/tsconfig_cros_components_ts.json" ]
  deps = [ ":cros_components__ts_library" ]
}

rewrite_imports("cros_components_ts") {
  base_dir =
      "$root_gen_dir/ui/webui/resources/tsc/cros_components/to_be_rewritten"

  # Rollup maps URLs like `chrome://resources/cros_components/*` to
  # files in
  # `$root_gen_dir/ui/webui/resources/tsc/cros_components/*`.
  out_dir = "$root_gen_dir/ui/webui/resources/tsc/cros_components/"

  in_files = []
  foreach(_file, _cros_components_ts_files) {
    in_files += [ string_replace(_file, ".ts", ".js") ]
  }

  import_mappings = [
    "lit|//resources/mwc/lit/index.js",
    "@lit/task|//resources/mwc/lit/index.js",

    "@material/|chrome://resources/mwc/@material/",

    # Add js prefix's to any internal imports so they work at runtime.
    "(?P<filepath>\.\.?/[\w\-/]+)\$|\g<filepath>.js",
  ]

  deps = [
    ":cros_components__ts_library",
    ":rename_tsconfig",
    "//third_party/material_web_components:mwc_rewrite",
  ]
}

generate_grd("cros_components_grdp") {
  grd_prefix = "cros_components"
  out_grd = "${target_gen_dir}/resources.grdp"

  manifest_files = []

  deps = [ ":cros_components_ts" ]
  manifest_files += filter_include(get_target_outputs(":cros_components_ts"),
                                   [ "*.manifest" ])
  resource_path_prefix = "cros_components"
  input_files_base_dir = rebase_path(".", "//")
  input_files = [ "src/lottie_renderer/lottie_worker.js" ]
  resource_path_rewrites = [
    "src/lottie_renderer/lottie_worker.js|lottie_renderer/lottie_worker.js",
  ]
}