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 15 additions and 11 deletions
Showing only changes of commit 076cda8008 - Show all commits

View File

@ -6,20 +6,19 @@
<style>%css%</style> <style>%css%</style>
<script type="module" defer> <script type="module" defer>
import View from '/js/views/%canonicalViewName%.js'; import View from '/js/views/%canonicalViewName%.js';
import {locals} from '/js/stores.js'; import * as stores from '/js/stores.js';
const localStore = stores[Object.keys(stores)[0]].locals;
locals.set(%locals%); const locals = %locals%;
localStore.set((key, args) => {
return locals[args ?
`'${key}', \`${args}\``
: `'${key}'`];
});
new View({ new View({
hydrate: true, hydrate: true,
target: document.body, target: document.body,
props: {
locals: (key, args) => {
return locals[args ?
`'${key}', \`${args}\``
: `'${key}'`];
},
},
}); });
</script> </script>
</head> </head>

View File

@ -221,7 +221,7 @@ export default class SvelteViewEngine extends ViewEngine {
logger.info(canonicalName + ' > ', 'Replacing backend calls'); logger.info(canonicalName + ' > ', 'Replacing backend calls');
// Skip replace if there is no swaf export // Skip replace if there is no swaf export
if (!code.match(/export[ \n]+let[ \n]+locals[ \n]*=[ \n]*{[ \n]*}/)) { if (!code.match(/import[ \n]+\{[ \n]*locals[ \n]*\}[ \n]+from[ \n]+["'](\.\.\/)+ts\/stores\.js["']/)) {
return { return {
backendCalls: [], backendCalls: [],
code: code, code: code,
@ -296,7 +296,12 @@ export default class SvelteViewEngine extends ViewEngine {
// Load locals into locals store // Load locals into locals store
const localsModulePath = "../../build/ts/stores.js"; const localsModulePath = "../../build/ts/stores.js";
const localsModule = await import(localsModulePath); const localsModule = await import(localsModulePath);
localsModule.locals.set(ViewEngine.getGlobals()); const locals = ViewEngine.getGlobals();
localsModule.locals.set((key: string, args: string) => {
return locals[args ?
`'${key}', \`${args}\``
: `'${key}'`];
});
// Load module and render // Load module and render
return requireFromString(svelteSsr.js.code, file + nanoid() + '.js').default.render(); return requireFromString(svelteSsr.js.code, file + nanoid() + '.js').default.render();