piral-svelte 1.0.0-pre.2137 → 1.0.1-beta.5640

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2019 - 2021 smapiot
3
+ Copyright (c) 2019 - 2023 smapiot
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -1,8 +1,8 @@
1
- [![Piral Logo](https://github.com/smapiot/piral/raw/master/docs/assets/logo.png)](https://piral.io)
1
+ [![Piral Logo](https://github.com/smapiot/piral/raw/main/docs/assets/logo.png)](https://piral.io)
2
2
 
3
- # [Piral Svelte](https://piral.io) · [![GitHub License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/smapiot/piral/blob/master/LICENSE) [![npm version](https://img.shields.io/npm/v/piral-svelte.svg?style=flat)](https://www.npmjs.com/package/piral-svelte) [![tested with jest](https://img.shields.io/badge/tested_with-jest-99424f.svg)](https://jestjs.io) [![Gitter Chat](https://badges.gitter.im/gitterHQ/gitter.png)](https://gitter.im/piral-io/community)
3
+ # [Piral Svelte](https://piral.io) · [![GitHub License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/smapiot/piral/blob/main/LICENSE) [![npm version](https://img.shields.io/npm/v/piral-svelte.svg?style=flat)](https://www.npmjs.com/package/piral-svelte) [![tested with jest](https://img.shields.io/badge/tested_with-jest-99424f.svg)](https://jestjs.io) [![Gitter Chat](https://badges.gitter.im/gitterHQ/gitter.png)](https://gitter.im/piral-io/community)
4
4
 
5
- This is a plugin that only has a peer dependency to `piral-core`. What `piral-svelte` brings to the table is a set of Pilet API extensions that can be used with `piral` or `piral-core`.
5
+ This is a plugin that has no peer dependencies. What `piral-svelte` brings to the table is a set of Pilet API extensions that can be used with `piral` or `piral-core`.
6
6
 
7
7
  The set includes a Svelte converter for any component registration, as well as a `fromSvelte` shortcut together with a `svelte-extension` web component.
8
8
 
@@ -43,7 +43,7 @@ Alternatively, if `piral-svelte` has not been added to the Piral instance you ca
43
43
 
44
44
  ```ts
45
45
  import { PiletApi } from '<name-of-piral-instance>';
46
- import { fromSvelte } from 'piral-svelte';
46
+ import { fromSvelte } from 'piral-svelte/convert';
47
47
  import SveltePage from './Page.svelte';
48
48
 
49
49
  export function setup(piral: PiletApi) {
@@ -51,6 +51,67 @@ export function setup(piral: PiletApi) {
51
51
  }
52
52
  ```
53
53
 
54
+ How you built your Svelte-pilet is up to you. If you use Webpack then the bundler options such as `piral-cli-webpack` or `piral-cli-webpack5` can be leveraged. In these cases you'd need to install the `svelte-loader` package and create a custom *webpack.config.js*:
55
+
56
+ ```sh
57
+ npm i svelte-loader --save-dev
58
+ ```
59
+
60
+ The Webpack configuration can be rather simplistic, as shown on the [svelte-loader readme](https://github.com/sveltejs/svelte-loader). In many cases you can use the convenience `extend-webpack` module.
61
+
62
+ This is how your *webpack.config.js* can look like with the convenience module:
63
+
64
+ ```js
65
+ const extendWebpack = require('piral-svelte/extend-webpack');
66
+
67
+ module.exports = extendWebpack({});
68
+ ```
69
+
70
+ For using `piral-svelte/extend-webpack` you must have installed:
71
+
72
+ - `svelte-loader`
73
+ - `webpack`, e.g., via `piral-cli-webpack5`
74
+
75
+ You can do that via:
76
+
77
+ ```sh
78
+ npm i svelte-loader piral-cli-webpack5 --save-dev
79
+ ```
80
+
81
+ The available options for `piral-svelte/extend-webpack` are the same as for the options of the `svelte-loader`, e.g.:
82
+
83
+ ```js
84
+ const extendWebpack = require('piral-svelte/extend-webpack');
85
+
86
+ module.exports = extendWebpack({
87
+ emitCss: false,
88
+ compilerOptions: {
89
+ css: false,
90
+ },
91
+ });
92
+ ```
93
+
94
+ You can also customize the options even more:
95
+
96
+ ```js
97
+ const extendWebpack = require('piral-svelte/extend-webpack');
98
+
99
+ const applySvelte = extendWebpack({
100
+ emitCss: false,
101
+ compilerOptions: {
102
+ css: false,
103
+ },
104
+ });
105
+
106
+ module.exports = config => {
107
+ config = applySvelte(config);
108
+
109
+ // your changes to config
110
+
111
+ return config;
112
+ };
113
+ ```
114
+
54
115
  :::
55
116
 
56
117
  ::: summary: For Piral instance developers
@@ -113,7 +174,7 @@ For Svelte to work the Svelte compiler and the associated Parcel plugin need to
113
174
  npm i svelte parcel-plugin-svelte --save-dev
114
175
  ```
115
176
 
116
- Furthermore, since Svelte distributes its source code as ES6 we need to change the browserlist setting in the *package.json* of the pilet:
177
+ Furthermore, since Svelte distributes its source code as ES6 we need to change the `browserslist` setting in the *package.json* of the pilet:
117
178
 
118
179
  ```json
119
180
  {
package/convert.d.ts CHANGED
@@ -1,9 +1,18 @@
1
- import type { HtmlComponent } from 'piral-core';
2
- import { createExtension } from './lib/extension';
3
- declare const convert: <TProps extends import("piral-core").BaseComponentProps>(Component: import("./lib/types").SvelteModule<TProps>, captured?: Record<string, any>) => import("piral-core").ForeignComponent<TProps>;
1
+ import { createConverter } from './esm/converter';
2
+ export interface HtmlComponent<TProps> {
3
+ component: {
4
+ mount(element: HTMLElement, props: TProps, ctx: any, locals: any): void;
5
+ update?(element: HTMLElement, props: TProps, ctx: any, locals: any): void;
6
+ unmount?(element: HTMLElement, locals: any): void;
7
+ };
8
+ type: 'html';
9
+ }
4
10
  export interface SvelteConverter {
5
- (...params: Parameters<typeof convert>): HtmlComponent<any>;
11
+ (...params: Parameters<ReturnType<typeof createConverter>>): HtmlComponent<any>;
6
12
  }
7
- export declare const fromSvelte: SvelteConverter;
8
- export declare const createSvelteExtension: typeof createExtension;
9
- export {};
13
+ export declare function createSvelteConverter(...params: Parameters<typeof createConverter>): {
14
+ from: SvelteConverter;
15
+ Extension: string;
16
+ };
17
+ declare const fromSvelte: SvelteConverter, SvelteExtension: string;
18
+ export { fromSvelte, SvelteExtension };
package/convert.js CHANGED
@@ -1,11 +1,16 @@
1
- "use strict";
2
- exports.__esModule = true;
3
- exports.createSvelteExtension = exports.fromSvelte = void 0;
4
- var converter_1 = require("./lib/converter");
5
- var extension_1 = require("./lib/extension");
6
- var convert = converter_1.createConverter();
7
- exports.fromSvelte = function (Component, captured) { return ({
8
- type: 'html',
9
- component: convert(Component, captured)
10
- }); };
11
- exports.createSvelteExtension = extension_1.createExtension;
1
+ import { createConverter } from './esm/converter';
2
+ export function createSvelteConverter() {
3
+ var params = [];
4
+ for (var _i = 0; _i < arguments.length; _i++) {
5
+ params[_i] = arguments[_i];
6
+ }
7
+ var convert = createConverter.apply(void 0, params);
8
+ var Extension = convert.Extension;
9
+ var from = function (Component, captured) { return ({
10
+ type: 'html',
11
+ component: convert(Component, captured),
12
+ }); };
13
+ return { from: from, Extension: Extension };
14
+ }
15
+ var _a = createSvelteConverter(), fromSvelte = _a.from, SvelteExtension = _a.Extension;
16
+ export { fromSvelte, SvelteExtension };
@@ -1,3 +1,13 @@
1
- import { ForeignComponent, BaseComponentProps } from 'piral-core';
2
- import { SvelteModule } from './types';
3
- export declare function createConverter(): <TProps extends BaseComponentProps>(Component: SvelteModule<TProps>, captured?: Record<string, any>) => ForeignComponent<TProps>;
1
+ import type { ForeignComponent, BaseComponentProps } from 'piral-core';
2
+ import type { SvelteModule } from './types';
3
+ export interface SvelteConverterOptions {
4
+ /**
5
+ * Defines the name of the extension component.
6
+ * @default svelte-extension
7
+ */
8
+ selector?: string;
9
+ }
10
+ export declare function createConverter(config?: SvelteConverterOptions): {
11
+ <TProps extends BaseComponentProps>(Component: SvelteModule<TProps>, captured?: Record<string, any>): ForeignComponent<TProps>;
12
+ Extension: string;
13
+ };
package/esm/converter.js CHANGED
@@ -1,30 +1,26 @@
1
- import { __assign } from "tslib";
2
- export function createConverter() {
3
- var convert = function (Component, captured) {
4
- var instance = undefined;
5
- return {
6
- mount: function (parent, data, ctx) {
7
- parent.addEventListener('render-html', function (ev) {
8
- var piral = data.piral;
9
- piral.renderHtmlExtension(ev.detail.target, ev.detail.props);
10
- }, false);
11
- instance = new Component({
12
- target: parent,
13
- props: __assign(__assign(__assign({}, captured), ctx), data),
14
- });
15
- },
16
- update: function (_, data) {
17
- Object.keys(data).forEach(function (key) {
18
- instance[key] = data[key];
19
- });
20
- },
21
- unmount: function (el) {
22
- instance.$destroy();
23
- instance = undefined;
24
- el.innerHTML = '';
25
- },
26
- };
27
- };
1
+ import { createExtension } from './extension';
2
+ export function createConverter(config = {}) {
3
+ const { selector = 'svelte-extension' } = config;
4
+ const Extension = createExtension(selector);
5
+ const convert = (Component, captured) => ({
6
+ mount(parent, data, ctx, locals) {
7
+ locals.instance = new Component({
8
+ target: parent,
9
+ props: Object.assign(Object.assign(Object.assign({}, captured), ctx), data),
10
+ });
11
+ },
12
+ update(el, data, ctx, locals) {
13
+ Object.keys(data).forEach((key) => {
14
+ locals.instance[key] = data[key];
15
+ });
16
+ },
17
+ unmount(el, locals) {
18
+ locals.instance.$destroy();
19
+ locals.instance = undefined;
20
+ el.innerHTML = '';
21
+ },
22
+ });
23
+ convert.Extension = Extension;
28
24
  return convert;
29
25
  }
30
26
  //# sourceMappingURL=converter.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"converter.js","sourceRoot":"","sources":["../src/converter.ts"],"names":[],"mappings":";AAGA,MAAM,UAAU,eAAe;IAC7B,IAAM,OAAO,GAAG,UACd,SAA+B,EAC/B,QAA8B;QAE9B,IAAI,QAAQ,GAAiC,SAAS,CAAC;QAEvD,OAAO;YACL,KAAK,EAAL,UAAM,MAAM,EAAE,IAAI,EAAE,GAAG;gBACrB,MAAM,CAAC,gBAAgB,CACrB,aAAa,EACb,UAAC,EAAe;oBACN,IAAA,KAAK,GAAK,IAAI,MAAT,CAAU;oBACvB,KAAK,CAAC,mBAAmB,CAAC,EAAE,CAAC,MAAM,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;gBAC/D,CAAC,EACD,KAAK,CACN,CAAC;gBACF,QAAQ,GAAG,IAAI,SAAS,CAAC;oBACvB,MAAM,EAAE,MAAM;oBACd,KAAK,iCACA,QAAQ,GACR,GAAG,GACH,IAAI,CACR;iBACF,CAAC,CAAC;YACL,CAAC;YACD,MAAM,YAAC,CAAC,EAAE,IAAI;gBACZ,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,UAAC,GAAG;oBAC5B,QAAQ,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC;gBAC5B,CAAC,CAAC,CAAC;YACL,CAAC;YACD,OAAO,YAAC,EAAE;gBACR,QAAQ,CAAC,QAAQ,EAAE,CAAC;gBACpB,QAAQ,GAAG,SAAS,CAAC;gBACrB,EAAE,CAAC,SAAS,GAAG,EAAE,CAAC;YACpB,CAAC;SACF,CAAC;IACJ,CAAC,CAAC;IACF,OAAO,OAAO,CAAC;AACjB,CAAC"}
1
+ {"version":3,"file":"converter.js","sourceRoot":"","sources":["../src/converter.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAe9C,MAAM,UAAU,eAAe,CAAC,SAAiC,EAAE;IACjE,MAAM,EAAE,QAAQ,GAAG,kBAAkB,EAAE,GAAG,MAAM,CAAC;IACjD,MAAM,SAAS,GAAG,eAAe,CAAC,QAAQ,CAAC,CAAC;IAC5C,MAAM,OAAO,GAAG,CACd,SAA+B,EAC/B,QAA8B,EACJ,EAAE,CAAC,CAAC;QAC9B,KAAK,CAAC,MAAM,EAAE,IAAI,EAAE,GAAG,EAAE,MAAmB;YAC1C,MAAM,CAAC,QAAQ,GAAG,IAAI,SAAS,CAAC;gBAC9B,MAAM,EAAE,MAAM;gBACd,KAAK,gDACA,QAAQ,GACR,GAAG,GACH,IAAI,CACR;aACF,CAAC,CAAC;QACL,CAAC;QACD,MAAM,CAAC,EAAE,EAAE,IAAI,EAAE,GAAG,EAAE,MAAmB;YACvC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE;gBAChC,MAAM,CAAC,QAAQ,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC;YACnC,CAAC,CAAC,CAAC;QACL,CAAC;QACD,OAAO,CAAC,EAAE,EAAE,MAAmB;YAC7B,MAAM,CAAC,QAAQ,CAAC,QAAQ,EAAE,CAAC;YAC3B,MAAM,CAAC,QAAQ,GAAG,SAAS,CAAC;YAC5B,EAAE,CAAC,SAAS,GAAG,EAAE,CAAC;QACpB,CAAC;KACF,CAAC,CAAC;IACH,OAAO,CAAC,SAAS,GAAG,SAAS,CAAC;IAC9B,OAAO,OAAO,CAAC;AACjB,CAAC"}
package/esm/create.d.ts CHANGED
@@ -1,14 +1,10 @@
1
1
  import type { PiralPlugin } from 'piral-core';
2
+ import { SvelteConverterOptions } from './converter';
2
3
  import type { PiletSvelteApi } from './types';
3
4
  /**
4
5
  * Available configuration options for the Svelte plugin.
5
6
  */
6
- export interface SvelteConfig {
7
- /**
8
- * Defines the name of the extension component.
9
- * @default svelte-extension
10
- */
11
- selector?: string;
7
+ export interface SvelteConfig extends SvelteConverterOptions {
12
8
  }
13
9
  /**
14
10
  * Creates new Pilet API extensions for integration of Svelte.
package/esm/create.js CHANGED
@@ -1,27 +1,20 @@
1
1
  import { createConverter } from './converter';
2
- import { createExtension } from './extension';
3
2
  /**
4
3
  * Creates new Pilet API extensions for integration of Svelte.
5
4
  */
6
- export function createSvelteApi(config) {
7
- if (config === void 0) { config = {}; }
8
- var selector = config.selector;
9
- return function (context) {
10
- var convert = createConverter();
11
- context.converters.svelte = function (_a) {
12
- var Component = _a.Component, captured = _a.captured;
13
- return convert(Component, captured);
14
- };
15
- createExtension(selector);
5
+ export function createSvelteApi(config = {}) {
6
+ return (context) => {
7
+ const convert = createConverter(config);
8
+ context.converters.svelte = ({ Component, captured }) => convert(Component, captured);
16
9
  return {
17
- fromSvelte: function (Component, captured) {
10
+ fromSvelte(Component, captured) {
18
11
  return {
19
12
  type: 'svelte',
20
- Component: Component,
21
- captured: captured,
13
+ Component,
14
+ captured,
22
15
  };
23
16
  },
24
- SvelteExtension: selector,
17
+ SvelteExtension: convert.Extension,
25
18
  };
26
19
  };
27
20
  }
package/esm/create.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"create.js","sourceRoot":"","sources":["../src/create.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAC9C,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAc9C;;GAEG;AACH,MAAM,UAAU,eAAe,CAAC,MAAyB;IAAzB,uBAAA,EAAA,WAAyB;IAC/C,IAAA,QAAQ,GAAK,MAAM,SAAX,CAAY;IAE5B,OAAO,UAAC,OAAO;QACb,IAAM,OAAO,GAAG,eAAe,EAAE,CAAC;QAClC,OAAO,CAAC,UAAU,CAAC,MAAM,GAAG,UAAC,EAAuB;gBAArB,SAAS,eAAA,EAAE,QAAQ,cAAA;YAAO,OAAA,OAAO,CAAC,SAAS,EAAE,QAAQ,CAAC;QAA5B,CAA4B,CAAC;QAEtF,eAAe,CAAC,QAAQ,CAAC,CAAC;QAE1B,OAAO;YACL,UAAU,YAAC,SAAS,EAAE,QAAQ;gBAC5B,OAAO;oBACL,IAAI,EAAE,QAAQ;oBACd,SAAS,WAAA;oBACT,QAAQ,UAAA;iBACT,CAAC;YACJ,CAAC;YACD,eAAe,EAAE,QAAQ;SAC1B,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC"}
1
+ {"version":3,"file":"create.js","sourceRoot":"","sources":["../src/create.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAA0B,MAAM,aAAa,CAAC;AAQtE;;GAEG;AACH,MAAM,UAAU,eAAe,CAAC,SAAuB,EAAE;IACvD,OAAO,CAAC,OAAO,EAAE,EAAE;QACjB,MAAM,OAAO,GAAG,eAAe,CAAC,MAAM,CAAC,CAAC;QACxC,OAAO,CAAC,UAAU,CAAC,MAAM,GAAG,CAAC,EAAE,SAAS,EAAE,QAAQ,EAAE,EAAE,EAAE,CAAC,OAAO,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAC;QAEtF,OAAO;YACL,UAAU,CAAC,SAAS,EAAE,QAAQ;gBAC5B,OAAO;oBACL,IAAI,EAAE,QAAQ;oBACd,SAAS;oBACT,QAAQ;iBACT,CAAC;YACJ,CAAC;YACD,eAAe,EAAE,OAAO,CAAC,SAAS;SACnC,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC"}
@@ -1 +1 @@
1
- export declare function createExtension(selector?: string): void;
1
+ export declare function createExtension(selector: string): string;
package/esm/extension.js CHANGED
@@ -1,28 +1,11 @@
1
- import { __extends } from "tslib";
2
1
  export function createExtension(selector) {
3
- if (selector === void 0) { selector = 'svelte-extension'; }
4
- if ('customElements' in window) {
5
- var SvelteExtension = /** @class */ (function (_super) {
6
- __extends(SvelteExtension, _super);
7
- function SvelteExtension() {
8
- return _super !== null && _super.apply(this, arguments) || this;
9
- }
10
- SvelteExtension.prototype.connectedCallback = function () {
11
- if (this.isConnected) {
12
- this.dispatchEvent(new CustomEvent('render-html', {
13
- bubbles: true,
14
- detail: {
15
- target: this,
16
- props: {
17
- name: this.getAttribute('name'),
18
- },
19
- },
20
- }));
21
- }
22
- };
23
- return SvelteExtension;
24
- }(HTMLElement));
25
- customElements.define(selector, SvelteExtension);
2
+ const defaultExtensionSelector = 'piral-extension';
3
+ if ('customElements' in window && selector && selector !== defaultExtensionSelector) {
4
+ const ExtensionBase = customElements.get(defaultExtensionSelector);
5
+ class AliasExtension extends ExtensionBase {
6
+ }
7
+ customElements.define(selector, AliasExtension);
26
8
  }
9
+ return selector || defaultExtensionSelector;
27
10
  }
28
11
  //# sourceMappingURL=extension.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"extension.js","sourceRoot":"","sources":["../src/extension.ts"],"names":[],"mappings":";AAAA,MAAM,UAAU,eAAe,CAAC,QAA6B;IAA7B,yBAAA,EAAA,6BAA6B;IAC3D,IAAI,gBAAgB,IAAI,MAAM,EAAE;QAC9B;YAA8B,mCAAW;YAAzC;;YAgBA,CAAC;YAfC,2CAAiB,GAAjB;gBACE,IAAI,IAAI,CAAC,WAAW,EAAE;oBACpB,IAAI,CAAC,aAAa,CAChB,IAAI,WAAW,CAAC,aAAa,EAAE;wBAC7B,OAAO,EAAE,IAAI;wBACb,MAAM,EAAE;4BACN,MAAM,EAAE,IAAI;4BACZ,KAAK,EAAE;gCACL,IAAI,EAAE,IAAI,CAAC,YAAY,CAAC,MAAM,CAAC;6BAChC;yBACF;qBACF,CAAC,CACH,CAAC;iBACH;YACH,CAAC;YACH,sBAAC;QAAD,CAAC,AAhBD,CAA8B,WAAW,GAgBxC;QAED,cAAc,CAAC,MAAM,CAAC,QAAQ,EAAE,eAAe,CAAC,CAAC;KAClD;AACH,CAAC"}
1
+ {"version":3,"file":"extension.js","sourceRoot":"","sources":["../src/extension.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,eAAe,CAAC,QAAgB;IAC9C,MAAM,wBAAwB,GAAG,iBAAiB,CAAC;IAEnD,IAAI,gBAAgB,IAAI,MAAM,IAAI,QAAQ,IAAI,QAAQ,KAAK,wBAAwB,EAAE;QACnF,MAAM,aAAa,GAAG,cAAc,CAAC,GAAG,CAAC,wBAAwB,CAAC,CAAC;QACnE,MAAM,cAAe,SAAQ,aAAa;SAAG;QAC7C,cAAc,CAAC,MAAM,CAAC,QAAQ,EAAE,cAAc,CAAC,CAAC;KACjD;IAED,OAAO,QAAQ,IAAI,wBAAwB,CAAC;AAC9C,CAAC"}
package/esm/types.d.ts CHANGED
@@ -10,7 +10,7 @@ export interface SvelteOptions<TProps> {
10
10
  target: Element;
11
11
  props: TProps;
12
12
  }
13
- export declare type SvelteComponentInstance<TProps> = TProps & {
13
+ export type SvelteComponentInstance<TProps> = TProps & {
14
14
  $destroy(): void;
15
15
  };
16
16
  export interface SvelteModule<TProps> {
@@ -0,0 +1,72 @@
1
+ const { resolve } = require('path');
2
+
3
+ const svelteLoader = require.resolve('svelte-loader');
4
+
5
+ module.exports =
6
+ (options = {}) =>
7
+ (config) => {
8
+ if (!config.resolve.alias) {
9
+ config.resolve.alias = {};
10
+ }
11
+
12
+ if (!config.resolve.extensions) {
13
+ config.resolve.extensions = ['.mjs', '.js'];
14
+ }
15
+
16
+ if (!config.resolve.mainFields) {
17
+ config.resolve.mainFields = ['browser', 'module', 'main'];
18
+ }
19
+
20
+ if (!config.resolve.conditionNames) {
21
+ config.resolve.conditionNames = ['import', 'module', 'require', 'node'];
22
+ }
23
+
24
+ config.resolve.alias.svelte = resolve('node_modules', 'svelte');
25
+ config.resolve.extensions.push('.svelte');
26
+ config.resolve.mainFields.unshift('svelte');
27
+ config.resolve.conditionNames.push('svelte');
28
+
29
+ function findRule(tester, changer) {
30
+ config.module.rules.forEach((rule) => {
31
+ if (rule.oneOf) {
32
+ rule.oneOf.forEach((r) => {
33
+ if (r.test && tester(r)) {
34
+ changer(r, rule.oneOf);
35
+ }
36
+ });
37
+ } else if (rule.test && tester(rule)) {
38
+ changer(rule, config.module.rules);
39
+ }
40
+ });
41
+ }
42
+
43
+ findRule(
44
+ (m) => m.test && m.test.toString() === /\.tsx?$/i.toString(),
45
+ (m, all) => {
46
+ const ruleIndex = all.indexOf(m);
47
+
48
+ all.splice(
49
+ ruleIndex,
50
+ 0,
51
+ {
52
+ test: /\.(html|svelte)$/,
53
+ use: {
54
+ loader: svelteLoader,
55
+ options: {
56
+ emitCss: true,
57
+ ...options,
58
+ },
59
+ },
60
+ },
61
+ {
62
+ test: /node_modules\/svelte\/.*\.mjs$/,
63
+ resolve: {
64
+ fullySpecified: false,
65
+ },
66
+ },
67
+ );
68
+ },
69
+ );
70
+
71
+ return config;
72
+ };
@@ -1,3 +1,13 @@
1
- import { ForeignComponent, BaseComponentProps } from 'piral-core';
2
- import { SvelteModule } from './types';
3
- export declare function createConverter(): <TProps extends BaseComponentProps>(Component: SvelteModule<TProps>, captured?: Record<string, any>) => ForeignComponent<TProps>;
1
+ import type { ForeignComponent, BaseComponentProps } from 'piral-core';
2
+ import type { SvelteModule } from './types';
3
+ export interface SvelteConverterOptions {
4
+ /**
5
+ * Defines the name of the extension component.
6
+ * @default svelte-extension
7
+ */
8
+ selector?: string;
9
+ }
10
+ export declare function createConverter(config?: SvelteConverterOptions): {
11
+ <TProps extends BaseComponentProps>(Component: SvelteModule<TProps>, captured?: Record<string, any>): ForeignComponent<TProps>;
12
+ Extension: string;
13
+ };
package/lib/converter.js CHANGED
@@ -1,33 +1,29 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createConverter = void 0;
4
- var tslib_1 = require("tslib");
5
- function createConverter() {
6
- var convert = function (Component, captured) {
7
- var instance = undefined;
8
- return {
9
- mount: function (parent, data, ctx) {
10
- parent.addEventListener('render-html', function (ev) {
11
- var piral = data.piral;
12
- piral.renderHtmlExtension(ev.detail.target, ev.detail.props);
13
- }, false);
14
- instance = new Component({
15
- target: parent,
16
- props: tslib_1.__assign(tslib_1.__assign(tslib_1.__assign({}, captured), ctx), data),
17
- });
18
- },
19
- update: function (_, data) {
20
- Object.keys(data).forEach(function (key) {
21
- instance[key] = data[key];
22
- });
23
- },
24
- unmount: function (el) {
25
- instance.$destroy();
26
- instance = undefined;
27
- el.innerHTML = '';
28
- },
29
- };
30
- };
4
+ const extension_1 = require("./extension");
5
+ function createConverter(config = {}) {
6
+ const { selector = 'svelte-extension' } = config;
7
+ const Extension = (0, extension_1.createExtension)(selector);
8
+ const convert = (Component, captured) => ({
9
+ mount(parent, data, ctx, locals) {
10
+ locals.instance = new Component({
11
+ target: parent,
12
+ props: Object.assign(Object.assign(Object.assign({}, captured), ctx), data),
13
+ });
14
+ },
15
+ update(el, data, ctx, locals) {
16
+ Object.keys(data).forEach((key) => {
17
+ locals.instance[key] = data[key];
18
+ });
19
+ },
20
+ unmount(el, locals) {
21
+ locals.instance.$destroy();
22
+ locals.instance = undefined;
23
+ el.innerHTML = '';
24
+ },
25
+ });
26
+ convert.Extension = Extension;
31
27
  return convert;
32
28
  }
33
29
  exports.createConverter = createConverter;
@@ -1 +1 @@
1
- {"version":3,"file":"converter.js","sourceRoot":"","sources":["../src/converter.ts"],"names":[],"mappings":";;;;AAGA,SAAgB,eAAe;IAC7B,IAAM,OAAO,GAAG,UACd,SAA+B,EAC/B,QAA8B;QAE9B,IAAI,QAAQ,GAAiC,SAAS,CAAC;QAEvD,OAAO;YACL,KAAK,EAAL,UAAM,MAAM,EAAE,IAAI,EAAE,GAAG;gBACrB,MAAM,CAAC,gBAAgB,CACrB,aAAa,EACb,UAAC,EAAe;oBACN,IAAA,KAAK,GAAK,IAAI,MAAT,CAAU;oBACvB,KAAK,CAAC,mBAAmB,CAAC,EAAE,CAAC,MAAM,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;gBAC/D,CAAC,EACD,KAAK,CACN,CAAC;gBACF,QAAQ,GAAG,IAAI,SAAS,CAAC;oBACvB,MAAM,EAAE,MAAM;oBACd,KAAK,yDACA,QAAQ,GACR,GAAG,GACH,IAAI,CACR;iBACF,CAAC,CAAC;YACL,CAAC;YACD,MAAM,YAAC,CAAC,EAAE,IAAI;gBACZ,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,UAAC,GAAG;oBAC5B,QAAQ,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC;gBAC5B,CAAC,CAAC,CAAC;YACL,CAAC;YACD,OAAO,YAAC,EAAE;gBACR,QAAQ,CAAC,QAAQ,EAAE,CAAC;gBACpB,QAAQ,GAAG,SAAS,CAAC;gBACrB,EAAE,CAAC,SAAS,GAAG,EAAE,CAAC;YACpB,CAAC;SACF,CAAC;IACJ,CAAC,CAAC;IACF,OAAO,OAAO,CAAC;AACjB,CAAC;AAvCD,0CAuCC"}
1
+ {"version":3,"file":"converter.js","sourceRoot":"","sources":["../src/converter.ts"],"names":[],"mappings":";;;AACA,2CAA8C;AAe9C,SAAgB,eAAe,CAAC,SAAiC,EAAE;IACjE,MAAM,EAAE,QAAQ,GAAG,kBAAkB,EAAE,GAAG,MAAM,CAAC;IACjD,MAAM,SAAS,GAAG,IAAA,2BAAe,EAAC,QAAQ,CAAC,CAAC;IAC5C,MAAM,OAAO,GAAG,CACd,SAA+B,EAC/B,QAA8B,EACJ,EAAE,CAAC,CAAC;QAC9B,KAAK,CAAC,MAAM,EAAE,IAAI,EAAE,GAAG,EAAE,MAAmB;YAC1C,MAAM,CAAC,QAAQ,GAAG,IAAI,SAAS,CAAC;gBAC9B,MAAM,EAAE,MAAM;gBACd,KAAK,gDACA,QAAQ,GACR,GAAG,GACH,IAAI,CACR;aACF,CAAC,CAAC;QACL,CAAC;QACD,MAAM,CAAC,EAAE,EAAE,IAAI,EAAE,GAAG,EAAE,MAAmB;YACvC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE;gBAChC,MAAM,CAAC,QAAQ,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC;YACnC,CAAC,CAAC,CAAC;QACL,CAAC;QACD,OAAO,CAAC,EAAE,EAAE,MAAmB;YAC7B,MAAM,CAAC,QAAQ,CAAC,QAAQ,EAAE,CAAC;YAC3B,MAAM,CAAC,QAAQ,GAAG,SAAS,CAAC;YAC5B,EAAE,CAAC,SAAS,GAAG,EAAE,CAAC;QACpB,CAAC;KACF,CAAC,CAAC;IACH,OAAO,CAAC,SAAS,GAAG,SAAS,CAAC;IAC9B,OAAO,OAAO,CAAC;AACjB,CAAC;AA9BD,0CA8BC"}
package/lib/create.d.ts CHANGED
@@ -1,14 +1,10 @@
1
1
  import type { PiralPlugin } from 'piral-core';
2
+ import { SvelteConverterOptions } from './converter';
2
3
  import type { PiletSvelteApi } from './types';
3
4
  /**
4
5
  * Available configuration options for the Svelte plugin.
5
6
  */
6
- export interface SvelteConfig {
7
- /**
8
- * Defines the name of the extension component.
9
- * @default svelte-extension
10
- */
11
- selector?: string;
7
+ export interface SvelteConfig extends SvelteConverterOptions {
12
8
  }
13
9
  /**
14
10
  * Creates new Pilet API extensions for integration of Svelte.
package/lib/create.js CHANGED
@@ -1,30 +1,23 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createSvelteApi = void 0;
4
- var converter_1 = require("./converter");
5
- var extension_1 = require("./extension");
4
+ const converter_1 = require("./converter");
6
5
  /**
7
6
  * Creates new Pilet API extensions for integration of Svelte.
8
7
  */
9
- function createSvelteApi(config) {
10
- if (config === void 0) { config = {}; }
11
- var selector = config.selector;
12
- return function (context) {
13
- var convert = converter_1.createConverter();
14
- context.converters.svelte = function (_a) {
15
- var Component = _a.Component, captured = _a.captured;
16
- return convert(Component, captured);
17
- };
18
- extension_1.createExtension(selector);
8
+ function createSvelteApi(config = {}) {
9
+ return (context) => {
10
+ const convert = (0, converter_1.createConverter)(config);
11
+ context.converters.svelte = ({ Component, captured }) => convert(Component, captured);
19
12
  return {
20
- fromSvelte: function (Component, captured) {
13
+ fromSvelte(Component, captured) {
21
14
  return {
22
15
  type: 'svelte',
23
- Component: Component,
24
- captured: captured,
16
+ Component,
17
+ captured,
25
18
  };
26
19
  },
27
- SvelteExtension: selector,
20
+ SvelteExtension: convert.Extension,
28
21
  };
29
22
  };
30
23
  }
package/lib/create.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"create.js","sourceRoot":"","sources":["../src/create.ts"],"names":[],"mappings":";;;AACA,yCAA8C;AAC9C,yCAA8C;AAc9C;;GAEG;AACH,SAAgB,eAAe,CAAC,MAAyB;IAAzB,uBAAA,EAAA,WAAyB;IAC/C,IAAA,QAAQ,GAAK,MAAM,SAAX,CAAY;IAE5B,OAAO,UAAC,OAAO;QACb,IAAM,OAAO,GAAG,2BAAe,EAAE,CAAC;QAClC,OAAO,CAAC,UAAU,CAAC,MAAM,GAAG,UAAC,EAAuB;gBAArB,SAAS,eAAA,EAAE,QAAQ,cAAA;YAAO,OAAA,OAAO,CAAC,SAAS,EAAE,QAAQ,CAAC;QAA5B,CAA4B,CAAC;QAEtF,2BAAe,CAAC,QAAQ,CAAC,CAAC;QAE1B,OAAO;YACL,UAAU,YAAC,SAAS,EAAE,QAAQ;gBAC5B,OAAO;oBACL,IAAI,EAAE,QAAQ;oBACd,SAAS,WAAA;oBACT,QAAQ,UAAA;iBACT,CAAC;YACJ,CAAC;YACD,eAAe,EAAE,QAAQ;SAC1B,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC;AApBD,0CAoBC"}
1
+ {"version":3,"file":"create.js","sourceRoot":"","sources":["../src/create.ts"],"names":[],"mappings":";;;AACA,2CAAsE;AAQtE;;GAEG;AACH,SAAgB,eAAe,CAAC,SAAuB,EAAE;IACvD,OAAO,CAAC,OAAO,EAAE,EAAE;QACjB,MAAM,OAAO,GAAG,IAAA,2BAAe,EAAC,MAAM,CAAC,CAAC;QACxC,OAAO,CAAC,UAAU,CAAC,MAAM,GAAG,CAAC,EAAE,SAAS,EAAE,QAAQ,EAAE,EAAE,EAAE,CAAC,OAAO,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAC;QAEtF,OAAO;YACL,UAAU,CAAC,SAAS,EAAE,QAAQ;gBAC5B,OAAO;oBACL,IAAI,EAAE,QAAQ;oBACd,SAAS;oBACT,QAAQ;iBACT,CAAC;YACJ,CAAC;YACD,eAAe,EAAE,OAAO,CAAC,SAAS;SACnC,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC;AAhBD,0CAgBC"}
@@ -1 +1 @@
1
- export declare function createExtension(selector?: string): void;
1
+ export declare function createExtension(selector: string): string;
package/lib/extension.js CHANGED
@@ -1,32 +1,15 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createExtension = void 0;
4
- var tslib_1 = require("tslib");
5
4
  function createExtension(selector) {
6
- if (selector === void 0) { selector = 'svelte-extension'; }
7
- if ('customElements' in window) {
8
- var SvelteExtension = /** @class */ (function (_super) {
9
- tslib_1.__extends(SvelteExtension, _super);
10
- function SvelteExtension() {
11
- return _super !== null && _super.apply(this, arguments) || this;
12
- }
13
- SvelteExtension.prototype.connectedCallback = function () {
14
- if (this.isConnected) {
15
- this.dispatchEvent(new CustomEvent('render-html', {
16
- bubbles: true,
17
- detail: {
18
- target: this,
19
- props: {
20
- name: this.getAttribute('name'),
21
- },
22
- },
23
- }));
24
- }
25
- };
26
- return SvelteExtension;
27
- }(HTMLElement));
28
- customElements.define(selector, SvelteExtension);
5
+ const defaultExtensionSelector = 'piral-extension';
6
+ if ('customElements' in window && selector && selector !== defaultExtensionSelector) {
7
+ const ExtensionBase = customElements.get(defaultExtensionSelector);
8
+ class AliasExtension extends ExtensionBase {
9
+ }
10
+ customElements.define(selector, AliasExtension);
29
11
  }
12
+ return selector || defaultExtensionSelector;
30
13
  }
31
14
  exports.createExtension = createExtension;
32
15
  //# sourceMappingURL=extension.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"extension.js","sourceRoot":"","sources":["../src/extension.ts"],"names":[],"mappings":";;;;AAAA,SAAgB,eAAe,CAAC,QAA6B;IAA7B,yBAAA,EAAA,6BAA6B;IAC3D,IAAI,gBAAgB,IAAI,MAAM,EAAE;QAC9B;YAA8B,2CAAW;YAAzC;;YAgBA,CAAC;YAfC,2CAAiB,GAAjB;gBACE,IAAI,IAAI,CAAC,WAAW,EAAE;oBACpB,IAAI,CAAC,aAAa,CAChB,IAAI,WAAW,CAAC,aAAa,EAAE;wBAC7B,OAAO,EAAE,IAAI;wBACb,MAAM,EAAE;4BACN,MAAM,EAAE,IAAI;4BACZ,KAAK,EAAE;gCACL,IAAI,EAAE,IAAI,CAAC,YAAY,CAAC,MAAM,CAAC;6BAChC;yBACF;qBACF,CAAC,CACH,CAAC;iBACH;YACH,CAAC;YACH,sBAAC;QAAD,CAAC,AAhBD,CAA8B,WAAW,GAgBxC;QAED,cAAc,CAAC,MAAM,CAAC,QAAQ,EAAE,eAAe,CAAC,CAAC;KAClD;AACH,CAAC;AAtBD,0CAsBC"}
1
+ {"version":3,"file":"extension.js","sourceRoot":"","sources":["../src/extension.ts"],"names":[],"mappings":";;;AAAA,SAAgB,eAAe,CAAC,QAAgB;IAC9C,MAAM,wBAAwB,GAAG,iBAAiB,CAAC;IAEnD,IAAI,gBAAgB,IAAI,MAAM,IAAI,QAAQ,IAAI,QAAQ,KAAK,wBAAwB,EAAE;QACnF,MAAM,aAAa,GAAG,cAAc,CAAC,GAAG,CAAC,wBAAwB,CAAC,CAAC;QACnE,MAAM,cAAe,SAAQ,aAAa;SAAG;QAC7C,cAAc,CAAC,MAAM,CAAC,QAAQ,EAAE,cAAc,CAAC,CAAC;KACjD;IAED,OAAO,QAAQ,IAAI,wBAAwB,CAAC;AAC9C,CAAC;AAVD,0CAUC"}
package/lib/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- var tslib_1 = require("tslib");
3
+ const tslib_1 = require("tslib");
4
4
  tslib_1.__exportStar(require("./create"), exports);
5
5
  tslib_1.__exportStar(require("./types"), exports);
6
6
  //# sourceMappingURL=index.js.map
package/lib/types.d.ts CHANGED
@@ -10,7 +10,7 @@ export interface SvelteOptions<TProps> {
10
10
  target: Element;
11
11
  props: TProps;
12
12
  }
13
- export declare type SvelteComponentInstance<TProps> = TProps & {
13
+ export type SvelteComponentInstance<TProps> = TProps & {
14
14
  $destroy(): void;
15
15
  };
16
16
  export interface SvelteModule<TProps> {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "piral-svelte",
3
- "version": "1.0.0-pre.2137",
3
+ "version": "1.0.1-beta.5640",
4
4
  "description": "Plugin for integrating Svelte components in Piral.",
5
5
  "keywords": [
6
6
  "piral",
@@ -19,14 +19,37 @@
19
19
  "module": "esm/index.js",
20
20
  "main": "lib/index.js",
21
21
  "typings": "lib/index.d.ts",
22
+ "exports": {
23
+ ".": {
24
+ "import": "./esm/index.js",
25
+ "require": "./lib/index.js"
26
+ },
27
+ "./convert": {
28
+ "import": "./convert.js"
29
+ },
30
+ "./extend-webpack": {
31
+ "require": "./extend-webpack.js"
32
+ },
33
+ "./esm/*": {
34
+ "import": "./esm/*"
35
+ },
36
+ "./lib/*": {
37
+ "require": "./lib/*"
38
+ },
39
+ "./_/*": {
40
+ "import": "./esm/*.js",
41
+ "require": "./lib/*.js"
42
+ },
43
+ "./package.json": "./package.json"
44
+ },
22
45
  "sideEffects": false,
23
46
  "files": [
24
47
  "esm",
25
48
  "lib",
26
49
  "src",
27
50
  "convert.d.ts",
28
- "convert.ts",
29
- "convert.js"
51
+ "convert.js",
52
+ "extend-webpack.js"
30
53
  ],
31
54
  "repository": {
32
55
  "type": "git",
@@ -36,18 +59,16 @@
36
59
  "url": "https://github.com/smapiot/piral/issues"
37
60
  },
38
61
  "scripts": {
62
+ "cleanup": "rimraf esm lib convert.d.ts convert.js",
39
63
  "build": "yarn build:commonjs && yarn build:esnext && yarn build:convert",
40
- "build:convert": "tsc convert.ts --skipLibCheck --declaration",
64
+ "build:convert": "tsc convert.ts --skipLibCheck --declaration --module esnext",
41
65
  "build:commonjs": "tsc --project tsconfig.json --outDir lib --module commonjs",
42
66
  "build:esnext": "tsc --project tsconfig.json --outDir esm --module esnext",
43
67
  "typedoc": "typedoc --json ../../../docs/types/piral-svelte.json src --exclude \"src/**/*.test.*\"",
44
68
  "test": "echo \"Error: run tests from root\" && exit 1"
45
69
  },
46
70
  "devDependencies": {
47
- "piral-core": "^1.0.0-pre.2137"
48
- },
49
- "peerDependencies": {
50
- "piral-core": "1.x"
71
+ "piral-core": "1.0.1-beta.5640"
51
72
  },
52
- "gitHead": "bebf402e81ae0511ff22439a5670d04bf5626b83"
73
+ "gitHead": "fa0a72b28fd0a20afec7ef491ec19e93c090fc72"
53
74
  }
package/src/converter.ts CHANGED
@@ -1,43 +1,47 @@
1
- import { ForeignComponent, BaseComponentProps } from 'piral-core';
2
- import { SvelteComponentInstance, SvelteModule } from './types';
1
+ import type { ForeignComponent, BaseComponentProps } from 'piral-core';
2
+ import { createExtension } from './extension';
3
+ import type { SvelteComponentInstance, SvelteModule } from './types';
3
4
 
4
- export function createConverter() {
5
+ export interface SvelteConverterOptions {
6
+ /**
7
+ * Defines the name of the extension component.
8
+ * @default svelte-extension
9
+ */
10
+ selector?: string;
11
+ }
12
+
13
+ interface SvelteState {
14
+ instance: SvelteComponentInstance<any>;
15
+ }
16
+
17
+ export function createConverter(config: SvelteConverterOptions = {}) {
18
+ const { selector = 'svelte-extension' } = config;
19
+ const Extension = createExtension(selector);
5
20
  const convert = <TProps extends BaseComponentProps>(
6
21
  Component: SvelteModule<TProps>,
7
22
  captured?: Record<string, any>,
8
- ): ForeignComponent<TProps> => {
9
- let instance: SvelteComponentInstance<any> = undefined;
10
-
11
- return {
12
- mount(parent, data, ctx) {
13
- parent.addEventListener(
14
- 'render-html',
15
- (ev: CustomEvent) => {
16
- const { piral } = data;
17
- piral.renderHtmlExtension(ev.detail.target, ev.detail.props);
18
- },
19
- false,
20
- );
21
- instance = new Component({
22
- target: parent,
23
- props: {
24
- ...captured,
25
- ...ctx,
26
- ...data,
27
- },
28
- });
29
- },
30
- update(_, data) {
31
- Object.keys(data).forEach((key) => {
32
- instance[key] = data[key];
33
- });
34
- },
35
- unmount(el) {
36
- instance.$destroy();
37
- instance = undefined;
38
- el.innerHTML = '';
39
- },
40
- };
41
- };
23
+ ): ForeignComponent<TProps> => ({
24
+ mount(parent, data, ctx, locals: SvelteState) {
25
+ locals.instance = new Component({
26
+ target: parent,
27
+ props: {
28
+ ...captured,
29
+ ...ctx,
30
+ ...data,
31
+ },
32
+ });
33
+ },
34
+ update(el, data, ctx, locals: SvelteState) {
35
+ Object.keys(data).forEach((key) => {
36
+ locals.instance[key] = data[key];
37
+ });
38
+ },
39
+ unmount(el, locals: SvelteState) {
40
+ locals.instance.$destroy();
41
+ locals.instance = undefined;
42
+ el.innerHTML = '';
43
+ },
44
+ });
45
+ convert.Extension = Extension;
42
46
  return convert;
43
47
  }
package/src/create.ts CHANGED
@@ -1,31 +1,20 @@
1
1
  import type { PiralPlugin } from 'piral-core';
2
- import { createConverter } from './converter';
3
- import { createExtension } from './extension';
2
+ import { createConverter, SvelteConverterOptions } from './converter';
4
3
  import type { PiletSvelteApi } from './types';
5
4
 
6
5
  /**
7
6
  * Available configuration options for the Svelte plugin.
8
7
  */
9
- export interface SvelteConfig {
10
- /**
11
- * Defines the name of the extension component.
12
- * @default svelte-extension
13
- */
14
- selector?: string;
15
- }
8
+ export interface SvelteConfig extends SvelteConverterOptions {}
16
9
 
17
10
  /**
18
11
  * Creates new Pilet API extensions for integration of Svelte.
19
12
  */
20
13
  export function createSvelteApi(config: SvelteConfig = {}): PiralPlugin<PiletSvelteApi> {
21
- const { selector } = config;
22
-
23
14
  return (context) => {
24
- const convert = createConverter();
15
+ const convert = createConverter(config);
25
16
  context.converters.svelte = ({ Component, captured }) => convert(Component, captured);
26
17
 
27
- createExtension(selector);
28
-
29
18
  return {
30
19
  fromSvelte(Component, captured) {
31
20
  return {
@@ -34,7 +23,7 @@ export function createSvelteApi(config: SvelteConfig = {}): PiralPlugin<PiletSve
34
23
  captured,
35
24
  };
36
25
  },
37
- SvelteExtension: selector,
26
+ SvelteExtension: convert.Extension,
38
27
  };
39
28
  };
40
29
  }
package/src/extension.ts CHANGED
@@ -1,23 +1,11 @@
1
- export function createExtension(selector = 'svelte-extension') {
2
- if ('customElements' in window) {
3
- class SvelteExtension extends HTMLElement {
4
- connectedCallback() {
5
- if (this.isConnected) {
6
- this.dispatchEvent(
7
- new CustomEvent('render-html', {
8
- bubbles: true,
9
- detail: {
10
- target: this,
11
- props: {
12
- name: this.getAttribute('name'),
13
- },
14
- },
15
- }),
16
- );
17
- }
18
- }
19
- }
1
+ export function createExtension(selector: string) {
2
+ const defaultExtensionSelector = 'piral-extension';
20
3
 
21
- customElements.define(selector, SvelteExtension);
4
+ if ('customElements' in window && selector && selector !== defaultExtensionSelector) {
5
+ const ExtensionBase = customElements.get(defaultExtensionSelector);
6
+ class AliasExtension extends ExtensionBase {}
7
+ customElements.define(selector, AliasExtension);
22
8
  }
9
+
10
+ return selector || defaultExtensionSelector;
23
11
  }
package/convert.ts DELETED
@@ -1,16 +0,0 @@
1
- import type { HtmlComponent } from 'piral-core';
2
- import { createConverter } from './lib/converter';
3
- import { createExtension } from './lib/extension';
4
-
5
- const convert = createConverter();
6
-
7
- export interface SvelteConverter {
8
- (...params: Parameters<typeof convert>): HtmlComponent<any>;
9
- }
10
-
11
- export const fromSvelte: SvelteConverter = (Component, captured) => ({
12
- type: 'html',
13
- component: convert(Component, captured),
14
- });
15
-
16
- export const createSvelteExtension = createExtension;