Prepare Gerrit core for building TypeScript plugins

Add standard configs for ts and rollup to plugins/ directory.

Change the gerrit_js_bundle Bazel macro to depend on the standard
rollup config.

Add gerrit-ts-api and polymer npm deps for @plugins_npm.

Change-Id: I7e313dd111d9eae662154c556f01c2a45ad675dc
diff --git a/plugins/BUILD b/plugins/BUILD
index dd0be66..ad517ed 100644
--- a/plugins/BUILD
+++ b/plugins/BUILD
@@ -6,6 +6,16 @@
     "CORE_PLUGINS",
     "CUSTOM_PLUGINS",
 )
+load("@npm//@bazel/typescript:index.bzl", "ts_config")
+
+package(default_visibility = ["//visibility:public"])
+
+exports_files(["rollup.config.js"])
+
+ts_config(
+    name = "plugin-tsconfig",
+    src = "tsconfig.json",
+)
 
 genrule2(
     name = "core",
@@ -16,7 +26,6 @@
           "ln -s $$ROOT/$$s $$TMP/WEB-INF/plugins;done;" +
           "cd $$TMP;" +
           "zip -qr $$ROOT/$@ .",
-    visibility = ["//visibility:public"],
 )
 
 PLUGIN_API = [
@@ -100,6 +109,7 @@
 java_binary(
     name = "bouncycastle-deploy-env",
     main_class = "Dummy",
+    visibility = ["//visibility:private"],
     runtime_deps = [
         "//lib/bouncycastle:bcpg",
         "//lib/bouncycastle:bcpkix",
@@ -111,27 +121,23 @@
     name = "plugin-api",
     deploy_env = ["bouncycastle-deploy-env"],
     main_class = "Dummy",
-    visibility = ["//visibility:public"],
     runtime_deps = [":plugin-lib"],
 )
 
 java_library(
     name = "plugin-lib",
-    visibility = ["//visibility:public"],
     exports = PLUGIN_API + EXPORTS,
 )
 
 java_library(
     name = "plugin-lib-neverlink",
     neverlink = 1,
-    visibility = ["//visibility:public"],
     exports = PLUGIN_API + EXPORTS,
 )
 
 java_binary(
     name = "plugin-api-sources",
     main_class = "Dummy",
-    visibility = ["//visibility:public"],
     runtime_deps = [
         "//antlr3:libquery_parser-src.jar",
         "//java/com/google/gerrit/common:libannotations-src.jar",
@@ -163,5 +169,4 @@
     ],
     pkgs = ["com.google.gerrit"],
     title = "Gerrit Review Plugin API Documentation",
-    visibility = ["//visibility:public"],
 )
diff --git a/plugins/package.json b/plugins/package.json
index 9f5c649..1d97a64 100644
--- a/plugins/package.json
+++ b/plugins/package.json
@@ -2,7 +2,10 @@
     "name": "polygerrit-plugin-dependencies-placeholder",
     "description": "Gerrit Code Review - Polygerrit plugin dependencies placeholder, expected to be overridden by plugins",
     "browser": true,
-    "dependencies": {},
+    "dependencies": {
+      "@polymer/polymer": "^3.4.1",
+      "@gerritcodereview/typescript-api": "3.4.2"
+    },
     "license": "Apache-2.0",
     "private": true
-}
\ No newline at end of file
+}
diff --git a/plugins/rollup.config.js b/plugins/rollup.config.js
new file mode 100644
index 0000000..6fb3784
--- /dev/null
+++ b/plugins/rollup.config.js
@@ -0,0 +1,59 @@
+/**
+ * @license
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+const path = require('path');
+
+// In this file word "plugin" refers to rollup plugin, not Gerrit plugin.
+// By default, require(plugin_name) tries to find module plugin_name starting
+// from the folder where this file (rollup.config.js) is located
+// (see https://www.typescriptlang.org/docs/handbook/module-resolution.html#node
+// and https://nodejs.org/api/modules.html#modules_all_together).
+// So, rollup.config.js can't be in polygerrit-ui/app dir and it should be in
+// tools/node_tools directory (where all plugins are installed).
+// But rollup_bundle rule copy this .config.js file to another directory,
+// so require(plugin_name) can't find a plugin.
+// To fix it, requirePlugin tries:
+// 1. resolve module id using default behavior, i.e. it starts from __dirname
+// 2. if module not found - it tries to resolve module starting from rollupBin
+//    location.
+// This workaround also gives us additional power - we can place .config.js
+// file anywhere in a source tree and add all plugins in the same package.json
+// file as rollup node module.
+function requirePlugin(id) {
+  const rollupBinDir = path.dirname(process.argv[1]);
+  const pluginPath = require.resolve(id, {paths: [__dirname, rollupBinDir] });
+  return require(pluginPath);
+}
+
+const resolve = requirePlugin('rollup-plugin-node-resolve');
+
+export default {
+  treeshake: false,
+  onwarn: warning => {
+    // No warnings from rollupjs are allowed.
+    // Most of the warnings are real error in our code (for example,
+    // if some import couldn't be resolved we can't continue, but rollup
+    // reports it as a warning)
+    throw new Error(warning.message);
+  },
+  // Context must be set to window to correctly process global variables
+  context: 'window',
+  plugins: [resolve({
+    customResolveOptions: {
+      moduleDirectory: 'external/plugins_npm/node_modules',
+    },
+  })],
+};
diff --git a/plugins/tsconfig.json b/plugins/tsconfig.json
new file mode 100644
index 0000000..f9f7712
--- /dev/null
+++ b/plugins/tsconfig.json
@@ -0,0 +1,7 @@
+{
+  "compilerOptions": {
+    "target": "es2018", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
+    "module": "es2015", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
+    "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
+  },
+}
diff --git a/plugins/yarn.lock b/plugins/yarn.lock
index a63f96e..4d0f91e 100644
--- a/plugins/yarn.lock
+++ b/plugins/yarn.lock
@@ -1,3 +1,20 @@
 # THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
 # yarn lockfile v1
-# This is an empty placeholder
\ No newline at end of file
+
+
+"@gerritcodereview/typescript-api@3.4.2":
+  version "3.4.2"
+  resolved "https://registry.yarnpkg.com/@gerritcodereview/typescript-api/-/typescript-api-3.4.2.tgz#79e8ff336608cbf18e651bfa9541d7bdead5e1f9"
+  integrity sha512-iqHd6G43pV4Wk5iNw95AQmWUBuIrY+dvQ1Ne8ZYkOwRhdruh4BAPhMtsmqWDlcVQbfcwZD5F2zFkGB4J4htggw==
+
+"@polymer/polymer@^3.4.1":
+  version "3.4.1"
+  resolved "https://registry.yarnpkg.com/@polymer/polymer/-/polymer-3.4.1.tgz#333bef25711f8411bb5624fb3eba8212ef8bee96"
+  integrity sha512-KPWnhDZibtqKrUz7enIPOiO4ZQoJNOuLwqrhV2MXzIt3VVnUVJVG5ORz4Z2sgO+UZ+/UZnPD0jqY+jmw/+a9mQ==
+  dependencies:
+    "@webcomponents/shadycss" "^1.9.1"
+
+"@webcomponents/shadycss@^1.9.1":
+  version "1.11.0"
+  resolved "https://registry.yarnpkg.com/@webcomponents/shadycss/-/shadycss-1.11.0.tgz#73e289996c002d8be694cd3be0e83c46ad25e7e0"
+  integrity sha512-L5O/+UPum8erOleNjKq6k58GVl3fNsEQdSOyh0EUhNmi7tHUyRuCJy1uqJiWydWcLARE5IPsMoPYMZmUGrz1JA==