chromium/ui/webui/resources/tools/bundle_js.gni

# 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("//build/config/chromeos/ui_mode.gni")
import("//third_party/node/node.gni")

template("bundle_js") {
  node(target_name) {
    script = "//ui/webui/resources/tools/bundle_js.py"

    forward_variables_from(invoker,
                           [
                             "visibility",
                             "deps",
                           ])

    # This depfile is generated by bundle_js.py
    depfile = "${target_gen_dir}/${target_name}.d"

    out_folder = target_gen_dir
    if (defined(invoker.out_folder)) {
      out_folder = invoker.out_folder
    }

    outputs = []
    input_count = 0
    foreach(f, invoker.js_module_in_files) {
      assert(get_path_info(f, "extension") == "js")
      bundle_path = get_path_info(f, "dir")
      input_count = input_count + 1
      out_file = string_replace(get_path_info(f, "file"), ".js", ".rollup.js")
      outputs += [
        "$out_folder/$bundle_path/$out_file",
        "$out_folder/$bundle_path/${out_file}.map",
      ]
      if (input_count == 2) {
        outputs += [
          "$out_folder/$bundle_path/shared.rollup.js",
          "$out_folder/$bundle_path/shared.rollup.js.map",
        ]
      }
    }

    outputs += [ "$out_folder/${target_name}_requestlist.txt" ]

    # Note that we have to manually pass the sources to our script if the
    # script needs them as inputs.
    args = [
      "--host",
      invoker.host,
      "--input",
      invoker.input,
      "--out_folder",
      rebase_path(out_folder, root_build_dir),
      "--depfile",
      rebase_path(depfile, root_build_dir),
      "--target_name",
      target_name,
    ]

    inputs = []
    if (defined(invoker.rollup_config)) {
      inputs += [ invoker.rollup_config ]
      args += [
        "--rollup_config",
        rebase_path(invoker.rollup_config, root_build_dir),
      ]
    } else {
      inputs += [ "//ui/webui/resources/tools/rollup_plugin.mjs" ]
      outputs += [ "$out_folder/rollup.config.mjs" ]
    }

    if (defined(invoker.excludes)) {
      args += [ "--exclude" ] + invoker.excludes
    }

    external_paths = []

    # Add invoker.external_paths before the default external_paths so that they
    # are considered first.
    if (defined(invoker.external_paths)) {
      external_paths += invoker.external_paths
    }

    # Specify default external_paths. The order here matters in multiple ways.
    #   1) chrome:// URLs need to be listed before scheme relative URLs
    #      (otherwise chrome-extension:// contexts break)
    #   2) Longer prefixes need to be listed before shorter prefixes, otherwise
    #      mappings are incorrect.
    #   3) chrome:// URLs should not be listed at all for chrome-untrusted://
    #      contexts, otherwise generated bundle includes chrome:// URLs which is
    #      incorrect.

    polymer_path =
        rebase_path("//third_party/polymer/v3_0/components-chromium/",
                    root_build_dir)
    resources_path =
        rebase_path("$root_gen_dir/ui/webui/resources/tsc/", root_build_dir)
    is_chrome_untrusted =
        string_replace(invoker.host, "chrome-untrusted://", "") != invoker.host

    if (is_chromeos_ash) {
      ash_resources_path =
          rebase_path("$root_gen_dir/ash/webui/common/resources/preprocessed/",
                      root_build_dir)
      if (!is_chrome_untrusted) {
        external_paths +=
            [ "chrome://resources/ash/common/|$ash_resources_path" ]
      }
      external_paths += [ "//resources/ash/common/|$ash_resources_path" ]
    }

    if (!is_chrome_untrusted) {
      external_paths += [ "chrome://resources/polymer/v3_0/|$polymer_path" ]
    }
    external_paths += [ "//resources/polymer/v3_0/|$polymer_path" ]

    lit_path = rebase_path("${root_gen_dir}/third_party/lit/v3_0/minified/",
                           root_build_dir)
    if (!is_chrome_untrusted) {
      external_paths += [ "chrome://resources/lit/v3_0/|$lit_path" ]
    }
    external_paths += [ "//resources/lit/v3_0/|$lit_path" ]

    if (!is_chrome_untrusted) {
      external_paths += [ "chrome://resources/|$resources_path" ]
    }
    external_paths += [ "//resources/|$resources_path" ]

    args += [ "--external_paths" ] + external_paths

    args += [ "--js_module_in_files" ] + invoker.js_module_in_files

    if (defined(invoker.out_manifest)) {
      args += [
        "--out-manifest",
        rebase_path(invoker.out_manifest, root_build_dir),
      ]
      outputs += [ invoker.out_manifest ]
    }
  }
}