Add support for view engines, PNPM
This commit is contained in:
33
src/views/PugViewEngine.ts
Normal file
33
src/views/PugViewEngine.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import {ViewEngine} from "./ViewEngine"
|
||||
import {Injectable} from "@extollo/di"
|
||||
import * as pug from "pug"
|
||||
|
||||
@Injectable()
|
||||
export class PugViewEngine extends ViewEngine {
|
||||
protected compileCache: {[key: string]: ((locals?: pug.LocalsObject) => string)} = {}
|
||||
|
||||
public renderString(templateString: string, locals: { [p: string]: any }): string | Promise<string> {
|
||||
return pug.compile(templateString, this.getOptions())(locals)
|
||||
}
|
||||
|
||||
public renderByName(templateName: string, locals: { [p: string]: any }): string | Promise<string> {
|
||||
let compiled = this.compileCache[templateName]
|
||||
if ( compiled ) return compiled(locals)
|
||||
|
||||
if ( !templateName.endsWith('.pug') ) templateName += '.pug'
|
||||
const filePath = this.path.concat(...templateName.split(':'))
|
||||
compiled = pug.compileFile(filePath.toLocal, this.getOptions())
|
||||
|
||||
this.compileCache[templateName] = compiled
|
||||
return compiled(locals)
|
||||
}
|
||||
|
||||
protected getOptions() {
|
||||
return {
|
||||
basedir: this.path.toLocal,
|
||||
debug: this.debug,
|
||||
compileDebug: this.debug,
|
||||
globals: [],
|
||||
}
|
||||
}
|
||||
}
|
||||
23
src/views/ViewEngine.ts
Normal file
23
src/views/ViewEngine.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import {AppClass} from "../lifecycle/AppClass"
|
||||
import {Config} from "../service/Config"
|
||||
import {Container} from "@extollo/di"
|
||||
import {UniversalPath} from "@extollo/util"
|
||||
|
||||
export abstract class ViewEngine extends AppClass {
|
||||
protected readonly config: Config
|
||||
protected readonly debug: boolean
|
||||
|
||||
constructor() {
|
||||
super()
|
||||
this.config = Container.getContainer().make(Config)
|
||||
this.debug = (this.config.get('server.mode', 'production') === 'development'
|
||||
|| this.config.get('server.debug', false))
|
||||
}
|
||||
|
||||
public get path(): UniversalPath {
|
||||
return this.app().appPath(...['resources', 'views']) // FIXME allow configuring
|
||||
}
|
||||
|
||||
public abstract renderString(templateString: string, locals: {[key: string]: any}): string | Promise<string>
|
||||
public abstract renderByName(templateName: string, locals: {[key: string]: any}): string | Promise<string>
|
||||
}
|
||||
66
src/views/ViewEngineFactory.ts
Normal file
66
src/views/ViewEngineFactory.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
import {
|
||||
AbstractFactory,
|
||||
Container,
|
||||
DependencyRequirement,
|
||||
PropertyDependency,
|
||||
isInstantiable,
|
||||
DEPENDENCY_KEYS_METADATA_KEY,
|
||||
DEPENDENCY_KEYS_PROPERTY_METADATA_KEY
|
||||
} from "@extollo/di"
|
||||
import {Collection, ErrorWithContext} from "@extollo/util"
|
||||
import {Logging} from "../service/Logging";
|
||||
import {Config} from "../service/Config";
|
||||
import {ViewEngine} from "./ViewEngine";
|
||||
import {PugViewEngine} from "./PugViewEngine";
|
||||
|
||||
export class ViewEngineFactory extends AbstractFactory {
|
||||
protected readonly logging: Logging
|
||||
protected readonly config: Config
|
||||
|
||||
constructor() {
|
||||
super({})
|
||||
this.logging = Container.getContainer().make<Logging>(Logging)
|
||||
this.config = Container.getContainer().make<Config>(Config)
|
||||
}
|
||||
|
||||
produce(dependencies: any[], parameters: any[]): ViewEngine {
|
||||
return new (this.getViewEngineClass())
|
||||
}
|
||||
|
||||
match(something: any) {
|
||||
return something === ViewEngine
|
||||
}
|
||||
|
||||
getDependencyKeys(): Collection<DependencyRequirement> {
|
||||
const meta = Reflect.getMetadata(DEPENDENCY_KEYS_METADATA_KEY, this.getViewEngineClass())
|
||||
if ( meta ) return meta
|
||||
return new Collection<DependencyRequirement>()
|
||||
}
|
||||
|
||||
getInjectedProperties(): Collection<PropertyDependency> {
|
||||
const meta = new Collection<PropertyDependency>()
|
||||
let currentToken = this.getViewEngineClass()
|
||||
|
||||
do {
|
||||
const loadedMeta = Reflect.getMetadata(DEPENDENCY_KEYS_PROPERTY_METADATA_KEY, currentToken)
|
||||
if ( loadedMeta ) meta.concat(loadedMeta)
|
||||
currentToken = Object.getPrototypeOf(currentToken)
|
||||
} while (Object.getPrototypeOf(currentToken) !== Function.prototype && Object.getPrototypeOf(currentToken) !== Object.prototype)
|
||||
|
||||
return meta
|
||||
}
|
||||
|
||||
protected getViewEngineClass() {
|
||||
const ViewEngineClass = this.config.get('server.view_engine.driver', PugViewEngine)
|
||||
|
||||
if ( !isInstantiable(ViewEngineClass) || !(ViewEngineClass.prototype instanceof ViewEngine) ) {
|
||||
const e = new ErrorWithContext('Provided session class does not extend from @extollo/lib.ViewEngine');
|
||||
e.context = {
|
||||
config_key: 'server.view_engine.driver',
|
||||
class: ViewEngineClass.toString(),
|
||||
}
|
||||
}
|
||||
|
||||
return ViewEngineClass
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user