anki/ts/lib/runtime-require.ts
Damien Elmes 12ea482c87 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.
2021-08-24 10:22:07 +10:00

20 lines
601 B
TypeScript

// 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;
};