@primate/svelte 0.2.0 → 0.3.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/package.json +1 -1
- package/src/module.js +45 -5
package/package.json
CHANGED
package/src/module.js
CHANGED
|
@@ -1,19 +1,37 @@
|
|
|
1
1
|
import * as compiler from "svelte/compiler";
|
|
2
|
-
import {File} from "runtime-compat/fs";
|
|
2
|
+
import {File, Path} from "runtime-compat/fs";
|
|
3
3
|
|
|
4
4
|
const endings = {
|
|
5
5
|
svelte: ".svelte",
|
|
6
6
|
js: ".svelte.js",
|
|
7
7
|
};
|
|
8
8
|
|
|
9
|
+
const type = "module";
|
|
10
|
+
|
|
9
11
|
const handler = path => (component, props = {}, {status = 200} = {}) =>
|
|
10
12
|
async (env, headers) => {
|
|
11
13
|
const {js} = endings;
|
|
12
|
-
const {html} = (await import(`${path.join(`${component}${js}`)}`))
|
|
14
|
+
const {html: body} = (await import(`${path.join(`${component}${js}`)}`))
|
|
13
15
|
.default.render(props);
|
|
14
16
|
|
|
17
|
+
const stringifiedProps = JSON.stringify(props);
|
|
18
|
+
// create an entry point
|
|
19
|
+
const entry = `import ${component} from "./${component}.svelte.js";
|
|
20
|
+
const props = JSON.parse('${stringifiedProps}');
|
|
21
|
+
new ${component}({
|
|
22
|
+
target: document.body,
|
|
23
|
+
hydrate: true,
|
|
24
|
+
props,
|
|
25
|
+
});
|
|
26
|
+
`;
|
|
27
|
+
const inline = true;
|
|
28
|
+
const integrity = await env.publish({code: entry, type, inline});
|
|
29
|
+
// hacky, CSP should be a map that gets flattened in the end
|
|
30
|
+
headers["Content-Security-Policy"] = headers["Content-Security-Policy"]
|
|
31
|
+
.replace("script-src 'self' ", `script-src 'self' '${integrity}' `);
|
|
32
|
+
|
|
15
33
|
// -> spread into new Response()
|
|
16
|
-
return [await env.render(
|
|
34
|
+
return [await env.render({body}), {
|
|
17
35
|
status,
|
|
18
36
|
headers: {...headers, "Content-Type": "text/html"},
|
|
19
37
|
}];
|
|
@@ -33,13 +51,35 @@ export default directory => async () => {
|
|
|
33
51
|
await Promise.all(components.map(async component => {
|
|
34
52
|
const file = await component.file.read();
|
|
35
53
|
const server = compiler.compile(file, options);
|
|
36
|
-
/* const client = compiler.compile(file, {
|
|
37
|
-
generate: "dom", hydratable: true, css: false}).js.code;*/
|
|
38
54
|
const compiled = server.js.code.replaceAll(svelte, js);
|
|
39
55
|
await File.write(`${component.path}.js`, compiled);
|
|
40
56
|
}));
|
|
41
57
|
|
|
42
58
|
return next(env);
|
|
43
59
|
},
|
|
60
|
+
publish: async (env, next) => {
|
|
61
|
+
const path = directory ?? env.paths.components;
|
|
62
|
+
const options = {
|
|
63
|
+
generate: "dom",
|
|
64
|
+
hydratable: true,
|
|
65
|
+
css: false,
|
|
66
|
+
sveltePath: "/svelte",
|
|
67
|
+
};
|
|
68
|
+
const components = await path.list(filename => filename.endsWith(svelte));
|
|
69
|
+
const scriptPath = ["node_modules", "svelte", "internal", "index.mjs"];
|
|
70
|
+
// publish svelte itself
|
|
71
|
+
const code = await Path.resolve().join(...scriptPath).file.read();
|
|
72
|
+
await env.publish({src: "svelte/internal.js", code, type});
|
|
73
|
+
await Promise.all(components.map(async component => {
|
|
74
|
+
const file = await component.file.read();
|
|
75
|
+
const client = compiler.compile(file, options);
|
|
76
|
+
const compiled = client.js.code
|
|
77
|
+
.replaceAll(svelte, js)
|
|
78
|
+
.replace("/svelte/internal", "/svelte/internal.js");
|
|
79
|
+
await env.publish({src: `${component.name}.js`, code: compiled, type});
|
|
80
|
+
await File.write(`${component.path}.client.js`, compiled);
|
|
81
|
+
}));
|
|
82
|
+
return next(env);
|
|
83
|
+
},
|
|
44
84
|
};
|
|
45
85
|
};
|