@merkur/uhtml 0.35.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,7 @@
1
+ Copyright 2020 Miroslav Jancarik jancarikmiroslav@gmail.com
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,47 @@
1
+ <p align="center">
2
+ <a href="https://merkur.js.org/docs/getting-started" title="Getting started">
3
+ <img src="https://raw.githubusercontent.com/mjancarik/merkur/master/images/merkur-illustration.png" width="100px" height="100px" alt="Merkur illustration"/>
4
+ </a>
5
+ </p>
6
+
7
+ # Merkur
8
+
9
+ [![Build Status](https://github.com/mjancarik/merkur/workflows/CI/badge.svg)](https://github.com/mjancarik/merkur/actions/workflows/ci.yml)
10
+ [![NPM package version](https://img.shields.io/npm/v/@merkur/core/latest.svg)](https://www.npmjs.com/package/@merkur/core)
11
+ ![npm bundle size (scoped version)](https://img.shields.io/bundlephobia/minzip/@merkur/core/latest)
12
+ [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
13
+
14
+ The [Merkur](https://merkur.js.org/) is tiny extensible javascript library for front-end microservices(micro frontends). It allows by default server side rendering for loading performance boost. You can connect it with other frameworks or languages because merkur defines easy API. You can use one of six predefined template's library [Preact](https://preactjs.com/), [µhtml](https://github.com/WebReflection/uhtml#readme), [Svelte](https://svelte.dev/) and [vanilla](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) but you can easily extend for others.
15
+
16
+ ## Features
17
+ - Flexible templating engine
18
+ - Usable with all tech stacks
19
+ - SSR-ready by default
20
+ - Easy extensible with plugins
21
+ - Tiny - 1 KB minified + gzipped
22
+
23
+ ## Getting started
24
+
25
+ ```bash
26
+ npx @merkur/create-widget <name>
27
+
28
+ cd name
29
+
30
+ npm run dev // Point your browser at http://localhost:4444/
31
+ ```
32
+ ![alt text](https://raw.githubusercontent.com/mjancarik/merkur/master/images/hello-widget.png "Merkur example, hello widget")
33
+ ## Documentation
34
+
35
+ To check out [live demo](https://merkur.js.org/demo) and [docs](https://merkur.js.org/docs), visit [https://merkur.js.org](https://merkur.js.org).
36
+
37
+ ## Contribution
38
+
39
+ Contribute to this project via [Pull-Requests](https://github.com/mjancarik/merkur/pulls).
40
+
41
+ We are following [Conventional Commits Specification](https://www.conventionalcommits.org/en/v1.0.0/#summary). To simplify the commit process, you can use `npm run commit` command. It opens an interactive interface, which should help you with commit message composition.
42
+
43
+ Thank you to all the people who already contributed to Merkur!
44
+
45
+ <a href="https://github.com/mjancarik/merkur/graphs/contributors">
46
+ <img src="https://contrib.rocks/image?repo=mjancarik/merkur" />
47
+ </a>
package/cli/index.mjs ADDED
@@ -0,0 +1,33 @@
1
+ import { fileURLToPath } from 'node:url';
2
+
3
+ export default function ({ emitter, EMITTER_EVENTS }) {
4
+ emitter.on(
5
+ EMITTER_EVENTS.MERKUR_CONFIG,
6
+ function defaultEntries({ merkurConfig }) {
7
+ merkurConfig.defaultEntries = {
8
+ client: [
9
+ fileURLToPath(import.meta.resolve('@merkur/uhtml/entries/client.js')),
10
+ ],
11
+ server: [
12
+ fileURLToPath(import.meta.resolve('@merkur/uhtml/entries/server.js')),
13
+ ],
14
+ };
15
+
16
+ return merkurConfig;
17
+ },
18
+ );
19
+
20
+ emitter.on(
21
+ EMITTER_EVENTS.TASK_BUILD,
22
+ function defaultBuild({ build, config }) {
23
+ build = {
24
+ ...build,
25
+ external: config.isServer
26
+ ? [...(build.external ?? []), 'ucontent']
27
+ : build.external,
28
+ };
29
+
30
+ return build;
31
+ },
32
+ );
33
+ }
@@ -0,0 +1,7 @@
1
+ import { createUHtmlWidget } from '@merkur/uhtml/client';
2
+
3
+ import widgetProperties from '@widget';
4
+
5
+ createUHtmlWidget({
6
+ ...widgetProperties,
7
+ });
@@ -0,0 +1,7 @@
1
+ import { createUHtmlWidget } from '@merkur/uhtml/server';
2
+
3
+ import widgetProperties from '@widget';
4
+
5
+ export const createWidget = createUHtmlWidget({
6
+ ...widgetProperties,
7
+ });
@@ -0,0 +1,60 @@
1
+ 'use strict';
2
+
3
+ var uhtml = require('uhtml');
4
+ var core = require('@merkur/core');
5
+ var helpers = require('@merkur/plugin-component/helpers');
6
+
7
+ /**
8
+ * Client Factory for creating merkur widgets with uhtml renderer.
9
+ */
10
+ function createUHtmlWidget({ name, version, $dependencies, viewFactory, ...restProps }) {
11
+ const widgetFactory = async (widgetParams) => core.createMerkurWidget({
12
+ ...restProps,
13
+ ...widgetParams,
14
+ $dependencies: {
15
+ ...$dependencies,
16
+ render: uhtml.render,
17
+ html: uhtml.html,
18
+ },
19
+ async mount(widget) {
20
+ await helpers.mapViews(widget, viewFactory, ({ View, ErrorView, container, ...rest }) => {
21
+ if (!container) {
22
+ return;
23
+ }
24
+ const { render } = widget.$dependencies;
25
+ // @ts-expect-error the @merkur/plugin-error is optional
26
+ if (widget?.error?.status) {
27
+ return ErrorView
28
+ ? render(container, ErrorView(widget))
29
+ : render(container, '');
30
+ }
31
+ return render(container, View(widget));
32
+ });
33
+ },
34
+ async update(widget) {
35
+ await helpers.mapViews(widget, viewFactory, ({ View, container }) => {
36
+ if (!container) {
37
+ return;
38
+ }
39
+ widget.$dependencies.render(container, View(widget));
40
+ });
41
+ },
42
+ async unmount(widget) {
43
+ await helpers.mapViews(widget, viewFactory, ({ container }) => {
44
+ if (!container) {
45
+ return;
46
+ }
47
+ widget.$dependencies.render(container, widget.$dependencies.html ``);
48
+ });
49
+ },
50
+ });
51
+ // Register widget factory on client
52
+ core.createMerkur().register({
53
+ name,
54
+ version,
55
+ createWidget: widgetFactory,
56
+ });
57
+ return widgetFactory;
58
+ }
59
+
60
+ exports.createUHtmlWidget = createUHtmlWidget;
@@ -0,0 +1 @@
1
+ export * from './factory/client';
@@ -0,0 +1,60 @@
1
+ 'use strict';
2
+
3
+ var uhtml = require('uhtml');
4
+ var core = require('@merkur/core');
5
+ var helpers = require('@merkur/plugin-component/helpers');
6
+
7
+ /**
8
+ * Client Factory for creating merkur widgets with uhtml renderer.
9
+ */
10
+ function createUHtmlWidget({ name, version, $dependencies, viewFactory, ...restProps }) {
11
+ const widgetFactory = async (widgetParams) => core.createMerkurWidget({
12
+ ...restProps,
13
+ ...widgetParams,
14
+ $dependencies: {
15
+ ...$dependencies,
16
+ render: uhtml.render,
17
+ html: uhtml.html,
18
+ },
19
+ async mount(widget) {
20
+ await helpers.mapViews(widget, viewFactory, ({ View, ErrorView, container, ...rest }) => {
21
+ if (!container) {
22
+ return;
23
+ }
24
+ const { render } = widget.$dependencies;
25
+ // @ts-expect-error the @merkur/plugin-error is optional
26
+ if (widget?.error?.status) {
27
+ return ErrorView
28
+ ? render(container, ErrorView(widget))
29
+ : render(container, '');
30
+ }
31
+ return render(container, View(widget));
32
+ });
33
+ },
34
+ async update(widget) {
35
+ await helpers.mapViews(widget, viewFactory, ({ View, container }) => {
36
+ if (!container) {
37
+ return;
38
+ }
39
+ widget.$dependencies.render(container, View(widget));
40
+ });
41
+ },
42
+ async unmount(widget) {
43
+ await helpers.mapViews(widget, viewFactory, ({ container }) => {
44
+ if (!container) {
45
+ return;
46
+ }
47
+ widget.$dependencies.render(container, widget.$dependencies.html ``);
48
+ });
49
+ },
50
+ });
51
+ // Register widget factory on client
52
+ core.createMerkur().register({
53
+ name,
54
+ version,
55
+ createWidget: widgetFactory,
56
+ });
57
+ return widgetFactory;
58
+ }
59
+
60
+ exports.createUHtmlWidget = createUHtmlWidget;
@@ -0,0 +1,58 @@
1
+ import { render, html } from 'uhtml';
2
+ import { createMerkur, createMerkurWidget } from '@merkur/core';
3
+ import { mapViews } from '@merkur/plugin-component/helpers';
4
+
5
+ /**
6
+ * Client Factory for creating merkur widgets with uhtml renderer.
7
+ */
8
+ function createUHtmlWidget({ name, version, $dependencies, viewFactory, ...restProps }) {
9
+ const widgetFactory = async (widgetParams) => createMerkurWidget({
10
+ ...restProps,
11
+ ...widgetParams,
12
+ $dependencies: {
13
+ ...$dependencies,
14
+ render,
15
+ html,
16
+ },
17
+ async mount(widget) {
18
+ await mapViews(widget, viewFactory, ({ View, ErrorView, container, ...rest }) => {
19
+ if (!container) {
20
+ return;
21
+ }
22
+ const { render } = widget.$dependencies;
23
+ // @ts-expect-error the @merkur/plugin-error is optional
24
+ if (widget?.error?.status) {
25
+ return ErrorView
26
+ ? render(container, ErrorView(widget))
27
+ : render(container, '');
28
+ }
29
+ return render(container, View(widget));
30
+ });
31
+ },
32
+ async update(widget) {
33
+ await mapViews(widget, viewFactory, ({ View, container }) => {
34
+ if (!container) {
35
+ return;
36
+ }
37
+ widget.$dependencies.render(container, View(widget));
38
+ });
39
+ },
40
+ async unmount(widget) {
41
+ await mapViews(widget, viewFactory, ({ container }) => {
42
+ if (!container) {
43
+ return;
44
+ }
45
+ widget.$dependencies.render(container, widget.$dependencies.html ``);
46
+ });
47
+ },
48
+ });
49
+ // Register widget factory on client
50
+ createMerkur().register({
51
+ name,
52
+ version,
53
+ createWidget: widgetFactory,
54
+ });
55
+ return widgetFactory;
56
+ }
57
+
58
+ export { createUHtmlWidget };
@@ -0,0 +1,13 @@
1
+ /// <reference path="../../../src/modules.d.ts" />
2
+ import { render, html } from 'uhtml';
3
+ import { WidgetParams, defineWidget } from '@merkur/core';
4
+ declare module '@merkur/core' {
5
+ interface WidgetDependencies {
6
+ render: typeof render;
7
+ html: typeof html;
8
+ }
9
+ }
10
+ /**
11
+ * Client Factory for creating merkur widgets with uhtml renderer.
12
+ */
13
+ export declare function createUHtmlWidget({ name, version, $dependencies, viewFactory, ...restProps }: Parameters<typeof defineWidget>[0]): (widgetParams: WidgetParams) => Promise<import("@merkur/core").Widget>;
@@ -0,0 +1,5 @@
1
+ import { WidgetParams, defineWidget } from '@merkur/core';
2
+ /**
3
+ * Server Factory for creating merkur widgets with uhtml renderer.
4
+ */
5
+ export declare function createUHtmlWidget({ viewFactory, $dependencies, ...restProps }: Parameters<typeof defineWidget>[0]): (widgetParams: WidgetParams) => import("@merkur/core").Widget;
@@ -0,0 +1 @@
1
+ export * from './factory/server';
@@ -0,0 +1 @@
1
+ export * from './factory/client';
@@ -0,0 +1,13 @@
1
+ /// <reference path="../../../src/modules.d.ts" />
2
+ import { render, html } from 'uhtml';
3
+ import { WidgetParams, defineWidget } from '@merkur/core';
4
+ declare module '@merkur/core' {
5
+ interface WidgetDependencies {
6
+ render: typeof render;
7
+ html: typeof html;
8
+ }
9
+ }
10
+ /**
11
+ * Client Factory for creating merkur widgets with uhtml renderer.
12
+ */
13
+ export declare function createUHtmlWidget({ name, version, $dependencies, viewFactory, ...restProps }: Parameters<typeof defineWidget>[0]): (widgetParams: WidgetParams) => Promise<import("@merkur/core").Widget>;
@@ -0,0 +1,5 @@
1
+ import { WidgetParams, defineWidget } from '@merkur/core';
2
+ /**
3
+ * Server Factory for creating merkur widgets with uhtml renderer.
4
+ */
5
+ export declare function createUHtmlWidget({ viewFactory, $dependencies, ...restProps }: Parameters<typeof defineWidget>[0]): (widgetParams: WidgetParams) => import("@merkur/core").Widget;
@@ -0,0 +1,48 @@
1
+ 'use strict';
2
+
3
+ var ucontent = require('ucontent');
4
+ var core = require('@merkur/core');
5
+
6
+ /**
7
+ * Server Factory for creating merkur widgets with uhtml renderer.
8
+ */
9
+ function createUHtmlWidget({ viewFactory, $dependencies, ...restProps }) {
10
+ return (widgetParams) => core.createMerkurWidget({
11
+ ...restProps,
12
+ ...widgetParams,
13
+ $dependencies: {
14
+ ...$dependencies,
15
+ html: ucontent.html,
16
+ },
17
+ async mount(widget) {
18
+ const { View: MainView, ErrorView, slot = {}, } = await viewFactory(widget);
19
+ /**
20
+ * Wrapper around $dependencies.render function which
21
+ * handles connection to ErrorView and error plugin when defined.
22
+ */
23
+ const renderView = (View) => {
24
+ // @ts-expect-error the @merkur/plugin-error is optional
25
+ if (widget?.error?.status && ErrorView) {
26
+ return ErrorView(widget);
27
+ }
28
+ // @ts-expect-error the @merkur/plugin-error is optional
29
+ if (widget?.error?.status) {
30
+ return null;
31
+ }
32
+ return View(widget);
33
+ };
34
+ return {
35
+ html: renderView(MainView),
36
+ slot: Object.keys(slot).reduce((acc, cur) => {
37
+ acc[cur] = {
38
+ name: slot[cur].name,
39
+ html: renderView(slot[cur].View),
40
+ };
41
+ return acc;
42
+ }, {}),
43
+ };
44
+ },
45
+ });
46
+ }
47
+
48
+ exports.createUHtmlWidget = createUHtmlWidget;
@@ -0,0 +1 @@
1
+ export * from './factory/server';
@@ -0,0 +1,48 @@
1
+ 'use strict';
2
+
3
+ var ucontent = require('ucontent');
4
+ var core = require('@merkur/core');
5
+
6
+ /**
7
+ * Server Factory for creating merkur widgets with uhtml renderer.
8
+ */
9
+ function createUHtmlWidget({ viewFactory, $dependencies, ...restProps }) {
10
+ return (widgetParams) => core.createMerkurWidget({
11
+ ...restProps,
12
+ ...widgetParams,
13
+ $dependencies: {
14
+ ...$dependencies,
15
+ html: ucontent.html,
16
+ },
17
+ async mount(widget) {
18
+ const { View: MainView, ErrorView, slot = {}, } = await viewFactory(widget);
19
+ /**
20
+ * Wrapper around $dependencies.render function which
21
+ * handles connection to ErrorView and error plugin when defined.
22
+ */
23
+ const renderView = (View) => {
24
+ // @ts-expect-error the @merkur/plugin-error is optional
25
+ if (widget?.error?.status && ErrorView) {
26
+ return ErrorView(widget);
27
+ }
28
+ // @ts-expect-error the @merkur/plugin-error is optional
29
+ if (widget?.error?.status) {
30
+ return null;
31
+ }
32
+ return View(widget);
33
+ };
34
+ return {
35
+ html: renderView(MainView),
36
+ slot: Object.keys(slot).reduce((acc, cur) => {
37
+ acc[cur] = {
38
+ name: slot[cur].name,
39
+ html: renderView(slot[cur].View),
40
+ };
41
+ return acc;
42
+ }, {}),
43
+ };
44
+ },
45
+ });
46
+ }
47
+
48
+ exports.createUHtmlWidget = createUHtmlWidget;
@@ -0,0 +1,46 @@
1
+ import { html } from 'ucontent';
2
+ import { createMerkurWidget } from '@merkur/core';
3
+
4
+ /**
5
+ * Server Factory for creating merkur widgets with uhtml renderer.
6
+ */
7
+ function createUHtmlWidget({ viewFactory, $dependencies, ...restProps }) {
8
+ return (widgetParams) => createMerkurWidget({
9
+ ...restProps,
10
+ ...widgetParams,
11
+ $dependencies: {
12
+ ...$dependencies,
13
+ html,
14
+ },
15
+ async mount(widget) {
16
+ const { View: MainView, ErrorView, slot = {}, } = await viewFactory(widget);
17
+ /**
18
+ * Wrapper around $dependencies.render function which
19
+ * handles connection to ErrorView and error plugin when defined.
20
+ */
21
+ const renderView = (View) => {
22
+ // @ts-expect-error the @merkur/plugin-error is optional
23
+ if (widget?.error?.status && ErrorView) {
24
+ return ErrorView(widget);
25
+ }
26
+ // @ts-expect-error the @merkur/plugin-error is optional
27
+ if (widget?.error?.status) {
28
+ return null;
29
+ }
30
+ return View(widget);
31
+ };
32
+ return {
33
+ html: renderView(MainView),
34
+ slot: Object.keys(slot).reduce((acc, cur) => {
35
+ acc[cur] = {
36
+ name: slot[cur].name,
37
+ html: renderView(slot[cur].View),
38
+ };
39
+ return acc;
40
+ }, {}),
41
+ };
42
+ },
43
+ });
44
+ }
45
+
46
+ export { createUHtmlWidget };
package/package.json ADDED
@@ -0,0 +1,57 @@
1
+ {
2
+ "name": "@merkur/uhtml",
3
+ "version": "0.35.0",
4
+ "description": "Collection of helpers to aid with Svelte integration to @merkur",
5
+ "scripts": {
6
+ "test": "echo 'Tests pass.'",
7
+ "test:es:version": "echo 'Tests pass.'",
8
+ "build": "rollup -c rollup.config.mjs",
9
+ "prepare": "npm run build",
10
+ "dev": "rollup -c rollup.config.mjs -w"
11
+ },
12
+ "exports": {
13
+ "./entries/client.js": "./entries/client.js",
14
+ "./entries/server.js": "./entries/server.js",
15
+ "./webpack": "./webpack/index.js",
16
+ "./cli": "./cli/index.mjs",
17
+ "./client": {
18
+ "types": "./lib/client/client.d.ts",
19
+ "import": "./lib/client/client.mjs",
20
+ "require": "./lib/client/client.cjs"
21
+ },
22
+ "./server": {
23
+ "types": "./lib/server/server.d.ts",
24
+ "import": "./lib/server/server.mjs",
25
+ "require": "./lib/server/server.cjs"
26
+ }
27
+ },
28
+ "keywords": [
29
+ "merkur",
30
+ "uhtml",
31
+ "micro",
32
+ "frontend"
33
+ ],
34
+ "repository": {
35
+ "type": "git",
36
+ "url": "git+https://github.com/mjancarik/merkur.git"
37
+ },
38
+ "author": "Jan Šimeček",
39
+ "license": "ISC",
40
+ "bugs": {
41
+ "url": "https://github.com/mjancarik/merkur/issues"
42
+ },
43
+ "publishConfig": {
44
+ "registry": "https://registry.npmjs.org/",
45
+ "access": "public"
46
+ },
47
+ "peerDependencies": {
48
+ "@merkur/core": ">=0.34",
49
+ "@merkur/plugin-component": ">=0.34",
50
+ "@merkur/tool-webpack": ">=0.28"
51
+ },
52
+ "dependencies": {
53
+ "ucontent": "^2.0.0",
54
+ "uhtml": "^3.2.2"
55
+ },
56
+ "gitHead": "383f29bdf67a7f4252876867687371a8d26f53f2"
57
+ }
@@ -0,0 +1,33 @@
1
+ const path = require('path');
2
+ const fs = require('fs');
3
+
4
+ function applyUHtmlConfig(config, { cwd, isServer }) {
5
+ // Check for existence of widget entry points
6
+ if (
7
+ config.entry.widget &&
8
+ fs.existsSync(path.join(cwd, config.entry.widget))
9
+ ) {
10
+ return config;
11
+ }
12
+
13
+ // TODO should probably be moved to root config, when all frameworks are supported (this will be removed with webpack deprecation)
14
+ // Set custom aliases to widget entry point
15
+ config.resolve = {
16
+ ...config.resolve,
17
+ alias: {
18
+ '@widget': path.join(cwd, './src/widget.js'),
19
+ ...config.resolve.alias,
20
+ },
21
+ };
22
+
23
+ // Add default client/server entries if there are no custom ones
24
+ config.entry.widget = require.resolve(
25
+ `@merkur/uhtml/entries/${isServer ? 'server' : 'client'}.js`,
26
+ );
27
+
28
+ return config;
29
+ }
30
+
31
+ module.exports = {
32
+ applyUHtmlConfig,
33
+ };