@primate/webc 0.1.2 → 0.3.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.
@@ -0,0 +1,12 @@
1
+ import Runtime from "#Runtime";
2
+ import type FileRef from "@rcompat/fs/FileRef";
3
+ export default class Default extends Runtime {
4
+ compile: {
5
+ client: (text: string, view: FileRef) => Promise<{
6
+ js: string;
7
+ }>;
8
+ normalize: (name: string) => Promise<string>;
9
+ server: (text: string) => string;
10
+ };
11
+ }
12
+ //# sourceMappingURL=Default.d.ts.map
@@ -0,0 +1,29 @@
1
+ import Runtime from "#Runtime";
2
+ import fail from "@primate/core/fail";
3
+ const script_re = /(?<=<script)>(?<code>.*?)(?=<\/script>)/gus;
4
+ const webc_class_name_re = /export default class (?<name>.*?) extends/u;
5
+ export default class Default extends Runtime {
6
+ compile = {
7
+ client: async (text, view) => {
8
+ const [script] = [...text.matchAll(script_re)]
9
+ .map(({ groups }) => groups.code);
10
+ const { name } = script.match(webc_class_name_re)?.groups
11
+ ?? { name: undefined };
12
+ if (name === undefined)
13
+ throw fail("view at {0} has no class name", view);
14
+ const tag = (await this.normalize(view
15
+ .debase(view.directory, "/").path)).replace("_", "-");
16
+ const js = `${script}
17
+ globalThis.customElements.define("${tag}", ${name});
18
+ `;
19
+ return { js };
20
+ },
21
+ normalize: this.normalize,
22
+ server: (text) => {
23
+ const [code] = [...text.matchAll(script_re)]
24
+ .map(({ groups }) => groups.code);
25
+ return code;
26
+ },
27
+ };
28
+ }
29
+ //# sourceMappingURL=Default.js.map
@@ -0,0 +1,16 @@
1
+ import type App from "@primate/core/App";
2
+ import FrontendModule from "@primate/core/frontend/Module";
3
+ import type Render from "@primate/core/frontend/Render";
4
+ import type Next from "@primate/core/Next";
5
+ import type FileRef from "@rcompat/fs/FileRef";
6
+ export default class Runtime extends FrontendModule {
7
+ #private;
8
+ name: string;
9
+ defaultExtensions: string[];
10
+ layouts: boolean;
11
+ client: boolean;
12
+ render: Render;
13
+ get views(): FileRef | undefined;
14
+ init<T extends App>(app: T, next: Next<T>): import("@rcompat/type/MaybePromise").default<T>;
15
+ }
16
+ //# sourceMappingURL=Runtime.d.ts.map
@@ -0,0 +1,19 @@
1
+ import FrontendModule from "@primate/core/frontend/Module";
2
+ export default class Runtime extends FrontendModule {
3
+ #views;
4
+ name = "webc";
5
+ defaultExtensions = [".webc"];
6
+ layouts = false;
7
+ client = true;
8
+ render = (_view, _props) => {
9
+ return { body: "", head: "" };
10
+ };
11
+ get views() {
12
+ return this.#views;
13
+ }
14
+ init(app, next) {
15
+ this.#views = app.path.views;
16
+ return super.init(app, next);
17
+ }
18
+ }
19
+ //# sourceMappingURL=Runtime.js.map
@@ -0,0 +1,13 @@
1
+ import type Dict from "@rcompat/type/Dict";
2
+ export default class Component extends HTMLElement {
3
+ #private;
4
+ static tag: string;
5
+ constructor(props?: Dict);
6
+ get props(): Dict;
7
+ set props(props: Dict);
8
+ connectedCallback(): void;
9
+ render(props: Dict): string;
10
+ mounted(_: ShadowRoot): void;
11
+ toString(): string;
12
+ }
13
+ //# sourceMappingURL=Component.d.ts.map
@@ -0,0 +1,38 @@
1
+ export default class Component extends HTMLElement {
2
+ #props;
3
+ #component;
4
+ static tag = "";
5
+ constructor(props) {
6
+ super();
7
+ this.#props = props === undefined
8
+ // <sub-component name="bob" />
9
+ ? Object.fromEntries(this.getAttributeNames().map(key => [key, this.getAttribute(key)]))
10
+ // new SubComponent({ name: "bob" }).render()
11
+ : props;
12
+ }
13
+ get props() {
14
+ return this.#props;
15
+ }
16
+ set props(props) {
17
+ this.#props = props;
18
+ }
19
+ connectedCallback() {
20
+ this.attachShadow({ mode: "open" });
21
+ const root = this.shadowRoot;
22
+ if (root === null) {
23
+ return;
24
+ }
25
+ root.innerHTML = this.render(this.props);
26
+ this.mounted(root);
27
+ }
28
+ render(props) {
29
+ return "";
30
+ }
31
+ mounted(_) {
32
+ // noop
33
+ }
34
+ toString() {
35
+ return this.render(this.props);
36
+ }
37
+ }
38
+ //# sourceMappingURL=Component.js.map
@@ -0,0 +1,9 @@
1
+ import type Dict from "@rcompat/type/Dict";
2
+ type Props = {
3
+ props: Dict;
4
+ };
5
+ export default class WebComponentsClient {
6
+ static mount(view: string, props: Props): void;
7
+ }
8
+ export {};
9
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,7 @@
1
+ import * as views from "webc:views";
2
+ export default class WebComponentsClient {
3
+ static mount(view, props) {
4
+ globalThis.document.body.appendChild(new views[view](props.props));
5
+ }
6
+ }
7
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,6 @@
1
+ import type Dict from "@rcompat/type/Dict";
2
+ export default class Component {
3
+ render(_props: Dict): string;
4
+ mounted(_: ShadowRoot): void;
5
+ }
6
+ //# sourceMappingURL=Component.d.ts.map
@@ -0,0 +1,9 @@
1
+ export default class Component {
2
+ render(_props) {
3
+ return "";
4
+ }
5
+ mounted(_) {
6
+ // noop
7
+ }
8
+ }
9
+ //# sourceMappingURL=Component.js.map
@@ -0,0 +1,2 @@
1
+ export { default } from "#Component";
2
+ //# sourceMappingURL=Component.d.ts.map
@@ -0,0 +1,2 @@
1
+ export { default } from "#Component";
2
+ //# sourceMappingURL=Component.js.map
@@ -0,0 +1,2 @@
1
+ export { default } from "#client/index";
2
+ //# sourceMappingURL=browser.d.ts.map
@@ -0,0 +1,2 @@
1
+ export { default } from "#client/index";
2
+ //# sourceMappingURL=browser.js.map
@@ -0,0 +1,4 @@
1
+ import Default from "#Default";
2
+ declare const _default: (options?: typeof Default.input) => Default;
3
+ export default _default;
4
+ //# sourceMappingURL=default.d.ts.map
@@ -0,0 +1,3 @@
1
+ import Default from "#Default";
2
+ export default (options) => new Default(options);
3
+ //# sourceMappingURL=default.js.map
@@ -0,0 +1,4 @@
1
+ import Runtime from "#Runtime";
2
+ declare const _default: (options?: typeof Runtime.input) => Runtime;
3
+ export default _default;
4
+ //# sourceMappingURL=runtime.d.ts.map
@@ -0,0 +1,3 @@
1
+ import Runtime from "#Runtime";
2
+ export default (options) => new Runtime(options);
3
+ //# sourceMappingURL=runtime.js.map
package/package.json CHANGED
@@ -1,50 +1,53 @@
1
1
  {
2
2
  "name": "@primate/webc",
3
- "version": "0.1.2",
4
- "description": "Primate Web Components frontend",
5
- "homepage": "https://primatejs.com/modules/webc",
6
- "bugs": "https://github.com/primatejs/primate/issues",
3
+ "version": "0.3.0",
4
+ "description": "Primate web components frontend",
5
+ "homepage": "https://primate.run/docs/frontend/web-components",
6
+ "bugs": "https://github.com/primate-run/primate/issues",
7
7
  "license": "MIT",
8
8
  "files": [
9
- "src/**/*.js",
10
- "!src/**/*.spec.js"
9
+ "/lib/**/*.js",
10
+ "/lib/**/*.d.ts",
11
+ "!/**/*.spec.*"
11
12
  ],
12
13
  "repository": {
13
14
  "type": "git",
14
- "url": "https://github.com/primatejs/primate",
15
+ "url": "https://github.com/primate-run/primate",
15
16
  "directory": "packages/webc"
16
17
  },
17
18
  "dependencies": {
18
- "@rcompat/fs": "^0.7.0",
19
- "@primate/frontend": "^0.16.3"
19
+ "@rcompat/fs": "^0.21.1",
20
+ "@primate/core": "^0.3.0"
20
21
  },
21
22
  "peerDependencies": {
22
- "primate": "^0.32.5"
23
+ "primate": "^0.34.0"
23
24
  },
24
25
  "type": "module",
25
26
  "imports": {
26
27
  "#*": {
27
- "livetypes": "./src/private/*.js",
28
- "default": "./src/private/*.js"
28
+ "apekit": "./src/private/*.ts",
29
+ "default": "./lib/private/*.js"
29
30
  },
30
- "#build": {
31
- "livetypes": "./src/private/build/index.js",
32
- "default": "./src/private/build/index.js"
33
- },
34
- "#serve": {
35
- "livetypes": "./src/private/serve/index.js",
36
- "default": "./src/private/serve/index.js"
37
- },
38
- "#client": {
39
- "livetypes": "./src/private/client/index.js",
40
- "default": "./src/private/client/index.js"
31
+ "#Component": {
32
+ "apekit": "./src/private/client/Component.ts",
33
+ "browser": "./lib/private/client/Component.js",
34
+ "default": "./lib/private/server/Component.js"
41
35
  }
42
36
  },
43
37
  "exports": {
44
38
  ".": {
45
- "runtime": "./src/public/runtime.js",
46
- "default": "./src/public/default.js"
39
+ "browser": "./lib/public/browser.js",
40
+ "runtime": "./lib/public/runtime.js",
41
+ "default": "./lib/public/default.js"
47
42
  },
48
- "./Component": "./src/public/Component.js"
43
+ "./*": {
44
+ "apekit": "./src/public/*.ts",
45
+ "default": "./lib/public/*.js"
46
+ }
47
+ },
48
+ "scripts": {
49
+ "build": "npm run clean && tsc",
50
+ "clean": "rm -rf ./lib",
51
+ "lint": "eslint ."
49
52
  }
50
53
  }
@@ -1,20 +0,0 @@
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
- };
@@ -1,21 +0,0 @@
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
- };
@@ -1,14 +0,0 @@
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
- });
@@ -1,20 +0,0 @@
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
- `;
@@ -1,11 +0,0 @@
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
- });
@@ -1 +0,0 @@
1
- export default ".webc";
@@ -1 +0,0 @@
1
- export default "webc";
@@ -1 +0,0 @@
1
- export default "@primate/webc";
@@ -1,11 +0,0 @@
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
- };
@@ -1,7 +0,0 @@
1
- import handler from "./handler.js";
2
-
3
- export default extension => (app, next) => {
4
- app.register(extension, handler);
5
-
6
- return next(app);
7
- };
@@ -1,28 +0,0 @@
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
-
@@ -1,10 +0,0 @@
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
- });
@@ -1,8 +0,0 @@
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
- });