@primate/svelte 0.15.0 → 0.15.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/client/client.js +18 -0
- package/src/client/create-root.js +25 -0
- package/src/client/exports.js +3 -0
- package/src/client/liveview.js +16 -0
- package/src/client/rootname.js +1 -0
- package/src/module.js +12 -60
package/package.json
CHANGED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import rootname from "./rootname.js";
|
|
2
|
+
import liveview from "./liveview.js";
|
|
3
|
+
|
|
4
|
+
export default ({names, data}, options) => `
|
|
5
|
+
import * as components from "app";
|
|
6
|
+
const getComponents = names =>
|
|
7
|
+
Object.entries(components).filter(([name, component]) =>
|
|
8
|
+
names.includes(name))
|
|
9
|
+
.map(([, component]) => component);
|
|
10
|
+
let root = new components.${rootname}({
|
|
11
|
+
target: document.body,
|
|
12
|
+
hydrate: true,
|
|
13
|
+
props: {
|
|
14
|
+
components: getComponents([${names.map(name => `"${name}"`)}]),
|
|
15
|
+
data: JSON.parse(${JSON.stringify(JSON.stringify(data))}),
|
|
16
|
+
},
|
|
17
|
+
});
|
|
18
|
+
${options.liveview ? liveview : ""}`;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export default (length, data) => {
|
|
2
|
+
const n = length - 1;
|
|
3
|
+
const body = Array.from({length: n}, (_, i) => i - 1)
|
|
4
|
+
.reduceRight((child, _, i) => `
|
|
5
|
+
{#if components[${i + 1}]}
|
|
6
|
+
<svelte:component this={components[${i}]} ${data}={data[${i}]}>
|
|
7
|
+
${child}
|
|
8
|
+
</svelte:component>
|
|
9
|
+
{:else}
|
|
10
|
+
<svelte:component this={components[${i}]} ${data}={data[${i}]} />
|
|
11
|
+
{/if}
|
|
12
|
+
`, `<svelte:component this={components[${n}]} ${data}={data[${n}]} />`);
|
|
13
|
+
|
|
14
|
+
return `
|
|
15
|
+
<script>
|
|
16
|
+
import {afterUpdate} from "svelte";
|
|
17
|
+
export let components;
|
|
18
|
+
export let data;
|
|
19
|
+
export let update = () => undefined;
|
|
20
|
+
|
|
21
|
+
afterUpdate(update);
|
|
22
|
+
</script>
|
|
23
|
+
${body}
|
|
24
|
+
`;
|
|
25
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import rootname from "./rootname.js";
|
|
2
|
+
|
|
3
|
+
export default `
|
|
4
|
+
const {liveview} = components;
|
|
5
|
+
window.addEventListener("DOMContentLoaded", _ => liveview((props, update) => {
|
|
6
|
+
root.$destroy();
|
|
7
|
+
root = new components.${rootname}({
|
|
8
|
+
target: document.body,
|
|
9
|
+
hydrate: true,
|
|
10
|
+
props: {
|
|
11
|
+
components: getComponents(props.names),
|
|
12
|
+
data: props.data,
|
|
13
|
+
update,
|
|
14
|
+
},
|
|
15
|
+
});
|
|
16
|
+
}));`;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default "root";
|
package/src/module.js
CHANGED
|
@@ -4,12 +4,12 @@ import {Response, Status, MediaType} from "runtime-compat/http";
|
|
|
4
4
|
import * as compiler from "svelte/compiler";
|
|
5
5
|
import {tryreturn} from "runtime-compat/async";
|
|
6
6
|
import errors from "./errors.js";
|
|
7
|
+
import {client, create_root, rootname} from "./client/exports.js";
|
|
7
8
|
|
|
8
9
|
const endings = {
|
|
9
10
|
svelte: ".svelte",
|
|
10
11
|
js: ".svelte.js",
|
|
11
12
|
};
|
|
12
|
-
const rootname = "root";
|
|
13
13
|
|
|
14
14
|
const hash = async (string, algorithm = "sha-256") => {
|
|
15
15
|
const base = 16;
|
|
@@ -25,11 +25,6 @@ const normalize = async path => `_${await hash(path)}`;
|
|
|
25
25
|
|
|
26
26
|
const filename = `${rootname}.js`;
|
|
27
27
|
|
|
28
|
-
const options = {
|
|
29
|
-
server: {generate: "ssr", hydratable: true},
|
|
30
|
-
client: {generate: "dom", hydratable: true},
|
|
31
|
-
};
|
|
32
|
-
|
|
33
28
|
const type = "module";
|
|
34
29
|
|
|
35
30
|
const load = async path =>
|
|
@@ -43,6 +38,10 @@ const encoder = new TextEncoder();
|
|
|
43
38
|
|
|
44
39
|
const handler = (name, props = {}, {status = Status.OK, page} = {}) =>
|
|
45
40
|
async (app, {layouts = [], as_layout} = {}, request) => {
|
|
41
|
+
const options = {
|
|
42
|
+
liveview: app.liveview !== undefined,
|
|
43
|
+
};
|
|
44
|
+
const {headers} = request;
|
|
46
45
|
const {paths} = app.build;
|
|
47
46
|
const make = make_component(paths.server.join(app.config.build.app));
|
|
48
47
|
if (as_layout) {
|
|
@@ -58,8 +57,7 @@ const handler = (name, props = {}, {status = Status.OK, page} = {}) =>
|
|
|
58
57
|
const names = await Promise.all(components.map(component =>
|
|
59
58
|
normalize(component.name)));
|
|
60
59
|
|
|
61
|
-
|
|
62
|
-
if (liveview && request.headers.get(app.liveview.header) !== undefined) {
|
|
60
|
+
if (options.liveview && headers.get(app.liveview.header) !== undefined) {
|
|
63
61
|
return new Response(JSON.stringify({names, data}), {
|
|
64
62
|
status,
|
|
65
63
|
headers: {...await app.headers(),
|
|
@@ -73,67 +71,21 @@ const handler = (name, props = {}, {status = Status.OK, page} = {}) =>
|
|
|
73
71
|
data,
|
|
74
72
|
});
|
|
75
73
|
|
|
76
|
-
const code =
|
|
77
|
-
import * as components from "app";
|
|
78
|
-
const getComponents = names =>
|
|
79
|
-
Object.entries(components).filter(([name, component]) =>
|
|
80
|
-
names.includes(name))
|
|
81
|
-
.map(([, component]) => component);
|
|
82
|
-
let root = new components.${rootname}({
|
|
83
|
-
target: document.body,
|
|
84
|
-
hydrate: true,
|
|
85
|
-
props: {
|
|
86
|
-
components: getComponents([${names.map(name => `"${name}"`)}]),
|
|
87
|
-
data: JSON.parse(${JSON.stringify(JSON.stringify(data))}),
|
|
88
|
-
},
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
${liveview ? `
|
|
92
|
-
const {liveview} = components;
|
|
93
|
-
window.addEventListener("DOMContentLoaded", _ => liveview(props => {
|
|
94
|
-
root.$destroy();
|
|
95
|
-
root = new components.${rootname}({
|
|
96
|
-
target: document.body,
|
|
97
|
-
hydrate: true,
|
|
98
|
-
props: {
|
|
99
|
-
components: getComponents(props.names),
|
|
100
|
-
data: props.data,
|
|
101
|
-
},
|
|
102
|
-
});
|
|
103
|
-
}));
|
|
104
|
-
` : ""}
|
|
105
|
-
`;
|
|
74
|
+
const code = client({names, data}, options);
|
|
106
75
|
|
|
107
76
|
await app.publish({code, type, inline: true});
|
|
108
77
|
|
|
109
|
-
const headers = await app.headers();
|
|
78
|
+
const headers$ = await app.headers();
|
|
110
79
|
|
|
111
80
|
return new Response(await app.render({body: html, page}), {
|
|
112
81
|
status,
|
|
113
|
-
headers: {...headers
|
|
82
|
+
headers: {...headers$, "Content-Type": MediaType.TEXT_HTML},
|
|
114
83
|
});
|
|
115
84
|
};
|
|
116
85
|
|
|
117
|
-
const
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
.reduceRight((child, _, i) => `
|
|
121
|
-
{#if components[${i + 1}]}
|
|
122
|
-
<svelte:component this={components[${i}]} ${data}={data[${i}]}>
|
|
123
|
-
${child}
|
|
124
|
-
</svelte:component>
|
|
125
|
-
{:else}
|
|
126
|
-
<svelte:component this={components[${i}]} ${data}={data[${i}]} />
|
|
127
|
-
{/if}
|
|
128
|
-
`, `<svelte:component this={components[${n}]} ${data}={data[${n}]} />`);
|
|
129
|
-
|
|
130
|
-
return `
|
|
131
|
-
<script>
|
|
132
|
-
export let components;
|
|
133
|
-
export let data;
|
|
134
|
-
</script>
|
|
135
|
-
${body}
|
|
136
|
-
`;
|
|
86
|
+
const options = {
|
|
87
|
+
server: {generate: "ssr", hydratable: true},
|
|
88
|
+
client: {generate: "dom", hydratable: true},
|
|
137
89
|
};
|
|
138
90
|
|
|
139
91
|
const {svelte, js} = endings;
|