Fixes production build with custom services
This commit is contained in:
@@ -1,22 +1,23 @@
|
||||
import type { ServiceHandler } from './service';
|
||||
|
||||
type ServiceRecord = {
|
||||
handler: ServiceHandler;
|
||||
component: any;
|
||||
};
|
||||
const services: Record<string, ServiceHandler> = {};
|
||||
|
||||
const services: Record<string, ServiceRecord> = {};
|
||||
const components: Record<string, any> = {};
|
||||
|
||||
function registerService(type: string, handler: ServiceHandler, component: any) {
|
||||
services[type] = { handler, component };
|
||||
function registerService(type: string, handler: ServiceHandler) {
|
||||
services[type] = handler;
|
||||
}
|
||||
|
||||
function registerComponent(type: string, component: any) {
|
||||
components[type] = component;
|
||||
}
|
||||
|
||||
export function getServiceHandler(type: string): ServiceHandler | undefined {
|
||||
return services[type]?.handler;
|
||||
return services[type];
|
||||
}
|
||||
|
||||
export function getServiceComponent(type: string): any {
|
||||
return services[type]?.component;
|
||||
return components[type];
|
||||
}
|
||||
|
||||
export async function initServices() {
|
||||
@@ -24,20 +25,32 @@ export async function initServices() {
|
||||
|
||||
for (const [modulePath, load] of Object.entries(services)) {
|
||||
try {
|
||||
const { handle, path } = (await load()) as any;
|
||||
const { handle } = (await load()) as any;
|
||||
if (handle == undefined) {
|
||||
throw new Error(`${modulePath} does not export 'handle'`);
|
||||
}
|
||||
if (path == undefined) {
|
||||
throw new Error(`${modulePath} does not export 'path'`);
|
||||
}
|
||||
|
||||
const module = await import(/* @vite-ignore */ '/src/lib/services/' + path);
|
||||
|
||||
const typeName = modulePath.slice(18, -12);
|
||||
registerService(typeName, handle, module.default);
|
||||
registerService(typeName, handle);
|
||||
} catch (err) {
|
||||
console.error(`Could not load service definition from '${modulePath}': ${err}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function initComponents() {
|
||||
const services = import.meta.glob('/src/lib/services/**/+content.svelte');
|
||||
|
||||
for (const [componentPath, load] of Object.entries(services)) {
|
||||
try {
|
||||
const module = (await load()) as any;
|
||||
|
||||
if (module == undefined) {
|
||||
throw new Error(`${componentPath} does not load`);
|
||||
}
|
||||
const typeName = componentPath.slice(18, -16);
|
||||
registerComponent(typeName, module.default);
|
||||
} catch (err) {
|
||||
console.error(`Could not load component definition from '${componentPath}': ${err}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user