From 12ea482c8768e7e6f33e4e531682968d6ef077b6 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Tue, 24 Aug 2021 10:19:26 +1000 Subject: [PATCH] expose require() instead of the svelte global - Means add-on authors should not need to inject any code in their build - Should be more flexible - we can export multiple libraries if we wish, and don't have to worry about require() being clobbered by old add-ons. --- ts/deck-options/index.ts | 2 +- ts/editor/index.ts | 2 +- ts/lib/runtime-require.ts | 19 +++++++++++++++++++ ts/sveltelib/BUILD.bazel | 1 + ts/sveltelib/export-internal.ts | 6 ------ ts/sveltelib/export-runtime.ts | 11 +++++++++++ 6 files changed, 33 insertions(+), 8 deletions(-) create mode 100644 ts/lib/runtime-require.ts delete mode 100644 ts/sveltelib/export-internal.ts create mode 100644 ts/sveltelib/export-runtime.ts 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;