UNPKG

edge.js

Version:
139 lines (138 loc) 3.78 kB
/** * edge * * (c) EdgeJS * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ import type { ComponentsTree, LoaderContract, LoaderTemplate } from './types.js'; /** * The job of a loader is to load the template from a given path. * The base loader (shipped with edge) looks for files on the * file-system and reads them synchronously. * * You are free to define your own loaders that implements the [[LoaderContract]] interface. */ export declare class Loader implements LoaderContract { #private; /** * Returns an object of mounted directories with their public * names. * * ```js * loader.mounted * // output * * { * default: '/users/virk/code/app/views', * foo: '/users/virk/code/app/foo', * } * ``` */ get mounted(): { [key: string]: string; }; /** * Returns an object of templates registered as a raw string * * ```js * loader.templates * // output * * { * 'form.label': { template: 'Template contents' } * } * ``` */ get templates(): { [templatePath: string]: LoaderTemplate; }; /** * Mount a directory with a name for resolving views. If name is set * to `default`, then you can resolve views without prefixing the * disk name. * * ```js * loader.mount('default', join(__dirname, 'views')) * * // mount a named disk * loader.mount('admin', join(__dirname, 'admin/views')) * ``` */ mount(diskName: string, dirPath: string | URL): void; /** * Remove the previously mounted dir. * * ```js * loader.unmount('default') * ``` */ unmount(diskName: string): void; /** * Make path to a given template. The paths are resolved from the root * of the mounted directory. * * ```js * loader.makePath('welcome') // returns {diskRootPath}/welcome.edge * loader.makePath('admin::welcome') // returns {adminRootPath}/welcome.edge * loader.makePath('users.list') // returns {diskRootPath}/users/list.edge * ``` * * @throws Error if disk is not mounted and attempting to make path for it. */ makePath(templatePath: string): string; /** * Resolves the template by reading its contents from the disk * * ```js * loader.resolve('welcome', true) * * // output * { * template: `<h1> Template content </h1>`, * } * ``` */ resolve(templatePath: string): LoaderTemplate; /** * Register in memory template for a given path. This is super helpful * when distributing components. * * ```js * loader.register('welcome', { * template: '<h1> Template content </h1>', * Presenter: class Presenter { * constructor (state) { * this.state = state * } * } * }) * ``` * * @throws Error if template content is empty. */ register(templatePath: string, contents: LoaderTemplate): void; /** * Remove registered template */ remove(templatePath: string): void; /** * Returns a list of components from all the disks. We assume * the components are stored within the components directory. * * Also, we treat all in-memory templates as components. * * The return path is same the path you will pass to the `@component` * tag. */ listComponents(): ComponentsTree; /** * Returns a list of templates from all the disks and in-memory * templates as well */ listTemplates(): { diskName: string; templates: string[]; }[]; }