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
Showing only changes of commit bd2b7e7579 - Show all commits

View File

@ -104,42 +104,20 @@ export default class SvelteViewEngine extends ViewEngine {
const props = JSON.stringify(localMap); const props = JSON.stringify(localMap);
// Replaces // Replaces
const replaces: { [key: string]: string } = { const replaceMap: Record<string, string> = {
canonicalViewName: canonicalViewName, canonicalViewName: canonicalViewName,
props: props, props: props,
head: head, head: head,
html: html, html: html,
css: css, css: css,
}; };
const replaceOperations: Record<number, { return rawOutput.replace(
key: string; new RegExp(Object.keys(replaceMap).map(str => `%${str}%`).join('|'), 'g'),
replaceValue: string; (substring) => {
} | undefined> = {}; console.log(substring);
for (const entry of Object.entries(replaces)) { return replaceMap[substring.slice(1, substring.length - 1)];
const matches = rawOutput.matchAll(new RegExp(`%${entry[0]}%`, 'g')); },
for (const match of matches) { );
if (typeof match.index === 'number') {
replaceOperations[match.index] = {
key: `%${entry[0]}%`,
replaceValue: entry[1],
};
}
}
}
let output = '';
for (let i = 0; i < rawOutput.length; i++) {
const replaceOperation = replaceOperations[i];
if (replaceOperation) {
output += replaceOperation.replaceValue;
i += replaceOperation.key.length - 1;
} else {
output += rawOutput[i];
}
}
return output;
} }
public async stop(): Promise<void> { public async stop(): Promise<void> {