[shaders] Use type definitions from latest or local version

This also fixes the flag aliases, which do not combine like
I thought they did.

Change-Id: I9da11898a87a86e90c2c73801fed923c977bfdde
Reviewed-on: https://skia-review.googlesource.com/c/buildbot/+/495257
Reviewed-by: John Stiles <johnstiles@google.com>
Commit-Queue: Kevin Lubick <kjlubick@google.com>
diff --git a/.bazelrc b/.bazelrc
index e08951a..b42c32e 100644
--- a/.bazelrc
+++ b/.bazelrc
@@ -141,6 +141,6 @@
 # the command line easier.
 build --flag_alias=with_local_debugger=no//debugger-app/wasm_libs:use_debugger_from_container
 build --flag_alias=with_local_wasm=no//jsfiddle/wasm_libs:use_libraries_from_container
-build --flag_alias=with_local_canvaskit=no//particles/wasm_libs:use_canvaskit_from_container
-build --flag_alias=with_local_canvaskit=no//shaders/wasm_libs:use_canvaskit_from_container
-build --flag_alias=with_local_canvaskit=no//skottie/wasm_libs:use_canvaskit_from_container
+build --flag_alias=particles_local_canvaskit=no//particles/wasm_libs:use_canvaskit_from_container
+build --flag_alias=shaders_local_canvaskit=no//shaders/wasm_libs:use_canvaskit_from_container
+build --flag_alias=skottie_local_canvaskit=no//skottie/wasm_libs:use_canvaskit_from_container
diff --git a/particles/Makefile b/particles/Makefile
index 4baea91..8de6d26 100644
--- a/particles/Makefile
+++ b/particles/Makefile
@@ -46,7 +46,7 @@
 .PHONY: run-with-custom
 run-with-custom:
 	echo "Using the wasm files in //particles/wasm_libs/local_build"
-	$(BAZEL) build //particles/... --with_local_canvaskit
+	$(BAZEL) build //particles/... --particles_local_canvaskit
 	../_bazel_bin/particles/go/particles/particles_/particles --local \
 		--resources_dir ../_bazel_bin/particles/pages/development \
 		--scrapexchange=http://localhost:9000
diff --git a/shaders/.gitignore b/shaders/.gitignore
index 550a6e3..c4d655c 100644
--- a/shaders/.gitignore
+++ b/shaders/.gitignore
@@ -1,2 +1,3 @@
 build/
-wsam_libs/local_build/
+wasm_libs/local_build/
+wasm_libs/types/
diff --git a/shaders/Makefile b/shaders/Makefile
index ad138a7..ca7c7e1 100644
--- a/shaders/Makefile
+++ b/shaders/Makefile
@@ -47,7 +47,7 @@
 .PHONY: run-with-custom
 run-with-custom:
 	echo "Using the wasm files in //shaders/wasm_libs/local_build"
-	$(BAZEL) build //shaders/... --with_local_canvaskit
+	$(BAZEL) build //shaders/... --shaders_local_canvaskit
 	../_bazel_bin/shaders/go/shaders/shaders_/shaders --local \
 		--resources_dir ../_bazel_bin/shaders/pages/development \
 		--scrapexchange=http://localhost:9000 \
diff --git a/shaders/README.md b/shaders/README.md
index 8db7d2e..ea45815 100644
--- a/shaders/README.md
+++ b/shaders/README.md
@@ -3,12 +3,14 @@
 This is the code for https://shaders.skia.org, a site to allow editing and
 running SkSL shaders using WASM in the browser.
 
-To run shaders locally with a custom build of CanvasKit, copy the files to
-//shaders/wasm_libs/local_build and run:
+To run shaders locally with a custom build of CanvasKit, copy the js and wasm files to
+//shaders/wasm_libs/local_build. Then, copy @skia//canvaskit/npm-build/types/index.d.ts to
+//shaders/wasm_libs/local_build/canvaskit.d.ts and run:
 ```
 make run-with-custom
 ```
-Do not check in those files you copied.
+Do not check in those files you copied. You may also need to copy the canvaskit.d.ts to
+//shaders/wasm_libs/types/ in order to appease some IDEs.
 
 ## Images
 
diff --git a/shaders/modules/shadernode/BUILD.bazel b/shaders/modules/shadernode/BUILD.bazel
index e850a62..89679e4 100644
--- a/shaders/modules/shadernode/BUILD.bazel
+++ b/shaders/modules/shadernode/BUILD.bazel
@@ -12,8 +12,8 @@
     deps = [
         ":index_ts_lib",
         "//shaders/modules/json:index_ts_lib",
+        "//shaders/wasm_libs:canvaskit_types",  # keep
         "@npm//@types/chai",
-        "@npm//canvaskit-wasm",
         "@npm//chai",
         "@npm//fetch-mock",
     ],
@@ -26,7 +26,7 @@
     deps = [
         "//infra-sk/modules/uniform:uniform_ts_lib",
         "//shaders/modules/json:index_ts_lib",
-        "@npm//canvaskit-wasm",
+        "//shaders/wasm_libs:canvaskit_types",  # keep
         "@npm//common-sk",
         "@npm//elements-sk",
     ],
diff --git a/shaders/modules/shadernode/index.ts b/shaders/modules/shadernode/index.ts
index f75da5a..8a824f2 100644
--- a/shaders/modules/shadernode/index.ts
+++ b/shaders/modules/shadernode/index.ts
@@ -8,13 +8,12 @@
  */
 import { jsonOrThrow } from 'common-sk/modules/jsonOrThrow';
 import { errorMessage } from 'elements-sk/errorMessage';
-import {
+import type {
   CanvasKit,
-  Image,
   MallocObj,
   RuntimeEffect,
   Shader,
-} from 'canvaskit-wasm';
+} from '../../wasm_libs/types/canvaskit';
 import { Uniform } from '../../../infra-sk/modules/uniform/uniform';
 import { ChildShader, ScrapBody, ScrapID } from '../json';
 
diff --git a/shaders/modules/shadernode/index_test.ts b/shaders/modules/shadernode/index_test.ts
index 5d8a848..258c010 100644
--- a/shaders/modules/shadernode/index_test.ts
+++ b/shaders/modules/shadernode/index_test.ts
@@ -2,7 +2,7 @@
 import './index';
 import fetchMock, { MockRequest, MockResponse } from 'fetch-mock';
 import { assert } from 'chai';
-import { CanvasKit, CanvasKitInit as CKInit } from 'canvaskit-wasm';
+import type { CanvasKit, CanvasKitInit as CKInit } from '../../wasm_libs/types/canvaskit';
 import {
   childShaderArraysDiffer,
   childShadersAreDifferent,
diff --git a/shaders/modules/shaders-app-sk/BUILD.bazel b/shaders/modules/shaders-app-sk/BUILD.bazel
index fbfad6f..df4e38e 100644
--- a/shaders/modules/shaders-app-sk/BUILD.bazel
+++ b/shaders/modules/shaders-app-sk/BUILD.bazel
@@ -48,8 +48,8 @@
         "//infra-sk/modules/uniform:uniform_ts_lib",
         "//shaders/modules/shadernode:index_ts_lib",
         "//shaders/modules/sksl-constants:sksl-constants_ts_lib",
+        "//shaders/wasm_libs:canvaskit_types",  # keep
         "@npm//@types/codemirror",
-        "@npm//canvaskit-wasm",
         "@npm//codemirror",
         "@npm//common-sk",
         "@npm//elements-sk",
diff --git a/shaders/modules/shaders-app-sk/shaders-app-sk.ts b/shaders/modules/shaders-app-sk/shaders-app-sk.ts
index 6042f70..9ae0515 100644
--- a/shaders/modules/shaders-app-sk/shaders-app-sk.ts
+++ b/shaders/modules/shaders-app-sk/shaders-app-sk.ts
@@ -18,7 +18,7 @@
   Surface,
   Canvas,
   Paint,
-} from 'canvaskit-wasm';
+} from '../../wasm_libs/types/canvaskit';
 import { isDarkMode } from '../../../infra-sk/modules/theme-chooser-sk/theme-chooser-sk';
 
 import 'elements-sk/error-toast-sk';
diff --git a/shaders/wasm_libs/BUILD.bazel b/shaders/wasm_libs/BUILD.bazel
index 8fd69ae..17e38d6 100644
--- a/shaders/wasm_libs/BUILD.bazel
+++ b/shaders/wasm_libs/BUILD.bazel
@@ -1,4 +1,5 @@
 load("//infra-sk:index.bzl", "bool_flag", "extract_files_from_skia_wasm_container")
+load("@build_bazel_rules_nodejs//:index.bzl", "js_library")
 
 bool_flag(
     default = True,
@@ -12,16 +13,47 @@
     name = "fetch_canvaskit_wasm",
     testonly = True,
     outs = [
+        "from_container/canvaskit.d.ts",
         "from_container/canvaskit.js",
         "from_container/canvaskit.wasm",
     ],
     container_files = [
+        "/tmp/canvaskit/canvaskit.d.ts",
         "/tmp/canvaskit/canvaskit.js",
         "/tmp/canvaskit/canvaskit.wasm",
     ],
     visibility = ["//shaders:__subpackages__"],
 )
 
+# This js_library rule makes the source a viable dependency of ts_library rules (and thus visible
+# during compilation).
+js_library(
+    name = "canvaskit_types",
+    srcs = [
+        "types/canvaskit.d.ts",
+    ],
+    visibility = ["//shaders:__subpackages__"],
+)
+
+# This copy rule makes it so we can import from '../../wasm_libs/types/canvaskit' and not
+# '../../wasm_libs/from_container/canvaskit', which does not make the source easily toggleable.
+genrule(
+    name = "copy_canvaskit_types",
+    srcs = ["canvaskit.d.ts"],
+    outs = ["types/canvaskit.d.ts"],
+    # Copy the one input file to the one output location
+    cmd = "cp $< $@",
+)
+
+filegroup(
+    name = "canvaskit.d.ts",
+    srcs = select({
+        ":use_canvaskit_from_container_true": ["from_container/canvaskit.d.ts"],
+        ":use_canvaskit_from_container_false": ["local_build/canvaskit.d.ts"],
+    }),
+    visibility = ["//shaders:__subpackages__"],
+)
+
 filegroup(
     name = "canvaskit.js",
     srcs = select({
diff --git a/skottie/Makefile b/skottie/Makefile
index 887b93e..e2e13ff 100644
--- a/skottie/Makefile
+++ b/skottie/Makefile
@@ -43,7 +43,7 @@
 .PHONY: run-with-custom
 run-with-custom:
 	echo "Using the wasm files in //skottie/wasm_libs/local_build"
-	$(BAZEL) build //skottie/... --with_local_canvaskit
+	$(BAZEL) build //skottie/... --skottie_local_canvaskit
 	../_bazel_bin/skottie/go/skottie/skottie_/skottie \
 		--config ./local_config.json5