Reland "Fix typescript compilation with ts_library"

This reverts commit a7de1bbca072ff9d5a46d17ad5c71c01d1ed2288.

Reason for revert: Suspected issue with local machine. Chromium CQ was green.

Original change's description:
> Revert "Fix typescript compilation with ts_library"
> 
> This reverts commit 85de0bc72ef7929abfa352bd7a0efa016cdf5fbb.
> 
> Reason for revert: From Chase: build error after this change rolled into Chromium at https://crrev.com/c/2008257
> 
> DISABLE_THIRD_PARTY_CHECK=Bypassing checks for revert
> 
> Original change's description:
> > Fix typescript compilation with ts_library
> >
> > This CL prepares ts_library to work correctly by building both in Chromium
> > as well as in DevTools. A follow-up CL will add a simple ts_library
> > to verify that it keeps on working.
> >
> > Bug: 1011811
> > Change-Id: I482761bfbcbf459478dc7f51f2df6576ac872de6
> > DISABLE_THIRD_PARTY_CHECK=TypeScript build configuration changes
> > Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/1960456
> > Reviewed-by: Paul Lewis <[email protected]>
> > Commit-Queue: Tim van der Lippe <[email protected]>
> 
> [email protected],[email protected],[email protected]
> 
> 
> Bug: 1011811
> Change-Id: Id354882f8f8706241ac377e62ff68cc9eb6ff994
> Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/2013141
> Reviewed-by: Paul Lewis <[email protected]>
> Commit-Queue: Paul Lewis <[email protected]>

[email protected],[email protected],[email protected]

Change-Id: Iafc3a005d53fc040ca2a1708c6f2fef1ac53c48d
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Bug: 1011811
Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/2013144
Reviewed-by: Tim van der Lippe <[email protected]>
Commit-Queue: Tim van der Lippe <[email protected]>
diff --git a/BUILD.gn b/BUILD.gn
index 15d1d0a..b7ce283 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -2186,20 +2186,22 @@
   ]
 }
 
-devtools_frontend_resources_deps = [
-  ":aria_properties",
-  ":build_release_devtools",
-  ":copy_devtools_modules",
-  ":copy_embedder_scripts",
-  ":copy_emulated_devices_images",
-  ":copy_htaccess",
-  ":copy_inspector_images",
-  ":copy_lighthouse_locale_files",
-  ":copy_wasm_deps",
-  ":devtools_extension_api",
-  ":frontend_protocol_sources",
-  ":supported_css_properties",
-]
+devtools_typescript_deps = [ "front_end/common:common" ]
+
+devtools_frontend_resources_deps = devtools_typescript_deps + [
+                                     ":aria_properties",
+                                     ":build_release_devtools",
+                                     ":copy_devtools_modules",
+                                     ":copy_embedder_scripts",
+                                     ":copy_emulated_devices_images",
+                                     ":copy_htaccess",
+                                     ":copy_inspector_images",
+                                     ":copy_lighthouse_locale_files",
+                                     ":copy_wasm_deps",
+                                     ":devtools_extension_api",
+                                     ":frontend_protocol_sources",
+                                     ":supported_css_properties",
+                                   ]
 
 if (debug_devtools) {
   devtools_frontend_resources_deps += [
diff --git a/build_overrides/BUILDCONFIG.gn b/build_overrides/BUILDCONFIG.gn
index cee7a95..ac85c0f 100644
--- a/build_overrides/BUILDCONFIG.gn
+++ b/build_overrides/BUILDCONFIG.gn
@@ -40,6 +40,11 @@
   # every toolchain can pass through the "global" value via toolchain_args().
   host_toolchain = ""
 
+  # Needed to properly resolve the locations for the TypeScript build system
+  # when we are building for DevTools only. This arg will not be defined in
+  # Chromium itself.
+  use_devtools_typescript = true
+
   # DON'T ADD MORE FLAGS HERE. Read the comment above.
 }
 
diff --git a/front_end/common/BUILD.gn b/front_end/common/BUILD.gn
new file mode 100644
index 0000000..4c830dc
--- /dev/null
+++ b/front_end/common/BUILD.gn
@@ -0,0 +1,17 @@
+# Copyright 2019 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+import("../../third_party/typescript/typescript.gni")
+
+group("common") {
+  public_deps = [
+    ":trie",
+  ]
+}
+
+ts_library("trie") {
+  sources = [
+    "Trie.js",
+  ]
+}
diff --git a/third_party/typescript/ts_library.py b/third_party/typescript/ts_library.py
index 5b47faf..5c285c4 100644
--- a/third_party/typescript/ts_library.py
+++ b/third_party/typescript/ts_library.py
@@ -11,11 +11,21 @@
 _CURRENT_DIR = path.join(path.dirname(__file__))
 TSC_LOCATION = path.join(_CURRENT_DIR, '..', '..', 'node_modules', 'typescript', 'bin', 'tsc')
 
-ROOT_TS_CONFIG_LOCATION = path.join(_CURRENT_DIR, 'tsconfig.json')
+try:
+    old_sys_path = sys.path[:]
+    sys.path.append(path.join(_CURRENT_DIR, '..', '..', 'scripts'))
+    import devtools_paths
+finally:
+    sys.path = old_sys_path
+NODE_LOCATION = devtools_paths.node_path()
+
+ROOT_TS_CONFIG_LOCATION = path.join(_CURRENT_DIR, '..', '..', 'tsconfig.json')
 
 
 def runTsc(tsconfig_location):
-    process = subprocess.Popen([TSC_LOCATION, '-b', tsconfig_location], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+    process = subprocess.Popen([NODE_LOCATION, TSC_LOCATION, '-b', tsconfig_location],
+                               stdout=subprocess.PIPE,
+                               stderr=subprocess.PIPE)
     stdout, stderr = process.communicate()
     # TypeScript does not correctly write to stderr because of https://github.com/microsoft/TypeScript/issues/33849
     return process.returncode, stdout + stderr
@@ -24,7 +34,7 @@
 def main():
     parser = argparse.ArgumentParser()
     parser.add_argument('-s', '--sources', nargs='*', required=True, help='List of TypeScript source files')
-    parser.add_argument('-b', '--tsconfig_location', required=True)
+    parser.add_argument('-b', '--tsconfig_output_location', required=True)
     opts = parser.parse_args()
     with open(ROOT_TS_CONFIG_LOCATION) as root_tsconfig:
         try:
@@ -33,7 +43,7 @@
             print('Encountered error while loading root tsconfig:')
             print(e)
             return 1
-    tsconfig_output_location = path.join(os.getcwd(), opts.tsconfig_location)
+    tsconfig_output_location = path.join(os.getcwd(), opts.tsconfig_output_location)
     tsconfig['files'] = [path.join(os.getcwd(), src) for src in opts.sources]
     tsconfig['compilerOptions']['outDir'] = path.dirname(tsconfig_output_location)
     with open(tsconfig_output_location, 'w') as generated_tsconfig:
diff --git a/third_party/typescript/tsconfig.json b/third_party/typescript/tsconfig.json
deleted file mode 100644
index 1d5057b..0000000
--- a/third_party/typescript/tsconfig.json
+++ /dev/null
@@ -1,6 +0,0 @@
-{
-  "compilerOptions": {
-    "target": "esnext",
-    "module": "esnext"
-  }
-}
\ No newline at end of file
diff --git a/third_party/typescript/typescript.gni b/third_party/typescript/typescript.gni
index 9f8f989..e2b5a4a 100644
--- a/third_party/typescript/typescript.gni
+++ b/third_party/typescript/typescript.gni
@@ -2,7 +2,11 @@
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
 
-script_path = "//third_party/typescript"
+if (defined(use_devtools_typescript) && use_devtools_typescript) {
+  devtools_location = ""
+} else {
+  devtools_location = "third_party/devtools-frontend/src"
+}
 
 # Defines a target that compiles .ts files using TypeScript.
 # A temporary tsconfig.json is generated which uses the
@@ -17,17 +21,25 @@
 #   }
 template("ts_library") {
   action(target_name) {
-    script = "//third_party/typescript/ts_library.py"
+    script = "//$devtools_location/third_party/typescript/ts_library.py"
 
-    forward_variables_from(invoker, [ "sources", "visibility" ])
+    forward_variables_from(invoker,
+                           [
+                             "sources",
+                             "visibility",
+                           ])
+
+    inputs = [
+      "//$devtools_location/tsconfig.json",
+    ]
 
     args = [
-      "--tsconfig_location",
-      rebase_path(target_gen_dir, root_build_dir) + "/tsconfig.json",
+      "--tsconfig_output_location",
+      rebase_path(target_gen_dir, root_build_dir) + "/$target_name-tsconfig.json",
     ]
     args += [ "--sources" ] + rebase_path(sources, root_build_dir)
 
-    output_files = [ "$target_gen_dir/tsconfig.json" ]
+    output_files = [ "$target_gen_dir/$target_name-tsconfig.json" ]
 
     foreach(src, sources) {
       js_renamed_path =
@@ -42,5 +54,5 @@
 set_defaults("ts_library") {
   # Build output should be private and directories should export all relevant
   # components in a group
-  visibility = [":*"]
+  visibility = [ ":*" ]
 }
diff --git a/tsconfig.json b/tsconfig.json
index e963c4d..91fa835 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -18,6 +18,5 @@
     "forceConsistentCasingInFileNames": true,
     "noImplicitReturns": true,
     "noFallthroughCasesInSwitch": true
-    }
   }
 }