chromium/ui/file_manager/BUILD.gn

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

import("//chrome/common/features.gni")
import(
    "//tools/code_coverage/js_source_maps/create_js_source_maps/create_js_source_maps.gni")
import(
    "//tools/code_coverage/js_source_maps/merge_js_source_maps/merge_js_source_maps.gni")
import("//tools/grit/grit_rule.gni")
import("//tools/grit/preprocess_if_expr.gni")
import("//tools/polymer/html_to_wrapper.gni")
import("//tools/typescript/ts_definitions.gni")
import("//tools/typescript/webui_ts_library.gni")
import("//ui/file_manager/file_names.gni")
import("//ui/webui/resources/tools/generate_grd.gni")
import("//ui/webui/webui_features.gni")

grit("resources") {
  source = "file_manager_resources.grd"
  defines = [ "optimize_webui=$optimize_webui" ]
  outputs = [
    "grit/file_manager_resources.h",
    "grit/file_manager_resources_map.cc",
    "grit/file_manager_resources_map.h",
    "file_manager_resources.pak",
    "file_manager_resources.rc",
  ]
}

component("file_manager") {
  sources = [ "file_manager_export.h" ]
  public_deps = [
    ":file_manager_gen_resources",
    ":resources",
    "//base",
  ]
  defines = [ "FILE_MANAGER_IMPLEMENTATION" ]
}

preprocess_folder = "$target_gen_dir/preprocessed"

# Move files to the preprocessed folder to combine all resources in the same
# folder for JS bundling with rollup.
# This is for files direct from the src/:  input_folder="./".
preprocess_if_expr("preprocess_static") {
  in_folder = "./"
  out_manifest = "$target_gen_dir/manifest_preprocess_static.json"
  if (enable_webui_inline_sourcemaps) {
    out_folder = "${preprocess_folder}/_tmp"
    enable_removal_comments = enable_webui_inline_sourcemaps
  } else {
    out_folder = preprocess_folder
  }

  in_files = []
}

if (enable_webui_inline_sourcemaps) {
  create_js_source_maps("create_source_maps_static") {
    inline_sourcemaps = true

    sources =
        filter_include(get_target_outputs(":preprocess_static"), [ "*.js" ])
    originals = []
    outputs = []

    foreach(in_file, sources) {
      rebased_path = rebase_path(in_file, "${preprocess_folder}/_tmp")
      originals += [ rebased_path ]
      outputs += [ "${preprocess_folder}/" + rebased_path ]
    }
    deps = [ ":preprocess_static" ]
  }
}

preprocess_if_expr("preprocess_static_image_loader") {
  in_folder = "."
  out_manifest = "$target_gen_dir/manifest_preprocess_static_image_loader.json"
  if (enable_webui_inline_sourcemaps) {
    out_folder = "${preprocess_folder}/_tmp"
    enable_removal_comments = enable_webui_inline_sourcemaps
  } else {
    out_folder = preprocess_folder
  }

  in_files = image_loader_ts
}

if (enable_webui_inline_sourcemaps) {
  create_js_source_maps("create_source_maps_image_loader") {
    inline_sourcemaps = true

    sources =
        filter_include(get_target_outputs(":preprocess_static_image_loader"),
                       [
                         "*.js",
                         "*.ts",
                       ])
    originals = []
    outputs = []

    foreach(in_file, sources) {
      rebased_path = rebase_path(in_file, "${preprocess_folder}/_tmp")
      originals += [ rebased_path ]
      outputs += [ "${preprocess_folder}/" + rebased_path ]
    }
    deps = [ ":preprocess_static_image_loader" ]
  }
}

# This is for files that are generated in "gen/": input_folder=target_gen_dir.
preprocess_if_expr("preprocess_generated") {
  in_folder = target_gen_dir
  out_folder = preprocess_folder
  out_manifest = "$target_gen_dir/manifest_preprocess_generated.json"

  in_files = generated_js_files + ts_generated_templates

  deps = [
    ":html_wrapper_files",
    "//ui/file_manager/base/gn:file_types_data_gen_js",
    "//ui/file_manager/file_manager/foreground/elements:polymer_elements",
  ]
}

common_path_mappings = [
  "chrome-extension://pmfjbimdmchhbnneeidfognadeopoehp/*|preprocessed/image_loader/*",
  "tslib|" + rebase_path(
          "//third_party/material_web_components/components-chromium/node_modules/tslib/tslib.d.ts",
          target_gen_dir),
  "chrome://resources/mwc/*|" + rebase_path(
          "//third_party/material_web_components/components-chromium/node_modules/*",
          target_gen_dir),
  "chrome://resources/cr_components/color_change_listener/*|" + rebase_path(
          "$root_gen_dir/ui/webui/resources/tsc/cr_components/color_change_listener/*",
          target_gen_dir),
  "chrome://file-manager/*|" +
      rebase_path("$preprocess_folder/file_manager/*", target_gen_dir),
]

webui_ts_library("build_ts") {
  root_dir = preprocess_folder
  if (enable_webui_inline_sourcemaps) {
    out_dir = "$target_gen_dir/tsc/_tmp"
  } else {
    out_dir = "$target_gen_dir/tsc"
  }
  tsconfig_base = "tsconfig_base.json"
  composite = true

  path_mappings = common_path_mappings

  extra_deps = [
    ":copy_definitions_to_gen",
    ":copy_definitions_to_preprocess",
    ":generate_definitions",
    ":preprocess_generated",
    "//ash/webui/common/resources:generate_definitions",
  ]

  if (enable_webui_inline_sourcemaps) {
    enable_source_maps = true
    extra_deps += [
      ":create_source_maps_copy_ts",
      ":create_source_maps_image_loader",
      ":create_source_maps_static",
    ]
  } else {
    extra_deps += [
      ":copy_ts",
      ":preprocess_static",
      ":preprocess_static_image_loader",
    ]
  }

  deps = [
    "//ash/webui/common/resources:build_ts",
    "//ash/webui/common/resources/cr_elements:build_ts",
    "//third_party/cros-components:cros_components_ts",
    "//third_party/material_web_components:bundle_lit_ts",
    "//third_party/material_web_components:library",
    "//third_party/polymer/v3_0:library",
    "//ui/webui/resources/js:build_ts",
  ]

  in_files = generated_js_files + ts_files + ts_polymer +
             ts_generated_templates + shared_image_loader_ts

  definitions = other_dts_files
  foreach(_file, generate_definitions_js_files) {
    definitions +=
        [ "$target_gen_dir/" + string_replace(_file, ".js", ".d.ts") ]
  }
  foreach(_file, checked_in_dts_files) {
    definitions += [ "$preprocess_folder/$_file" ]
  }
}

if (enable_webui_inline_sourcemaps) {
  merge_js_source_maps("merge_source_maps") {
    deps = [ ":build_ts" ]
    manifest_files =
        filter_include(get_target_outputs(":build_ts"), [ "*_manifest.json" ])
    js_files = filter_include(get_target_outputs(":build_ts"), [ "*.js" ])

    sources = []
    outputs = []

    out_dir = "$target_gen_dir/tsc"
    foreach(_js_file, js_files) {
      sources += [ _js_file ]
      outputs +=
          [ string_replace(_js_file, "$target_gen_dir/tsc/_tmp", out_dir) ]
    }
  }
}

webui_ts_library("test_build_ts") {
  testonly = true
  root_dir = preprocess_folder
  out_dir = "$target_gen_dir/tsc"
  tsconfig_base = "tsconfig_base.json"
  enable_source_maps = false

  in_files = ts_test_files

  path_mappings =
      common_path_mappings + [
        # for chai_assert.js.
        "chrome://webui-test/chromeos/*|" +
            rebase_path("$root_gen_dir/chrome/test/data/webui/chromeos/tsc/*",
                        target_gen_dir),
        "chrome://webui-test/*|" +
            rebase_path("$root_gen_dir/chrome/test/data/webui/tsc/*",
                        target_gen_dir),
      ]

  definitions = other_dts_files
  foreach(_file, checked_in_dts_files) {
    definitions += [ "$preprocess_folder/$_file" ]
  }

  deps = [
    ":build_ts",
    "//ash/webui/common/resources:build_ts",
    "//ash/webui/common/resources/cr_elements:build_ts",
    "//chrome/test/data/webui:build_ts",
    "//chrome/test/data/webui/chromeos:build_ts",
    "//third_party/cros-components:cros_components_ts",
    "//third_party/material_web_components:bundle_lit_ts",
    "//third_party/material_web_components:library",
    "//third_party/polymer/v3_0:library",
    "//ui/webui/resources/cr_components/color_change_listener:build_ts",
    "//ui/webui/resources/js:build_ts",
  ]
  extra_deps = [
    ":copy_definitions_to_gen",
    ":copy_definitions_to_preprocess",
  ]
  if (enable_webui_inline_sourcemaps) {
    extra_deps += [
      ":create_source_maps_copy_ts",
      ":create_source_maps_static",
    ]
  } else {
    extra_deps += [
      ":copy_ts",
      ":preprocess_static",
    ]
  }
}

ts_definitions("generate_definitions") {
  root_dir = "."
  out_dir = target_gen_dir

  extra_deps = [ "//ash/webui/common/resources:generate_definitions" ]

  js_files = generate_definitions_js_files
}

# Copy definition files to GEN folder, so they can import relatively the
# generated definitions.
copy("copy_definitions_to_gen") {
  sources = checked_in_dts_files
  outputs = [ "$target_gen_dir/{{source_target_relative}}" ]
}

# Copy definition files to $preprocess_folder so TSC can find them.
copy("copy_definitions_to_preprocess") {
  sources = checked_in_dts_files
  outputs = [ "$preprocess_folder/{{source_target_relative}}" ]
}

preprocess_if_expr("copy_ts") {
  in_folder = "."
  out_manifest = "$target_gen_dir/manifest_preprocess_copy_ts.json"
  if (enable_webui_inline_sourcemaps) {
    out_folder = "${preprocess_folder}/_tmp"
    enable_removal_comments = enable_webui_inline_sourcemaps
  } else {
    out_folder = preprocess_folder
  }

  in_files = ts_files + ts_test_files + ts_polymer
}

if (enable_webui_inline_sourcemaps) {
  create_js_source_maps("create_source_maps_copy_ts") {
    inline_sourcemaps = true

    sources = filter_include(get_target_outputs(":copy_ts"), [ "*.ts" ])
    originals = []
    outputs = []

    foreach(in_file, sources) {
      rebased_path = rebase_path(in_file, "${preprocess_folder}/_tmp")
      originals += [ rebased_path ]
      outputs += [ "${preprocess_folder}/" + rebased_path ]
    }
    deps = [ ":copy_ts" ]
  }
}

copy("for_image_loader") {
  sources = [
    "$target_gen_dir/tsc/file_manager/common/js/file_types_base.js",
    "$target_gen_dir/tsc/file_manager/common/js/lru_cache.js",
  ]
  outputs = [ "$target_gen_dir/file_manager/common/js/{{source_file_part}}" ]
  if (enable_webui_inline_sourcemaps) {
    deps = [ ":merge_source_maps" ]
  } else {
    deps = [ ":build_ts" ]
  }
}

copy("for_image_loader_unittests") {
  sources = []
  foreach(f, image_loader_js) {
    sources += [ "$target_gen_dir/image_loader/tsc/$f" ]
  }

  deps = [ "image_loader:build_ts" ]

  outputs = [ "$target_gen_dir/image_loader/{{source_file_part}}" ]
}

grd_prefix = "file_manager_gen"
generated_grd = "$target_gen_dir/${grd_prefix}_resources.grd"

generate_grd("build_grd") {
  out_grd = generated_grd

  grdp_files = [ "$target_gen_dir/file_manager/static_resources.grdp" ]

  deps = [
    "file_manager:build_static_grdp",
    "//ui/file_manager/image_loader:build",
  ]

  input_files_base_dir = rebase_path(target_gen_dir, root_build_dir)
  input_files = [ "image_loader/background.rollup.js" ]

  resource_path_rewrites =
      [ "image_loader/background.rollup.js|image_loader/background.js" ]

  if (optimize_webui) {
    input_files +=
        [
          "file_manager/background/js/runtime_loaded_test_util.rollup.js",
          "file_manager/foreground/js/deferred_elements.rollup.js",
          "file_manager/foreground/js/main.rollup.js",
          "file_manager/foreground/js/metadata/metadata_dispatcher.rollup.js",
          "file_manager/foreground/js/shared.rollup.js",

          # For Image Loader unittests:
          "file_manager/common/js/file_types_base.js",
          "file_manager/common/js/file_types_data.js",
          "file_manager/common/js/lru_cache.js",
        ] + shared_image_loader_js

    resource_path_rewrites += [
      "file_manager/background/js/runtime_loaded_test_util.rollup.js|file_manager/background/js/runtime_loaded_test_util.js",
      "file_manager/foreground/js/deferred_elements.rollup.js|file_manager/foreground/js/deferred_elements.js",
      "file_manager/foreground/js/main.rollup.js|file_manager/foreground/js/main.js",
      "file_manager/foreground/js/metadata/metadata_dispatcher.rollup.js|file_manager/foreground/js/metadata/metadata_dispatcher.js",
    ]

    deps += [
      ":for_image_loader",
      ":for_image_loader_unittests",
      "//ui/file_manager/file_manager/background/js:build",
      "//ui/file_manager/file_manager/foreground/js:build",
      "//ui/file_manager/file_manager/foreground/js:build_worker",
    ]
  } else {
    if (enable_webui_inline_sourcemaps) {
      deps += [ ":merge_source_maps" ]
      manifest_files = filter_include(get_target_outputs(":merge_source_maps"),
                                      [ "*_manifest__processed.json" ])
    } else {
      deps += [ ":build_ts" ]
      manifest_files =
          filter_include(get_target_outputs(":build_ts"), [ "*manifest.json" ])
    }
  }
}

# Resources for the JS modules.
grit("file_manager_gen_resources") {
  defines = chrome_grit_defines

  # These arguments are needed since the grd is generated at build time.
  enable_input_discovery_for_gn_analyze = false

  source = generated_grd
  deps = [ ":build_grd" ]

  outputs = [
    "grit/file_manager_gen_resources.h",
    "grit/file_manager_gen_resources_map.cc",
    "grit/file_manager_gen_resources_map.h",
    "file_manager_gen_resources.pak",
  ]
  output_dir = target_gen_dir
}

html_to_wrapper("html_wrapper_files") {
  in_files = ts_templates
  template = "native"
}

# GRD for test files.
generate_grd("build_tests_grdp") {
  testonly = true
  grd_prefix = "file_manager_test"
  out_grd = "$target_gen_dir/tests_resources.grdp"

  input_files_base_dir = rebase_path(".", "//")
  input_files = unittest_harness
}

# GRD for the actual application files that are processed by TS compiler.
generate_grd("build_tests_gen_grdp") {
  testonly = true
  grd_prefix = "file_manager_test"
  out_grd = "$target_gen_dir/tests_gen_resources.grdp"

  deps = [
    ":for_image_loader_unittests",
    ":test_build_ts",
  ]

  manifest_files = filter_include(get_target_outputs(":test_build_ts"),
                                  [ "*_manifest.json" ])

  input_files_base_dir = rebase_path(target_gen_dir, root_build_dir)

  # Removed `shared_image_loader_js` because it's already included via the
  # `:build_ts` below.
  input_files = image_loader_js - shared_image_loader_js

  if (enable_webui_inline_sourcemaps) {
    deps += [ ":merge_source_maps" ]
    manifest_files += filter_include(get_target_outputs(":merge_source_maps"),
                                     [ "*_manifest__processed.json" ])
  } else {
    deps += [ ":build_ts" ]
    manifest_files +=
        filter_include(get_target_outputs(":build_ts"), [ "*manifest.json" ])
  }
}