• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# -*- bazel-starlark -*-
2# Copyright 2025 The Chromium Authors
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5"""Siso configuration for devtools-frontend."""
6
7load("@builtin//path.star", "path")
8load("@builtin//struct.star", "module")
9load("./config.star", "config")
10load("./tsc.star", "tsc")
11
12# TODO: crbug.com/1478909 - Specify typescript inputs in GN config.
13def __filegroups(ctx):
14    return {
15        "third_party/devtools-frontend/src/node_modules/typescript:typescript": {
16            "type": "glob",
17            "includes": ["*"],
18        },
19        "third_party/devtools-frontend/src/node_modules:node_modules": {
20            "type": "glob",
21            "includes": ["*.js", "*.json", "*.ts"],
22        },
23    }
24
25def __step_config(ctx, step_config):
26    step_config["input_deps"].update({
27        "third_party/devtools-frontend/src/third_party/typescript/ts_library.py": [
28            "third_party/devtools-frontend/src/node_modules/typescript:typescript",
29            "third_party/devtools-frontend/src/node_modules:node_modules",
30        ],
31    })
32
33    # TODO: b/308405411 - Enable remote-devtools-frontend-typescript by default.
34    if config.get(ctx, "remote-devtools-frontend-typescript"):
35        step_config["rules"].extend([
36            {
37                "name": "devtools-frontend/typescript/ts_library",
38                "command_prefix": "python3 ../../third_party/devtools-frontend/src/third_party/typescript/ts_library.py",
39                "remote": True,
40                "handler": "devtools_frontend/typescript_ts_library",
41                "output_local": True,
42                "timeout": "2m",
43            },
44        ])
45    return step_config
46
47def _ts_library(ctx, cmd):
48    # Handler for https://crsrc.org/c/third_party/devtools-frontend/src/third_party/typescript/ts_library.py
49    # Note that this is a different script from https://crsrc.org/c/tools/typescript/ts_library.py
50    deps = []
51    sources = []
52    tsconfig_path = None
53    flag = None
54    for i, arg in enumerate(cmd.args):
55        if flag != "" and arg.startswith("-"):
56            flag = ""
57        if arg == "--tsconfig_output_location":
58            tsconfig_path = ctx.fs.canonpath(cmd.args[i + 1])
59            continue
60        if arg in ("--deps", "--sources"):
61            flag = arg
62            continue
63        if flag == "--deps":
64            deps.append(arg)
65            continue
66        if flag == "--sources":
67            sources.append(ctx.fs.canonpath(arg))
68            continue
69    if not tsconfig_path:
70        fail("missing --tsconfig_output_location")
71    tsconfig = {"files": [], "references": []}
72    tsconfig_dir = path.dir(tsconfig_path)
73    for s in sources:
74        tsconfig["files"].append(path.rel(tsconfig_dir, s))
75    for d in deps:
76        tsconfig["references"].append({"path": d})
77        refpath = path.join(tsconfig_dir, d)
78        refdir = path.dir(refpath)
79
80        # TODO: crbug.com/1503020 - Fix devtools_entrypoint to propagate .d.ts output.
81        dpath = path.join(refdir, path.base(refdir) + ".d.ts")
82        if ctx.fs.exists(dpath):
83            sources.append(dpath)
84
85    inputs = tsc.scandeps(ctx, tsconfig_path, tsconfig)
86
87    # Sources and imported files might be located in different dirs. source vs gen.
88    # Try to collect the corresponding files in source or gen dir.
89    # TODO: crbug.com/1505319 - Fix devtools_module import issues.
90    files = {}
91    gen_dir = None
92
93    # Infer source files from gen file.
94    for f in cmd.inputs + inputs:
95        if f.startswith("out/"):
96            # Remove out/{subdir}/gen.
97            splits = f.split("/", 3)
98            if len(splits) < 4:
99                continue
100            gen_dir = path.join(splits[0], splits[1], splits[2])
101            f = splits[3]
102            if ctx.fs.exists(f) and not f in files:
103                files[f] = True
104                continue
105            if f.endswith(".js"):
106                f = f.removesuffix(".js") + ".d.ts"
107                if ctx.fs.exists(f) and not f in files:
108                    files[f] = True
109
110    # Infer gen files from source file.
111    if gen_dir:
112        for f in cmd.inputs + inputs:
113            if f.endswith(".ts"):
114                f = path.join(gen_dir, f)
115                f = f.removesuffix(".ts") + ".d.ts"
116                if ctx.fs.exists(f) and not f in files:
117                    files[f] = True
118            if f.endswith(".js"):
119                f = path.join(gen_dir, f)
120                f = f.removesuffix(".js") + ".d.ts"
121                if ctx.fs.exists(f) and not f in files:
122                    files[f] = True
123
124    ctx.actions.fix(inputs = cmd.inputs + inputs + sources + files.keys())
125
126devtools_frontend = module(
127    "devtools_frontend",
128    step_config = __step_config,
129    handlers = {
130        "devtools_frontend/typescript_ts_library": _ts_library,
131    },
132    filegroups = __filegroups,
133)
134