UNPKG

7.47 kBTypeScriptView Raw
1// Type definitions for pug 2.0
2// Project: https://github.com/pugjs/pug
3// Definitions by: TonyYang <https://github.com/TonyPythoneer>
4// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
5
6/**
7 * Table of Contents
8 *
9 * - Options https://pugjs.org/api/reference.html#options
10 * - Methods https://pugjs.org/api/reference.html#methods
11 *
12 * The order of contents is according to pugjs API document.
13 */
14declare module 'pug' {
15 ////////////////////////////////////////////////////////////
16 /// Options https://pugjs.org/api/reference.html#options ///
17 ////////////////////////////////////////////////////////////
18 export interface Options {
19 /** The name of the file being compiled. Used in exceptions, and required for relative includes and extends. Defaults to 'Pug'. */
20 filename?: string | undefined,
21 /** The root directory of all absolute inclusion. */
22 basedir?: string | undefined,
23 /** If the doctype is not specified as part of the template, you can specify it here. It is sometimes useful to get self-closing tags and remove mirroring of boolean attributes; see doctype documentation for more information. */
24 doctype?: string | undefined,
25 /** Adds whitespace to the resulting HTML to make it easier for a human to read using ' ' as indentation. If a string is specified, that will be used as indentation instead (e.g. '\t'). Defaults to false. */
26 pretty?: boolean | string | undefined,
27 /** Hash table of custom filters. Defaults to undefined. */
28 filters?: any,
29 /** Use a self namespace to hold the locals. It will speed up the compilation, but instead of writing variable you will have to write self.variable to access a property of the locals object. Defaults to false. */
30 self?: boolean | undefined,
31 /** If set to true, the tokens and function body are logged to stdout. */
32 debug?: boolean | undefined,
33 /** If set to true, the function source will be included in the compiled template for better error messages (sometimes useful in development). It is enabled by default unless used with Express in production mode. */
34 compileDebug?: boolean | undefined,
35 /** Add a list of global names to make accessible in templates. */
36 globals?: Array<string> | undefined,
37 /** If set to true, compiled functions are cached. filename must be set as the cache key. Only applies to render functions. Defaults to false. */
38 cache?: boolean | undefined,
39 /** Inline runtime functions instead of require-ing them from a shared version. For compileClient functions, the default is true so that one does not have to include the runtime. For all other compilation or rendering types, the default is false. */
40 inlineRuntimeFunctions?: boolean | undefined,
41 /** The name of the template function. Only applies to compileClient functions. Defaults to 'template'. */
42 name?: string | undefined
43 }
44
45 ////////////////////////////////////////////////////////////
46 /// Methods https://pugjs.org/api/reference.html#methods ///
47 ////////////////////////////////////////////////////////////
48 /**
49 * Compile a Pug template to a function which can be rendered multiple times with different locals.
50 */
51 export function compile(template: string, options?: Options): compileTemplate;
52
53 /**
54 * Compile a Pug template from a file to a function which can be rendered multiple times with different locals.
55 */
56 export function compileFile(path: string, options?: Options): compileTemplate;
57
58 /**
59 * Compile a Pug template to a string of JavaScript that can be used client side along with the Pug runtime.
60 */
61 export function compileClient(template: string, options?: Options): string;
62
63 /**
64 * Compile a Pug template to an object of the form:
65 * {
66 * 'body': 'function (locals) {...}',
67 * 'dependencies': ['filename.pug']
68 * }
69 * that can be used client side along with the Pug runtime.
70 * You should only use this method if you need dependencies to implement something like watching for changes to the Pug files.
71 */
72 export function compileClientWithDependenciesTracked(template: string, options?: Options): {
73 body: string;
74 dependencies: string[];
75 };
76
77 /**
78 * Compile a Pug template file to a string of JavaScript that can be used client side along with the Pug runtime.
79 */
80 export function compileFileClient(path: string, options?: Options): string;
81
82 /**
83 * Compile a Pug template and render it without locals to html string.
84 */
85 export function render(template: string): string;
86 /**
87 * Compile a Pug template and render it with locals to html string.
88 * @param {(Options & LocalsObject)} options Pug Options and rendering locals
89 */
90 export function render(template: string, options: Options & LocalsObject): string;
91 /**
92 * Compile a Pug template and render it without locals to html string.
93 * @param {((err: Error | null, html: string) => void)} callback Node.js-style callback receiving the rendered results. This callback is called synchronously.
94 */
95 export function render(template: string, callback: (err: Error | null, html: string) => void): void;
96 /**
97 * Compile a Pug template and render it with locals to html string.
98 * @param {(Options & LocalsObject)} options Pug Options and rendering locals
99 * @param {((err: Error | null, html: string) => void)} callback Node.js-style callback receiving the rendered results. This callback is called synchronously.
100 */
101 export function render(template: string, options: Options & LocalsObject, callback: (err: Error | null, html: string) => void): void;
102
103 /**
104 * Compile a Pug template from a file and render it without locals to html string.
105 */
106 export function renderFile(path: string): string;
107 /**
108 * Compile a Pug template from a file and render it with locals to html string.
109 * @param {(Options & LocalsObject)} options Pug Options and rendering locals
110 */
111 export function renderFile(path: string, options: Options & LocalsObject): string;
112 /**
113 * Compile a Pug template from a file and render it without locals to html string.
114 * @param {((err: Error | null, html: string) => void)} callback Node.js-style callback receiving the rendered results. This callback is called synchronously.
115 */
116 export function renderFile(path: string, callback: (err: Error | null, html: string) => void): void;
117 /**
118 * Compile a Pug template from a file and render it with locals to html string.
119 * @param {(Options & LocalsObject)} options Pug Options and rendering locals
120 * @param {((err: Error | null, html: string) => void)} callback Node.js-style callback receiving the rendered results. This callback is called synchronously.
121 */
122 export function renderFile(path: string, options: Options & LocalsObject, callback: (err: Error | null, html: string) => void): void;
123
124 ///////////////////
125 /// Types ///
126 ///////////////////
127
128 /**
129 * A function that can be use to render html string of compiled template.
130 */
131 export type compileTemplate = (locals?: LocalsObject) => string;
132
133 /**
134 * An object that can have multiple properties of any type.
135 */
136 export interface LocalsObject {
137 [propName: string]: any;
138 }
139}