Make nunjucks and static file server compatible with pkg

This commit is contained in:
Alice Gaudon 2020-09-24 22:33:37 +02:00
parent 87aae6bb33
commit 5dc0bd710a
4 changed files with 12 additions and 10 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "wms-core", "name": "wms-core",
"version": "0.22.0-rc.18", "version": "0.22.0-rc.19",
"description": "Node web application framework and toolbelt.", "description": "Node web application framework and toolbelt.",
"repository": "https://gitlab.com/ArisuOngaku/wms-core", "repository": "https://gitlab.com/ArisuOngaku/wms-core",
"author": "Alice Gaudon <alice@gaudon.pro>", "author": "Alice Gaudon <alice@gaudon.pro>",

View File

@ -7,12 +7,13 @@ import {ServerError} from "../HttpError";
import * as querystring from "querystring"; import * as querystring from "querystring";
import {ParsedUrlQueryInput} from "querystring"; import {ParsedUrlQueryInput} from "querystring";
import * as util from "util"; import * as util from "util";
import * as path from "path";
export default class NunjucksComponent extends ApplicationComponent<void> { export default class NunjucksComponent extends ApplicationComponent<void> {
private readonly viewsPath: string; private readonly viewsPath: string[];
private env?: Environment; private env?: Environment;
public constructor(viewsPath: string = 'views') { public constructor(viewsPath: string[] = ['views']) {
super(); super();
this.viewsPath = viewsPath; this.viewsPath = viewsPath;
} }
@ -28,13 +29,12 @@ export default class NunjucksComponent extends ApplicationComponent<void> {
} }
} }
this.env = nunjucks.configure([ this.env = new nunjucks.Environment([
this.viewsPath, ...this.viewsPath.map(path => new nunjucks.FileSystemLoader(path)),
'views', new nunjucks.FileSystemLoader(path.join(__dirname, '../../../views')),
'node_modules/wms-core/views' new nunjucks.FileSystemLoader(path.join(__dirname, '../views')),
], { ], {
autoescape: true, autoescape: true,
express: app,
noCache: !config.get('view.cache'), noCache: !config.get('view.cache'),
throwOnUndefined: true, throwOnUndefined: true,
}) })
@ -54,6 +54,7 @@ export default class NunjucksComponent extends ApplicationComponent<void> {
.addFilter('hex', (v: number) => { .addFilter('hex', (v: number) => {
return v.toString(16); return v.toString(16);
}); });
this.env.express(app);
app.set('view engine', 'njk'); app.set('view engine', 'njk');
} }

View File

@ -1,6 +1,7 @@
import ApplicationComponent from "../ApplicationComponent"; import ApplicationComponent from "../ApplicationComponent";
import express, {Router} from "express"; import express, {Router} from "express";
import {PathParams} from "express-serve-static-core"; import {PathParams} from "express-serve-static-core";
import * as path from "path";
export default class ServeStaticDirectoryComponent extends ApplicationComponent<void> { export default class ServeStaticDirectoryComponent extends ApplicationComponent<void> {
private readonly root: string; private readonly root: string;
@ -8,7 +9,7 @@ export default class ServeStaticDirectoryComponent extends ApplicationComponent<
constructor(root: string, routePath?: PathParams) { constructor(root: string, routePath?: PathParams) {
super(); super();
this.root = root; this.root = path.join(__dirname, '../../../', root);
this.path = routePath; this.path = routePath;
} }

View File

@ -70,7 +70,7 @@ export class TestApp extends Application {
this.use(new ServeStaticDirectoryComponent('public')); this.use(new ServeStaticDirectoryComponent('public'));
// Dynamic views and routes // Dynamic views and routes
this.use(new NunjucksComponent('test/views')); this.use(new NunjucksComponent(['test/views', 'views']));
this.use(new RedirectBackComponent()); this.use(new RedirectBackComponent());
// Services // Services