|
@@ -2,7 +2,7 @@ import pug from "pug";
|
|
|
import fs from "fs";
|
|
|
import path from "path";
|
|
|
|
|
|
-import { Renderer } from "./Renderer";
|
|
|
+import { CompiledTemplate, Renderer } from "./Renderer";
|
|
|
import Plugin from "./compiler/Plugin";
|
|
|
|
|
|
export namespace Compiler {
|
|
@@ -21,43 +21,6 @@ export namespace Compiler {
|
|
|
}
|
|
|
|
|
|
export default class PupperCompiler {
|
|
|
- /**
|
|
|
- * Compiles a single template file into a renderer instance
|
|
|
- * @param file The file to be compiled
|
|
|
- * @returns
|
|
|
- */
|
|
|
- public compileFile(file: string, options: Compiler.Options = {}): Renderer {
|
|
|
- return this.compile(fs.readFileSync(file, "utf8"), {
|
|
|
- ...options,
|
|
|
- pug: {
|
|
|
- basedir: path.dirname(file),
|
|
|
- filename: file,
|
|
|
- }
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Parses the compiler options into pug options
|
|
|
- * and put our plugins into it
|
|
|
- * @param options The compiler options
|
|
|
- * @returns
|
|
|
- */
|
|
|
- private getPugOptions(options: Compiler.Options = {}): pug.Options {
|
|
|
- // Create a new parser for this pug instance
|
|
|
- const parser = new Plugin();
|
|
|
-
|
|
|
- return {
|
|
|
- name: "pupper",
|
|
|
- filename: "pupper.pug",
|
|
|
- compileDebug: options.debug || false,
|
|
|
- // Always use self to prevent conflicts with other compilers
|
|
|
- self: true,
|
|
|
- // @ts-ignore
|
|
|
- plugins: [parser],
|
|
|
- ...options.pug || {}
|
|
|
- };
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* Compiles a template string into a renderer instance
|
|
|
* @param template The template string to be compiled
|
|
@@ -65,11 +28,9 @@ export default class PupperCompiler {
|
|
|
* @returns
|
|
|
*/
|
|
|
public compile(template: string, options: Compiler.Options = {}): Renderer {
|
|
|
-
|
|
|
-
|
|
|
try {
|
|
|
return new Renderer(
|
|
|
- pug.compile(template, this.getPugOptions(options))
|
|
|
+ pug.compile(template, this.getPugOptions(options)) as CompiledTemplate
|
|
|
);
|
|
|
} catch(e) {
|
|
|
throw (options.debug ? e : new Error("Failed to compile template:" + e.message));
|
|
@@ -77,23 +38,62 @@ export default class PupperCompiler {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Compiles a template string to a string
|
|
|
+ * Compiles a single template file into a renderer instance
|
|
|
+ * @param file The file to be compiled
|
|
|
+ * @returns
|
|
|
+ */
|
|
|
+ public compileFile(file: string, options: Compiler.Options = {}): Renderer {
|
|
|
+ const pugOptions = this.getPugOptions(options);
|
|
|
+ pugOptions.basedir = path.dirname(file);
|
|
|
+ pugOptions.filename = file;
|
|
|
+
|
|
|
+ return this.compile(fs.readFileSync(file, "utf8"), pugOptions);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Compiles a pupper template to a Javascript module
|
|
|
* @param template The template to be compiled
|
|
|
* @param options
|
|
|
* @returns
|
|
|
*/
|
|
|
public compileToString(template: string, options: Compiler.Options = {}): string {
|
|
|
- const parser = new Plugin();
|
|
|
-
|
|
|
try {
|
|
|
- const rendered = pug.compileClient(template, this.getPugOptions(options));
|
|
|
+ const pugOptions = this.getPugOptions(options);
|
|
|
+ const rendered = pug.compileClient(template, pugOptions);
|
|
|
|
|
|
return /*javascript*/`
|
|
|
${rendered}
|
|
|
- module.exports = pupper;
|
|
|
+ module.exports = ${pugOptions.name};
|
|
|
`;
|
|
|
} catch(e) {
|
|
|
throw (options.debug ? e : new Error("Failed to compile template:" + e.message));
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Parses the compiler options into pug options
|
|
|
+ * and put our plugins into it
|
|
|
+ * @param options The compiler options
|
|
|
+ * @returns
|
|
|
+ */
|
|
|
+ private getPugOptions(options: Compiler.Options = {}): pug.Options {
|
|
|
+ const pugOptions: pug.Options = {
|
|
|
+ // We use "render" as the function name
|
|
|
+ name: "render",
|
|
|
+ // The default filename (when no filename is given) is template.pupper
|
|
|
+ filename: "template.pupper",
|
|
|
+ compileDebug: options.debug || false,
|
|
|
+ // Always use self to prevent conflicts with other compilers
|
|
|
+ self: true,
|
|
|
+ plugins: [],
|
|
|
+ ...options.pug
|
|
|
+ };
|
|
|
+
|
|
|
+ // Create a new parser for this pug instance
|
|
|
+ pugOptions.plugins.push(
|
|
|
+ new Plugin(pugOptions)
|
|
|
+ );
|
|
|
+
|
|
|
+ return pugOptions;
|
|
|
+ }
|
|
|
}
|