2021-03-24 16:07:50 +01:00
|
|
|
import {Express, Router} from "express";
|
2021-03-18 16:22:25 +01:00
|
|
|
|
2021-05-03 19:29:22 +02:00
|
|
|
import ApplicationComponent from "../ApplicationComponent.js";
|
2021-05-04 17:04:14 +02:00
|
|
|
import AssetCompiler from "../frontend/AssetCompiler.js";
|
|
|
|
import AssetPreCompiler from "../frontend/AssetPreCompiler.js";
|
2021-05-03 19:29:22 +02:00
|
|
|
import ViewEngine from "../frontend/ViewEngine.js";
|
2021-11-24 22:08:38 +01:00
|
|
|
import LazyLocalsCoreComponent from "./core/LazyLocalsCoreComponent.js";
|
2021-05-03 19:29:22 +02:00
|
|
|
|
2021-03-18 16:22:25 +01:00
|
|
|
export default class FrontendToolsComponent extends ApplicationComponent {
|
2021-05-04 17:04:14 +02:00
|
|
|
private readonly publicDir: string;
|
|
|
|
private readonly assetPreCompilers: AssetPreCompiler[];
|
2021-03-18 16:22:25 +01:00
|
|
|
|
|
|
|
public constructor(
|
2021-05-04 17:04:14 +02:00
|
|
|
private readonly assetCompiler: AssetCompiler,
|
|
|
|
...assetPreCompilers: AssetPreCompiler[]
|
2021-03-18 16:22:25 +01:00
|
|
|
) {
|
|
|
|
super();
|
2021-05-04 17:04:14 +02:00
|
|
|
this.assetPreCompilers = assetPreCompilers;
|
|
|
|
this.publicDir = this.assetCompiler.targetDir;
|
|
|
|
|
|
|
|
for (const assetPreCompiler of this.assetPreCompilers) {
|
|
|
|
if (assetPreCompiler.isPublic()) {
|
|
|
|
this.assetCompiler.addExtension(assetPreCompiler.getExtension());
|
|
|
|
}
|
|
|
|
}
|
2021-03-18 16:22:25 +01:00
|
|
|
}
|
|
|
|
|
2021-03-24 16:07:50 +01:00
|
|
|
public async start(app: Express): Promise<void> {
|
2021-05-04 17:04:14 +02:00
|
|
|
this.hookPreCompilers();
|
|
|
|
|
2021-03-24 21:41:13 +01:00
|
|
|
// Setup express view engine
|
2021-04-28 14:53:46 +02:00
|
|
|
let main = true;
|
2021-05-04 17:04:14 +02:00
|
|
|
for (const assetPreCompiler of this.assetPreCompilers) {
|
|
|
|
if (assetPreCompiler instanceof ViewEngine) {
|
2021-11-24 22:08:38 +01:00
|
|
|
assetPreCompiler.setup(app, main, this.getApp().as(LazyLocalsCoreComponent));
|
2021-05-04 17:04:14 +02:00
|
|
|
main = false;
|
|
|
|
}
|
2021-04-28 14:53:46 +02:00
|
|
|
}
|
2021-03-24 16:07:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public async stop(): Promise<void> {
|
2021-05-04 17:04:14 +02:00
|
|
|
for (const assetPreCompiler of this.assetPreCompilers) {
|
|
|
|
await assetPreCompiler.stop();
|
2021-04-28 14:53:46 +02:00
|
|
|
}
|
2021-03-18 16:22:25 +01:00
|
|
|
}
|
|
|
|
|
2021-11-24 22:08:38 +01:00
|
|
|
public async initRoutes(router: Router): Promise<void> {
|
2021-04-28 14:10:29 +02:00
|
|
|
router.use((req, res, next) => {
|
2021-11-24 22:08:38 +01:00
|
|
|
// Request context locals
|
2021-04-28 14:10:29 +02:00
|
|
|
res.locals.url = req.url;
|
|
|
|
res.locals.params = req.params;
|
|
|
|
res.locals.query = req.query;
|
|
|
|
res.locals.body = req.body;
|
|
|
|
|
|
|
|
next();
|
|
|
|
});
|
2021-03-18 16:22:25 +01:00
|
|
|
}
|
2021-03-26 10:56:32 +01:00
|
|
|
|
2021-05-04 17:04:14 +02:00
|
|
|
public hookPreCompilers(): void {
|
|
|
|
for (const assetPreCompiler of this.assetPreCompilers) {
|
|
|
|
assetPreCompiler.onPreCompile(async watch => {
|
|
|
|
await this.assetCompiler.compile(watch);
|
|
|
|
});
|
|
|
|
assetPreCompiler.onInputChange(async restart => {
|
|
|
|
await this.assetCompiler.stopWatching(restart);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public async preCompileViews(watch: boolean): Promise<void> {
|
|
|
|
for (const viewEngine of this.assetPreCompilers) {
|
|
|
|
await viewEngine.preCompileAll(watch);
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.assetCompiler.compile(watch);
|
|
|
|
|
|
|
|
if (watch) {
|
|
|
|
this.hookPreCompilers();
|
|
|
|
}
|
|
|
|
}
|
2021-03-18 16:22:25 +01:00
|
|
|
}
|