feat(src/core/plugins/index.ts): LazyLoad core plugins for slightly
faster startup
This commit is contained in:
parent
bbc3b9bcb7
commit
554f50e235
1 changed files with 31 additions and 7 deletions
|
|
@ -3,16 +3,40 @@ import { PluginInstanceInternal } from "@lib/addons/plugins/types";
|
|||
interface CorePlugin {
|
||||
default: PluginInstanceInternal;
|
||||
preenabled: boolean;
|
||||
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;
|
||||
}
|
||||
|
||||
// Called from @lib/plugins
|
||||
export const getCorePlugins = (): Record<string, CorePlugin> => ({
|
||||
"bunny.quickinstall": { ...require("./quickinstall"), preenabled: true },
|
||||
"bunny.badges": require("./badges"),
|
||||
"bunny.notrack": require("./notrack"),
|
||||
"bunny.messagefix": require("./messagefix"),
|
||||
"bunny.fixembed": require("./fixembed"),
|
||||
"bunny.enhancements": require("./shiggyenhancements")
|
||||
export const getCorePlugins = (): CorePluginRecord => ({
|
||||
"bunny.quickinstall": makeLazyPlugin(() => require("./quickinstall"), true),
|
||||
"bunny.badges": makeLazyPlugin(() => require("./badges")),
|
||||
"bunny.notrack": makeLazyPlugin(() => require("./notrack")),
|
||||
"bunny.messagefix": makeLazyPlugin(() => require("./messagefix")),
|
||||
"bunny.fixembed": makeLazyPlugin(() => require("./fixembed")),
|
||||
"bunny.enhancements": makeLazyPlugin(() => require("./shiggyenhancements")),
|
||||
});
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue