@primate/webc 0.1.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 ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "@primate/webc",
3
+ "version": "0.1.0",
4
+ "description": "Primate Web Components frontend",
5
+ "homepage": "https://primatejs.com/modules/webc",
6
+ "bugs": "https://github.com/primatejs/primate/issues",
7
+ "license": "MIT",
8
+ "files": [
9
+ "src/**/*.js",
10
+ "!src/**/*.spec.js"
11
+ ],
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "https://github.com/primatejs/primate",
15
+ "directory": "packages/webc"
16
+ },
17
+ "dependencies": {
18
+ "@rcompat/fs": "^0.4.0",
19
+ "@primate/frontend": "^0.16.0"
20
+ },
21
+ "peerDependencies": {
22
+ "primate": "^0.32.0"
23
+ },
24
+ "type": "module",
25
+ "imports": {
26
+ "#*": {
27
+ "@primate/lt": "./src/private/*.js",
28
+ "default": "./src/private/*.js"
29
+ },
30
+ "#build": {
31
+ "@primate/lt": "./src/private/build/index.js",
32
+ "default": "./src/private/build/index.js"
33
+ },
34
+ "#serve": {
35
+ "@primate/lt": "./src/private/serve/index.js",
36
+ "default": "./src/private/serve/index.js"
37
+ },
38
+ "#client": {
39
+ "@primate/lt": "./src/private/client/index.js",
40
+ "default": "./src/private/client/index.js"
41
+ }
42
+ },
43
+ "exports": {
44
+ ".": {
45
+ "runtime": "./src/public/runtime.js",
46
+ "default": "./src/public/default.js"
47
+ },
48
+ "./Component": "./src/public/Component.js"
49
+ }
50
+ }
@@ -0,0 +1,20 @@
1
+ import no_class_name from "#error/no-class-name";
2
+ import file from "@rcompat/fs/file";
3
+
4
+ const script_re = /(?<=<script)>(?<code>.*?)(?=<\/script>)/gus;
5
+ const webc_class_name_re = /export default class (?<name>.*?) extends/u;
6
+
7
+ export default (app, extension) => async (text, component) => {
8
+ const [script] = await Promise.all([...text.matchAll(script_re)]
9
+ .map(({ groups: { code } }) => code));
10
+ const { name } = script.match(webc_class_name_re)?.groups ?? {};
11
+ // app.assert(name !== undefined, NoClassName._(component))
12
+ name === undefined && no_class_name(component);
13
+ const tag = file(component)
14
+ .debase(`${app.path.components}/`)
15
+ .path.replaceAll("/", "-").slice(0, -extension.length);
16
+
17
+ const js = `${script} globalThis.customElements.define("${tag}", ${name});`;
18
+
19
+ return { js };
20
+ };
@@ -0,0 +1,21 @@
1
+ import name from "#name";
2
+ import compile from "@primate/frontend/core/compile";
3
+ import client from "./client.js";
4
+ import publish from "./publish.js";
5
+
6
+ export default extension => async (app, next) => {
7
+ app.register(extension, {
8
+ ...await compile({
9
+ app,
10
+ extension,
11
+ name,
12
+ compile: { client: client(app, extension) },
13
+ }),
14
+ // noop
15
+ server: _ => _,
16
+ });
17
+
18
+ app.build.plugin(publish(app, extension));
19
+
20
+ return next(app);
21
+ };
@@ -0,0 +1,14 @@
1
+ import file from "@rcompat/fs/file";
2
+ import client from "./client.js";
3
+
4
+ export default (app, extension) => ({
5
+ name: "webc",
6
+ setup(build) {
7
+ build.onLoad({ filter: new RegExp(`${extension}$`, "u") }, async args => {
8
+ // Load the file from the file system
9
+ const source = await file(args.path).text();
10
+
11
+ return { contents: (await client(app, extension)(source, args.path)).js };
12
+ });
13
+ },
14
+ });
@@ -0,0 +1,20 @@
1
+ export default (name, props) => `
2
+ import * as components from "app";
3
+
4
+ globalThis.customElements.define("p-wrap-with", class extends HTMLElement {
5
+ connectedCallback() {
6
+ this.attachShadow({ mode: "open" });
7
+
8
+ const id = this.getAttribute("id");
9
+ const wrapped = globalThis.registry[id];
10
+ this.shadowRoot.appendChild(wrapped);
11
+ wrapped.render();
12
+ delete globalThis.registry[id];
13
+ }
14
+ });
15
+ globalThis.registry = {};
16
+
17
+ const element = globalThis.document.createElement("${name.replaceAll("/", "-")}");
18
+ element.props = ${JSON.stringify(props)};
19
+ globalThis.document.body.appendChild(element)
20
+ `;
@@ -0,0 +1,11 @@
1
+ import pkgname from "#pkgname";
2
+ import log from "@primate/core/log";
3
+ import file from "@rcompat/fs/file";
4
+
5
+ export default (...params) => log.error({
6
+ params,
7
+ name: file(import.meta.url).base,
8
+ module: pkgname,
9
+ message: "the component at {0} is missing a class name",
10
+ fix: "add a class name to the component",
11
+ });
@@ -0,0 +1 @@
1
+ export default ".webc";
@@ -0,0 +1 @@
1
+ export default "webc";
@@ -0,0 +1 @@
1
+ export default "@primate/webc";
@@ -0,0 +1,11 @@
1
+ import client from "#client";
2
+
3
+ export default (name, props = {}, options = {}) => async app => {
4
+ const [component] = name.split(".");
5
+ const assets = [await app.inline(client(component, props), "module")];
6
+ const head = assets.map(asset => asset.head).join("\n");
7
+ const script_src = assets.map(asset => asset.integrity);
8
+ const headers = app.headers({ "script-src": script_src });
9
+
10
+ return app.view({ head, headers, body: "", ...options });
11
+ };
@@ -0,0 +1,7 @@
1
+ import handler from "./handler.js";
2
+
3
+ export default extension => (app, next) => {
4
+ app.register(extension, handler);
5
+
6
+ return next(app);
7
+ };
@@ -0,0 +1,28 @@
1
+ const ServerHTMLElement = class {};
2
+
3
+ const HTMLElement = globalThis.HTMLElement ?? ServerHTMLElement;
4
+
5
+ export default class Component extends HTMLElement {
6
+ constructor(props) {
7
+ super();
8
+ this.props = props === undefined
9
+ // <sub-component name="bob" />
10
+ ? Object.fromEntries(this.getAttributeNames().map(key =>
11
+ [key, this.getAttribute(key)]))
12
+ // new SubComponent({ name: "bob" }).render()
13
+ : props;
14
+ }
15
+
16
+ connectedCallback() {
17
+ this.attachShadow({ mode: "open" });
18
+ this.shadowRoot.innerHTML = this.render(this.props);
19
+ this.mounted?.(this.shadowRoot);
20
+ }
21
+
22
+ toString() {
23
+ const uuid = crypto.randomUUID();
24
+ globalThis.registry[uuid] = this;
25
+ return `<p-wrap-with id="${uuid}"></p-wrap-with>`;
26
+ }
27
+ }
28
+
@@ -0,0 +1,10 @@
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 ({ extension = default_extension } = {}) => ({
7
+ name: pkgname,
8
+ build: build(extension),
9
+ serve: serve(extension),
10
+ });
@@ -0,0 +1,8 @@
1
+ import default_extension from "#extension";
2
+ import pkgname from "#pkgname";
3
+ import serve from "#serve";
4
+
5
+ export default ({ extension = default_extension } = {}) => ({
6
+ name: pkgname,
7
+ serve: serve(extension),
8
+ });