@madgex/design-system-ce 5.1.0-alpha.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,332 @@
1
+ /* eslint-disable no-cond-assign */
2
+ /* eslint-disable no-param-reassign */
3
+ /* eslint-disable promise/catch-or-return */
4
+ /* eslint-disable guard-for-in */
5
+ /* eslint-disable no-nested-ternary */
6
+ /* eslint-disable no-restricted-syntax */
7
+ /* eslint-disable no-undef */
8
+ /* eslint-disable no-underscore-dangle */
9
+ /* eslint-disable import/no-extraneous-dependencies */
10
+ /* eslint-disable max-classes-per-file */
11
+ import { createVNode, defineComponent, nextTick, warn } from '@vue/runtime-core';
12
+ import { camelize, extend, hyphenate, isArray, toNumber } from '@vue/shared';
13
+ import HTMLParsedElement from 'html-parsed-element';
14
+ import { hydrate, render } from '@vue/runtime-dom';
15
+
16
+ const BaseClass = typeof HTMLElement !== 'undefined' ? HTMLParsedElement : class {};
17
+
18
+ const __DEV__ = process.env.NODE_ENV !== 'production';
19
+
20
+ export class VueElement extends BaseClass {
21
+ constructor(_def, _props = {}, _config = {}, _hydrate) {
22
+ super();
23
+ this._def = _def;
24
+ this._props = _props;
25
+ this._config = _config;
26
+ /**
27
+ * @internal
28
+ */
29
+ this._instance = null;
30
+ this._connected = false;
31
+ this._resolved = false;
32
+ this._numberProps = null;
33
+ this._config = extend(
34
+ {
35
+ shadowRoot: true,
36
+ },
37
+ this._config
38
+ );
39
+ if (this._config.shadowRoot) {
40
+ if (this.shadowRoot && _hydrate) {
41
+ _hydrate(this._createVNode(), this._root);
42
+ } else {
43
+ if (__DEV__ && this.shadowRoot) {
44
+ warn(
45
+ `Custom element has pre-rendered declarative shadow root but is not ` +
46
+ `defined as hydratable. Use \`defineSSRCustomElement\`.`
47
+ );
48
+ }
49
+
50
+ this.attachShadow({ mode: 'open' });
51
+ }
52
+ } else if (_hydrate) {
53
+ _hydrate(this._createVNode(), this._root);
54
+ }
55
+ }
56
+
57
+ get _root() {
58
+ return this._config.shadowRoot ? this.shadowRoot : this;
59
+ }
60
+
61
+ connectedCallback() {
62
+ if (this._config.shadowRoot) {
63
+ this._connect();
64
+ } else {
65
+ super.connectedCallback();
66
+ }
67
+ }
68
+
69
+ // use of parsedCallback when shadowRoot is disabled
70
+ // to wait for slots to be parsed
71
+ // see https://stackoverflow.com/a/52884370
72
+ parsedCallback() {
73
+ if (!this._config.shadowRoot) {
74
+ this._connect();
75
+ }
76
+ }
77
+
78
+ _connect() {
79
+ this._connected = true;
80
+ if (!this._instance) {
81
+ this._resolveDef();
82
+ }
83
+ }
84
+
85
+ disconnectedCallback() {
86
+ this._connected = false;
87
+ nextTick(() => {
88
+ if (!this._connected) {
89
+ render(null, this._root);
90
+ this._instance = null;
91
+ }
92
+ });
93
+ }
94
+
95
+ /**
96
+ * resolve inner component definition (handle possible async component)
97
+ */
98
+ _resolveDef() {
99
+ if (this._resolved) {
100
+ return;
101
+ }
102
+
103
+ this._resolved = true;
104
+ // set initial attrs
105
+ for (let i = 0; i < this.attributes.length; i++) {
106
+ this._setAttr(this.attributes[i].name);
107
+ }
108
+
109
+ // watch future attr changes
110
+ new MutationObserver((mutations) => {
111
+ for (const m of mutations) {
112
+ this._setAttr(m.attributeName);
113
+ }
114
+ }).observe(this, { attributes: true });
115
+ const resolve = (def) => {
116
+ const { props, styles } = def;
117
+ const hasOptions = !isArray(props);
118
+ const rawKeys = props ? (hasOptions ? Object.keys(props) : props) : [];
119
+ // cast Number-type props set before resolve
120
+ let numberProps;
121
+
122
+ // add props check to fix https://github.com/vuejs/core/issues/5326
123
+ if (hasOptions && props) {
124
+ for (const key in this._props) {
125
+ const opt = props[key];
126
+
127
+ if (opt === Number || (opt && opt.type === Number)) {
128
+ this._props[key] = toNumber(this._props[key]);
129
+ (numberProps || (numberProps = Object.create(null)))[key] = true;
130
+ }
131
+ }
132
+ }
133
+
134
+ this._numberProps = numberProps;
135
+ // check if there are props set pre-upgrade or connect
136
+ for (const key of Object.keys(this)) {
137
+ if (key[0] !== '_') {
138
+ this._setProp(key, this[key], true, false);
139
+ }
140
+ }
141
+
142
+ // defining getter/setters on prototype
143
+ for (const key of rawKeys.map(camelize)) {
144
+ Object.defineProperty(this, key, {
145
+ get() {
146
+ return this._getProp(key);
147
+ },
148
+ set(val) {
149
+ this._setProp(key, val);
150
+ },
151
+ });
152
+ }
153
+
154
+ // replace slot
155
+ if (!this._config.shadowRoot) {
156
+ this._slots = Array.from(this.children).map((n) => n.cloneNode(true));
157
+ this.replaceChildren();
158
+ }
159
+
160
+ // apply CSS
161
+ this._applyStyles(styles);
162
+ // initial render
163
+ this._update();
164
+ };
165
+ const asyncDef = this._def.__asyncLoader;
166
+
167
+ if (asyncDef) {
168
+ asyncDef().then(resolve);
169
+ } else {
170
+ resolve(this._def);
171
+ }
172
+ }
173
+
174
+ _setAttr(key) {
175
+ let value = this.getAttribute(key);
176
+
177
+ if (this._numberProps && this._numberProps[key]) {
178
+ value = toNumber(value);
179
+ }
180
+
181
+ this._setProp(camelize(key), value, false);
182
+ }
183
+
184
+ /**
185
+ * @internal
186
+ */
187
+ _getProp(key) {
188
+ return this._props[key];
189
+ }
190
+
191
+ /**
192
+ * @internal
193
+ */
194
+ _setProp(key, val, shouldReflect = true, shouldUpdate = true) {
195
+ if (val !== this._props[key]) {
196
+ this._props[key] = val;
197
+ if (shouldUpdate && this._instance) {
198
+ this._update();
199
+ }
200
+
201
+ // reflect
202
+ if (shouldReflect) {
203
+ if (val === true) {
204
+ this.setAttribute(hyphenate(key), '');
205
+ } else if (typeof val === 'string' || typeof val === 'number') {
206
+ this.setAttribute(hyphenate(key), `${val}`);
207
+ } else if (!val) {
208
+ this.removeAttribute(hyphenate(key));
209
+ }
210
+ }
211
+ }
212
+ }
213
+
214
+ _update() {
215
+ render(this._createVNode(), this._root);
216
+ }
217
+
218
+ _createVNode() {
219
+ let childs = null;
220
+
221
+ // web components without shadow DOM do not supports native slot
222
+ // so, we create a VNode based on the content of child nodes.
223
+ // NB: named slots are currently not supported
224
+ if (!this._config.shadowRoot) {
225
+ childs = () => {
226
+ const toObj = (a) => {
227
+ const res = {};
228
+
229
+ for (let i = 0, l = a.length; i < l; i++) {
230
+ const attr = a[i];
231
+
232
+ res[attr.nodeName] = attr.nodeValue;
233
+ }
234
+
235
+ return res;
236
+ };
237
+
238
+ return this._slots.map((n) => {
239
+ const attrs = n.attributes ? toObj(n.attributes) : {};
240
+
241
+ attrs.innerHTML = n.innerHTML;
242
+ return createVNode(n.tagName, attrs, null);
243
+ });
244
+ };
245
+ }
246
+
247
+ const vnode = createVNode(this._def, extend({}, this._props), childs);
248
+
249
+ if (!this._instance) {
250
+ vnode.ce = (instance) => {
251
+ this._instance = instance;
252
+ if (this._config.shadowRoot) {
253
+ instance.isCE = true;
254
+ }
255
+
256
+ // HMR
257
+ if (__DEV__) {
258
+ instance.ceReload = (newStyles) => {
259
+ // always reset styles
260
+ if (this._styles) {
261
+ this._styles.forEach((s) => this._root.removeChild(s));
262
+ this._styles.length = 0;
263
+ }
264
+
265
+ this._applyStyles(newStyles);
266
+ // if this is an async component, ceReload is called from the inner
267
+ // component so no need to reload the async wrapper
268
+ if (!this._def.__asyncLoader) {
269
+ // reload
270
+ this._instance = null;
271
+ this._update();
272
+ }
273
+ };
274
+ }
275
+
276
+ // intercept emit
277
+ instance.emit = (event, ...args) => {
278
+ this.dispatchEvent(
279
+ new CustomEvent(event, {
280
+ detail: args,
281
+ })
282
+ );
283
+ };
284
+
285
+ // locate nearest Vue custom element parent for provide/inject
286
+ let parent = this;
287
+
288
+ while ((parent = parent && (parent.parentNode || parent.host))) {
289
+ if (parent instanceof VueElement) {
290
+ instance.parent = parent._instance;
291
+ break;
292
+ }
293
+ }
294
+ };
295
+ }
296
+
297
+ return vnode;
298
+ }
299
+
300
+ _applyStyles(styles) {
301
+ if (styles) {
302
+ styles.forEach((css) => {
303
+ const s = document.createElement('style');
304
+
305
+ s.textContent = css;
306
+ this._root.appendChild(s);
307
+ // record for HMR
308
+ if (__DEV__) {
309
+ (this._styles || (this._styles = [])).push(s);
310
+ }
311
+ });
312
+ }
313
+ }
314
+ }
315
+
316
+ export function defineCustomElement(options, config, hydrate_) {
317
+ const Comp = defineComponent(options);
318
+
319
+ class VueCustomElement extends VueElement {
320
+ constructor(initialProps) {
321
+ super(Comp, initialProps, config, hydrate_);
322
+ }
323
+ }
324
+
325
+ VueCustomElement.def = Comp;
326
+ return VueCustomElement;
327
+ }
328
+
329
+ export const defineSSRCustomElement = (options, config) => {
330
+ // @ts-expect-error
331
+ return defineCustomElement(options, config, hydrate);
332
+ };
package/vite.config.js ADDED
@@ -0,0 +1,46 @@
1
+ /* eslint-disable import/no-extraneous-dependencies */
2
+ import path from 'path';
3
+ import { defineConfig } from 'vite';
4
+ import vue from '@vitejs/plugin-vue';
5
+ import legacy from '@vitejs/plugin-legacy';
6
+
7
+ const input =
8
+ process.env.NODE_ENV === 'production'
9
+ ? path.resolve(__dirname, './index.js')
10
+ : path.resolve(__dirname, './index.html');
11
+
12
+ export default defineConfig({
13
+ server: {
14
+ port: 8112,
15
+ },
16
+ plugins: [
17
+ vue(),
18
+ legacy({
19
+ targets: ['defaults', 'not IE 11'],
20
+ }),
21
+ ],
22
+ test: {
23
+ globals: true,
24
+ environment: 'jsdom',
25
+ },
26
+ resolve: {
27
+ alias: {
28
+ vue: require.resolve('vue/dist/vue.esm-bundler.js'),
29
+ },
30
+ },
31
+
32
+ build: {
33
+ manifest: true,
34
+ // assetsDir: './dist/',
35
+ outDir: path.resolve(__dirname, './dist/'),
36
+ rollupOptions: {
37
+ // overwrite default .html entry
38
+ input,
39
+ output: {
40
+ entryFileNames: `[name].js`,
41
+ chunkFileNames: `[name].js`,
42
+ assetFileNames: `[name].[ext]`,
43
+ },
44
+ },
45
+ },
46
+ });