@primate/solid 0.2.0 → 0.4.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 +19 -0
- package/package.json +55 -10
- package/src/private/build/client.js +7 -0
- package/src/private/build/index.js +14 -0
- package/src/private/build/publish.js +26 -0
- package/src/private/build/server.js +8 -0
- package/src/private/client/create-root.js +33 -0
- package/src/private/client/expose.js +7 -0
- package/src/private/client/index.js +25 -0
- package/src/private/client/spa.js +14 -0
- package/src/private/context/app.js +3 -0
- package/src/private/context/head.js +3 -0
- package/src/private/extension.js +1 -0
- package/src/private/i18n/index.js +10 -0
- package/src/private/i18n/locale.js +37 -0
- package/src/private/name.js +1 -0
- package/src/private/pkgname.js +1 -0
- package/src/private/serve/index.js +16 -0
- package/src/private/serve/render.js +17 -0
- package/src/public/context/app.js +1 -0
- package/src/public/context/head.js +1 -0
- package/src/public/default.js +14 -0
- package/src/public/head.js +92 -0
- package/src/public/i18n.js +1 -0
- package/src/public/locale.js +1 -0
- package/src/public/runtime.js +12 -0
- package/README.md +0 -5
- package/src/client/client.js +0 -20
- package/src/client/create-root.js +0 -16
- package/src/client/exports.js +0 -4
- package/src/client/hydrate.js +0 -4
- package/src/client/liveview.js +0 -11
- package/src/client/rootname.js +0 -1
- package/src/errors.js +0 -6
- package/src/errors.json +0 -10
- package/src/exports.js +0 -1
- package/src/module.js +0 -126
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/solid",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Primate Solid
|
|
3
|
+
"version": "0.4.0",
|
|
4
|
+
"description": "Primate Solid frontend",
|
|
5
5
|
"homepage": "https://primatejs.com/modules/solid",
|
|
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,14 +15,60 @@
|
|
|
16
15
|
"directory": "packages/solid"
|
|
17
16
|
},
|
|
18
17
|
"dependencies": {
|
|
19
|
-
"@
|
|
20
|
-
"
|
|
21
|
-
"
|
|
22
|
-
"
|
|
18
|
+
"@rcompat/build": "^0.4.0",
|
|
19
|
+
"@rcompat/fs": "^0.4.0",
|
|
20
|
+
"@rcompat/platform": "^0.3.0",
|
|
21
|
+
"@primate/frontend": "^0.16.0"
|
|
23
22
|
},
|
|
24
23
|
"peerDependencies": {
|
|
25
|
-
"
|
|
24
|
+
"@babel/core": "7",
|
|
25
|
+
"babel-preset-solid": "1",
|
|
26
|
+
"solid-js": "1",
|
|
27
|
+
"@primate/i18n": "^0.8.0",
|
|
28
|
+
"primate": "^0.32.0"
|
|
29
|
+
},
|
|
30
|
+
"peerDependenciesMeta": {
|
|
31
|
+
"@primate/i18n": {
|
|
32
|
+
"optional": true
|
|
33
|
+
}
|
|
26
34
|
},
|
|
27
35
|
"type": "module",
|
|
28
|
-
"
|
|
29
|
-
|
|
36
|
+
"imports": {
|
|
37
|
+
"#*": {
|
|
38
|
+
"@primate/lt": "./src/private/*.js",
|
|
39
|
+
"default": "./src/private/*.js"
|
|
40
|
+
},
|
|
41
|
+
"#build": {
|
|
42
|
+
"@primate/lt": "./src/private/build/index.js",
|
|
43
|
+
"default": "./src/private/build/index.js"
|
|
44
|
+
},
|
|
45
|
+
"#serve": {
|
|
46
|
+
"@primate/lt": "./src/private/serve/index.js",
|
|
47
|
+
"default": "./src/private/serve/index.js"
|
|
48
|
+
},
|
|
49
|
+
"#context/*": {
|
|
50
|
+
"@primate/lt": "./src/private/context/*.js",
|
|
51
|
+
"default": "./src/private/context/*.js"
|
|
52
|
+
},
|
|
53
|
+
"#client": {
|
|
54
|
+
"@primate/lt": "./src/private/client/index.js",
|
|
55
|
+
"default": "./src/private/client/index.js"
|
|
56
|
+
},
|
|
57
|
+
"#client/*": {
|
|
58
|
+
"@primate/lt": "./src/private/client/*.js",
|
|
59
|
+
"default": "./src/private/client/*.js"
|
|
60
|
+
},
|
|
61
|
+
"#i18n/*": {
|
|
62
|
+
"@primate/lt": "./src/private/i18n/*.js",
|
|
63
|
+
"default": "./src/private/i18n/*.js"
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
"exports": {
|
|
67
|
+
".": {
|
|
68
|
+
"runtime": "./src/public/runtime.js",
|
|
69
|
+
"default": "./src/public/default.js"
|
|
70
|
+
},
|
|
71
|
+
"./*": "./src/public/*.js",
|
|
72
|
+
"./context/*": "./src/public/context/*.js"
|
|
73
|
+
}
|
|
74
|
+
}
|
|
@@ -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,26 @@
|
|
|
1
|
+
import file from "@rcompat/fs/file";
|
|
2
|
+
import client from "./client.js";
|
|
3
|
+
|
|
4
|
+
const root_filter = /^root:solid/u;
|
|
5
|
+
|
|
6
|
+
export default (app, extension) => ({
|
|
7
|
+
name: "solid",
|
|
8
|
+
setup(build) {
|
|
9
|
+
build.onResolve({ filter: root_filter }, ({ path }) => {
|
|
10
|
+
return { path, namespace: "solidroot" };
|
|
11
|
+
});
|
|
12
|
+
build.onLoad({ filter: root_filter }, ({ path }) => {
|
|
13
|
+
const contents = app.build.load(path);
|
|
14
|
+
return contents
|
|
15
|
+
? { contents, loader: "js", resolveDir: app.root.path }
|
|
16
|
+
: null;
|
|
17
|
+
});
|
|
18
|
+
build.onLoad({ filter: new RegExp(`${extension}$`, "u") }, async args => {
|
|
19
|
+
// Load the file from the file system
|
|
20
|
+
const source = await file(args.path).text();
|
|
21
|
+
|
|
22
|
+
// Convert JSX syntax to JavaScript
|
|
23
|
+
return { contents: (await client(source)).js };
|
|
24
|
+
});
|
|
25
|
+
},
|
|
26
|
+
});
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export default length => {
|
|
2
|
+
const n = length - 1;
|
|
3
|
+
const body = Array.from({ length: n }, (_, i) => i - 1)
|
|
4
|
+
.reduceRight((child, _, i) => `components[${i + 1}] !== undefined
|
|
5
|
+
? createComponent(components[${i}], {request, ...data[${i}],
|
|
6
|
+
children: ${child}})
|
|
7
|
+
: createComponent(components[${i}], {request, ...data[${i}]})
|
|
8
|
+
`, `createComponent(components[${n}], {request, ...data[${n}]})`);
|
|
9
|
+
|
|
10
|
+
return `
|
|
11
|
+
import { createSignal } from "solid-js";
|
|
12
|
+
import { createComponent } from "solid-js/web";
|
|
13
|
+
import AppContext from "@primate/solid/context/app";
|
|
14
|
+
import HeadContext from "@primate/solid/context/head";
|
|
15
|
+
|
|
16
|
+
export default ({
|
|
17
|
+
components,
|
|
18
|
+
data,
|
|
19
|
+
request,
|
|
20
|
+
context: c,
|
|
21
|
+
push_heads: value,
|
|
22
|
+
}) => {
|
|
23
|
+
const [context, setContext] = createSignal(c);
|
|
24
|
+
const $value = { context, setContext };
|
|
25
|
+
|
|
26
|
+
return <AppContext.Provider value={$value}>
|
|
27
|
+
<HeadContext.Provider value={value}>
|
|
28
|
+
{${body}}
|
|
29
|
+
</HeadContext.Provider>
|
|
30
|
+
</AppContext.Provider>
|
|
31
|
+
}
|
|
32
|
+
`;
|
|
33
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { generateHydrationScript } from "solid-js/web";
|
|
2
|
+
import spa from "./spa.js";
|
|
3
|
+
|
|
4
|
+
const { groups: { code: hydration_script } } = generateHydrationScript()
|
|
5
|
+
.match(/^<script>(?<code>.*?)<\/script>/u);
|
|
6
|
+
|
|
7
|
+
export default ({ names, data, context, request }, options) => `
|
|
8
|
+
import * as components from "app";
|
|
9
|
+
import { hydrate_solid, render_solid, SolidHead } from "app";
|
|
10
|
+
|
|
11
|
+
window._$HY = { events: [], completed: new WeakSet(), r: {} };
|
|
12
|
+
|
|
13
|
+
${hydration_script}
|
|
14
|
+
|
|
15
|
+
SolidHead.clear();
|
|
16
|
+
let dispose = hydrate_solid(() => components.root_solid({
|
|
17
|
+
components: [${names.map(name => `components.${name}`).join(", ")}],
|
|
18
|
+
data: ${JSON.stringify(data)},
|
|
19
|
+
context: ${JSON.stringify(context)},
|
|
20
|
+
request: {
|
|
21
|
+
...${JSON.stringify(request)},
|
|
22
|
+
url: new URL(location.href),
|
|
23
|
+
},
|
|
24
|
+
}), globalThis.window.document.body);
|
|
25
|
+
${options.spa ? spa : ""}`;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export default `
|
|
2
|
+
const { spa } = components;
|
|
3
|
+
window.addEventListener("DOMContentLoaded", _ => spa((props, update) => {
|
|
4
|
+
dispose();
|
|
5
|
+
dispose = render_solid(() => components.root_solid({
|
|
6
|
+
components: props.names.map(name => components[name]),
|
|
7
|
+
data: props.data,
|
|
8
|
+
context: props.context,
|
|
9
|
+
request: {
|
|
10
|
+
...props.request,
|
|
11
|
+
url: new URL(location.href),
|
|
12
|
+
},
|
|
13
|
+
}), globalThis.window.document.body);
|
|
14
|
+
}));`;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default ".jsx";
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import resolve from "@primate/i18n/core/resolve";
|
|
2
|
+
import AppContext from "#context/app";
|
|
3
|
+
import { useContext } from "solid-js";
|
|
4
|
+
import locale_store from "./locale.js";
|
|
5
|
+
|
|
6
|
+
export default (key, placeholders) => {
|
|
7
|
+
locale_store.init();
|
|
8
|
+
const { locales, locale } = useContext(AppContext).context().i18n;
|
|
9
|
+
return resolve(locales[locale], key, placeholders);
|
|
10
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import AppContext from "#context/app";
|
|
2
|
+
import save from "@primate/i18n/core/save";
|
|
3
|
+
import { useContext } from "solid-js";
|
|
4
|
+
|
|
5
|
+
const $ = {
|
|
6
|
+
context: Symbol("context"),
|
|
7
|
+
setContext: Symbol("setContext"),
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export default {
|
|
11
|
+
[$.context]: undefined,
|
|
12
|
+
[$.setContext]: undefined,
|
|
13
|
+
get() {
|
|
14
|
+
this.init();
|
|
15
|
+
return this[$.context].i18n.locale;
|
|
16
|
+
},
|
|
17
|
+
set(locale) {
|
|
18
|
+
this.init();
|
|
19
|
+
this[$.setContext]({
|
|
20
|
+
...this[$.context],
|
|
21
|
+
i18n: {
|
|
22
|
+
locale,
|
|
23
|
+
locales: this[$.context].i18n.locales,
|
|
24
|
+
},
|
|
25
|
+
});
|
|
26
|
+
save(locale);
|
|
27
|
+
},
|
|
28
|
+
init() {
|
|
29
|
+
if (this[$.context] !== undefined) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const { context, setContext } = useContext(AppContext);
|
|
34
|
+
this[$.context] = context();
|
|
35
|
+
this[$.setContext] = setContext;
|
|
36
|
+
},
|
|
37
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default "solid";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default "@primate/solid";
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import handler from "@primate/frontend/core/handler";
|
|
2
|
+
import client from "#client";
|
|
3
|
+
import name from "#name";
|
|
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,17 @@
|
|
|
1
|
+
import { renderToString } from "solid-js/web";
|
|
2
|
+
|
|
3
|
+
export default (component, props) => {
|
|
4
|
+
const heads = [];
|
|
5
|
+
const push_heads = sub_heads => {
|
|
6
|
+
heads.push(...sub_heads);
|
|
7
|
+
};
|
|
8
|
+
const body = renderToString(() => component({ ...props, push_heads }));
|
|
9
|
+
|
|
10
|
+
if (heads.filter(head => head.startsWith("<title")).length > 1) {
|
|
11
|
+
const error = "May only contain one <title> across component hierarchy";
|
|
12
|
+
throw new Error(error);
|
|
13
|
+
}
|
|
14
|
+
const head = heads.join("\n");
|
|
15
|
+
|
|
16
|
+
return { body, head };
|
|
17
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default } from "#context/app";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default } from "#context/head";
|
|
@@ -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,92 @@
|
|
|
1
|
+
import HeadContext from "#context/head";
|
|
2
|
+
import platform from "@rcompat/platform";
|
|
3
|
+
import { onCleanup, onMount, useContext } from "solid-js";
|
|
4
|
+
|
|
5
|
+
const is_client = platform === "browser";
|
|
6
|
+
|
|
7
|
+
const to_array = maybe => Array.isArray(maybe) ? maybe : [maybe];
|
|
8
|
+
|
|
9
|
+
const data_attribute = "data-sh";
|
|
10
|
+
const data_ssr = "ssr";
|
|
11
|
+
const allowed = ["title", "meta", "style", "meta", "link", "script", "base"];
|
|
12
|
+
|
|
13
|
+
const make_tag = (tag, id) => {
|
|
14
|
+
const parts = tag.split(" ");
|
|
15
|
+
return [
|
|
16
|
+
parts[0],
|
|
17
|
+
`${data_attribute}="${id}"`,
|
|
18
|
+
...parts.slice(1),
|
|
19
|
+
].join(" ");
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const get_tag = child => child.match(/^<(?<tag>[a-z]*) .*$/u).groups.tag;
|
|
23
|
+
|
|
24
|
+
const render = (maybe_children, id) => {
|
|
25
|
+
const children = to_array(maybe_children);
|
|
26
|
+
|
|
27
|
+
if (is_client) {
|
|
28
|
+
// DOM elements
|
|
29
|
+
const titles = children.filter(({ tagName }) => tagName === "TITLE");
|
|
30
|
+
const others = children.filter(({ tagName }) => tagName !== "TITLE");
|
|
31
|
+
|
|
32
|
+
titles.forEach(title => {
|
|
33
|
+
globalThis.document.title = title.innerText;
|
|
34
|
+
});
|
|
35
|
+
others.forEach(other => {
|
|
36
|
+
other.dataset.sh = id;
|
|
37
|
+
globalThis.document.head.prepend(other);
|
|
38
|
+
});
|
|
39
|
+
} else {
|
|
40
|
+
const tags = children.map(({ t: tag }) => tag);
|
|
41
|
+
const all_good = tags.every(tag => allowed.includes(get_tag(tag)));
|
|
42
|
+
if (!all_good) {
|
|
43
|
+
const bad = get_tag(tags.find(tag => !allowed.includes(get_tag(tag))));
|
|
44
|
+
const alloweds = `${allowed.map(tag => `<${tag}>`).join(", ")}`;
|
|
45
|
+
const error = `Head may only contain ${alloweds}, found <${bad}>`;
|
|
46
|
+
throw new Error(error);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const titles = tags.filter(tag => get_tag(tag) === "title");
|
|
50
|
+
const others = tags.filter(tag => get_tag(tag) !== "title");
|
|
51
|
+
|
|
52
|
+
return [
|
|
53
|
+
...titles,
|
|
54
|
+
...others.map(other => make_tag(other, id)),
|
|
55
|
+
];
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
const clear = (data_value) => {
|
|
60
|
+
const selector = `[${data_attribute}="${data_value}"]`;
|
|
61
|
+
globalThis.document.querySelectorAll(selector).forEach(element => {
|
|
62
|
+
element.remove();
|
|
63
|
+
});
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
const Head = function Head(props) {
|
|
67
|
+
let id;
|
|
68
|
+
onMount(() => {
|
|
69
|
+
if (is_client) {
|
|
70
|
+
id = crypto.randomUUID();
|
|
71
|
+
render(props.children, id);
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
onCleanup(() => {
|
|
76
|
+
if (is_client) {
|
|
77
|
+
// remove managed tags
|
|
78
|
+
clear(id);
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
if (!is_client) {
|
|
83
|
+
const context = useContext(HeadContext);
|
|
84
|
+
context(render(props.children, data_ssr));
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// no return, nothing rendered
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
Head.clear = (data_value = data_ssr) => clear(data_value);
|
|
91
|
+
|
|
92
|
+
export default Head;
|
|
@@ -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
package/src/client/client.js
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import {generateHydrationScript} from "solid-js/web";
|
|
2
|
-
import rootname from "./rootname.js";
|
|
3
|
-
import liveview from "./liveview.js";
|
|
4
|
-
|
|
5
|
-
const {groups: {code: hydration_script}} = generateHydrationScript()
|
|
6
|
-
.match(/^<script>(?<code>.*?)<\/script>/u);
|
|
7
|
-
|
|
8
|
-
export default ({names, data}, options) => `
|
|
9
|
-
import * as components from "app";
|
|
10
|
-
import {hydrate_solid, render_solid} from "app";
|
|
11
|
-
|
|
12
|
-
window._$HY = {events: [], completed: new WeakSet(), r: {}};
|
|
13
|
-
|
|
14
|
-
${hydration_script}
|
|
15
|
-
|
|
16
|
-
let dispose = hydrate_solid(() => components.${rootname}({
|
|
17
|
-
components: [${names.map(name => `components.${name}`).join(", ")}],
|
|
18
|
-
data: JSON.parse(${JSON.stringify(JSON.stringify(data))}),
|
|
19
|
-
}), globalThis.window.document.body);
|
|
20
|
-
${options.liveview ? liveview : ""}`;
|
|
@@ -1,16 +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) => `components[${i + 1}] !== undefined
|
|
5
|
-
? createComponent(components[${i}], {${data}: data[${i}],
|
|
6
|
-
children: ${child}})
|
|
7
|
-
: createComponent(components[${i}], {${data}: data[${i}]})
|
|
8
|
-
`, `createComponent(components[${n}], {${data}: data[${n}]})`);
|
|
9
|
-
|
|
10
|
-
return `
|
|
11
|
-
import {createComponent} from "solid-js/web";
|
|
12
|
-
export default function Root({components, data} = {}) {
|
|
13
|
-
return ${body};
|
|
14
|
-
};
|
|
15
|
-
`;
|
|
16
|
-
};
|
package/src/client/exports.js
DELETED
package/src/client/hydrate.js
DELETED
package/src/client/liveview.js
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import rootname from "./rootname.js";
|
|
2
|
-
|
|
3
|
-
export default `
|
|
4
|
-
const {liveview} = components;
|
|
5
|
-
window.addEventListener("DOMContentLoaded", _ => liveview((props, update) => {
|
|
6
|
-
dispose();
|
|
7
|
-
dispose = render_solid(() => components.${rootname}({
|
|
8
|
-
components: props.names.map(name => components[name]),
|
|
9
|
-
data: props.data,
|
|
10
|
-
}), globalThis.window.document.body);
|
|
11
|
-
}));`;
|
package/src/client/rootname.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export default "solid_root";
|
package/src/errors.js
DELETED
package/src/errors.json
DELETED
package/src/exports.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {default} from "./module.js";
|
package/src/module.js
DELETED
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
import {Path} from "runtime-compat/fs";
|
|
2
|
-
import {Response, Status, MediaType} from "runtime-compat/http";
|
|
3
|
-
import * as base from "@primate/frontend";
|
|
4
|
-
import {renderToString as render} from "solid-js/web";
|
|
5
|
-
import {transformAsync} from "@babel/core";
|
|
6
|
-
import solid from "babel-preset-solid";
|
|
7
|
-
import {client, create_root, rootname, hydrate} from "./client/exports.js";
|
|
8
|
-
|
|
9
|
-
const normalize = base.normalize("solid");
|
|
10
|
-
|
|
11
|
-
const import$ = async (module, app, copy_dependency) => {
|
|
12
|
-
const {library, packager} = app;
|
|
13
|
-
const {http: {static: {root}}} = app.config;
|
|
14
|
-
const parts = module.split("/");
|
|
15
|
-
const path = [library, ...parts];
|
|
16
|
-
const pkg = await Path.resolve().join(...path, packager).json();
|
|
17
|
-
if (copy_dependency) {
|
|
18
|
-
const dependency = Path.resolve().join(...path);
|
|
19
|
-
const to = app.runpath(app.config.location.client, library, ...parts);
|
|
20
|
-
await dependency.file.copy(`${to}`);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const entry = pkg.exports["."].browser.import;
|
|
24
|
-
app.importmaps[module] = new Path(root, library, module, entry).path;
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
const type = "module";
|
|
28
|
-
|
|
29
|
-
const handler = config => (name, props = {}, {status = Status.OK, page} = {}) =>
|
|
30
|
-
async (app, {layouts = [], as_layout} = {}, request) => {
|
|
31
|
-
const {make, root} = config;
|
|
32
|
-
const {headers} = request;
|
|
33
|
-
const options = {
|
|
34
|
-
liveview: app.liveview !== undefined,
|
|
35
|
-
};
|
|
36
|
-
if (as_layout) {
|
|
37
|
-
return make(name, props);
|
|
38
|
-
}
|
|
39
|
-
const components = (await Promise.all(layouts.map(layout =>
|
|
40
|
-
layout(app, {as_layout: true}, request)
|
|
41
|
-
)))
|
|
42
|
-
/* set the actual page as the last component */
|
|
43
|
-
.concat(await make(name, props));
|
|
44
|
-
|
|
45
|
-
const data = components.map(component => component.props);
|
|
46
|
-
const names = await Promise.all(components.map(component =>
|
|
47
|
-
normalize(component.name)));
|
|
48
|
-
if (options.liveview && headers.get(app.liveview.header) !== undefined) {
|
|
49
|
-
return new Response(JSON.stringify({names, data}), {
|
|
50
|
-
status,
|
|
51
|
-
headers: {...await app.headers(),
|
|
52
|
-
"Content-Type": MediaType.APPLICATION_JSON},
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
const imported = (await import(root)).default;
|
|
57
|
-
const body = render(() => imported({
|
|
58
|
-
components: components.map(({component}) => component),
|
|
59
|
-
data,
|
|
60
|
-
}));
|
|
61
|
-
|
|
62
|
-
const code = client({names, data}, options);
|
|
63
|
-
|
|
64
|
-
await app.publish({code, type, inline: true});
|
|
65
|
-
// needs to be called before app.render
|
|
66
|
-
const headers$ = await app.headers();
|
|
67
|
-
|
|
68
|
-
return new Response(await app.render({body, page}), {
|
|
69
|
-
status,
|
|
70
|
-
headers: {...headers$, "Content-Type": MediaType.TEXT_HTML},
|
|
71
|
-
});
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
export default ({
|
|
75
|
-
directory,
|
|
76
|
-
dynamicProps,
|
|
77
|
-
extension = "jsx",
|
|
78
|
-
} = {}) => {
|
|
79
|
-
return {
|
|
80
|
-
name: "primate:solid",
|
|
81
|
-
register(app, next) {
|
|
82
|
-
app.register(extension, handler(base.register({app, rootname})));
|
|
83
|
-
|
|
84
|
-
return next(app);
|
|
85
|
-
},
|
|
86
|
-
async compile(app, next) {
|
|
87
|
-
await base.compile({
|
|
88
|
-
app,
|
|
89
|
-
directory: directory ?? app.config.location.components,
|
|
90
|
-
extension,
|
|
91
|
-
rootname,
|
|
92
|
-
create_root,
|
|
93
|
-
dynamicProps,
|
|
94
|
-
async compile(file) {
|
|
95
|
-
const presets = [[solid, {generate: "ssr", hydratable: true}]];
|
|
96
|
-
return (await transformAsync(file, {presets})).code;
|
|
97
|
-
},
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
return next(app);
|
|
101
|
-
},
|
|
102
|
-
async publish(app, next) {
|
|
103
|
-
// import libs
|
|
104
|
-
await import$("solid-js", app, true);
|
|
105
|
-
await import$("solid-js/web", app);
|
|
106
|
-
// export hydration code
|
|
107
|
-
app.export({type: "script", code: hydrate});
|
|
108
|
-
|
|
109
|
-
await base.publish({
|
|
110
|
-
app,
|
|
111
|
-
directory: directory ?? app.config.location.components,
|
|
112
|
-
extension,
|
|
113
|
-
rootname,
|
|
114
|
-
create_root,
|
|
115
|
-
dynamicProps,
|
|
116
|
-
normalize,
|
|
117
|
-
async compile(file) {
|
|
118
|
-
const presets = [[solid, {generate: "dom", hydratable: true}]];
|
|
119
|
-
return {js: (await transformAsync(file, {presets})).code};
|
|
120
|
-
},
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
return next(app);
|
|
124
|
-
},
|
|
125
|
-
};
|
|
126
|
-
};
|