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",
"version": "0.22.0-rc.18",
"version": "0.22.0-rc.19",
"description": "Node web application framework and toolbelt.",
"repository": "https://gitlab.com/ArisuOngaku/wms-core",
"author": "Alice Gaudon <alice@gaudon.pro>",

View File

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

View File

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

View File

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