@nectary/assets 3.1.2 → 3.2.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/bundle.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  // Reminder: Keep this list updated as we add new assets
2
-
3
2
  // Icons Branded
4
3
  export { IconBranded5gReadiness } from './icons-branded/5g-readiness'
5
4
  export { IconBrandedAbcd } from './icons-branded/abcd'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nectary/assets",
3
- "version": "3.1.2",
3
+ "version": "3.2.0",
4
4
  "files": [
5
5
  "**/*/*.css",
6
6
  "**/*/*.json",
@@ -1,3 +1,2 @@
1
1
  export declare const ASSETS_STORE_KEY: unique symbol;
2
2
  export declare const ASSETS_REGISTRY_URL = "https://registry.npmjs.org/@nectary/assets";
3
- export declare const ASSETS_CDN_URL = "https://esm.sh/@nectary/assets";
@@ -1,8 +1,6 @@
1
1
  const ASSETS_STORE_KEY = Symbol.for("NectaryAssetsStore");
2
2
  const ASSETS_REGISTRY_URL = "https://registry.npmjs.org/@nectary/assets";
3
- const ASSETS_CDN_URL = "https://esm.sh/@nectary/assets";
4
3
  export {
5
- ASSETS_CDN_URL,
6
4
  ASSETS_REGISTRY_URL,
7
5
  ASSETS_STORE_KEY
8
6
  };
@@ -4,49 +4,28 @@ export type SinchElementName = `sinch-${string}`;
4
4
  export interface GlobalManagerConfig {
5
5
  storeKey: symbol;
6
6
  registryUrl: string;
7
- cdnUrl: string;
8
7
  baseElementNames: Set<string>;
9
8
  nameToPathMap?: Map<string, string>;
10
9
  }
11
10
  export interface GlobalManagerInitOptions {
12
11
  /**
13
- * If components should be preloaded from the bundle module (nectary/components/bundle) (Not supported for nectary/assets).
14
- *
15
- * When preload is true:
16
- *
17
- * targetlibVersion will only work since v5 of nectary/components and v3 of nectary/assets.
18
- *
19
- * parchPreviousVersions will have no effect since its not needed.
20
- *
21
- * fallbackLoaderBundle will be used instead of fallbackLoader
22
- */
23
- preload?: boolean;
24
- /**
25
- * Target version of the library to resolve the constructor from the CDN.
26
- *
27
- * If left unspecified, it will resolve to the latest version.
12
+ * URL to resolve the modules from
28
13
  */
29
- targetlibVersion?: string;
14
+ cdnUrl: string;
30
15
  /**
31
- * If previous versions of Nectary should opt in to using Global Components.
32
- *
33
- * @default true
16
+ * Fallback URL to resolve the modules from if `cdnUrl` fails
34
17
  */
35
- patchPerviousVersions?: boolean;
18
+ fallbackCdnUrl?: string;
36
19
  /**
37
- * Fallback loader if the CDN fails.
20
+ * Preloads all components from the bundle.js module
38
21
  */
39
- fallbackLoader?: (name: string) => Promise<any>;
22
+ preload?: boolean;
40
23
  /**
41
- * Should be used like this:
24
+ * Target version of the library to resolve
42
25
  *
43
- * fallbackLoaderBundle: () => import("@nectary/components/bundle")
44
- */
45
- fallbackLoaderBundle?: () => Promise<any>;
46
- /**
47
- * Constructors will be resolved from fallback directly and CDN will not be used.
26
+ * If left unspecified, it will resolve to the latest version
48
27
  */
49
- useFallbackExclusively?: boolean;
28
+ targetlibVersion?: string;
50
29
  }
51
30
  declare abstract class GlobalElementsManager {
52
31
  private config;
@@ -57,9 +36,10 @@ declare abstract class GlobalElementsManager {
57
36
  private loadModuleWithFallback;
58
37
  private createLoader;
59
38
  private preloadBundle;
60
- init(options?: GlobalManagerInitOptions): Promise<void>;
39
+ init(options: GlobalManagerInitOptions): Promise<void>;
61
40
  whenLoaded(): Promise<void>;
62
- private getImportPathFromName;
41
+ private getImportPath;
42
+ private getModulePath;
63
43
  preload(name: SinchElementName | SinchElementName[] | "all"): Promise<void>;
64
44
  }
65
45
  declare class GlobalAssetsManagerImpl extends GlobalElementsManager {
@@ -1,13 +1,12 @@
1
1
  import { GlobalElementsManager } from "./shared/global-elements-manager.js";
2
2
  import { NAME_TO_PATH_MAP, BASE_ASSET_NAMES } from "./asset-names.js";
3
- import { ASSETS_CDN_URL, ASSETS_REGISTRY_URL, ASSETS_STORE_KEY } from "./global-assets-constants.js";
3
+ import { ASSETS_REGISTRY_URL, ASSETS_STORE_KEY } from "./global-assets-constants.js";
4
4
  class GlobalAssetsManagerImpl extends GlobalElementsManager {
5
5
  static instance = null;
6
6
  constructor() {
7
7
  super({
8
8
  storeKey: ASSETS_STORE_KEY,
9
9
  registryUrl: ASSETS_REGISTRY_URL,
10
- cdnUrl: ASSETS_CDN_URL,
11
10
  baseElementNames: BASE_ASSET_NAMES,
12
11
  nameToPathMap: NAME_TO_PATH_MAP
13
12
  });
@@ -2,49 +2,28 @@ import type { SinchElementName } from './nectary-element-base';
2
2
  interface GlobalManagerConfig {
3
3
  storeKey: symbol;
4
4
  registryUrl: string;
5
- cdnUrl: string;
6
5
  baseElementNames: Set<string>;
7
6
  nameToPathMap?: Map<string, string>;
8
7
  }
9
8
  interface GlobalManagerInitOptions {
10
9
  /**
11
- * If components should be preloaded from the bundle module (nectary/components/bundle) (Not supported for nectary/assets).
12
- *
13
- * When preload is true:
14
- *
15
- * targetlibVersion will only work since v5 of nectary/components and v3 of nectary/assets.
16
- *
17
- * parchPreviousVersions will have no effect since its not needed.
18
- *
19
- * fallbackLoaderBundle will be used instead of fallbackLoader
20
- */
21
- preload?: boolean;
22
- /**
23
- * Target version of the library to resolve the constructor from the CDN.
24
- *
25
- * If left unspecified, it will resolve to the latest version.
10
+ * URL to resolve the modules from
26
11
  */
27
- targetlibVersion?: string;
12
+ cdnUrl: string;
28
13
  /**
29
- * If previous versions of Nectary should opt in to using Global Components.
30
- *
31
- * @default true
14
+ * Fallback URL to resolve the modules from if `cdnUrl` fails
32
15
  */
33
- patchPerviousVersions?: boolean;
16
+ fallbackCdnUrl?: string;
34
17
  /**
35
- * Fallback loader if the CDN fails.
18
+ * Preloads all components from the bundle.js module
36
19
  */
37
- fallbackLoader?: (name: string) => Promise<any>;
20
+ preload?: boolean;
38
21
  /**
39
- * Should be used like this:
22
+ * Target version of the library to resolve
40
23
  *
41
- * fallbackLoaderBundle: () => import("@nectary/components/bundle")
42
- */
43
- fallbackLoaderBundle?: () => Promise<any>;
44
- /**
45
- * Constructors will be resolved from fallback directly and CDN will not be used.
24
+ * If left unspecified, it will resolve to the latest version
46
25
  */
47
- useFallbackExclusively?: boolean;
26
+ targetlibVersion?: string;
48
27
  }
49
28
  export declare abstract class GlobalElementsManager {
50
29
  private config;
@@ -55,9 +34,10 @@ export declare abstract class GlobalElementsManager {
55
34
  private loadModuleWithFallback;
56
35
  private createLoader;
57
36
  private preloadBundle;
58
- init(options?: GlobalManagerInitOptions): Promise<void>;
37
+ init(options: GlobalManagerInitOptions): Promise<void>;
59
38
  whenLoaded(): Promise<void>;
60
- private getImportPathFromName;
39
+ private getImportPath;
40
+ private getModulePath;
61
41
  preload(name: SinchElementName | SinchElementName[] | 'all'): Promise<void>;
62
42
  }
63
43
  export {};
@@ -39,10 +39,10 @@ class GlobalElementsManager {
39
39
  toClassName(itemName) {
40
40
  return itemName.split("-").map((part) => part.charAt(0).toUpperCase() + part.slice(1)).join("");
41
41
  }
42
- async loadModuleWithFallback(importPath, fallbackThunk, useFallbackExclusively = false) {
43
- if (useFallbackExclusively && fallbackThunk != null) {
44
- return fallbackThunk();
45
- }
42
+ async loadModuleWithFallback(modulePath) {
43
+ const store = getStore(this.config.storeKey);
44
+ const importPath = this.getImportPath(store.cdnUrl, modulePath);
45
+ const fallbackImportPath = this.getImportPath(store.fallbackCdnUrl, modulePath);
46
46
  try {
47
47
  const module = await import(
48
48
  /* webpackIgnore: true */
@@ -50,12 +50,16 @@ class GlobalElementsManager {
50
50
  );
51
51
  return module;
52
52
  } catch (error) {
53
- if (fallbackThunk != null) {
53
+ if (fallbackImportPath !== null) {
54
54
  try {
55
- return fallbackThunk();
55
+ const fallbackModule = await import(
56
+ /* webpackIgnore: true */
57
+ fallbackImportPath
58
+ );
59
+ return fallbackModule;
56
60
  } catch (fallbackError) {
57
61
  console.error(`Nectary Primary load failed: ${importPath}`, error);
58
- console.error(`Nectary fallback load failed:`, fallbackError);
62
+ console.error(`Nectary fallback load failed: ${fallbackImportPath}`, fallbackError);
59
63
  throw fallbackError;
60
64
  }
61
65
  }
@@ -63,34 +67,26 @@ class GlobalElementsManager {
63
67
  throw error;
64
68
  }
65
69
  }
66
- createLoader(importPath, itemName, fallbackLoader, useFallbackExclusively = false) {
70
+ createLoader(modulePath, itemName) {
67
71
  return async () => {
68
- const module = await this.loadModuleWithFallback(
69
- importPath,
70
- fallbackLoader != null ? () => fallbackLoader(itemName) : null,
71
- useFallbackExclusively
72
- );
72
+ const module = await this.loadModuleWithFallback(modulePath);
73
73
  const className = this.toClassName(itemName);
74
74
  const globalConstructor = module[className];
75
75
  if (globalConstructor == null) {
76
- throw new Error(`Nectary element ${className} not found in module: ${importPath}`);
76
+ throw new Error(`Nectary element ${className} not found in module`);
77
77
  }
78
78
  globalConstructor.isGlobal = true;
79
79
  return globalConstructor;
80
80
  };
81
81
  }
82
- async preloadBundle(importPath, fallbackLoaderBundle, useFallbackExclusively = false) {
82
+ async preloadBundle() {
83
83
  const store = getStore(this.config.storeKey);
84
- const module = await this.loadModuleWithFallback(
85
- importPath,
86
- fallbackLoaderBundle,
87
- useFallbackExclusively
88
- );
84
+ const module = await this.loadModuleWithFallback("bundle");
89
85
  for (const itemName of this.config.baseElementNames) {
90
86
  const className = this.toClassName(itemName);
91
87
  const globalConstructor = module[className];
92
88
  if (globalConstructor == null) {
93
- console.error(`Nectary element ${className} not found in module: ${importPath}`);
89
+ console.error(`Nectary element ${className} not found in module`);
94
90
  continue;
95
91
  }
96
92
  globalConstructor.isGlobal = true;
@@ -99,41 +95,35 @@ class GlobalElementsManager {
99
95
  window.customElements.define(sinchName, globalConstructor);
100
96
  }
101
97
  }
102
- async init(options = {}) {
98
+ async init(options) {
103
99
  const store = getStore(this.config.storeKey);
104
100
  if (store.hasInitialized) {
105
101
  console.warn(`${this.constructor.name} has already been initialized`);
106
102
  return;
107
103
  }
108
104
  store.hasInitialized = true;
105
+ store.cdnUrl = options.cdnUrl;
106
+ store.fallbackCdnUrl = options.fallbackCdnUrl ?? "";
107
+ store.targetlibVersion = options.targetlibVersion ?? "";
109
108
  store.preload = options.preload ?? false;
110
- store.patchPerviousVersions = options.patchPerviousVersions ?? true;
111
- store.useFallbackExclusively = options.useFallbackExclusively ?? false;
112
109
  try {
113
110
  if (store.preload) {
114
- const version = options.targetlibVersion;
115
- store.targetlibVersion = version ?? store.targetlibVersion;
116
- const importPath = version != null ? `${this.config.cdnUrl}@${version}/es2022/bundle.mjs` : `${this.config.cdnUrl}/bundle`;
117
- await this.preloadBundle(importPath, options.fallbackLoaderBundle, store.useFallbackExclusively);
111
+ await this.preloadBundle();
118
112
  } else {
119
- if (store.patchPerviousVersions) {
120
- this.patchCustomElements();
121
- }
122
- this.config.baseElementNames.forEach((name) => {
123
- const importPath = `${this.config.cdnUrl}/${this.getImportPathFromName(name)}`;
124
- store.definitions.set(`sinch-${name}`, this.createLoader(importPath, name, options.fallbackLoader, store.useFallbackExclusively));
125
- });
126
- if (options.targetlibVersion != null) {
127
- store.targetlibVersion = options.targetlibVersion;
128
- } else {
113
+ this.patchCustomElements();
114
+ const setDefinitions = () => {
115
+ this.config.baseElementNames.forEach((name) => {
116
+ const modulePath = this.getModulePath(name);
117
+ store.definitions.set(`sinch-${name}`, this.createLoader(modulePath, name));
118
+ });
119
+ };
120
+ setDefinitions();
121
+ if (store.targetlibVersion.length === 0) {
129
122
  const registry = await fetch(`${this.config.registryUrl}/latest`);
130
123
  const registryData = await registry.json();
131
124
  store.targetlibVersion = registryData.version;
132
125
  }
133
- this.config.baseElementNames.forEach((name) => {
134
- const importPath = `${this.config.cdnUrl}@${store.targetlibVersion}/es2022/${this.getImportPathFromName(name)}.mjs`;
135
- store.definitions.set(`sinch-${name}`, this.createLoader(importPath, name, options.fallbackLoader, store.useFallbackExclusively));
136
- });
126
+ setDefinitions();
137
127
  }
138
128
  } finally {
139
129
  store.loadPromise.resolve();
@@ -143,7 +133,24 @@ class GlobalElementsManager {
143
133
  const store = getStore(this.config.storeKey);
144
134
  return store.loadPromise.promise;
145
135
  }
146
- getImportPathFromName(name) {
136
+ getImportPath(cdnUrl, modulePath) {
137
+ if (cdnUrl.length === 0) {
138
+ return null;
139
+ }
140
+ const store = getStore(this.config.storeKey);
141
+ const host = new URL(cdnUrl).host;
142
+ if (host === "esm.sh") {
143
+ if (store.targetlibVersion.length !== 0) {
144
+ return `${cdnUrl}@${store.targetlibVersion}/es2022/${modulePath}.mjs`;
145
+ }
146
+ return `${cdnUrl}/${modulePath}`;
147
+ }
148
+ if (store.targetlibVersion.length !== 0) {
149
+ return `${cdnUrl}/${store.targetlibVersion}/${modulePath}.js`;
150
+ }
151
+ return `${cdnUrl}/latest/${modulePath}.js`;
152
+ }
153
+ getModulePath(name) {
147
154
  for (const [key, value] of this.config.nameToPathMap ?? []) {
148
155
  const splittedName = name.startsWith(key) ? name.split(key) : [name];
149
156
  if (splittedName.length > 1) {
@@ -3,9 +3,9 @@ interface GlobalStore {
3
3
  definitions: Map<SinchElementName, () => Promise<any>>;
4
4
  hasInitialized: boolean;
5
5
  targetlibVersion: string;
6
- patchPerviousVersions: boolean;
7
- useFallbackExclusively: boolean;
8
6
  preload: boolean;
7
+ cdnUrl: string;
8
+ fallbackCdnUrl: string;
9
9
  loadPromise: {
10
10
  promise: Promise<void>;
11
11
  resolve: (value: void) => void;
@@ -13,8 +13,8 @@ const getStore = (storeKey) => {
13
13
  definitions: /* @__PURE__ */ new Map(),
14
14
  hasInitialized: false,
15
15
  targetlibVersion: "",
16
- patchPerviousVersions: false,
17
- useFallbackExclusively: false,
16
+ cdnUrl: "",
17
+ fallbackCdnUrl: "",
18
18
  preload: false,
19
19
  loadPromise: createDeferredPromise()
20
20
  };