Switch tsc_wrapped to a .js entry point

This lets it work under the linker, where the execpath to the .js file
is hard to calculate from the .ts input file path.

This also wires up the ts-lit-plugin under Bazel if it appears in the tsconfig settings.

Closes #490

PiperOrigin-RevId: 294743226
diff --git a/.bazelci/presubmit.yml b/.bazelci/presubmit.yml
index 86727d1..9695da4 100644
--- a/.bazelci/presubmit.yml
+++ b/.bazelci/presubmit.yml
@@ -1,4 +1,5 @@
 ---
+bazel: 2.1.0
 platforms:
   ubuntu1604:
     run_targets:
diff --git a/.bazelversion b/.bazelversion
new file mode 100644
index 0000000..50aea0e
--- /dev/null
+++ b/.bazelversion
@@ -0,0 +1 @@
+2.1.0
\ No newline at end of file
diff --git a/internal/BUILD.bazel b/internal/BUILD.bazel
index 0c70b53..bd2aa0c 100644
--- a/internal/BUILD.bazel
+++ b/internal/BUILD.bazel
@@ -17,6 +17,7 @@
 load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_binary")
 load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
 load("@npm_bazel_jasmine//:index.bzl", "jasmine_node_test")
+load("@npm//typescript:index.bzl", "tsc")
 load("@npm_bazel_typescript//:index.bzl", "ts_library")
 
 package(default_visibility = ["//visibility:public"])
@@ -29,43 +30,35 @@
     visibility = ["//visibility:public"],
 )
 
-# Vanilla typescript compiler: run the tsc.js binary distributed by TypeScript
-nodejs_binary(
-    name = "tsc",
-    data = [
-        "@npm//source-map-support",
-        "@npm//typescript",
+_TSC_WRAPPED_TESTS = [
+    "**/test_support.ts",
+    "**/*_test.ts",
+]
+
+_TSC_WRAPPED_SRCS = glob(
+    [
+        "tsc_wrapped/*.ts",
+        "tsetse/**/*.ts",
     ],
-    entry_point = "@npm//:node_modules/typescript/lib/tsc.js",
-    visibility = ["//internal:__subpackages__"],
+    exclude = _TSC_WRAPPED_TESTS + ["tsetse/tests/**"],
 )
 
+_TSC_WRAPPED_JS = [k[:-3] + ".js" for k in _TSC_WRAPPED_SRCS]
+_TSC_WRAPPED_TYPINGS = [k[:-3] + ".d.ts" for k in _TSC_WRAPPED_SRCS]
 # Build our custom compiler using the vanilla one
-ts_library(
+tsc(
     name = "tsc_wrapped",
-    srcs = glob(
-        [
-            "tsc_wrapped/*.ts",
-            "tsetse/**/*.ts",
-        ],
-        exclude = [
-            "**/test_support.ts",
-            "**/*_test.ts",
-        ],
-    ),
-    compiler = ":tsc",
-    data = [
-        "//third_party/github.com/bazelbuild/bazel/src/main/protobuf:worker_protocol.proto",
+    args = [
+        "--declaration",
+        "-p",
+        "$(execpath //internal:tsconfig.json)",
+        "--outDir",
+        "$(RULEDIR)",
     ],
-    module_name = "@bazel/typescript",
-    module_root = "tsc_wrapped/index.d.ts",
-    supports_workers = False,
-    tsconfig = "//internal:tsc_wrapped/tsconfig.json",
+    outs = _TSC_WRAPPED_JS + _TSC_WRAPPED_TYPINGS,
     visibility = ["//visibility:public"],
-    # Cannot have any deps (except npm fine grained deps) because it doesn't
-    # work with vanilla tsc.
-    # Workaround for https://github.com/Microsoft/TypeScript/issues/22208
-    deps = [
+    data = _TSC_WRAPPED_SRCS + [
+        "//internal:tsconfig.json",
         "@npm//@types/node",
         "@npm//protobufjs",
         "@npm//tsickle",
@@ -88,23 +81,16 @@
         "@npm//tsutils",
         "@npm//typescript",
     ],
-    entry_point = ":tsc_wrapped/tsc_wrapped.ts",
+    entry_point = ":tsc_wrapped/tsc_wrapped.js",
     visibility = ["//visibility:public"],
 )
 
 ts_library(
     name = "test_lib",
-    srcs = glob([
-        "tsc_wrapped/*_test.ts",
-        "tsetse/**/*_test.ts",
-    ]) + [
-        "tsc_wrapped/test_support.ts",
-        "tsetse/util/testing/test_support.ts",
-    ],
+    srcs = glob(_TSC_WRAPPED_TESTS) + _TSC_WRAPPED_TYPINGS,
     compiler = "@build_bazel_rules_typescript//internal:tsc_wrapped_bin",
-    tsconfig = "//internal:tsc_wrapped/tsconfig.json",
+    tsconfig = "//internal:tsconfig.json",
     deps = [
-        ":tsc_wrapped",
         "@npm//@types/jasmine",
         "@npm//@types/node",
         "@npm//tsickle",
@@ -115,8 +101,9 @@
 jasmine_node_test(
     name = "test",
     srcs = [],
-    deps = [
+    deps = _TSC_WRAPPED_JS + [
         ":test_lib",
+        "//third_party/github.com/bazelbuild/bazel/src/main/protobuf:worker_protocol.proto",
         "@npm//jasmine",
         "@npm//protobufjs",
         "@npm//source-map",
diff --git a/internal/tsc_wrapped/tsc_wrapped.ts b/internal/tsc_wrapped/tsc_wrapped.ts
index 8d8e537..848ba2e 100644
--- a/internal/tsc_wrapped/tsc_wrapped.ts
+++ b/internal/tsc_wrapped/tsc_wrapped.ts
@@ -371,6 +371,18 @@
           compilerHost.inputFiles, options, compilerHost, oldProgram));
   cache.putProgram(bazelOpts.target, program);
 
+  for (const pluginConfig of options['plugins'] as ts.PluginImport[] || []) {
+    if (pluginConfig.name === 'ts-lit-plugin') {
+      const litTscPlugin =
+          // Lazy load, so that code that does not use the plugin doesn't even
+          // have to spend the time to parse and load the plugin's source.
+          //
+          // tslint:disable-next-line:no-require-imports
+          new (require('ts-lit-plugin/lib/bazel-plugin').Plugin)(
+              program, pluginConfig) as DiagnosticPlugin;
+      diagnosticPlugins.push(litTscPlugin);
+    }
+  }
 
   if (!bazelOpts.isJsTranspilation) {
     // If there are any TypeScript type errors abort now, so the error
diff --git a/internal/tsc_wrapped/tsconfig.json b/internal/tsc_wrapped/tsconfig.json.oss
similarity index 100%
rename from internal/tsc_wrapped/tsconfig.json
rename to internal/tsc_wrapped/tsconfig.json.oss
diff --git a/internal/tsc_wrapped/worker.ts b/internal/tsc_wrapped/worker.ts
index f6bc505..71fdf25 100644
--- a/internal/tsc_wrapped/worker.ts
+++ b/internal/tsc_wrapped/worker.ts
@@ -88,8 +88,15 @@
     // workspace location
     // This extra lookup should never happen in google3. It's only needed for
     // local development in the rules_typescript repo.
-    protofile = require.resolve(
-        'build_bazel_rules_typescript/third_party/github.com/bazelbuild/bazel/src/main/protobuf/worker_protocol.proto');
+
+    const runfiles = process.env['BAZEL_NODE_RUNFILES_HELPER'];
+    if (runfiles) {
+      protofile = require(runfiles).resolve(
+          'build_bazel_rules_typescript/third_party/github.com/bazelbuild/bazel/src/main/protobuf/worker_protocol.proto');
+    } else {
+      protofile =
+          'build_bazel_rules_typescript/third_party/github.com/bazelbuild/bazel/src/main/protobuf/worker_protocol.proto';
+    }
   }
 
   const protoNamespace = protobufjs.loadSync(protofile);
diff --git a/internal/tsconfig.json b/internal/tsconfig.json
new file mode 100644
index 0000000..f69878c
--- /dev/null
+++ b/internal/tsconfig.json
@@ -0,0 +1,26 @@
+{
+  "compilerOptions": {
+    "strict": true,
+    "module": "CommonJS",
+    "target": "ES2015",
+    "types": [
+      "node",
+    ],
+    "lib": [
+      "dom",
+      "es5",
+      "es2015.core",
+      "es2015.collection",
+      "es2015.promise",
+      // This will need to become es2018.asynciterable when
+      // bumping the version of TypeScript in rules_typescript/package.json
+      "es2018.asynciterable"
+    ]
+  },
+  // We run the plain tsc compiler so we need to exclude files we don't want it to visit
+  "exclude": [
+    "**/test_support.ts",
+    "**/*_test.ts",
+    "*/tests"
+  ]
+}
diff --git a/package.bzl b/package.bzl
index cb33eab..1d77473 100644
--- a/package.bzl
+++ b/package.bzl
@@ -30,8 +30,8 @@
     _maybe(
         http_archive,
         name = "build_bazel_rules_nodejs",
-        sha256 = "3887b948779431ac443e6a64f31b9e1e17b8d386a31eebc50ec1d9b0a6cabd2b",
-        urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/1.0.0/rules_nodejs-1.0.0.tar.gz"],
+        sha256 = "b6670f9f43faa66e3009488bbd909bc7bc46a5a9661a33f6bc578068d1837f37",
+        urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/1.3.0/rules_nodejs-1.3.0.tar.gz"],
     )
 
     # For protocol buffers
diff --git a/package.json b/package.json
index c87382d..8854919 100644
--- a/package.json
+++ b/package.json
@@ -3,14 +3,14 @@
     "homepage": "https://github.com/bazelbuild/rules_typescript",
     "license": "Apache-2.0",
     "devDependencies": {
-        "@bazel/bazel": "^2.0.0",
+        "@bazel/bazelisk": "^1.3.0",
         "@bazel/buildifier": "^0.29.0",
         "@bazel/ibazel": "^0.11.0",
         "@bazel/jasmine": "^1.0.0",
         "@bazel/typescript": "^1.0.0",
         "@types/jasmine": "^2.8.2",
         "@types/long": "^4.0.0",
-        "@types/node": "10.12.20",
+        "@types/node": "^12.0.0",
         "@types/source-map": "^0.5.1",
         "@types/tmp": "^0.0.33",
         "clang-format": "1.0.49",
@@ -32,17 +32,17 @@
         "shelljs": "^0.8.2",
         "source-map-support": "0.5.9",
         "tmp": "0.0.33",
-        "tsickle": "0.33.1",
+        "tsickle": "0.38.0",
         "tsutils": "2.27.2",
-        "typescript": "~3.1.6",
+        "typescript": "^3.7.5",
         "which": "~1.0.5"
     },
     "scripts": {
-        "pree2e": "webdriver-manager update $CHROMEDRIVER_VERSION_ARG && bazel build //examples/app:e2e //examples/protocol_buffers:e2e",
+        "pree2e": "webdriver-manager update $CHROMEDRIVER_VERSION_ARG && bazelisk build //examples/app:e2e //examples/protocol_buffers:e2e",
         "e2e": "yarn e2e-examples-app-devserver && yarn e2e-examples-app-prodserver && yarn e2e-examples-protobuf-devserver && yarn e2e-examples-protobuf-prodserver",
-        "e2e-examples-app-devserver": "concurrently \"bazel run //examples/app:devserver\" \"while ! nc -z 127.0.0.1 8080; do sleep 1; done && protractor --suite app\" --kill-others --success first",
-        "e2e-examples-app-prodserver": "concurrently \"bazel run //examples/app:prodserver\" \"while ! nc -z 127.0.0.1 8080; do sleep 1; done && protractor --suite app\" --kill-others --success first",
-        "e2e-examples-protobuf-devserver": "concurrently \"bazel run //examples/protocol_buffers:devserver\" \"while ! nc -z 127.0.0.1 8080; do sleep 1; done && protractor --suite protocol_buffers\" --kill-others --success first",
-        "e2e-examples-protobuf-prodserver": "concurrently \"bazel run //examples/protocol_buffers:prodserver\" \"while ! nc -z 127.0.0.1 8080; do sleep 1; done && protractor --suite protocol_buffers\" --kill-others --success first"
+        "e2e-examples-app-devserver": "concurrently \"bazelisk run //examples/app:devserver\" \"while ! nc -z 127.0.0.1 8080; do sleep 1; done && protractor --suite app\" --kill-others --success first",
+        "e2e-examples-app-prodserver": "concurrently \"bazelisk run //examples/app:prodserver\" \"while ! nc -z 127.0.0.1 8080; do sleep 1; done && protractor --suite app\" --kill-others --success first",
+        "e2e-examples-protobuf-devserver": "concurrently \"bazelisk run //examples/protocol_buffers:devserver\" \"while ! nc -z 127.0.0.1 8080; do sleep 1; done && protractor --suite protocol_buffers\" --kill-others --success first",
+        "e2e-examples-protobuf-prodserver": "concurrently \"bazelisk run //examples/protocol_buffers:prodserver\" \"while ! nc -z 127.0.0.1 8080; do sleep 1; done && protractor --suite protocol_buffers\" --kill-others --success first"
     }
 }
diff --git a/yarn.lock b/yarn.lock
index f60c13d..90b9892 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -2,31 +2,10 @@
 # yarn lockfile v1
 
 
-"@bazel/bazel-darwin_x64@2.0.0":
-  version "2.0.0"
-  resolved "https://registry.yarnpkg.com/@bazel/bazel-darwin_x64/-/bazel-darwin_x64-2.0.0.tgz#bd678069216dd470c6816a22c405f21e7f048038"
-  integrity sha512-I/pP+B+2xfY0g+OEpEcVnk8rizuC761pAzBOQjP3b+gz3AzeRgm05CpcSY7tfPIppMSYoy3uTZJ1XlwgUg7IQQ==
-
-"@bazel/bazel-linux_x64@2.0.0":
-  version "2.0.0"
-  resolved "https://registry.yarnpkg.com/@bazel/bazel-linux_x64/-/bazel-linux_x64-2.0.0.tgz#2c76e3301e9178a90ec3ad00649e89b953eda0b7"
-  integrity sha512-iOr45G+511IbP7e+ISriG97WpfCAVXekTrTgL5mGg3NDBFCVNs350VquHAvmlXAoP5+IEug2pCOlkdEl4bLl8g==
-
-"@bazel/bazel-win32_x64@2.0.0":
-  version "2.0.0"
-  resolved "https://registry.yarnpkg.com/@bazel/bazel-win32_x64/-/bazel-win32_x64-2.0.0.tgz#f12ac0738d2eac0fd255f099776194807cedfe50"
-  integrity sha512-5qs2qoa/paG/YYEM0yvrwuJIShoPVK2FX+Oz9jEWAQJsmU4drHA9Aq+gbBOirEFLmvYhleZ9XORCwu/5uAo8vA==
-
-"@bazel/bazel@^2.0.0":
-  version "2.0.0"
-  resolved "https://registry.yarnpkg.com/@bazel/bazel/-/bazel-2.0.0.tgz#feb8cf5a40ea6437ef69cac2a92072118b11c230"
-  integrity sha512-KQbv5dHNSfutbhXCc3KVMuBXPpUh6Af/hT9IRIaMTuiB6Nq2gEW9Z3aNqncopdZqV848V/qYxnqPnQ+S37fMyQ==
-  dependencies:
-    "@bazel/hide-bazel-files" latest
-  optionalDependencies:
-    "@bazel/bazel-darwin_x64" "2.0.0"
-    "@bazel/bazel-linux_x64" "2.0.0"
-    "@bazel/bazel-win32_x64" "2.0.0"
+"@bazel/bazelisk@^1.3.0":
+  version "1.3.0"
+  resolved "https://registry.yarnpkg.com/@bazel/bazelisk/-/bazelisk-1.3.0.tgz#dc312dd30ad01e9af86e53b40795ab6e545fa55b"
+  integrity sha512-73H1nq3572tTf+dhDT86aWQN+LCyfxrh05jabqPXp6cpR8soxte3gS5oUqkN36fUe+J2HzNiV4CXZTz4Xytd3Q==
 
 "@bazel/buildifier-darwin_x64@0.29.0":
   version "0.29.0"
@@ -52,11 +31,6 @@
     "@bazel/buildifier-linux_x64" "0.29.0"
     "@bazel/buildifier-win32_x64" "0.29.0"
 
-"@bazel/hide-bazel-files@latest":
-  version "1.0.0"
-  resolved "https://registry.yarnpkg.com/@bazel/hide-bazel-files/-/hide-bazel-files-1.0.0.tgz#779070dcb5ae121ff6c72d73bf3fb7bf68285d75"
-  integrity sha512-dfw2W7xDUPlRMcDMVO8gDkX9Xb7Thy3sP4PDODv+eiHOvwIi116X/wwy7mQUZISkJdEJ1zWy9ydpzvfetpYh4w==
-
 "@bazel/ibazel@^0.11.0":
   version "0.11.0"
   resolved "https://registry.yarnpkg.com/@bazel/ibazel/-/ibazel-0.11.0.tgz#6879c8032f5bee81ca632456304c16525160f47f"
@@ -144,16 +118,16 @@
   resolved "https://registry.yarnpkg.com/@types/long/-/long-4.0.0.tgz#719551d2352d301ac8b81db732acb6bdc28dbdef"
   integrity sha512-1w52Nyx4Gq47uuu0EVcsHBxZFJgurQ+rTKS3qMHxR1GY2T8c2AJYd6vZoZ9q1rupaDjU0yT+Jc2XTyXkjeMA+Q==
 
-"@types/node@10.12.20":
-  version "10.12.20"
-  resolved "https://registry.yarnpkg.com/@types/node/-/node-10.12.20.tgz#f79f959acd3422d0889bd1ead1664bd2d17cd367"
-  integrity sha512-9spv6SklidqxevvZyOUGjZVz4QRXGu2dNaLyXIFzFYZW0AGDykzPRIUFJXTlQXyfzAucddwTcGtJNim8zqSOPA==
-
 "@types/node@^10.1.0":
   version "10.14.6"
   resolved "https://registry.yarnpkg.com/@types/node/-/node-10.14.6.tgz#9cbfcb62c50947217f4d88d4d274cc40c22625a9"
   integrity sha512-Fvm24+u85lGmV4hT5G++aht2C5I4Z4dYlWZIh62FAfFO/TfzXtPpoLI6I7AuBWkIFqZCnhFOoTT7RjjaIL5Fjg==
 
+"@types/node@^12.0.0":
+  version "12.12.27"
+  resolved "https://registry.yarnpkg.com/@types/node/-/node-12.12.27.tgz#d7506f73160ad30fcebbcf5b8b7d2d976e649e42"
+  integrity sha512-odQFl/+B9idbdS0e8IxDl2ia/LP8KZLXhV3BUeI98TrZp0uoIzQPhGd+5EtzHmT0SMOIaPd7jfz6pOHLWTtl7A==
+
 "@types/node@^6.0.46":
   version "6.0.92"
   resolved "https://registry.yarnpkg.com/@types/node/-/node-6.0.92.tgz#e7f721ae282772e12ba2579968c00d9cce422c5d"
@@ -3195,11 +3169,6 @@
   resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263"
   integrity sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==
 
-source-map@^0.7.3:
-  version "0.7.3"
-  resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.7.3.tgz#5302f8169031735226544092e64981f751750383"
-  integrity sha512-CkCj6giN3S+n9qrYiBTX5gystlENnRW5jZeNLHpe6aue+SrHcG5VYwujhW9s4dY31mEGsxBDrHR6oI69fTXsaQ==
-
 spawn-command@^0.0.2-1:
   version "0.0.2"
   resolved "https://registry.yarnpkg.com/spawn-command/-/spawn-command-0.0.2.tgz#9544e1a43ca045f8531aac1a48cb29bdae62338e"
@@ -3464,14 +3433,10 @@
   resolved "https://registry.yarnpkg.com/tree-kill/-/tree-kill-1.2.0.tgz#5846786237b4239014f05db156b643212d4c6f36"
   integrity sha512-DlX6dR0lOIRDFxI0mjL9IYg6OTncLm/Zt+JiBhE5OlFcAR8yc9S7FFXU9so0oda47frdM/JFsk7UjNt9vscKcg==
 
-tsickle@0.33.1:
-  version "0.33.1"
-  resolved "https://registry.yarnpkg.com/tsickle/-/tsickle-0.33.1.tgz#eee4ebabeda3bcd8afc32cee34c822cbe3e839ec"
-  integrity sha512-SpW2G3PvDGs4a5sMXPlWnCWHWRviWjSlI3U0734e3fU3U39VAE0NPr8M3W1cuL/OU/YXheYipGeEwtIJ5k0NHQ==
-  dependencies:
-    minimist "^1.2.0"
-    mkdirp "^0.5.1"
-    source-map "^0.7.3"
+tsickle@0.38.0:
+  version "0.38.0"
+  resolved "https://registry.yarnpkg.com/tsickle/-/tsickle-0.38.0.tgz#89f5952c9bb3ba0b36dc384975e23cf90e584822"
+  integrity sha512-k7kI6afBuLd2jIrj9JR8lKhEkp99sFVRKQbHeaHQkdvDaH5AvzwqA/qX+aNj28OfuAsWryOKAZoXm24l7JelEw==
 
 tslib@^1.8.1:
   version "1.9.0"
@@ -3505,10 +3470,10 @@
     media-typer "0.3.0"
     mime-types "~2.1.18"
 
-typescript@~3.1.6:
-  version "3.1.6"
-  resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.1.6.tgz#b6543a83cfc8c2befb3f4c8fba6896f5b0c9be68"
-  integrity sha512-tDMYfVtvpb96msS1lDX9MEdHrW4yOuZ4Kdc4Him9oU796XldPYF/t2+uKoX0BBa0hXXwDlqYQbXY5Rzjzc5hBA==
+typescript@^3.7.5:
+  version "3.7.5"
+  resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.7.5.tgz#0692e21f65fd4108b9330238aac11dd2e177a1ae"
+  integrity sha512-/P5lkRXkWHNAbcJIiHPfRoKqyd7bsyCma1hZNUGfn20qm64T6ZBlrzprymeu918H+mB/0rIg2gGK/BXkhhYgBw==
 
 uglify-js@^3.1.4:
   version "3.7.4"