@pure-ds/core 0.7.2 → 0.7.4

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.
@@ -1 +1,306 @@
1
- async function P(...h){let i={};h.length&&typeof h[h.length-1]=="object"&&(i=h.pop()||{});let m=h,{baseURL:f,mapper:w=n=>`${n}.js`,onError:E=(n,r)=>console.error(`[defineWebComponents] ${n}:`,r)}=i,d=f?new URL(f,typeof location<"u"?location.href:import.meta.url):new URL("./",import.meta.url),l=n=>n.toLowerCase().replace(/(^|-)([a-z])/g,(r,s,u)=>u.toUpperCase()),b=async n=>{try{if(customElements.get(n))return{tag:n,status:"already-defined"};let r=w(n),u=await import(r instanceof URL?r.href:new URL(r,d).href),c=u?.default??u?.[l(n)];if(!c){if(customElements.get(n))return{tag:n,status:"self-defined"};throw new Error(`No export found for ${n}. Expected default export or named export "${l(n)}".`)}return customElements.get(n)?{tag:n,status:"race-already-defined"}:(customElements.define(n,c),{tag:n,status:"defined"})}catch(r){throw E(n,r),r}};return Promise.all(m.map(b))}var M=class{constructor(i={}){let{baseURL:m,mapper:f,onError:w,predicate:E=()=>!0,attributeModule:d="data-module",root:l=document,scanExisting:b=!0,debounceMs:n=16,observeShadows:r=!0,enhancers:s=[],patchAttachShadow:u=!0}=i,c=new Set,y=new Set,$=new Set,S=new Map,U=new WeakMap,v=new WeakMap,p=0,T=!1,x=null,W=e=>{if(!e||!s.length)return;let o=v.get(e);o||(o=new Set,v.set(e,o));for(let t of s)if(!(!t.selector||!t.run)&&!o.has(t.selector))try{e.matches&&e.matches(t.selector)&&(t.run(e),o.add(t.selector))}catch(a){console.warn(`[AutoDefiner] Error applying enhancer for selector "${t.selector}":`,a)}},A=(e,o)=>{if(!T&&!(!e||!e.includes("-"))&&!customElements.get(e)&&!y.has(e)&&!$.has(e)){if(o&&o.getAttribute){let t=o.getAttribute(d);t&&!S.has(e)&&S.set(e,t)}c.add(e),O()}},O=()=>{p||(p=setTimeout(N,n))},L=e=>{if(e){if(e.nodeType===1){let o=e,t=o.tagName?.toLowerCase();t&&t.includes("-")&&!customElements.get(t)&&E(t,o)&&A(t,o),W(o),r&&o.shadowRoot&&C(o.shadowRoot)}e.querySelectorAll&&e.querySelectorAll("*").forEach(o=>{let t=o.tagName?.toLowerCase();t&&t.includes("-")&&!customElements.get(t)&&E(t,o)&&A(t,o),W(o),r&&o.shadowRoot&&C(o.shadowRoot)})}},C=e=>{if(!e||U.has(e))return;L(e);let o=new MutationObserver(t=>{for(let a of t)a.addedNodes?.forEach(R=>{L(R)}),a.type==="attributes"&&a.target&&L(a.target)});o.observe(e,{childList:!0,subtree:!0,attributes:!0,attributeFilter:[d,...s.map(t=>t.selector).filter(t=>t.startsWith("data-"))]}),U.set(e,o)};async function N(){if(clearTimeout(p),p=0,!c.size)return;let e=Array.from(c);c.clear(),e.forEach(o=>y.add(o));try{let o=t=>S.get(t)??(f?f(t):`${t}.js`);await P(...e,{baseURL:m,mapper:o,onError:(t,a)=>{$.add(t),w?.(t,a)}})}catch{}finally{e.forEach(o=>y.delete(o))}}let _=l===document?document.documentElement:l,j=new MutationObserver(e=>{for(let o of e)o.addedNodes?.forEach(t=>{L(t)}),o.type==="attributes"&&o.target&&L(o.target)});if(j.observe(_,{childList:!0,subtree:!0,attributes:!0,attributeFilter:[d,...s.map(e=>e.selector).filter(e=>e.startsWith("data-"))]}),r&&u&&Element.prototype.attachShadow){let e=Element.prototype.attachShadow;Element.prototype.attachShadow=function(t){let a=e.call(this,t);if(t&&t.mode==="open"){C(a);let R=this.tagName?.toLowerCase();R&&R.includes("-")&&!customElements.get(R)&&A(R,this)}return a},x=()=>Element.prototype.attachShadow=e}return b&&L(_),{stop(){T=!0,j.disconnect(),x&&x(),p&&(clearTimeout(p),p=0),U.forEach(e=>e.disconnect())},flush:N}}static async define(...i){let m={};i.length&&typeof i[i.length-1]=="object"&&(m=i.pop()||{});let f=i,{baseURL:w,mapper:E=r=>`${r}.js`,onError:d=(r,s)=>console.error(`[defineWebComponents] ${r}:`,s)}=m,l=w?new URL(w,typeof location<"u"?location.href:import.meta.url):new URL("./",import.meta.url),b=r=>r.toLowerCase().replace(/(^|-)([a-z])/g,(s,u,c)=>c.toUpperCase()),n=async r=>{try{if(customElements.get(r))return{tag:r,status:"already-defined"};let s=E(r),c=await import(s instanceof URL?s.href:new URL(s,l).href),y=c?.default??c?.[b(r)];if(!y){if(customElements.get(r))return{tag:r,status:"self-defined"};throw new Error(`No export found for ${r}. Expected default export or named export "${b(r)}".`)}return customElements.get(r)?{tag:r,status:"race-already-defined"}:(customElements.define(r,y),{tag:r,status:"defined"})}catch(s){throw d(r,s),s}};return Promise.all(f.map(n))}};export{M as AutoDefiner};
1
+ // node_modules/pure-web/src/js/auto-definer.js
2
+ async function defineWebComponents(...args) {
3
+ let opts = {};
4
+ if (args.length && typeof args[args.length - 1] === "object") {
5
+ opts = args.pop() || {};
6
+ }
7
+ const tags = args;
8
+ const {
9
+ baseURL,
10
+ mapper = (tag) => `${tag}.js`,
11
+ onError = (tag, err) => console.error(`[defineWebComponents] ${tag}:`, err)
12
+ } = opts;
13
+ const base = baseURL ? new URL(
14
+ baseURL,
15
+ typeof location !== "undefined" ? location.href : import.meta.url
16
+ ) : new URL("./", import.meta.url);
17
+ const toPascal = (tag) => tag.toLowerCase().replace(/(^|-)([a-z])/g, (_, __, c) => c.toUpperCase());
18
+ const loadOne = async (tag) => {
19
+ try {
20
+ if (customElements.get(tag))
21
+ return { tag, status: "already-defined" };
22
+ const spec = mapper(tag);
23
+ const href = spec instanceof URL ? spec.href : new URL(spec, base).href;
24
+ const mod = await import(href);
25
+ const Named = mod?.default ?? mod?.[toPascal(tag)];
26
+ if (!Named) {
27
+ if (customElements.get(tag))
28
+ return { tag, status: "self-defined" };
29
+ throw new Error(
30
+ `No export found for ${tag}. Expected default export or named export "${toPascal(
31
+ tag
32
+ )}".`
33
+ );
34
+ }
35
+ if (!customElements.get(tag)) {
36
+ customElements.define(tag, Named);
37
+ return { tag, status: "defined" };
38
+ }
39
+ return { tag, status: "race-already-defined" };
40
+ } catch (err) {
41
+ onError(tag, err);
42
+ throw err;
43
+ }
44
+ };
45
+ return Promise.all(tags.map(loadOne));
46
+ }
47
+ var AutoDefiner = class {
48
+ constructor(options = {}) {
49
+ const {
50
+ baseURL,
51
+ mapper,
52
+ onError,
53
+ predicate = () => true,
54
+ attributeModule = "data-module",
55
+ root = document,
56
+ scanExisting = true,
57
+ debounceMs = 16,
58
+ observeShadows = true,
59
+ enhancers = [],
60
+ // [{String selector, Function run(elem)}]
61
+ patchAttachShadow = true
62
+ } = options;
63
+ const pending = /* @__PURE__ */ new Set();
64
+ const inFlight = /* @__PURE__ */ new Set();
65
+ const knownMissing = /* @__PURE__ */ new Set();
66
+ const perTagModulePath = /* @__PURE__ */ new Map();
67
+ const shadowObservers = /* @__PURE__ */ new WeakMap();
68
+ const enhancerApplied = /* @__PURE__ */ new WeakMap();
69
+ let timer = 0;
70
+ let stopped = false;
71
+ let restoreAttachShadow = null;
72
+ const applyEnhancers = (element) => {
73
+ if (!element || !enhancers.length)
74
+ return;
75
+ let appliedEnhancers = enhancerApplied.get(element);
76
+ if (!appliedEnhancers) {
77
+ appliedEnhancers = /* @__PURE__ */ new Set();
78
+ enhancerApplied.set(element, appliedEnhancers);
79
+ }
80
+ for (const enhancer of enhancers) {
81
+ if (!enhancer.selector || !enhancer.run)
82
+ continue;
83
+ if (appliedEnhancers.has(enhancer.selector))
84
+ continue;
85
+ try {
86
+ if (element.matches && element.matches(enhancer.selector)) {
87
+ enhancer.run(element);
88
+ appliedEnhancers.add(enhancer.selector);
89
+ }
90
+ } catch (err) {
91
+ console.warn(
92
+ `[AutoDefiner] Error applying enhancer for selector "${enhancer.selector}":`,
93
+ err
94
+ );
95
+ }
96
+ }
97
+ };
98
+ const queueTag = (tag, el) => {
99
+ if (stopped)
100
+ return;
101
+ if (!tag || !tag.includes("-"))
102
+ return;
103
+ if (customElements.get(tag))
104
+ return;
105
+ if (inFlight.has(tag))
106
+ return;
107
+ if (knownMissing.has(tag))
108
+ return;
109
+ if (el && el.getAttribute) {
110
+ const override = el.getAttribute(attributeModule);
111
+ if (override && !perTagModulePath.has(tag)) {
112
+ perTagModulePath.set(tag, override);
113
+ }
114
+ }
115
+ pending.add(tag);
116
+ schedule();
117
+ };
118
+ const schedule = () => {
119
+ if (timer)
120
+ return;
121
+ timer = setTimeout(flush, debounceMs);
122
+ };
123
+ const crawlTree = (rootNode) => {
124
+ if (!rootNode)
125
+ return;
126
+ if (rootNode.nodeType === 1) {
127
+ const el = (
128
+ /** @type {Element} */
129
+ rootNode
130
+ );
131
+ const tag = el.tagName?.toLowerCase();
132
+ if (tag && tag.includes("-") && !customElements.get(tag) && predicate(tag, el)) {
133
+ queueTag(tag, el);
134
+ }
135
+ applyEnhancers(el);
136
+ if (observeShadows && el.shadowRoot) {
137
+ observeShadowRoot(el.shadowRoot);
138
+ }
139
+ }
140
+ if (rootNode.querySelectorAll) {
141
+ rootNode.querySelectorAll("*").forEach((e) => {
142
+ const t = e.tagName?.toLowerCase();
143
+ if (t && t.includes("-") && !customElements.get(t) && predicate(t, e)) {
144
+ queueTag(t, e);
145
+ }
146
+ applyEnhancers(e);
147
+ if (observeShadows && e.shadowRoot) {
148
+ observeShadowRoot(e.shadowRoot);
149
+ }
150
+ });
151
+ }
152
+ };
153
+ const observeShadowRoot = (sr) => {
154
+ if (!sr || shadowObservers.has(sr))
155
+ return;
156
+ crawlTree(sr);
157
+ const mo = new MutationObserver((mutations) => {
158
+ for (const m of mutations) {
159
+ m.addedNodes?.forEach((n) => {
160
+ crawlTree(n);
161
+ });
162
+ if (m.type === "attributes" && m.target) {
163
+ crawlTree(m.target);
164
+ }
165
+ }
166
+ });
167
+ mo.observe(sr, {
168
+ childList: true,
169
+ subtree: true,
170
+ attributes: true,
171
+ attributeFilter: [
172
+ attributeModule,
173
+ ...enhancers.map((e) => e.selector).filter((s) => s.startsWith("data-"))
174
+ ]
175
+ });
176
+ shadowObservers.set(sr, mo);
177
+ };
178
+ async function flush() {
179
+ clearTimeout(timer);
180
+ timer = 0;
181
+ if (!pending.size)
182
+ return;
183
+ const tags = Array.from(pending);
184
+ pending.clear();
185
+ tags.forEach((t) => inFlight.add(t));
186
+ try {
187
+ const effectiveMapper = (tag) => perTagModulePath.get(tag) ?? (mapper ? mapper(tag) : `${tag}.js`);
188
+ await defineWebComponents(...tags, {
189
+ baseURL,
190
+ mapper: effectiveMapper,
191
+ onError: (tag, err) => {
192
+ knownMissing.add(tag);
193
+ onError?.(tag, err);
194
+ }
195
+ });
196
+ } catch {
197
+ } finally {
198
+ tags.forEach((t) => inFlight.delete(t));
199
+ }
200
+ }
201
+ const mountNode = root === document ? document.documentElement : root;
202
+ const obs = new MutationObserver((mutations) => {
203
+ for (const m of mutations) {
204
+ m.addedNodes?.forEach((n) => {
205
+ crawlTree(n);
206
+ });
207
+ if (m.type === "attributes" && m.target) {
208
+ crawlTree(m.target);
209
+ }
210
+ }
211
+ });
212
+ obs.observe(mountNode, {
213
+ childList: true,
214
+ subtree: true,
215
+ attributes: true,
216
+ attributeFilter: [
217
+ attributeModule,
218
+ ...enhancers.map((e) => e.selector).filter((s) => s.startsWith("data-"))
219
+ ]
220
+ });
221
+ if (observeShadows && patchAttachShadow && Element.prototype.attachShadow) {
222
+ const orig = Element.prototype.attachShadow;
223
+ Element.prototype.attachShadow = function patchedAttachShadow(init) {
224
+ const sr = orig.call(this, init);
225
+ if (init && init.mode === "open") {
226
+ observeShadowRoot(sr);
227
+ const tag = this.tagName?.toLowerCase();
228
+ if (tag && tag.includes("-") && !customElements.get(tag)) {
229
+ queueTag(tag, this);
230
+ }
231
+ }
232
+ return sr;
233
+ };
234
+ restoreAttachShadow = () => Element.prototype.attachShadow = orig;
235
+ }
236
+ if (scanExisting) {
237
+ crawlTree(mountNode);
238
+ }
239
+ return {
240
+ stop() {
241
+ stopped = true;
242
+ obs.disconnect();
243
+ if (restoreAttachShadow)
244
+ restoreAttachShadow();
245
+ if (timer) {
246
+ clearTimeout(timer);
247
+ timer = 0;
248
+ }
249
+ shadowObservers.forEach((mo) => mo.disconnect());
250
+ },
251
+ flush
252
+ };
253
+ }
254
+ /**
255
+ * Dynamically load and (idempotently) define a set of web components by tag name.
256
+ */
257
+ static async define(...args) {
258
+ let opts = {};
259
+ if (args.length && typeof args[args.length - 1] === "object") {
260
+ opts = args.pop() || {};
261
+ }
262
+ const tags = args;
263
+ const {
264
+ baseURL,
265
+ mapper = (tag) => `${tag}.js`,
266
+ onError = (tag, err) => console.error(`[defineWebComponents] ${tag}:`, err)
267
+ } = opts;
268
+ const base = baseURL ? new URL(
269
+ baseURL,
270
+ typeof location !== "undefined" ? location.href : import.meta.url
271
+ ) : new URL("./", import.meta.url);
272
+ const toPascal = (tag) => tag.toLowerCase().replace(/(^|-)([a-z])/g, (_, __, c) => c.toUpperCase());
273
+ const loadOne = async (tag) => {
274
+ try {
275
+ if (customElements.get(tag))
276
+ return { tag, status: "already-defined" };
277
+ const spec = mapper(tag);
278
+ const href = spec instanceof URL ? spec.href : new URL(spec, base).href;
279
+ const mod = await import(href);
280
+ const Named = mod?.default ?? mod?.[toPascal(tag)];
281
+ if (!Named) {
282
+ if (customElements.get(tag))
283
+ return { tag, status: "self-defined" };
284
+ throw new Error(
285
+ `No export found for ${tag}. Expected default export or named export "${toPascal(
286
+ tag
287
+ )}".`
288
+ );
289
+ }
290
+ if (!customElements.get(tag)) {
291
+ customElements.define(tag, Named);
292
+ return { tag, status: "defined" };
293
+ }
294
+ return { tag, status: "race-already-defined" };
295
+ } catch (err) {
296
+ onError(tag, err);
297
+ throw err;
298
+ }
299
+ };
300
+ return Promise.all(tags.map(loadOne));
301
+ }
302
+ };
303
+ export {
304
+ AutoDefiner
305
+ };
306
+ //# sourceMappingURL=pds-auto-definer.js.map