Skip to content

Commit

Permalink
Adjust starter for content-driven comps + fix var eval (#20).
Browse files Browse the repository at this point in the history
  • Loading branch information
jimafisk committed Dec 8, 2020
1 parent 229fdeb commit e9852fa
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 57 deletions.
5 changes: 3 additions & 2 deletions cmd/build/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,9 @@ func compileSvelte(ctx *v8go.Context, SSRctx *v8go.Context, layoutPath string, d
// Remove allComponents object (leaving just componentSignature) for SSR.
reAllComponentsDot := regexp.MustCompile(`allComponents\.`)
ssrStr = reAllComponentsDot.ReplaceAllString(ssrStr, "")
reAllComponentsBracket := regexp.MustCompile(`allComponents\[\"(.*)\"\]`)
ssrStr = reAllComponentsBracket.ReplaceAllString(ssrStr, "${1}")
reAllComponentsBracket := regexp.MustCompile(`allComponents\[\"?(.*)\"?\]`)
//ssrStr = reAllComponentsBracket.ReplaceAllString(ssrStr, "eval(${1})")
ssrStr = reAllComponentsBracket.ReplaceAllString(ssrStr, "globalThis[${1}]")

// Add component to context so it can be used to render HTML in data_source.go.
_, addSSRCompErr := SSRctx.RunScript(ssrStr, "create_ssr")
Expand Down
26 changes: 26 additions & 0 deletions defaults/content/blog/components.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"title": "Dynamic components example",
"body": [
"You can load components from your JSON data source without explicitly",
"importing each template using the <b>allComponents</b> helper. This is an",
"object that holds a reference to each '.svelte' file in your project. To pull a",
"template out of allComponents, you need to use a <em>component signature</em>.",
"Luckily they are easy to figure out, just convert '/' and '.' characters to '_'",
"in your layout path, so for example layout/components/template.svelte becomes",
"layout_components_template_svelte."
],
"components": [
{
"title": "For example we could grab the 'template' component again:",
"component": "layout_components_template_svelte",
"fields": {"type": "index"}
},
{
"title": "Or we could grab the 'incrementer':",
"component": "layout_components_incrementer_svelte",
"fields": {}
}
],
"author": "Jim Fisk",
"date": "8/25/2020"
}
6 changes: 0 additions & 6 deletions defaults/content/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,5 @@
"Take a look around to see how things work.",
"The bottom of each page will tell you where to find the corresponding template in your project.",
"If you get stuck, check out our <a href='https://plenti.co/docs' target='blank' rel='noopener noreferrer'>docs</a>. If you need extra help, <a href='https://github.com/plentico/plenti/issues/new' target='blank' rel='noopener noreferrer'>let us know</a>! Enjoy :)"
],
"components": [
{
"component": "template",
"fields": {"type": "index"}
}
]
}
10 changes: 10 additions & 0 deletions defaults/layout/content/blog.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
const unsubscribe = count.subscribe(value => {
count_value = value;
});
// Content driven dynamic components example:
export let components, allComponents;
</script>

<h1>{title}</h1>
Expand All @@ -30,4 +33,11 @@

<Uses type="blog" />

{#if components}
{#each components as { title, component, fields }}
{title}
<svelte:component this="{allComponents[component]}" {...fields} />
{/each}
{/if}

<p><a href="/">Back home</a></p>
14 changes: 2 additions & 12 deletions defaults/layout/content/index.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script>
export let title, intro, components, allContent;
import Grid from '../components/grid.svelte';
import { loadComponent } from '../scripts/load_component.svelte';
import Uses from "../components/template.svelte";
</script>

<h1>{title}</h1>
Expand All @@ -18,14 +18,4 @@
<br />
</div>

{#if components}
{#each components as { component, fields }}
{#await loadComponent(component)}
loading component...
{:then compClass}
<svelte:component this="{compClass}" {...fields} />
{:catch error}
{console.log(error.message)}
{/await}
{/each}
{/if}
<Uses type="index" />
9 changes: 0 additions & 9 deletions defaults/layout/scripts/load_component.svelte

This file was deleted.

66 changes: 38 additions & 28 deletions generated/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,32 @@ node_modules`),
"body": ["text"],
"author": "text",
"date": "date"
}`),
"/content/blog/components.json": []byte(`{
"title": "Dynamic components example",
"body": [
"You can load components from your JSON data source without explicitly",
"importing each template using the <b>allComponents</b> helper. This is an",
"object that holds a reference to each '.svelte' file in your project. To pull a",
"template out of allComponents, you need to use a <em>component signature</em>.",
"Luckily they are easy to figure out, just convert '/' and '.' characters to '_'",
"in your layout path, so for example layout/components/template.svelte becomes",
"layout_components_template_svelte."
],
"components": [
{
"title": "For example we could grab the 'template' component again:",
"component": "layout_components_template_svelte",
"fields": {"type": "index"}
},
{
"title": "Or we could grab the 'incrementer':",
"component": "layout_components_incrementer_svelte",
"fields": {}
}
],
"author": "Jim Fisk",
"date": "8/25/2020"
}`),
"/content/blog/perry.json": []byte(`{
"title": "Customize your Planarian",
Expand Down Expand Up @@ -64,12 +90,6 @@ node_modules`),
"Take a look around to see how things work.",
"The bottom of each page will tell you where to find the corresponding template in your project.",
"If you get stuck, check out our <a href='https://plenti.co/docs' target='blank' rel='noopener noreferrer'>docs</a>. If you need extra help, <a href='https://github.com/plentico/plenti/issues/new' target='blank' rel='noopener noreferrer'>let us know</a>! Enjoy :)"
],
"components": [
{
"component": "template",
"fields": {"type": "index"}
}
]
}`),
"/content/pages/_blueprint.json": []byte(`{
Expand Down Expand Up @@ -219,6 +239,9 @@ node_modules`),
const unsubscribe = count.subscribe(value => {
count_value = value;
});
// Content driven dynamic components example:
export let components, allComponents;
</script>
<h1>{title}</h1>
Expand All @@ -239,12 +262,19 @@ node_modules`),
<Uses type="blog" />
{#if components}
{#each components as { title, component, fields }}
{title}
<svelte:component this="{allComponents[component]}" {...fields} />
{/each}
{/if}
<p><a href="/">Back home</a></p>
`),
"/layout/content/index.svelte": []byte(`<script>
export let title, intro, components, allContent;
import Grid from '../components/grid.svelte';
import { loadComponent } from '../scripts/load_component.svelte';
import Uses from "../components/template.svelte";
</script>
<h1>{title}</h1>
Expand All @@ -261,17 +291,7 @@ node_modules`),
<br />
</div>
{#if components}
{#each components as { component, fields }}
{#await loadComponent(component)}
loading component...
{:then compClass}
<svelte:component this="{compClass}" {...fields} />
{:catch error}
{console.log(error.message)}
{/await}
{/each}
{/if}`),
<Uses type="index" />`),
"/layout/content/pages.svelte": []byte(`<script>
export let title, description;
import Uses from "../components/template.svelte";
Expand Down Expand Up @@ -442,16 +462,6 @@ node_modules`),
margin-right: 10px;
}
</style>
`),
"/layout/scripts/load_component.svelte": []byte(`<script context="module">
export const loadComponent = component => {
let compClassPromise = import("../components/" + component + ".svelte").then(res => res.default);
// Fix "Unhandled promise rejection" error.
// See: https://github.com/sveltejs/sapper/issues/487#issuecomment-529145749
compClassPromise.catch(err => null)
return compClassPromise;
}
</script>
`),
"/layout/scripts/make_title.svelte": []byte(`<script context="module">
export const makeTitle = filename => {
Expand Down

0 comments on commit e9852fa

Please sign in to comment.