Add trace_processor bundle

We had to shave a couple of yaks to get here:

- Add -s MODULARIZE=1 to emscripten flags:
  This causes emscripten to generate a slighty more sane interface
  (where you can pass in Module rather than just adding to window).

- In order for typescript to import trace_processor.js we need to copy
  it to out/blah/obj/ui/gen (which is symlinked to ui/src/gen). Hence
  a new target named wasm_gen.

- However this means we need a .d.ts file for the generated
  trace_processor.js. We would normally just commit this but it needs
  to live in ui/src/gen (next to trace_processor.js) and ui/src/gen
  is a symlink to the output directory. So instead we add an extra
  output to wasm_lib to copy gn/standalone/wasm_typescript_declaration.d.ts
  next to each wasm_lib.js

- trace_processor.js conditionally executes require('fs') (when running
  under node) this confuses rollup so we need to teach it not to worry
  about require('fs') (and similar requires).

Change-Id: I17d10a9e2b46449b34f72e189116e67c9919b3fe
diff --git a/ui/BUILD.gn b/ui/BUILD.gn
index 3a98f45..d8f3688 100644
--- a/ui/BUILD.gn
+++ b/ui/BUILD.gn
@@ -17,6 +17,7 @@
 import("../protos/perfetto/trace_processor/proto_files.gni")
 
 ui_dir = "$root_build_dir/ui"
+ui_gen_dir = "$target_out_dir/gen"
 nodejs_root = "../buildtools/nodejs"
 nodejs_bin = rebase_path("$nodejs_root/bin", root_build_dir)
 
@@ -27,6 +28,7 @@
   deps = [
     ":index_release",
     ":main_bundle_release",
+    ":wasm_bundle_release",
     ":wasm_release",
     ":worker_bundle_release",
   ]
@@ -144,11 +146,17 @@
   output = "$target_out_dir/worker_bundle.js"
 }
 
+bundle("wasm_bundle") {
+  deps = [
+    ":transpile_all_ts",
+  ]
+  input = "$target_out_dir/wasm.js"
+  output = "$target_out_dir/wasm_bundle.js"
+}
+
 # +----------------------------------------------------------------------------+
 # | Protobuf: gen rules to create .js and .d.ts files from protos.             |
 # +----------------------------------------------------------------------------+
-proto_gen_dir = "$target_out_dir/gen"
-
 node_bin("protos_to_js") {
   inputs = []
   foreach(proto, trace_processor_protos) {
@@ -156,7 +164,7 @@
   }
   inputs += [ "../protos/perfetto/config/perfetto_config.proto" ]
   outputs = [
-    "$proto_gen_dir/protos.js",
+    "$ui_gen_dir/protos.js",
   ]
   node_cmd = "pbjs"
   args = [
@@ -178,10 +186,10 @@
     ":protos_to_js",
   ]
   inputs = [
-    "$proto_gen_dir/protos.js",
+    "$ui_gen_dir/protos.js",
   ]
   outputs = [
-    "$proto_gen_dir/protos.d.ts",
+    "$ui_gen_dir/protos.d.ts",
   ]
   node_cmd = "pbts"
   args = [
@@ -201,16 +209,19 @@
 node_bin("transpile_all_ts") {
   sources = [
     "src/main.ts",
+    "src/wasm.ts",
     "src/worker.ts",
   ]
   deps = [
     ":dist_symlink",
     ":protos_to_ts",
+    ":wasm_gen",
   ]
   inputs = sources + [ "tsconfig.json" ]
   outputs = [
     "$target_out_dir/main.js",
     "$target_out_dir/worker.js",
+    "$target_out_dir/wasm.js",
   ]
 
   # Find all *.ts files and pass them as input triggers. This does NOT affect
@@ -264,17 +275,40 @@
   output = "$ui_dir/worker_bundle.js"
 }
 
+sorcery("wasm_bundle_release") {
+  deps = [
+    ":wasm_bundle",
+  ]
+  input = "$target_out_dir/wasm_bundle.js"
+  output = "$ui_dir/wasm_bundle.js"
+}
+
 copy("wasm_release") {
   deps = [
+    "//src/trace_processor:trace_processor.wasm($wasm_toolchain)",
+  ]
+  sources = [
+    "$root_build_dir/wasm/trace_processor.wasm",
+  ]
+  outputs = [
+    "$ui_dir/{{source_file_part}}",
+  ]
+}
+
+copy("wasm_gen") {
+  deps = [
+    ":dist_symlink",
+    "//src/trace_processor:trace_processor.d.ts($wasm_toolchain)",
     "//src/trace_processor:trace_processor.js($wasm_toolchain)",
     "//src/trace_processor:trace_processor.wasm($wasm_toolchain)",
   ]
   sources = [
+    "$root_build_dir/wasm/trace_processor.d.ts",
     "$root_build_dir/wasm/trace_processor.js",
     "$root_build_dir/wasm/trace_processor.wasm",
   ]
   outputs = [
-    "$ui_dir/wasm/{{source_file_part}}",
+    "$ui_gen_dir/{{source_file_part}}",
   ]
 }