@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.
- package/lib/private/Default.d.ts +12 -0
- package/lib/private/Default.js +29 -0
- package/lib/private/Runtime.d.ts +16 -0
- package/lib/private/Runtime.js +19 -0
- package/lib/private/client/Component.d.ts +13 -0
- package/lib/private/client/Component.js +38 -0
- package/lib/private/client/index.d.ts +9 -0
- package/lib/private/client/index.js +7 -0
- package/lib/private/server/Component.d.ts +6 -0
- package/lib/private/server/Component.js +9 -0
- package/lib/public/Component.d.ts +2 -0
- package/lib/public/Component.js +2 -0
- package/lib/public/browser.d.ts +2 -0
- package/lib/public/browser.js +2 -0
- package/lib/public/default.d.ts +4 -0
- package/lib/public/default.js +3 -0
- package/lib/public/runtime.d.ts +4 -0
- package/lib/public/runtime.js +3 -0
- package/package.json +29 -26
- package/src/private/build/client.js +0 -20
- package/src/private/build/index.js +0 -21
- package/src/private/build/publish.js +0 -14
- package/src/private/client/index.js +0 -20
- package/src/private/error/no-class-name.js +0 -11
- package/src/private/extension.js +0 -1
- package/src/private/name.js +0 -1
- package/src/private/pkgname.js +0 -1
- package/src/private/serve/handler.js +0 -11
- package/src/private/serve/index.js +0 -7
- package/src/public/Component.js +0 -28
- package/src/public/default.js +0 -10
- package/src/public/runtime.js +0 -8
|
@@ -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
|
package/package.json
CHANGED
|
@@ -1,50 +1,53 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@primate/webc",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Primate
|
|
5
|
-
"homepage": "https://
|
|
6
|
-
"bugs": "https://github.com/
|
|
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
|
-
"
|
|
10
|
-
"
|
|
9
|
+
"/lib/**/*.js",
|
|
10
|
+
"/lib/**/*.d.ts",
|
|
11
|
+
"!/**/*.spec.*"
|
|
11
12
|
],
|
|
12
13
|
"repository": {
|
|
13
14
|
"type": "git",
|
|
14
|
-
"url": "https://github.com/
|
|
15
|
+
"url": "https://github.com/primate-run/primate",
|
|
15
16
|
"directory": "packages/webc"
|
|
16
17
|
},
|
|
17
18
|
"dependencies": {
|
|
18
|
-
"@rcompat/fs": "^0.
|
|
19
|
-
"@primate/
|
|
19
|
+
"@rcompat/fs": "^0.21.1",
|
|
20
|
+
"@primate/core": "^0.3.0"
|
|
20
21
|
},
|
|
21
22
|
"peerDependencies": {
|
|
22
|
-
"primate": "^0.
|
|
23
|
+
"primate": "^0.34.0"
|
|
23
24
|
},
|
|
24
25
|
"type": "module",
|
|
25
26
|
"imports": {
|
|
26
27
|
"#*": {
|
|
27
|
-
"
|
|
28
|
-
"default": "./
|
|
28
|
+
"apekit": "./src/private/*.ts",
|
|
29
|
+
"default": "./lib/private/*.js"
|
|
29
30
|
},
|
|
30
|
-
"#
|
|
31
|
-
"
|
|
32
|
-
"
|
|
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
|
-
"
|
|
46
|
-
"
|
|
39
|
+
"browser": "./lib/public/browser.js",
|
|
40
|
+
"runtime": "./lib/public/runtime.js",
|
|
41
|
+
"default": "./lib/public/default.js"
|
|
47
42
|
},
|
|
48
|
-
"
|
|
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
|
-
});
|
package/src/private/extension.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export default ".webc";
|
package/src/private/name.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export default "webc";
|
package/src/private/pkgname.js
DELETED
|
@@ -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
|
-
};
|
package/src/public/Component.js
DELETED
|
@@ -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
|
-
|
package/src/public/default.js
DELETED
|
@@ -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
|
-
});
|