@primate/solid 0.1.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 -9
- 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 -6
- 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 -212
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,13 +15,60 @@
|
|
|
16
15
|
"directory": "packages/solid"
|
|
17
16
|
},
|
|
18
17
|
"dependencies": {
|
|
19
|
-
"
|
|
20
|
-
"
|
|
21
|
-
"
|
|
18
|
+
"@rcompat/build": "^0.4.0",
|
|
19
|
+
"@rcompat/fs": "^0.4.0",
|
|
20
|
+
"@rcompat/platform": "^0.3.0",
|
|
21
|
+
"@primate/frontend": "^0.16.0"
|
|
22
22
|
},
|
|
23
23
|
"peerDependencies": {
|
|
24
|
-
"
|
|
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
|
+
}
|
|
25
34
|
},
|
|
26
35
|
"type": "module",
|
|
27
|
-
"
|
|
28
|
-
|
|
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,212 +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 {transformAsync} from "@babel/core";
|
|
5
|
-
import solid from "babel-preset-solid";
|
|
6
|
-
import {renderToString} from "solid-js/web";
|
|
7
|
-
import {tryreturn} from "runtime-compat/async";
|
|
8
|
-
import errors from "./errors.js";
|
|
9
|
-
import {client, create_root, rootname, hydrate} from "./client/exports.js";
|
|
10
|
-
|
|
11
|
-
// do not hard-depend on node
|
|
12
|
-
const packager = import.meta.runtime?.packager ?? "package.json";
|
|
13
|
-
const library = import.meta.runtime?.library ?? "node_modules";
|
|
14
|
-
|
|
15
|
-
const encoder = new TextEncoder();
|
|
16
|
-
const hash = async (string, algorithm = "sha-256") => {
|
|
17
|
-
const base = 16;
|
|
18
|
-
const target_pad_length = 2;
|
|
19
|
-
const target_slice = 12;
|
|
20
|
-
const bytes = await crypto.subtle.digest(algorithm, encoder.encode(string));
|
|
21
|
-
return Array.from(new Uint8Array(bytes))
|
|
22
|
-
.map(byte => byte.toString(base).padStart(target_pad_length, "0"))
|
|
23
|
-
.join("")
|
|
24
|
-
.slice(0, target_slice);
|
|
25
|
-
};
|
|
26
|
-
const normalize = async path => `solid_${await hash(path)}`;
|
|
27
|
-
|
|
28
|
-
const import$ = async (module, app, copy_dependency) => {
|
|
29
|
-
const {build: {modules}, http: {static: {root}}} = app.config;
|
|
30
|
-
const parts = module.split("/");
|
|
31
|
-
const path = [library, ...parts];
|
|
32
|
-
const pkg = await Path.resolve().join(...path, packager).json();
|
|
33
|
-
if (copy_dependency) {
|
|
34
|
-
const dependency = Path.resolve().join(...path);
|
|
35
|
-
const to = app.build.paths.client.join(modules, ...parts);
|
|
36
|
-
await dependency.file.copy(`${to}`);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
const entry = pkg.exports["."].browser.import;
|
|
40
|
-
app.importmaps[module] = new Path(root, modules, module, entry).path;
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
const filename = `${rootname}.js`;
|
|
44
|
-
const type = "module";
|
|
45
|
-
|
|
46
|
-
const load = async path =>
|
|
47
|
-
tryreturn(async () => (await import(`${path}.js`)).default)
|
|
48
|
-
.orelse(error => {console.log(error);return errors.MissingComponent.throw(path.name, path)});
|
|
49
|
-
|
|
50
|
-
const make_component = base => async (name, props) =>
|
|
51
|
-
({name, component: await load(base.join(name)), props});
|
|
52
|
-
|
|
53
|
-
const handler = (name, props = {}, {status = Status.OK} = {}) =>
|
|
54
|
-
async (app, {layouts = [], as_layout} = {}, request) => {
|
|
55
|
-
const options = {
|
|
56
|
-
liveview: app.liveview !== undefined,
|
|
57
|
-
};
|
|
58
|
-
const {headers} = request;
|
|
59
|
-
const {paths} = app.build;
|
|
60
|
-
const make = make_component(paths.server.join(app.config.build.app));
|
|
61
|
-
if (as_layout) {
|
|
62
|
-
return make(name, props);
|
|
63
|
-
}
|
|
64
|
-
const components = (await Promise.all(layouts.map(layout =>
|
|
65
|
-
layout(app, {as_layout: true}, request)
|
|
66
|
-
)))
|
|
67
|
-
/* set the actual page as the last component */
|
|
68
|
-
.concat(await make(name, props));
|
|
69
|
-
|
|
70
|
-
const data = components.map(component => component.props);
|
|
71
|
-
const names = await Promise.all(components.map(component =>
|
|
72
|
-
normalize(component.name)));
|
|
73
|
-
if (options.liveview && headers.get(app.liveview.header) !== undefined) {
|
|
74
|
-
return new Response(JSON.stringify({names, data}), {
|
|
75
|
-
status,
|
|
76
|
-
headers: {...await app.headers(),
|
|
77
|
-
"Content-Type": MediaType.APPLICATION_JSON},
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
const root = paths.server.join(filename);
|
|
82
|
-
const imported = (await import(root)).default;
|
|
83
|
-
const body = renderToString(() => imported({
|
|
84
|
-
components: components.map(({component}) => component),
|
|
85
|
-
data,
|
|
86
|
-
}));
|
|
87
|
-
|
|
88
|
-
const code = client({names, data}, options);
|
|
89
|
-
|
|
90
|
-
await app.publish({code, type, inline: true});
|
|
91
|
-
// needs to be called before app.render
|
|
92
|
-
const headers$ = await app.headers();
|
|
93
|
-
|
|
94
|
-
return new Response(await app.render({body}), {
|
|
95
|
-
status,
|
|
96
|
-
headers: {...headers$, "Content-Type": MediaType.TEXT_HTML},
|
|
97
|
-
});
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
const options = {
|
|
101
|
-
server: [solid, {
|
|
102
|
-
generate: "ssr",
|
|
103
|
-
hydratable: true,
|
|
104
|
-
}],
|
|
105
|
-
client: [solid, {
|
|
106
|
-
generate: "dom",
|
|
107
|
-
hydratable: true,
|
|
108
|
-
}],
|
|
109
|
-
};
|
|
110
|
-
|
|
111
|
-
const transform = {
|
|
112
|
-
async server(file) {
|
|
113
|
-
return (await transformAsync(file, {presets: [options.server]})).code;
|
|
114
|
-
},
|
|
115
|
-
async client(file) {
|
|
116
|
-
return (await transformAsync(file, {presets: [options.client]})).code;
|
|
117
|
-
},
|
|
118
|
-
};
|
|
119
|
-
|
|
120
|
-
export default ({
|
|
121
|
-
dynamicProps = "data",
|
|
122
|
-
extension = "jsx",
|
|
123
|
-
} = {}) => {
|
|
124
|
-
const env = {};
|
|
125
|
-
const copy_re = new RegExp(`^.*.(?:${extension})$`, "u");
|
|
126
|
-
const collect_re = new RegExp(`^.*.${extension}$`, "u");
|
|
127
|
-
const extensions = {
|
|
128
|
-
from: `.${extension}`,
|
|
129
|
-
to: `.${extension}.js`,
|
|
130
|
-
};
|
|
131
|
-
|
|
132
|
-
return {
|
|
133
|
-
name: "@primate/solid",
|
|
134
|
-
init(app, next/*0.21 compat, remove >0.21*/ = () => null) {
|
|
135
|
-
env.source = app.build.paths.components;
|
|
136
|
-
|
|
137
|
-
return next(app);
|
|
138
|
-
},
|
|
139
|
-
register(app, next) {
|
|
140
|
-
app.register(extension, handler);
|
|
141
|
-
return next(app);
|
|
142
|
-
},
|
|
143
|
-
async compile(app, next) {
|
|
144
|
-
// copy components to build/components
|
|
145
|
-
await app.copy(app.paths.components, env.source, copy_re);
|
|
146
|
-
const components = await env.source.collect(collect_re);
|
|
147
|
-
const target = app.build.paths.server.join(app.config.build.app);
|
|
148
|
-
await target.file.create();
|
|
149
|
-
|
|
150
|
-
await Promise.all(components.map(async component => {
|
|
151
|
-
const file = await component.file.read();
|
|
152
|
-
const code = await transform.server(file);
|
|
153
|
-
const to = target.join(`${component.path}.js`.replace(env.source, ""));
|
|
154
|
-
await to.directory.file.create();
|
|
155
|
-
await to.file.write(code.replaceAll(extensions.from, extensions.to));
|
|
156
|
-
}));
|
|
157
|
-
|
|
158
|
-
const root = await transform.server(create_root(app.layoutDepth,
|
|
159
|
-
dynamicProps));
|
|
160
|
-
const to = app.build.paths.server.join(filename);
|
|
161
|
-
await to.file.write(root);
|
|
162
|
-
|
|
163
|
-
return next(app);
|
|
164
|
-
},
|
|
165
|
-
async publish(app, next) {
|
|
166
|
-
const target = app.build.paths.client.join(app.config.build.app);
|
|
167
|
-
await target.file.create();
|
|
168
|
-
|
|
169
|
-
// create client components
|
|
170
|
-
const components = await env.source.collect(collect_re);
|
|
171
|
-
await Promise.all(components.map(async component => {
|
|
172
|
-
const name = component.path.replace(`${env.source}/`, "");
|
|
173
|
-
const file = await component.file.read();
|
|
174
|
-
const code = await transform.client(file);
|
|
175
|
-
const build = app.config.build.app;
|
|
176
|
-
const {path} = component;
|
|
177
|
-
|
|
178
|
-
{
|
|
179
|
-
{
|
|
180
|
-
const src = `${path}.js`.replace(`${env.source}`, _ => build);
|
|
181
|
-
await app.publish({src,
|
|
182
|
-
code: code.replaceAll(extensions.from, extensions.to),
|
|
183
|
-
type});
|
|
184
|
-
}
|
|
185
|
-
const imported = await normalize(name);
|
|
186
|
-
app.bootstrap({
|
|
187
|
-
type: "script",
|
|
188
|
-
code: `export {default as ${imported}} from "./${name}.js";\n`,
|
|
189
|
-
});
|
|
190
|
-
}
|
|
191
|
-
}));
|
|
192
|
-
|
|
193
|
-
await import$("solid-js", app, true);
|
|
194
|
-
await import$("solid-js/web", app);
|
|
195
|
-
|
|
196
|
-
app.bootstrap({type: "script", code: hydrate});
|
|
197
|
-
|
|
198
|
-
{
|
|
199
|
-
const code = await transform.client(create_root(app.layoutDepth,
|
|
200
|
-
dynamicProps));
|
|
201
|
-
const src = new Path(app.config.http.static.root, filename);
|
|
202
|
-
await app.publish({src, code, type});
|
|
203
|
-
}
|
|
204
|
-
{
|
|
205
|
-
const code = `export {default as ${rootname}} from "../${filename}";\n`;
|
|
206
|
-
app.bootstrap({type: "script", code});
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
return next(app);
|
|
210
|
-
},
|
|
211
|
-
};
|
|
212
|
-
};
|