@nectary/assets 3.5.6 → 3.6.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nectary/assets",
3
- "version": "3.5.6",
3
+ "version": "3.6.1",
4
4
  "files": [
5
5
  "**/*/*.css",
6
6
  "**/*/*.json",
@@ -38,6 +38,6 @@
38
38
  "vite": "^7.0.6"
39
39
  },
40
40
  "peerDependencies": {
41
- "@nectary/theme-base": "1.10.0"
41
+ "@nectary/theme-base": "1.11.1"
42
42
  }
43
43
  }
@@ -0,0 +1 @@
1
+
@@ -0,0 +1,10 @@
1
+ interface LoadModuleOptions {
2
+ cdnUrl: string;
3
+ fallbackCdnUrl: string;
4
+ version: string;
5
+ modulePath: string;
6
+ logPrefix?: string;
7
+ }
8
+ export declare const loadModuleWithFallback: <TModule = Record<string, any>>(options: LoadModuleOptions) => Promise<TModule>;
9
+ export declare const loadCSSModuleWithFallback: <TModule = Record<string, any>>(options: LoadModuleOptions) => Promise<TModule>;
10
+ export {};
@@ -0,0 +1,126 @@
1
+ const getImportPath = (cdnUrl, version, modulePath) => {
2
+ if (cdnUrl.length === 0) {
3
+ return null;
4
+ }
5
+ const host = new URL(cdnUrl).host;
6
+ if (host === "esm.sh") {
7
+ if (version.length !== 0) {
8
+ return `${cdnUrl}@${version}/es2022/${modulePath}.mjs`;
9
+ }
10
+ return `${cdnUrl}/${modulePath}`;
11
+ }
12
+ if (version.length !== 0) {
13
+ return `${cdnUrl}/${version}/${modulePath}.js`;
14
+ }
15
+ return `${cdnUrl}/latest/${modulePath}.js`;
16
+ };
17
+ const getCssImportPath = (cdnUrl, version, modulePath) => {
18
+ if (cdnUrl.length === 0) {
19
+ return null;
20
+ }
21
+ const host = new URL(cdnUrl).host;
22
+ if (host === "esm.sh") {
23
+ if (version.length !== 0) {
24
+ return `${cdnUrl}@${version}/es2022/${modulePath}.css`;
25
+ }
26
+ return `${cdnUrl}/${modulePath}`;
27
+ }
28
+ if (version.length !== 0) {
29
+ return `${cdnUrl}/${version}/${modulePath}.css`;
30
+ }
31
+ return `${cdnUrl}/latest/${modulePath}.css`;
32
+ };
33
+ const FALLBACK_DELAY_MS = 2e3;
34
+ const loadModuleWithFallback = async (options) => {
35
+ const { cdnUrl, fallbackCdnUrl, version, modulePath, logPrefix = "CDN" } = options;
36
+ const importPath = getImportPath(cdnUrl, version, modulePath);
37
+ const fallbackImportPath = getImportPath(fallbackCdnUrl, version, modulePath);
38
+ const promises = [
39
+ import(
40
+ /* webpackIgnore: true */
41
+ importPath
42
+ )
43
+ ];
44
+ let timeoutId = null;
45
+ if (fallbackImportPath !== null) {
46
+ promises.push(
47
+ new Promise((resolve) => {
48
+ timeoutId = setTimeout(() => resolve(import(
49
+ /* webpackIgnore: true */
50
+ fallbackImportPath
51
+ )), FALLBACK_DELAY_MS);
52
+ })
53
+ );
54
+ }
55
+ try {
56
+ const module = await Promise.any(promises);
57
+ if (timeoutId !== null) {
58
+ clearTimeout(timeoutId);
59
+ }
60
+ return module;
61
+ } catch (error) {
62
+ if (error instanceof AggregateError) {
63
+ console.error(`${logPrefix} primary load failed: ${importPath}`, error.errors[0]);
64
+ if (fallbackImportPath !== null) {
65
+ console.error(`${logPrefix} fallback load failed: ${fallbackImportPath}`, error.errors[1]);
66
+ }
67
+ } else {
68
+ console.error(`${logPrefix} failed to load module: ${importPath}`, error);
69
+ }
70
+ throw error;
71
+ }
72
+ };
73
+ const fetchCSS = async (url) => {
74
+ const res = await fetch(url);
75
+ if (!res.ok) {
76
+ throw new Error(`CSS fetch failed: ${res.status} ${url}`);
77
+ }
78
+ const css = await res.text();
79
+ return { default: css };
80
+ };
81
+ const loadCSSFromUrl = (url) => {
82
+ if (url.endsWith(".css")) {
83
+ return fetchCSS(url);
84
+ }
85
+ return import(
86
+ /* webpackIgnore: true */
87
+ url
88
+ );
89
+ };
90
+ const loadCSSModuleWithFallback = async (options) => {
91
+ const { cdnUrl, fallbackCdnUrl, version, modulePath, logPrefix = "CDN" } = options;
92
+ const importPath = getCssImportPath(cdnUrl, version, modulePath);
93
+ const fallbackImportPath = getCssImportPath(fallbackCdnUrl, version, modulePath);
94
+ const promises = [
95
+ loadCSSFromUrl(importPath)
96
+ ];
97
+ let timeoutId = null;
98
+ if (fallbackImportPath !== null) {
99
+ promises.push(
100
+ new Promise((resolve) => {
101
+ timeoutId = setTimeout(() => resolve(loadCSSFromUrl(fallbackImportPath)), FALLBACK_DELAY_MS);
102
+ })
103
+ );
104
+ }
105
+ try {
106
+ const module = await Promise.any(promises);
107
+ if (timeoutId !== null) {
108
+ clearTimeout(timeoutId);
109
+ }
110
+ return module;
111
+ } catch (error) {
112
+ if (error instanceof AggregateError) {
113
+ console.error(`${logPrefix} primary load failed: ${importPath}`, error.errors[0]);
114
+ if (fallbackImportPath !== null) {
115
+ console.error(`${logPrefix} fallback load failed: ${fallbackImportPath}`, error.errors[1]);
116
+ }
117
+ } else {
118
+ console.error(`${logPrefix} failed to load module: ${importPath}`, error);
119
+ }
120
+ throw error;
121
+ }
122
+ };
123
+ export {
124
+ loadCSSModuleWithFallback,
125
+ loadModuleWithFallback
126
+ };
@@ -0,0 +1 @@
1
+
@@ -0,0 +1,5 @@
1
+ export interface DeferredPromise<T = void> {
2
+ promise: Promise<T>;
3
+ resolve: (value: T) => void;
4
+ }
5
+ export declare const createDeferredPromise: <T = void>() => DeferredPromise<T>;
@@ -0,0 +1,10 @@
1
+ const createDeferredPromise = () => {
2
+ let resolve;
3
+ const promise = new Promise((r) => {
4
+ resolve = r;
5
+ });
6
+ return { promise, resolve };
7
+ };
8
+ export {
9
+ createDeferredPromise
10
+ };
@@ -31,12 +31,11 @@ export declare abstract class GlobalElementsManager {
31
31
  getConstructor(name: SinchElementName): Promise<CustomElementConstructor> | null;
32
32
  private patchCustomElements;
33
33
  private toClassName;
34
- private loadModuleWithFallback;
34
+ private loadModule;
35
35
  private createLoader;
36
36
  private preloadBundle;
37
37
  init(options: GlobalManagerInitOptions): Promise<void>;
38
38
  whenLoaded(): Promise<void>;
39
- private getImportPath;
40
39
  private getModulePath;
41
40
  preload(name: SinchElementName | SinchElementName[] | 'all'): Promise<void>;
42
41
  }
@@ -1,4 +1,6 @@
1
1
  import { isSinchElementName, sinchElementNameToBase } from "./nectary-element-base.js";
2
+ import "./deferred-promise.js";
3
+ import { loadModuleWithFallback } from "./cdn-loader.js";
2
4
  import { getConstructor, getStore } from "./global-elements-store.js";
3
5
  class GlobalElementsManager {
4
6
  config;
@@ -39,49 +41,19 @@ class GlobalElementsManager {
39
41
  toClassName(itemName) {
40
42
  return itemName.split("-").map((part) => part.charAt(0).toUpperCase() + part.slice(1)).join("");
41
43
  }
42
- async loadModuleWithFallback(modulePath) {
44
+ loadModule(modulePath) {
43
45
  const store = getStore(this.config.storeKey);
44
- const importPath = this.getImportPath(store.cdnUrl, modulePath);
45
- const fallbackImportPath = this.getImportPath(store.fallbackCdnUrl, modulePath);
46
- const promises = [
47
- import(
48
- /* webpackIgnore: true */
49
- importPath
50
- )
51
- ];
52
- let timeoutId = null;
53
- const FALLBACK_DELAY_MS = 2e3;
54
- if (fallbackImportPath !== null) {
55
- promises.push(
56
- new Promise((resolve) => {
57
- timeoutId = setTimeout(() => resolve(import(
58
- /* webpackIgnore: true */
59
- fallbackImportPath
60
- )), FALLBACK_DELAY_MS);
61
- })
62
- );
63
- }
64
- try {
65
- const module = await Promise.any(promises);
66
- if (timeoutId !== null) {
67
- clearTimeout(timeoutId);
68
- }
69
- return module;
70
- } catch (error) {
71
- if (error instanceof AggregateError) {
72
- console.error(`Nectary Primary load failed: ${importPath}`, error.errors[0]);
73
- if (fallbackImportPath !== null) {
74
- console.error(`Nectary fallback load failed: ${fallbackImportPath}`, error.errors[1]);
75
- }
76
- } else {
77
- console.error(`Nectary failed to load module: ${importPath}`, error);
78
- }
79
- throw error;
80
- }
46
+ return loadModuleWithFallback({
47
+ cdnUrl: store.cdnUrl,
48
+ fallbackCdnUrl: store.fallbackCdnUrl,
49
+ version: store.targetlibVersion,
50
+ modulePath,
51
+ logPrefix: "Nectary"
52
+ });
81
53
  }
82
54
  createLoader(modulePath, itemName) {
83
55
  return async () => {
84
- const module = await this.loadModuleWithFallback(modulePath);
56
+ const module = await this.loadModule(modulePath);
85
57
  const className = this.toClassName(itemName);
86
58
  const globalConstructor = module[className];
87
59
  if (globalConstructor == null) {
@@ -93,7 +65,7 @@ class GlobalElementsManager {
93
65
  }
94
66
  async preloadBundle() {
95
67
  const store = getStore(this.config.storeKey);
96
- const module = await this.loadModuleWithFallback("bundle");
68
+ const module = await this.loadModule("bundle");
97
69
  for (const itemName of this.config.baseElementNames) {
98
70
  const className = this.toClassName(itemName);
99
71
  const globalConstructor = module[className];
@@ -150,23 +122,6 @@ class GlobalElementsManager {
150
122
  const store = getStore(this.config.storeKey);
151
123
  return store.loadPromise.promise;
152
124
  }
153
- getImportPath(cdnUrl, modulePath) {
154
- if (cdnUrl.length === 0) {
155
- return null;
156
- }
157
- const store = getStore(this.config.storeKey);
158
- const host = new URL(cdnUrl).host;
159
- if (host === "esm.sh") {
160
- if (store.targetlibVersion.length !== 0) {
161
- return `${cdnUrl}@${store.targetlibVersion}/es2022/${modulePath}.mjs`;
162
- }
163
- return `${cdnUrl}/${modulePath}`;
164
- }
165
- if (store.targetlibVersion.length !== 0) {
166
- return `${cdnUrl}/${store.targetlibVersion}/${modulePath}.js`;
167
- }
168
- return `${cdnUrl}/latest/${modulePath}.js`;
169
- }
170
125
  getModulePath(name) {
171
126
  for (const [key, value] of this.config.nameToPathMap ?? []) {
172
127
  const splittedName = name.startsWith(key) ? name.split(key) : [name];
@@ -1,3 +1,4 @@
1
+ import type { DeferredPromise } from './deferred-promise';
1
2
  import type { SinchElementName } from './nectary-element-base';
2
3
  interface GlobalStore {
3
4
  definitions: Map<SinchElementName, () => Promise<any>>;
@@ -6,10 +7,7 @@ interface GlobalStore {
6
7
  preload: boolean;
7
8
  cdnUrl: string;
8
9
  fallbackCdnUrl: string;
9
- loadPromise: {
10
- promise: Promise<void>;
11
- resolve: (value: void) => void;
12
- };
10
+ loadPromise: DeferredPromise;
13
11
  }
14
12
  export declare const getStore: (storeKey: symbol) => GlobalStore;
15
13
  export declare const getConstructor: (storeKey: symbol, name: SinchElementName) => Promise<CustomElementConstructor> | null;
@@ -1,10 +1,4 @@
1
- const createDeferredPromise = () => {
2
- let resolve;
3
- const promise = new Promise((r) => {
4
- resolve = r;
5
- });
6
- return { promise, resolve };
7
- };
1
+ import { createDeferredPromise } from "./deferred-promise.js";
8
2
  const getStore = (storeKey) => {
9
3
  if (window[storeKey] != null) {
10
4
  return window[storeKey];
@@ -1,11 +1,16 @@
1
1
  import { NectaryElementBase, isSinchElementName, sinchElementNameToBase } from "./nectary-element-base.js";
2
+ import { createDeferredPromise } from "./deferred-promise.js";
3
+ import { loadCSSModuleWithFallback, loadModuleWithFallback } from "./cdn-loader.js";
2
4
  import { getConstructor, getStore } from "./global-elements-store.js";
3
5
  import { GlobalElementsManager } from "./global-elements-manager.js";
4
6
  export {
5
7
  GlobalElementsManager,
6
8
  NectaryElementBase,
9
+ createDeferredPromise,
7
10
  getConstructor,
8
11
  getStore,
9
12
  isSinchElementName,
13
+ loadCSSModuleWithFallback,
14
+ loadModuleWithFallback,
10
15
  sinchElementNameToBase
11
16
  };
@@ -1,3 +1,5 @@
1
1
  export * from './nectary-element-base';
2
+ export * from './deferred-promise';
3
+ export * from './cdn-loader';
2
4
  export * from './global-elements-store';
3
5
  export * from './global-elements-manager';
@@ -1,11 +1,16 @@
1
1
  import { NectaryElementBase, isSinchElementName, sinchElementNameToBase } from "./nectary-element-base.js";
2
+ import { createDeferredPromise } from "./deferred-promise.js";
3
+ import { loadCSSModuleWithFallback, loadModuleWithFallback } from "./cdn-loader.js";
2
4
  import { getConstructor, getStore } from "./global-elements-store.js";
3
5
  import { GlobalElementsManager } from "./global-elements-manager.js";
4
6
  export {
5
7
  GlobalElementsManager,
6
8
  NectaryElementBase,
9
+ createDeferredPromise,
7
10
  getConstructor,
8
11
  getStore,
9
12
  isSinchElementName,
13
+ loadCSSModuleWithFallback,
14
+ loadModuleWithFallback,
10
15
  sinchElementNameToBase
11
16
  };
@@ -1,52 +0,0 @@
1
- // Generated by dts-bundle-generator v9.5.1
2
-
3
- export type SinchElementName = `sinch-${string}`;
4
- export interface GlobalManagerConfig {
5
- storeKey: symbol;
6
- registryUrl: string;
7
- baseElementNames: Set<string>;
8
- nameToPathMap?: Map<string, string>;
9
- }
10
- export interface GlobalManagerInitOptions {
11
- /**
12
- * URL to resolve the modules from
13
- */
14
- cdnUrl: string;
15
- /**
16
- * Fallback URL to resolve the modules from if `cdnUrl` fails
17
- */
18
- fallbackCdnUrl?: string;
19
- /**
20
- * Preloads all components from the bundle.js module
21
- */
22
- preload?: boolean;
23
- /**
24
- * Target version of the library to resolve
25
- *
26
- * If left unspecified, it will resolve to the latest version
27
- */
28
- targetlibVersion?: string;
29
- }
30
- declare abstract class GlobalElementsManager {
31
- private config;
32
- constructor(config: GlobalManagerConfig);
33
- getConstructor(name: SinchElementName): Promise<CustomElementConstructor> | null;
34
- private patchCustomElements;
35
- private toClassName;
36
- private loadModuleWithFallback;
37
- private createLoader;
38
- private preloadBundle;
39
- init(options: GlobalManagerInitOptions): Promise<void>;
40
- whenLoaded(): Promise<void>;
41
- private getImportPath;
42
- private getModulePath;
43
- preload(name: SinchElementName | SinchElementName[] | "all"): Promise<void>;
44
- }
45
- declare class GlobalAssetsManagerImpl extends GlobalElementsManager {
46
- private static instance;
47
- constructor();
48
- static getInstance(): GlobalAssetsManagerImpl;
49
- }
50
- export declare const GlobalAssetsManager: GlobalAssetsManagerImpl;
51
-
52
- export {};