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