@primate/svelte 0.15.2 → 0.16.0
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/README.md +2 -3
- package/package.json +5 -4
- package/src/client/rootname.js +1 -1
- package/src/module.js +54 -129
package/README.md
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
# Svelte
|
|
1
|
+
# Svelte
|
|
2
2
|
|
|
3
|
-
This handler
|
|
4
|
-
with the .svelte extension.
|
|
3
|
+
This handler serves Svelte components with the `svelte` file extension.
|
|
5
4
|
|
|
6
5
|
## [Documentation](https://primatejs.com/modules/svelte)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@primate/svelte",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.16.0",
|
|
4
4
|
"description": "Primate Svelte module",
|
|
5
5
|
"homepage": "https://primatejs.com/modules/svelte",
|
|
6
6
|
"bugs": "https://github.com/primatejs/primate/issues",
|
|
@@ -16,11 +16,12 @@
|
|
|
16
16
|
"directory": "packages/svelte"
|
|
17
17
|
},
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"
|
|
20
|
-
"
|
|
19
|
+
"@primate/frontend": "^0.2.0",
|
|
20
|
+
"runtime-compat": "^0.25.7",
|
|
21
|
+
"svelte": "^4.2.0"
|
|
21
22
|
},
|
|
22
23
|
"peerDependencies": {
|
|
23
|
-
"primate": "0.
|
|
24
|
+
"primate": "0.22"
|
|
24
25
|
},
|
|
25
26
|
"type": "module",
|
|
26
27
|
"exports": "./src/exports.js"
|
package/src/client/rootname.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export default "
|
|
1
|
+
export default "svelte_root";
|
package/src/module.js
CHANGED
|
@@ -1,48 +1,18 @@
|
|
|
1
|
-
import crypto from "runtime-compat/crypto";
|
|
2
|
-
import {Path} from "runtime-compat/fs";
|
|
3
1
|
import {Response, Status, MediaType} from "runtime-compat/http";
|
|
2
|
+
import * as base from "@primate/frontend";
|
|
4
3
|
import * as compiler from "svelte/compiler";
|
|
5
|
-
import {tryreturn} from "runtime-compat/async";
|
|
6
|
-
import errors from "./errors.js";
|
|
7
4
|
import {client, create_root, rootname} from "./client/exports.js";
|
|
8
5
|
|
|
9
|
-
const
|
|
10
|
-
svelte: ".svelte",
|
|
11
|
-
js: ".svelte.js",
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
const encoder = new TextEncoder();
|
|
15
|
-
const hash = async (string, algorithm = "sha-256") => {
|
|
16
|
-
const base = 16;
|
|
17
|
-
const target_pad_length = 2;
|
|
18
|
-
const target_slice = 12;
|
|
19
|
-
const bytes = await crypto.subtle.digest(algorithm, encoder.encode(string));
|
|
20
|
-
return Array.from(new Uint8Array(bytes))
|
|
21
|
-
.map(byte => byte.toString(base).padStart(target_pad_length, "0"))
|
|
22
|
-
.join("")
|
|
23
|
-
.slice(0, target_slice);
|
|
24
|
-
};
|
|
25
|
-
const normalize = async path => `_${await hash(path)}`;
|
|
26
|
-
|
|
27
|
-
const filename = `${rootname}.js`;
|
|
28
|
-
|
|
6
|
+
const normalize = base.normalize("svelte");
|
|
29
7
|
const type = "module";
|
|
30
8
|
|
|
31
|
-
const
|
|
32
|
-
tryreturn(async () => (await import(`${path}.js`)).default)
|
|
33
|
-
.orelse(_ => errors.MissingComponent.throw(path.name, path));
|
|
34
|
-
|
|
35
|
-
const make_component = base => async (name, props) =>
|
|
36
|
-
({name, component: await load(base.join(name)), props});
|
|
37
|
-
|
|
38
|
-
const handler = (name, props = {}, {status = Status.OK, page} = {}) =>
|
|
9
|
+
const handler = config => (name, props = {}, {status = Status.OK, page} = {}) =>
|
|
39
10
|
async (app, {layouts = [], as_layout} = {}, request) => {
|
|
11
|
+
const {make, root} = config;
|
|
40
12
|
const options = {
|
|
41
13
|
liveview: app.liveview !== undefined,
|
|
42
14
|
};
|
|
43
15
|
const {headers} = request;
|
|
44
|
-
const {paths} = app.build;
|
|
45
|
-
const make = make_component(paths.server.join(app.config.build.app));
|
|
46
16
|
if (as_layout) {
|
|
47
17
|
return make(name, props);
|
|
48
18
|
}
|
|
@@ -63,9 +33,8 @@ const handler = (name, props = {}, {status = Status.OK, page} = {}) =>
|
|
|
63
33
|
});
|
|
64
34
|
}
|
|
65
35
|
|
|
66
|
-
const root = paths.server.join(filename);
|
|
67
36
|
const imported = (await import(root)).default;
|
|
68
|
-
const {html} = imported.render({
|
|
37
|
+
const {html: body, head} = imported.render({
|
|
69
38
|
components: components.map(({component}) => component),
|
|
70
39
|
data,
|
|
71
40
|
});
|
|
@@ -76,104 +45,60 @@ const handler = (name, props = {}, {status = Status.OK, page} = {}) =>
|
|
|
76
45
|
// needs to be called before app.render
|
|
77
46
|
const headers$ = await app.headers();
|
|
78
47
|
|
|
79
|
-
return new Response(await app.render({body
|
|
48
|
+
return new Response(await app.render({body, page, head}), {
|
|
80
49
|
status,
|
|
81
50
|
headers: {...headers$, "Content-Type": MediaType.TEXT_HTML},
|
|
82
51
|
});
|
|
83
52
|
};
|
|
84
53
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
await to.directory.file.create();
|
|
110
|
-
await to.file.write(server.js.code.replaceAll(svelte, js));
|
|
111
|
-
}));
|
|
112
|
-
|
|
113
|
-
const root = create_root(app.layoutDepth, dynamicProps);
|
|
114
|
-
const server = compiler.compile(root, options.server).js.code;
|
|
115
|
-
const to = app.build.paths.server.join(filename);
|
|
116
|
-
await to.file.write(server);
|
|
117
|
-
|
|
118
|
-
return next(app);
|
|
119
|
-
},
|
|
120
|
-
async publish(app, next) {
|
|
121
|
-
const source = app.build.paths.components;
|
|
122
|
-
const components = await source.collect(/^.*.svelte$/u);
|
|
123
|
-
await app.import("svelte");
|
|
124
|
-
|
|
125
|
-
// liveview
|
|
126
|
-
if (app.liveview !== undefined) {
|
|
127
|
-
app.bootstrap({
|
|
128
|
-
type: "script",
|
|
129
|
-
code: "export {default as liveview} from \"@primate/liveview\";\n",
|
|
54
|
+
export default ({
|
|
55
|
+
dynamicProps = "data",
|
|
56
|
+
extension = "svelte",
|
|
57
|
+
directory,
|
|
58
|
+
} = {}) => {
|
|
59
|
+
return {
|
|
60
|
+
name: "primate:svelte",
|
|
61
|
+
register(app, next) {
|
|
62
|
+
app.register(extension, handler(base.register({app, rootname})));
|
|
63
|
+
|
|
64
|
+
return next(app);
|
|
65
|
+
},
|
|
66
|
+
async compile(app, next) {
|
|
67
|
+
await base.compile({
|
|
68
|
+
app,
|
|
69
|
+
directory: directory ?? app.config.location.components,
|
|
70
|
+
extension,
|
|
71
|
+
rootname,
|
|
72
|
+
create_root,
|
|
73
|
+
dynamicProps,
|
|
74
|
+
compile(file) {
|
|
75
|
+
const options = {generate: "ssr", hydratable: true};
|
|
76
|
+
return compiler.compile(file, options).js.code;
|
|
77
|
+
},
|
|
130
78
|
});
|
|
131
|
-
}
|
|
132
|
-
await Promise.all(components.map(async component => {
|
|
133
|
-
const name = component.path.replace(`${source}/`, "");
|
|
134
|
-
const file = await component.file.read();
|
|
135
|
-
const client = compiler.compile(file, options.client);
|
|
136
|
-
const build = app.config.build.app;
|
|
137
|
-
const {path} = component;
|
|
138
79
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
app
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
const
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
// irrelevant without bundling
|
|
160
|
-
const src = `${path}.css`.replace(`${source}`, "");
|
|
161
|
-
app.bootstrap({type: "style", code: `import ".${src}";`});
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
}));
|
|
80
|
+
return next(app);
|
|
81
|
+
},
|
|
82
|
+
async publish(app, next) {
|
|
83
|
+
// import libs
|
|
84
|
+
await app.import("svelte");
|
|
85
|
+
|
|
86
|
+
await base.publish({
|
|
87
|
+
app,
|
|
88
|
+
directory: directory ?? app.config.location.components,
|
|
89
|
+
extension,
|
|
90
|
+
rootname,
|
|
91
|
+
create_root,
|
|
92
|
+
dynamicProps,
|
|
93
|
+
normalize,
|
|
94
|
+
compile(file) {
|
|
95
|
+
const options = {generate: "dom", hydratable: true};
|
|
96
|
+
const {js, css} = compiler.compile(file, options);
|
|
97
|
+
return {js: js.code, css: css.code};
|
|
98
|
+
},
|
|
99
|
+
});
|
|
165
100
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
const src = new Path(app.config.http.static.root, filename);
|
|
171
|
-
await app.publish({src, code, type});
|
|
172
|
-
}
|
|
173
|
-
{
|
|
174
|
-
const code = `export {default as ${rootname}} from "../${filename}";\n`;
|
|
175
|
-
app.bootstrap({type: "script", code});
|
|
176
|
-
}
|
|
177
|
-
return next(app);
|
|
178
|
-
},
|
|
179
|
-
});
|
|
101
|
+
return next(app);
|
|
102
|
+
},
|
|
103
|
+
};
|
|
104
|
+
};
|