Add svelte as a view engine to swaf #33

Merged
ashpie merged 97 commits from svelte into develop 2021-11-09 19:31:22 +01:00
2 changed files with 17 additions and 13 deletions
Showing only changes of commit 13bd933b0b - Show all commits

View File

@ -1,8 +1,18 @@
import {Router} from "express";
import ApplicationComponent from "../ApplicationComponent.js";
import FrontendToolsComponent from "./FrontendToolsComponent.js";
export default class FormHelperComponent extends ApplicationComponent {
public async init(): Promise<void> {
const globals = this.getApp().asOptional(FrontendToolsComponent)?.getGlobals();
if (globals) {
globals.set('validation', () => ({}));
globals.set('previousFormData', () => ({}));
}
}
public async initRoutes(router: Router): Promise<void> {
router.use((req, res, next) => {
let _validation: unknown | null;
@ -19,20 +29,11 @@ export default class FormHelperComponent extends ApplicationComponent {
res.locals.previousFormData = () => {
if (!_previousFormData) {
const v = req.flash('previousFormData');
_previousFormData = v.length > 0 ? v [0] : null;
_previousFormData = v.length > 0 ? v[0] : null;
}
return _previousFormData;
};
let _formPrefix: string | null;
res.locals.getFormPrefix = () => {
return _formPrefix;
};
res.locals.setFormPrefix = (formPrefix: string) => {
_formPrefix = formPrefix;
return '';
};
next();
});

View File

@ -242,8 +242,8 @@ export default class SvelteViewEngine extends ViewEngine {
}
let backendCall = output.substring(startIndex, endIndex);
if (backendCall.match(/([^()]+)\((.+?)\)/)) {
backendCall = backendCall.replace(/([^()]+)\((.+?)\)/, "'$1', `[$2]`");
if (backendCall.match(/([^()]+)\((.*?)\)/)) {
backendCall = backendCall.replace(/([^()]+)\((.*?)\)/, "'$1', `[$2]`");
} else {
backendCall = backendCall.replace(/([^()]+)(\(\))?/, "'$1'");
}
@ -309,7 +309,10 @@ export default class SvelteViewEngine extends ViewEngine {
if (arg.startsWith("'")) return '"' + arg.substring(1, arg.length - 1) + '"';
return arg;
})
.map(arg => Function(`"use strict";const $locals = arguments[0];return (${arg});`)(locals)); // Uses named parameter locals
.filter(arg => arg.length > 0)
.map(arg => {
return Function(`"use strict";const $locals = arguments[0];return (${arg});`)(locals);
}); // Uses named parameter locals
const f = locals[key];
if (typeof f !== 'function') throw new Error(key + ' is not a function.');