2024-10-13 00:35:36 +08:00
|
|
|
import { PluginInstanceInternal } from "@lib/addons/plugins/types";
|
2024-02-28 17:59:47 +08:00
|
|
|
|
2024-07-30 00:18:01 +08:00
|
|
|
interface CorePlugin {
|
2025-10-11 02:26:41 +02:00
|
|
|
default: PluginInstanceInternal;
|
|
|
|
|
preenabled: boolean;
|
2026-01-23 20:08:26 +01:00
|
|
|
loader: () => PluginInstanceInternal;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type CorePluginRecord = Record<string, CorePlugin>;
|
|
|
|
|
|
|
|
|
|
function makeLazyPlugin(loaderFn: () => any, preenabled = true): CorePlugin {
|
|
|
|
|
const container: any = {
|
|
|
|
|
loader: () => {
|
|
|
|
|
// loaderFn should synchronously require the module
|
|
|
|
|
const mod = loaderFn();
|
|
|
|
|
// module may be an object with `.default` or the instance directly
|
|
|
|
|
const inst = mod && (mod.default ?? mod);
|
|
|
|
|
return inst;
|
|
|
|
|
},
|
|
|
|
|
preenabled,
|
|
|
|
|
get default() {
|
|
|
|
|
// load and cache the instance on first access
|
|
|
|
|
const inst = container.loader();
|
|
|
|
|
Object.defineProperty(container, "default", { value: inst, writable: false, enumerable: true });
|
|
|
|
|
return inst;
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return container as CorePlugin;
|
2024-07-30 00:18:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Called from @lib/plugins
|
2026-01-23 20:08:26 +01:00
|
|
|
export const getCorePlugins = (): CorePluginRecord => ({
|
|
|
|
|
"bunny.quickinstall": makeLazyPlugin(() => require("./quickinstall"), true),
|
|
|
|
|
"bunny.badges": makeLazyPlugin(() => require("./badges")),
|
|
|
|
|
"bunny.notrack": makeLazyPlugin(() => require("./notrack")),
|
2026-04-26 11:28:06 +02:00
|
|
|
"bunny.fixembed": makeLazyPlugin(() => require("./fixembed")),
|
2024-07-30 00:18:01 +08:00
|
|
|
});
|
2024-02-28 17:59:47 +08:00
|
|
|
|
2024-07-30 00:18:01 +08:00
|
|
|
/**
|
|
|
|
|
* @internal
|
|
|
|
|
*/
|
2025-10-11 02:26:41 +02:00
|
|
|
export function defineCorePlugin(
|
|
|
|
|
instance: PluginInstanceInternal,
|
|
|
|
|
): PluginInstanceInternal {
|
|
|
|
|
// @ts-expect-error
|
|
|
|
|
instance[Symbol.for("bunny.core.plugin")] = true;
|
|
|
|
|
return instance;
|
2024-03-06 01:57:59 +08:00
|
|
|
}
|