Add svelte as a view engine to swaf #33
@ -1,66 +0,0 @@
|
||||
import nunjucks, {Environment} from "nunjucks";
|
||||
import config from "config";
|
||||
import {Express} from "express";
|
||||
import ApplicationComponent from "../ApplicationComponent";
|
||||
import Controller, {RouteParams} from "../Controller";
|
||||
import * as querystring from "querystring";
|
||||
import {ParsedUrlQueryInput} from "querystring";
|
||||
import * as util from "util";
|
||||
import * as path from "path";
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
* TODO: turn into a proper ViewEngine
|
||||
*/
|
||||
export default class NunjucksComponent extends ApplicationComponent {
|
||||
private readonly viewsPath: string[];
|
||||
private environment?: Environment;
|
||||
|
||||
public constructor(viewsPath: string[] = ['views']) {
|
||||
super();
|
||||
this.viewsPath = viewsPath;
|
||||
}
|
||||
|
||||
public async start(app: Express): Promise<void> {
|
||||
const opts = {
|
||||
autoescape: true,
|
||||
noCache: !config.get('view.cache'),
|
||||
throwOnUndefined: true,
|
||||
};
|
||||
this.environment = new nunjucks.Environment([
|
||||
...this.viewsPath.map(path => new nunjucks.FileSystemLoader(path, opts)),
|
||||
new nunjucks.FileSystemLoader(path.join(__dirname, '../../../views'), opts),
|
||||
new nunjucks.FileSystemLoader(path.join(__dirname, '../views'), opts),
|
||||
], opts)
|
||||
.addGlobal('route', (
|
||||
route: string,
|
||||
params: RouteParams = [],
|
||||
query: ParsedUrlQueryInput = {},
|
||||
absolute: boolean = false,
|
||||
): string => {
|
||||
return Controller.route(route, params, query, absolute);
|
||||
})
|
||||
.addGlobal('app_version', this.getApp().getVersion())
|
||||
.addGlobal('core_version', this.getApp().getCoreVersion())
|
||||
.addGlobal('querystring', querystring)
|
||||
.addGlobal('app', config.get('app'))
|
||||
|
||||
.addFilter('dump', (val) => {
|
||||
return util.inspect(val);
|
||||
})
|
||||
.addFilter('hex', (v: number) => {
|
||||
return v.toString(16);
|
||||
});
|
||||
|
||||
app.engine('njk', (path, options, callback) => {
|
||||
this.environment?.render(path, options, (err, res) => {
|
||||
callback(err, res || undefined);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
public getEnvironment(): Environment {
|
||||
if (!this.environment) throw new Error('Environment not initialized.');
|
||||
return this.environment;
|
||||
}
|
||||
}
|
@ -9,8 +9,8 @@ import ExpressAppComponent from "./ExpressAppComponent";
|
||||
import Application from "../Application";
|
||||
import RedisComponent from "./RedisComponent";
|
||||
import WebSocketListener from "../WebSocketListener";
|
||||
import NunjucksComponent from "./NunjucksComponent";
|
||||
import {Session} from "express-session";
|
||||
import ViewEngine from "../frontend/ViewEngine";
|
||||
|
||||
export default class WebSocketServerComponent extends ApplicationComponent {
|
||||
private wss?: WebSocket.Server;
|
||||
@ -19,9 +19,10 @@ export default class WebSocketServerComponent extends ApplicationComponent {
|
||||
private readonly application: Application,
|
||||
private readonly expressAppComponent: ExpressAppComponent,
|
||||
private readonly storeComponent: RedisComponent,
|
||||
private readonly nunjucksComponent?: NunjucksComponent,
|
||||
) {
|
||||
super();
|
||||
|
||||
ViewEngine.setGlobal('websocketUrl', config.get('public_websocket_url'));
|
||||
}
|
||||
|
||||
public async start(_app: Express): Promise<void> {
|
||||
@ -71,11 +72,6 @@ export default class WebSocketServerComponent extends ApplicationComponent {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
const env = this.nunjucksComponent?.getEnvironment();
|
||||
if (env) {
|
||||
env.addGlobal('websocketUrl', config.get('public_websocket_url'));
|
||||
}
|
||||
}
|
||||
|
||||
public async stop(): Promise<void> {
|
||||
|
Loading…
Reference in New Issue
Block a user