@primate/svelte 0.22.0 → 0.24.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/lib/app.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ declare module "app:svelte" {
2
+ import type { RequestPublic } from "@primate/core";
3
+ import type { Writable } from "svelte/store";
4
+ export const request: Writable<RequestPublic>;
5
+ }
package/lib/index.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ import "./app.d.ts";
2
+ export { default } from "#default";
3
+ export { default as client } from "#client/index";
@@ -0,0 +1,5 @@
1
+ import type Payload from "#client/Payload";
2
+ import type { Data } from "@primate/core/client";
3
+ type SvelteData = Data<Payload>;
4
+ export type { SvelteData as default };
5
+ //# sourceMappingURL=Data.d.ts.map
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=Data.js.map
@@ -0,0 +1,7 @@
1
+ import type { Dict } from "@rcompat/type";
2
+ type Payload = {
3
+ views: string[];
4
+ props: Dict[];
5
+ };
6
+ export type { Payload as default };
7
+ //# sourceMappingURL=Payload.d.ts.map
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=Payload.js.map
@@ -1,11 +1,5 @@
1
- import type ClientData from "@primate/core/client/Data";
2
- import type { Dict } from "@rcompat/type";
3
- type Data = ClientData<{
4
- views: string[];
5
- props: Dict[];
6
- }>;
1
+ import type Data from "#client/Data";
7
2
  export default class SvelteApp {
8
- static mount(_view: string, data: ClientData<Data>): void;
3
+ static mount(_view: string, data: Data): void;
9
4
  }
10
- export {};
11
5
  //# sourceMappingURL=app.d.ts.map
@@ -1,27 +1,26 @@
1
- import spa from "@primate/core/client/spa";
2
- import { hydrate, mount } from "svelte";
3
- import root from "svelte:root";
4
- import * as views from "svelte:views";
5
- const make_props = (data) => ({
6
- views: data.views.map(name => views[name]),
7
- props: data.props,
8
- request: {
9
- ...data.request,
10
- url: new URL(location.href),
11
- },
12
- update: () => undefined,
13
- });
1
+ import root from "#client/root";
2
+ import client from "@primate/core/client";
3
+ import RootView from "svelte:root";
14
4
  export default class SvelteApp {
15
5
  static mount(_view, data) {
16
- const _root = (data.ssr ? hydrate : mount)(root, {
17
- props: {
18
- p: make_props(data),
19
- },
6
+ const interactive = !data.ssr || data.csr;
7
+ if (!interactive)
8
+ return;
9
+ const Root = root[data.ssr ? "ssr" : "csr"](RootView, {
10
+ props: { p: root.toProps(data) },
20
11
  target: document.body,
21
12
  });
22
- data.spa && window.addEventListener("DOMContentLoaded", () => spa((_data, update) => {
23
- _root.p = { ...make_props(_data), update };
24
- }));
13
+ if (data.csr) {
14
+ const start = () => client.boot((_data, update) => {
15
+ Root.p = { ...root.toProps(_data), update };
16
+ });
17
+ if (document.readyState === "loading") {
18
+ window.addEventListener("DOMContentLoaded", start, { once: true });
19
+ }
20
+ else {
21
+ start();
22
+ }
23
+ }
25
24
  }
26
25
  }
27
26
  //# sourceMappingURL=app.js.map
@@ -1,9 +1,10 @@
1
- import { type FormInit } from "@primate/core/client";
1
+ import type { ClientMethod, FormInit } from "@primate/core/client";
2
2
  import type { Dict } from "@rcompat/type";
3
- import { type Readable } from "svelte/store";
3
+ import type { Readable } from "svelte/store";
4
4
  type SvelteFormView<Values extends Dict> = {
5
5
  id: string;
6
6
  submitting: boolean;
7
+ submitted: boolean;
7
8
  submit: (event?: Event) => Promise<void>;
8
9
  errors: readonly string[];
9
10
  field: <K extends keyof Values & string>(name: K) => {
@@ -16,6 +17,7 @@ type SvelteFormView<Values extends Dict> = {
16
17
  type Initial<Values extends Dict> = FormInit & {
17
18
  initial?: Values;
18
19
  };
20
+ declare function form<Values extends Dict>(action: ClientMethod<Values>, init?: Initial<Values>): Readable<SvelteFormView<Values>>;
19
21
  declare function form<Values extends Dict>(init: Initial<Values>): Readable<SvelteFormView<Values>>;
20
22
  declare function form(init?: FormInit): Readable<SvelteFormView<Dict>>;
21
23
  export default form;
@@ -1,14 +1,26 @@
1
1
  import client from "@primate/core/client";
2
2
  import { writable } from "svelte/store";
3
- function form(init) {
4
- const { initial, ...form_init } = init ?? {};
5
- const controller = client.createForm(form_init);
3
+ function form(action_or_init, maybe_init) {
4
+ const is_action = typeof action_or_init === "function";
5
+ const { initial, ...form_init } = (is_action ? (maybe_init ?? {}) : (action_or_init ?? {}));
6
+ const action = is_action ? action_or_init : undefined;
7
+ const contentType = is_action
8
+ ? action_or_init.contentType
9
+ : undefined;
10
+ const controller = client.createForm({
11
+ ...form_init,
12
+ ...(action !== undefined && {
13
+ action: action,
14
+ contentType,
15
+ }),
16
+ });
6
17
  const values = initial ?? {};
7
18
  const make_view = (snapshot) => {
8
19
  const form_errors = snapshot.errors.form;
9
20
  return {
10
21
  id: snapshot.id,
11
22
  submitting: snapshot.submitting,
23
+ submitted: snapshot.submitted,
12
24
  submit: controller.submit,
13
25
  errors: form_errors,
14
26
  field(name) {
@@ -28,7 +40,7 @@ function form(init) {
28
40
  submit: controller.submit,
29
41
  });
30
42
  const { subscribe, set } = writable(initial_view);
31
- controller.subscribe((snapshot) => {
43
+ controller.subscribe(snapshot => {
32
44
  set(make_view({
33
45
  ...snapshot,
34
46
  submit: controller.submit,
@@ -49,4 +49,4 @@ declare const client: {
49
49
  };
50
50
  };
51
51
  export default client;
52
- //# sourceMappingURL=client.d.ts.map
52
+ //# sourceMappingURL=index.d.ts.map
@@ -2,4 +2,4 @@ import form from "#client/form";
2
2
  import field from "#client/field";
3
3
  const client = { form, field };
4
4
  export default client;
5
- //# sourceMappingURL=client.js.map
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,33 @@
1
+ import type Data from "#client/Data";
2
+ import type { Dict } from "@rcompat/type";
3
+ import { hydrate, mount } from "svelte";
4
+ import type { Component } from "svelte";
5
+ type ViewComponent = Component<any>;
6
+ export type RootProps = {
7
+ views: ViewComponent[];
8
+ props: Dict[];
9
+ request: Data["request"] & {
10
+ url: URL;
11
+ };
12
+ update?: () => void;
13
+ };
14
+ declare function toProps(data: Data): {
15
+ views: Component<{}, {}, string>[];
16
+ props: Dict[];
17
+ request: {
18
+ url: URL;
19
+ context: Dict;
20
+ cookies: Dict<string>;
21
+ headers: Dict<string>;
22
+ path: Dict<string>;
23
+ query: Dict<string>;
24
+ };
25
+ update: () => undefined;
26
+ };
27
+ declare const root: {
28
+ toProps: typeof toProps;
29
+ ssr: typeof hydrate;
30
+ csr: typeof mount;
31
+ };
32
+ export default root;
33
+ //# sourceMappingURL=root.d.ts.map
@@ -0,0 +1,20 @@
1
+ import { hydrate, mount } from "svelte";
2
+ import * as views from "svelte:views";
3
+ function toProps(data) {
4
+ return {
5
+ views: data.views.map(name => views[name]),
6
+ props: data.props,
7
+ request: {
8
+ ...data.request,
9
+ url: new URL(data.request.url),
10
+ },
11
+ update: () => undefined,
12
+ };
13
+ }
14
+ const root = {
15
+ toProps,
16
+ ssr: hydrate,
17
+ csr: mount,
18
+ };
19
+ export default root;
20
+ //# sourceMappingURL=root.js.map
@@ -3,13 +3,13 @@ export default (depth, i18n_active) => {
3
3
  const body = Array.from({ length: n }, (_, i) => i - 1)
4
4
  .reduceRight((child, _, i) => `
5
5
  {#if p.views[${i + 1}]}
6
- <svelte:component this={p.views[${i}]} request={p.request} {...p.props[${i}]}>
6
+ <svelte:component this={p.views[${i}]} {...p.props[${i}]}>
7
7
  ${child}
8
8
  </svelte:component>
9
9
  {:else}
10
- <svelte:component this={p.views[${i}]} request={p.request} {...p.props[${i}]}/>
10
+ <svelte:component this={p.views[${i}]} {...p.props[${i}]}/>
11
11
  {/if}
12
- `, `<svelte:component this={p.views[${n}]} request={p.request} {...p.props[${n}]}/>`);
12
+ `, `<svelte:component this={p.views[${n}]} {...p.props[${n}]}/>`);
13
13
  const i18nImports = i18n_active
14
14
  ? `
15
15
  import t from "#i18n";
@@ -30,15 +30,21 @@ export default (depth, i18n_active) => {
30
30
  <script>
31
31
  import { afterUpdate, setContext } from "svelte";
32
32
  import context_name from "@primate/svelte/context-name";
33
+ import { request } from "@primate/svelte/app";
33
34
  ${i18nImports}
34
35
 
35
36
  export let p;
36
37
 
37
38
  setContext(context_name, p.request.context);
38
39
 
40
+ const { context, path, ...public_request } = p.request;
41
+ request.set(public_request);
42
+
39
43
  ${i18nInit}
40
44
 
41
45
  afterUpdate(() => {
46
+ const { context, path, ...public_request } = p.request;
47
+ request.set(public_request);
42
48
  p.update?.();
43
49
  });
44
50
  </script>
@@ -0,0 +1,7 @@
1
+ declare const _default: (input?: {
2
+ extensions?: string[] | undefined;
3
+ csr?: boolean | undefined;
4
+ ssr?: boolean | undefined;
5
+ } | undefined) => import("@primate/core/frontend").Module;
6
+ export default _default;
7
+ //# sourceMappingURL=default.d.ts.map
@@ -1,5 +1,6 @@
1
1
  import create_root from "#create-root";
2
- import Runtime from "#Runtime";
2
+ import init from "#init";
3
+ import frontend from "@primate/core/frontend";
3
4
  import esbuild from "esbuild";
4
5
  import { compile, compileModule } from "svelte/compiler";
5
6
  const strip = (code) => {
@@ -8,29 +9,33 @@ const strip = (code) => {
8
9
  format: "esm",
9
10
  }).code;
10
11
  };
11
- export default class Default extends Runtime {
12
- root = {
12
+ const options = {
13
+ experimental: { async: true },
14
+ };
15
+ export default frontend({
16
+ ...init,
17
+ root: {
13
18
  create: create_root,
14
- };
15
- css = {
19
+ },
20
+ css: {
16
21
  filter: /\.sveltecss$/,
17
- };
18
- compile = {
22
+ },
23
+ compile: {
19
24
  client: (text, file) => {
20
25
  const accessors = true;
21
26
  const { css, js } = file.path.endsWith(".js") || file.path.endsWith(".ts")
22
27
  // runes in .svelte.[j|t]s
23
- ? compileModule(strip(text), { generate: "client" })
24
- : compile(text, { accessors, generate: "client" });
28
+ ? compileModule(strip(text), { generate: "client", ...options })
29
+ : compile(text, { accessors, generate: "client", ...options });
25
30
  return { css: css?.code ?? "", js: js.code };
26
31
  },
27
32
  server: (text, file) => {
28
33
  const { js } = file?.path.endsWith(".js") || file?.path.endsWith(".ts")
29
34
  // runes in .svelte.[j|t]s
30
- ? compileModule(strip(text), { generate: "server" })
31
- : compile(text, { generate: "server" });
35
+ ? compileModule(strip(text), { generate: "server", ...options })
36
+ : compile(text, { generate: "server", ...options });
32
37
  return js.code;
33
38
  },
34
- };
35
- }
36
- //# sourceMappingURL=Default.js.map
39
+ },
40
+ });
41
+ //# sourceMappingURL=default.js.map
@@ -0,0 +1,5 @@
1
+ import type { Init } from "@primate/core/frontend";
2
+ import type { Component } from "svelte";
3
+ declare const module: Init<Component>;
4
+ export default module;
5
+ //# sourceMappingURL=init.d.ts.map
@@ -0,0 +1,14 @@
1
+ import { render } from "svelte/server";
2
+ const module = {
3
+ name: "svelte",
4
+ extensions: [".svelte.js", ".svelte.ts", ".svelte"],
5
+ layouts: true,
6
+ client: true,
7
+ async render(view, props) {
8
+ const { body, head } = await render(view, { props: { p: { ...props } } });
9
+ return { body, head };
10
+ },
11
+ conditions: ["svelte"],
12
+ };
13
+ export default module;
14
+ //# sourceMappingURL=init.js.map
@@ -0,0 +1,4 @@
1
+ import type { RequestPublic } from "@primate/core";
2
+ declare const _default: import("svelte/store").Writable<RequestPublic>;
3
+ export default _default;
4
+ //# sourceMappingURL=store.d.ts.map
@@ -0,0 +1,3 @@
1
+ import { writable } from "svelte/store";
2
+ export default writable();
3
+ //# sourceMappingURL=store.js.map
@@ -0,0 +1,7 @@
1
+ declare const _default: (input?: {
2
+ extensions?: string[] | undefined;
3
+ csr?: boolean | undefined;
4
+ ssr?: boolean | undefined;
5
+ } | undefined) => import("@primate/core/frontend").Module;
6
+ export default _default;
7
+ //# sourceMappingURL=runtime.d.ts.map
@@ -0,0 +1,4 @@
1
+ import init from "#init";
2
+ import frontend from "@primate/core/frontend";
3
+ export default frontend(init);
4
+ //# sourceMappingURL=runtime.js.map
@@ -0,0 +1,2 @@
1
+ export { default as request } from "#request/store";
2
+ //# sourceMappingURL=app.d.ts.map
@@ -0,0 +1,2 @@
1
+ export { default as request } from "#request/store";
2
+ //# sourceMappingURL=app.js.map
@@ -1,2 +1,3 @@
1
1
  export { default } from "#client/app";
2
+ export { default as client } from "#client/index";
2
3
  //# sourceMappingURL=browser.d.ts.map
@@ -1,2 +1,3 @@
1
1
  export { default } from "#client/app";
2
+ export { default as client } from "#client/index";
2
3
  //# sourceMappingURL=browser.js.map
@@ -1,4 +1,2 @@
1
- import Default from "#Default";
2
- declare const _default: (options?: typeof Default.input) => Default;
3
- export default _default;
1
+ export { default } from "#default";
4
2
  //# sourceMappingURL=default.d.ts.map
@@ -1,3 +1,2 @@
1
- import Default from "#Default";
2
- export default (options) => new Default(options);
1
+ export { default } from "#default";
3
2
  //# sourceMappingURL=default.js.map
@@ -1,4 +1,3 @@
1
- import Runtime from "#Runtime";
2
- declare const _default: (options?: typeof Runtime.input) => Runtime;
3
- export default _default;
1
+ export { default as client } from "#client/index";
2
+ export { default } from "#runtime";
4
3
  //# sourceMappingURL=runtime.d.ts.map
@@ -1,3 +1,3 @@
1
- import Runtime from "#Runtime";
2
- export default (options) => new Runtime(options);
1
+ export { default as client } from "#client/index";
2
+ export { default } from "#runtime";
3
3
  //# sourceMappingURL=runtime.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@primate/svelte",
3
- "version": "0.22.0",
3
+ "version": "0.24.0",
4
4
  "description": "Svelte for Primate",
5
5
  "homepage": "https://primate.run/docs/frontend/svelte",
6
6
  "bugs": "https://github.com/primate-run/primate/issues",
@@ -16,37 +16,41 @@
16
16
  "/lib/**/*.d.ts",
17
17
  "!/**/*.spec.*"
18
18
  ],
19
+ "types": "lib/index.d.ts",
19
20
  "dependencies": {
20
- "@rcompat/fs": "^0.25.2",
21
- "esbuild": "^0.27.3",
22
- "@primate/core": "^0.5.0"
21
+ "@rcompat/fs": "^0.33.2",
22
+ "esbuild": "^0.28.0",
23
+ "@primate/core": "^0.7.0"
23
24
  },
24
25
  "devDependencies": {
25
- "@rcompat/type": "^0.9.0"
26
+ "@rcompat/type": "^0.17.0"
26
27
  },
27
28
  "peerDependencies": {
28
29
  "svelte": "5",
29
- "primate": "^0.36.0"
30
+ "primate": "^0.38.0"
30
31
  },
31
32
  "imports": {
32
33
  "#*": {
33
- "apekit": "./src/private/*.ts",
34
+ "@primate/source": "./src/private/*.ts",
34
35
  "default": "./lib/private/*.js"
35
36
  }
36
37
  },
37
38
  "exports": {
38
39
  ".": {
40
+ "types": "./lib/index.d.ts",
41
+ "@primate/runtime": "./src/public/runtime.ts",
42
+ "@primate/source": "./src/public/default.ts",
39
43
  "browser": "./lib/public/browser.js",
40
44
  "runtime": "./lib/public/runtime.js",
41
45
  "default": "./lib/public/default.js"
42
46
  },
43
47
  "./*": {
44
- "apekit": "./src/public/*.ts",
48
+ "@primate/source": "./src/public/*.ts",
45
49
  "default": "./lib/public/*.js"
46
50
  }
47
51
  },
48
52
  "scripts": {
49
- "build": "npm run clean && tsc",
53
+ "build": "npm run clean && tsc && cp src/types/app.d.ts lib && cp src/types/index.d.ts lib",
50
54
  "clean": "rm -rf ./lib",
51
55
  "lint": "eslint ."
52
56
  }
@@ -1,18 +0,0 @@
1
- import Runtime from "#Runtime";
2
- import type { FileRef } from "@rcompat/fs";
3
- export default class Default extends Runtime {
4
- root: {
5
- create: (depth: number, i18n_active: boolean) => string;
6
- };
7
- css: {
8
- filter: RegExp;
9
- };
10
- compile: {
11
- client: (text: string, file: FileRef) => {
12
- css: string;
13
- js: string;
14
- };
15
- server: (text: string, file?: FileRef) => string;
16
- };
17
- }
18
- //# sourceMappingURL=Default.d.ts.map
@@ -1,12 +0,0 @@
1
- import FrontendModule from "@primate/core/frontend/Module";
2
- import type Render from "@primate/core/frontend/Render";
3
- import type { Component } from "svelte";
4
- export default class Runtime extends FrontendModule<Component> {
5
- name: string;
6
- defaultExtensions: string[];
7
- layouts: boolean;
8
- client: boolean;
9
- render: Render<Component>;
10
- conditions: string[];
11
- }
12
- //# sourceMappingURL=Runtime.d.ts.map
@@ -1,14 +0,0 @@
1
- import FrontendModule from "@primate/core/frontend/Module";
2
- import { render } from "svelte/server";
3
- export default class Runtime extends FrontendModule {
4
- name = "svelte";
5
- defaultExtensions = [".svelte.js", ".svelte.ts", ".svelte"];
6
- layouts = true;
7
- client = true;
8
- render = (view, props) => {
9
- const { head, html } = render(view, { props: { p: { ...props } } });
10
- return { body: html, head };
11
- };
12
- conditions = ["svelte"];
13
- }
14
- //# sourceMappingURL=Runtime.js.map