@primate/svelte 0.15.3 → 0.18.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/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) Terrablue <terrablue@proton.me> and contributors.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16
+ THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
package/package.json CHANGED
@@ -1,13 +1,12 @@
1
1
  {
2
2
  "name": "@primate/svelte",
3
- "version": "0.15.3",
4
- "description": "Primate Svelte module",
3
+ "version": "0.18.0",
4
+ "description": "Primate Svelte frontend",
5
5
  "homepage": "https://primatejs.com/modules/svelte",
6
6
  "bugs": "https://github.com/primatejs/primate/issues",
7
7
  "license": "MIT",
8
8
  "files": [
9
9
  "src/**/*.js",
10
- "src/errors.json",
11
10
  "!src/**/*.spec.js"
12
11
  ],
13
12
  "repository": {
@@ -16,12 +15,51 @@
16
15
  "directory": "packages/svelte"
17
16
  },
18
17
  "dependencies": {
19
- "runtime-compat": "^0.25.1",
20
- "svelte": "^4.1.2"
18
+ "@rcompat/fs": "^0.4.0",
19
+ "@primate/frontend": "^0.16.0"
21
20
  },
22
21
  "peerDependencies": {
23
- "primate": "0.21"
22
+ "svelte": "4",
23
+ "@primate/i18n": "^0.8.0",
24
+ "primate": "^0.32.0"
25
+ },
26
+ "peerDependenciesMeta": {
27
+ "@primate/i18n": {
28
+ "optional": true
29
+ }
24
30
  },
25
31
  "type": "module",
26
- "exports": "./src/exports.js"
27
- }
32
+ "imports": {
33
+ "#*": {
34
+ "@primate/lt": "./src/private/*.js",
35
+ "default": "./src/private/*.js"
36
+ },
37
+ "#build": {
38
+ "@primate/lt": "./src/private/build/index.js",
39
+ "default": "./src/private/build/index.js"
40
+ },
41
+ "#serve": {
42
+ "@primate/lt": "./src/private/serve/index.js",
43
+ "default": "./src/private/serve/index.js"
44
+ },
45
+ "#client": {
46
+ "@primate/lt": "./src/private/client/index.js",
47
+ "default": "./src/private/client/index.js"
48
+ },
49
+ "#client/*": {
50
+ "@primate/lt": "./src/private/client/*.js",
51
+ "default": "./src/private/client/*.js"
52
+ },
53
+ "#i18n/*": {
54
+ "@primate/lt": "./src/private/i18n/*.js",
55
+ "default": "./src/private/i18n/*.js"
56
+ }
57
+ },
58
+ "exports": {
59
+ ".": {
60
+ "runtime": "./src/public/runtime.js",
61
+ "default": "./src/public/default.js"
62
+ },
63
+ "./*": "./src/public/*.js"
64
+ }
65
+ }
@@ -0,0 +1,8 @@
1
+ import * as compiler from "svelte/compiler";
2
+
3
+ export default text => {
4
+ const options = { generate: "dom", hydratable: true };
5
+ const { js, css } = compiler.compile(text, options);
6
+ return { js: js.code, css: css.code };
7
+ };
8
+
@@ -0,0 +1,14 @@
1
+ import create_root from "#client/create-root";
2
+ import expose from "#client/expose";
3
+ import name from "#name";
4
+ import build from "@primate/frontend/core/build";
5
+ import client from "./client.js";
6
+ import publish from "./publish.js";
7
+ import server from "./server.js";
8
+
9
+ export default extension => build({
10
+ create_root,
11
+ extension,
12
+ name,
13
+ actions: { compile: { client, server }, expose, publish },
14
+ });
@@ -0,0 +1,40 @@
1
+ import file from "@rcompat/fs/file";
2
+ import webpath from "@rcompat/fs/webpath";
3
+ import client from "./client.js";
4
+
5
+ const css_filter = /\.sveltecss$/u;
6
+ const root_filter = /^root:svelte$/u;
7
+
8
+ export default (app, extension) => ({
9
+ name: "svelte",
10
+ setup(build) {
11
+ build.onResolve({ filter: css_filter }, ({ path }) => {
12
+ return { path, namespace: "sveltecss" };
13
+ });
14
+ build.onResolve({ filter: root_filter }, ({ path }) => {
15
+ return { path, namespace: "svelteroot" };
16
+ });
17
+ build.onLoad({ filter: css_filter }, ({ path }) => {
18
+ const contents = app.build.load(webpath(path));
19
+ return contents ? { contents, loader: "css", resolveDir: app.root.path } : null;
20
+ });
21
+ build.onLoad({ filter: root_filter }, ({ path }) => {
22
+ const contents = app.build.load(path);
23
+ return contents ? { contents, loader: "js", resolveDir: app.root.path } : null;
24
+ });
25
+ build.onLoad({ filter: new RegExp(`${extension}$`, "u") }, async args => {
26
+ // Load the file from the file system
27
+ const source = await file(args.path).text();
28
+
29
+ // Convert Svelte syntax to JavaScript
30
+ const { js, css } = client(source);
31
+ let contents = js;
32
+ if (css !== null && css !== "") {
33
+ const path = File.webpath(`${args.path}css`);
34
+ app.build.save(path, css);
35
+ contents += `\nimport "${path}";`;
36
+ }
37
+ return { contents };
38
+ });
39
+ },
40
+ });
@@ -0,0 +1,6 @@
1
+ import * as compiler from "svelte/compiler";
2
+
3
+ export default text => {
4
+ const options = { generate: "ssr", hydratable: true };
5
+ return compiler.compile(text, options).js.code;
6
+ };
@@ -0,0 +1,31 @@
1
+ export default length => {
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}]} {request} {...data[${i}]}>
7
+ ${child}
8
+ </svelte:component>
9
+ {:else}
10
+ <svelte:component this={components[${i}]} {request} {...data[${i}]}/>
11
+ {/if}
12
+ `, `<svelte:component this={components[${n}]} {request} {...data[${n}]}/>`);
13
+
14
+ return `
15
+ <script>
16
+ import { afterUpdate, setContext } from "svelte";
17
+ import context_name from "@primate/svelte/context-name";
18
+
19
+ export let components;
20
+ export let data;
21
+ export let request;
22
+ export let context;
23
+ export let update = () => undefined;
24
+
25
+ setContext(context_name, context);
26
+
27
+ afterUpdate(update);
28
+ </script>
29
+ ${body}
30
+ `;
31
+ };
@@ -0,0 +1 @@
1
+ export default "export { setContext } from \"svelte\";";
@@ -0,0 +1,18 @@
1
+ import spa from "./spa.js";
2
+
3
+ export default ({ names, data, context, request }, options) => `
4
+ import * as components from "app";
5
+ let root = new components.root_svelte({
6
+ target: document.body,
7
+ hydrate: true,
8
+ props: {
9
+ components: [${names.map(name => `components.${name}`).join(", ")}],
10
+ data: ${JSON.stringify(data)},
11
+ context: ${JSON.stringify(context)},
12
+ request: {
13
+ ...${JSON.stringify(request)},
14
+ url: new URL(location.href),
15
+ },
16
+ },
17
+ });
18
+ ${options.spa ? spa : ""}`;
@@ -0,0 +1,19 @@
1
+ export default `
2
+ const { spa } = components;
3
+ window.addEventListener("DOMContentLoaded", _ => spa((props, update) => {
4
+ root.$destroy();
5
+ root = new components.root_svelte({
6
+ target: document.body,
7
+ hydrate: true,
8
+ props: {
9
+ components: props.names.map(name => components[name]),
10
+ data: props.data,
11
+ context: props.context,
12
+ request: {
13
+ ...props.request,
14
+ url: new URL(location.href),
15
+ },
16
+ update,
17
+ },
18
+ });
19
+ }));`;
@@ -0,0 +1 @@
1
+ export default "__PRMT__";
@@ -0,0 +1 @@
1
+ export default ".svelte";
@@ -0,0 +1,10 @@
1
+ import context_name from "#context-name";
2
+ import locale_store from "#i18n/locale";
3
+ import resolve from "@primate/i18n/core/resolve";
4
+ import { getContext } from "svelte";
5
+ import { derived } from "svelte/store";
6
+
7
+ export default derived(locale_store, locale => (key, placeholders) => {
8
+ const { locales } = getContext(context_name).i18n;
9
+ return resolve(locales[locale], key, placeholders);
10
+ });
@@ -0,0 +1,19 @@
1
+ import context_name from "#context-name";
2
+ import save from "@primate/i18n/core/save";
3
+ import { getContext } from "svelte";
4
+ import { writable } from "svelte/store";
5
+
6
+ const store = writable("", set => {
7
+ const { locale } = getContext(context_name).i18n;
8
+ set(locale);
9
+
10
+ return () => undefined;
11
+ });
12
+
13
+ export default {
14
+ subscribe: store.subscribe,
15
+ set: locale => {
16
+ store.set(locale);
17
+ save(locale);
18
+ },
19
+ };
@@ -0,0 +1 @@
1
+ export default "svelte";
@@ -0,0 +1 @@
1
+ export default "@primate/svelte";
@@ -0,0 +1,16 @@
1
+ import client from "#client";
2
+ import name from "#name";
3
+ import handler from "@primate/frontend/core/handler";
4
+ import render from "./render.js";
5
+
6
+ export default (extension, spa) => (app, next) => {
7
+ app.register(extension, handler({
8
+ app,
9
+ render,
10
+ client,
11
+ name,
12
+ spa,
13
+ }));
14
+
15
+ return next(app);
16
+ };
@@ -0,0 +1,4 @@
1
+ export default (component, ...args) => {
2
+ const { html, head } = component.render(...args);
3
+ return { body: html, head };
4
+ };
@@ -0,0 +1 @@
1
+ export { default } from "#context-name";
@@ -0,0 +1,14 @@
1
+ import build from "#build";
2
+ import default_extension from "#extension";
3
+ import pkgname from "#pkgname";
4
+ import serve from "#serve";
5
+
6
+ export default ({
7
+ extension = default_extension,
8
+ // activate fetch-based browsing
9
+ spa = true,
10
+ } = {}) => ({
11
+ name: pkgname,
12
+ build: build(extension),
13
+ serve: serve(extension, spa),
14
+ });
@@ -0,0 +1 @@
1
+ export { default } from "#i18n/index";
@@ -0,0 +1 @@
1
+ export { default } from "#i18n/locale";
@@ -0,0 +1,12 @@
1
+ import default_extension from "#extension";
2
+ import pkgname from "#pkgname";
3
+ import serve from "#serve";
4
+
5
+ export default ({
6
+ extension = default_extension,
7
+ // activate fetch-based browsing
8
+ spa = true,
9
+ } = {}) => ({
10
+ name: pkgname,
11
+ serve: serve(extension, spa),
12
+ });
package/README.md DELETED
@@ -1,6 +0,0 @@
1
- # Svelte handler
2
-
3
- This handler module supports SSR and hydration and serves Svelte components
4
- with the .svelte extension.
5
-
6
- ## [Documentation](https://primatejs.com/modules/svelte)
@@ -1,14 +0,0 @@
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
- let root = new components.${rootname}({
7
- target: document.body,
8
- hydrate: true,
9
- props: {
10
- components: [${names.map(name => `components.${name}`).join(", ")}],
11
- data: JSON.parse(${JSON.stringify(JSON.stringify(data))}),
12
- },
13
- });
14
- ${options.liveview ? liveview : ""}`;
@@ -1,25 +0,0 @@
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
- };
@@ -1,3 +0,0 @@
1
- export {default as client} from "./client.js";
2
- export {default as create_root} from "./create-root.js";
3
- export {default as rootname} from "./rootname.js";
@@ -1,16 +0,0 @@
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: props.names.map(name => components[name]),
12
- data: props.data,
13
- update,
14
- },
15
- });
16
- }));`;
@@ -1 +0,0 @@
1
- export default "root";
package/src/errors.js DELETED
@@ -1,6 +0,0 @@
1
- import {Path} from "runtime-compat/fs";
2
- import {Logger} from "primate";
3
-
4
- const json = await new Path(import.meta.url).up(1).join("errors.json").json();
5
-
6
- export default Logger.err(json.errors, json.module);
package/src/errors.json DELETED
@@ -1,10 +0,0 @@
1
- {
2
- "module": "primate/svelte",
3
- "errors": {
4
- "MissingComponent": {
5
- "message": "missing component {0}",
6
- "fix": "create {1} or remove route function",
7
- "level": "Error"
8
- }
9
- }
10
- }
package/src/exports.js DELETED
@@ -1 +0,0 @@
1
- export {default} from "./module.js";
package/src/module.js DELETED
@@ -1,174 +0,0 @@
1
- import crypto from "runtime-compat/crypto";
2
- import {Path} from "runtime-compat/fs";
3
- import {Response, Status, MediaType} from "runtime-compat/http";
4
- import * as compiler from "svelte/compiler";
5
- import {tryreturn} from "runtime-compat/async";
6
- import errors from "./errors.js";
7
- import {client, create_root, rootname} from "./client/exports.js";
8
-
9
- const endings = {
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
-
29
- const type = "module";
30
-
31
- const load = async path =>
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} = {}) =>
39
- async (app, {layouts = [], as_layout} = {}, request) => {
40
- const options = {
41
- liveview: app.liveview !== undefined,
42
- };
43
- const {headers} = request;
44
- const {paths} = app.build;
45
- const make = make_component(paths.server.join(app.config.build.app));
46
- if (as_layout) {
47
- return make(name, props);
48
- }
49
- const components = (await Promise.all(layouts.map(layout =>
50
- layout(app, {as_layout: true}, request)
51
- )))
52
- /* set the actual page as the last component */
53
- .concat(await make(name, props));
54
-
55
- const data = components.map(component => component.props);
56
- const names = await Promise.all(components.map(component =>
57
- normalize(component.name)));
58
- if (options.liveview && headers.get(app.liveview.header) !== undefined) {
59
- return new Response(JSON.stringify({names, data}), {
60
- status,
61
- headers: {...await app.headers(),
62
- "Content-Type": MediaType.APPLICATION_JSON},
63
- });
64
- }
65
-
66
- const root = paths.server.join(filename);
67
- const imported = (await import(root)).default;
68
- const {html: body} = imported.render({
69
- components: components.map(({component}) => component),
70
- data,
71
- });
72
-
73
- const code = client({names, data}, options);
74
-
75
- await app.publish({code, type, inline: true});
76
- // needs to be called before app.render
77
- const headers$ = await app.headers();
78
-
79
- return new Response(await app.render({body, page}), {
80
- status,
81
- headers: {...headers$, "Content-Type": MediaType.TEXT_HTML},
82
- });
83
- };
84
-
85
- const options = {
86
- server: {generate: "ssr", hydratable: true},
87
- client: {generate: "dom", hydratable: true},
88
- };
89
-
90
- const {svelte, js} = endings;
91
- export default ({
92
- dynamicProps = "data",
93
- } = {}) => ({
94
- name: "@primate/svelte",
95
- register(app, next) {
96
- app.register("svelte", handler);
97
- return next(app);
98
- },
99
- async compile(app, next) {
100
- const source = app.build.paths.components;
101
- // copy components to build/components
102
- await app.copy(app.paths.components, source, /^.*.(?:js|svelte)$/u);
103
- const components = await source.collect(/^.*.svelte$/u);
104
- const target = app.build.paths.server.join(app.config.build.app);
105
- await target.file.create();
106
-
107
- await Promise.all(components.map(async component => {
108
- const file = await component.file.read();
109
- const server = compiler.compile(file, options.server);
110
- const to = target.join(`${component.path}.js`.replace(source, ""));
111
- await to.directory.file.create();
112
- await to.file.write(server.js.code.replaceAll(svelte, js));
113
- }));
114
-
115
- const root = create_root(app.layoutDepth, dynamicProps);
116
- const server = compiler.compile(root, options.server).js.code;
117
- const to = app.build.paths.server.join(filename);
118
- await to.file.write(server);
119
-
120
- return next(app);
121
- },
122
- async publish(app, next) {
123
- const source = app.build.paths.components;
124
- const components = await source.collect(/^.*.svelte$/u);
125
- await app.import("svelte");
126
-
127
- await Promise.all(components.map(async component => {
128
- const name = component.path.replace(`${source}/`, "");
129
- const file = await component.file.read();
130
- const client = compiler.compile(file, options.client);
131
- const build = app.config.build.app;
132
- const {path} = component;
133
-
134
- {
135
- {
136
- const code = client.js.code.replaceAll(svelte, js);
137
- const src = `${path}.js`.replace(`${source}`, _ => build);
138
- await app.publish({src, code, type});
139
- }
140
- const imported = await normalize(name);
141
- app.bootstrap({
142
- type: "script",
143
- code: `export {default as ${imported}} from "./${name}.js";\n`,
144
- });
145
- }
146
- if (client.css.code !== null) {
147
- {
148
- const {code} = client.css;
149
- const src = `${path}.css`.replace(`${source}`, _ => build);
150
- await app.publish({src, code, type: "style"});
151
- }
152
- {
153
- // sans app.config.build.app removal, for a bundler to resolve,
154
- // irrelevant without bundling
155
- const src = `${path}.css`.replace(`${source}`, "");
156
- app.bootstrap({type: "style", code: `import ".${src}";`});
157
- }
158
- }
159
- }));
160
-
161
- {
162
- const root = create_root(app.layoutDepth, dynamicProps);
163
- const client = compiler.compile(root, options.client);
164
- const code = client.js.code.replaceAll(svelte, js);
165
- const src = new Path(app.config.http.static.root, filename);
166
- await app.publish({src, code, type});
167
- }
168
- {
169
- const code = `export {default as ${rootname}} from "../${filename}";\n`;
170
- app.bootstrap({type: "script", code});
171
- }
172
- return next(app);
173
- },
174
- });