diff --git a/ts/deck-options/index.ts b/ts/deck-options/index.ts index ee406ead2..bb625b592 100644 --- a/ts/deck-options/index.ts +++ b/ts/deck-options/index.ts @@ -5,7 +5,7 @@ @typescript-eslint/no-explicit-any: "off", */ -import "sveltelib/export-internal"; +import "sveltelib/export-runtime"; import { getDeckOptionsInfo, DeckOptionsState } from "./lib"; import { setupI18n, ModuleName } from "lib/i18n"; diff --git a/ts/editor/index.ts b/ts/editor/index.ts index 7d39aad78..0e269d76b 100644 --- a/ts/editor/index.ts +++ b/ts/editor/index.ts @@ -6,7 +6,7 @@ @typescript-eslint/no-explicit-any: "off", */ -import "sveltelib/export-internal"; +import "sveltelib/export-runtime"; import { filterHTML } from "html-filter"; import { updateActiveButtons } from "./toolbar"; diff --git a/ts/lib/runtime-require.ts b/ts/lib/runtime-require.ts new file mode 100644 index 000000000..336b0270c --- /dev/null +++ b/ts/lib/runtime-require.ts @@ -0,0 +1,19 @@ +// Copyright: Ankitects Pty Ltd and contributors +// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html + +/* eslint +@typescript-eslint/no-explicit-any: "off", + */ + +/// This can be extended to allow require() calls at runtime, for libraries +/// that are not included at bundling time. +export const runtimeLibraries = {}; + +// Export require() as a global. +(globalThis as any).require = function (name: string): unknown { + const lib = runtimeLibraries[name]; + if (lib === undefined) { + throw new Error(`Cannot require(${name}) at runtime.`); + } + return lib; +}; diff --git a/ts/sveltelib/BUILD.bazel b/ts/sveltelib/BUILD.bazel index 60cbfd715..0803416df 100644 --- a/ts/sveltelib/BUILD.bazel +++ b/ts/sveltelib/BUILD.bazel @@ -13,6 +13,7 @@ ts_library( tsconfig = "//:tsconfig.json", visibility = ["//visibility:public"], deps = [ + "//ts/lib", "@npm//svelte", "@npm//tslib", ], diff --git a/ts/sveltelib/export-internal.ts b/ts/sveltelib/export-internal.ts deleted file mode 100644 index c83b80520..000000000 --- a/ts/sveltelib/export-internal.ts +++ /dev/null @@ -1,6 +0,0 @@ -// Copyright: Ankitects Pty Ltd and contributors -// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html - -// allow Svelte add-ons -import * as svelte_internal from "svelte/internal"; -window["svelte_internal"] = svelte_internal; diff --git a/ts/sveltelib/export-runtime.ts b/ts/sveltelib/export-runtime.ts new file mode 100644 index 000000000..f8ffeddcd --- /dev/null +++ b/ts/sveltelib/export-runtime.ts @@ -0,0 +1,11 @@ +// Copyright: Ankitects Pty Ltd and contributors +// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html +// +// Expose the Svelte runtime bundled with Anki, so that add-ons can require() it. +// If they were to bundle their own runtime, things like bindings and contexts +// would not work. + +import { runtimeLibraries } from "lib/runtime-require"; +import * as svelteRuntime from "svelte/internal"; + +runtimeLibraries["svelte/internal"] = svelteRuntime;