@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 +0 -1
- package/package.json +1 -1
- package/utils/global-assets-constants.d.ts +0 -1
- package/utils/global-assets-constants.js +0 -2
- package/utils/global-assets-manager.d.ts +12 -32
- package/utils/global-assets-manager.js +1 -2
- package/utils/shared/global-elements-manager.d.ts +12 -32
- package/utils/shared/global-elements-manager.js +50 -43
- package/utils/shared/global-elements-store.d.ts +2 -2
- package/utils/shared/global-elements-store.js +2 -2
package/bundle.ts
CHANGED
package/package.json
CHANGED
|
@@ -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
|
-
*
|
|
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
|
-
|
|
14
|
+
cdnUrl: string;
|
|
30
15
|
/**
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
* @default true
|
|
16
|
+
* Fallback URL to resolve the modules from if `cdnUrl` fails
|
|
34
17
|
*/
|
|
35
|
-
|
|
18
|
+
fallbackCdnUrl?: string;
|
|
36
19
|
/**
|
|
37
|
-
*
|
|
20
|
+
* Preloads all components from the bundle.js module
|
|
38
21
|
*/
|
|
39
|
-
|
|
22
|
+
preload?: boolean;
|
|
40
23
|
/**
|
|
41
|
-
*
|
|
24
|
+
* Target version of the library to resolve
|
|
42
25
|
*
|
|
43
|
-
*
|
|
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
|
-
|
|
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
|
|
39
|
+
init(options: GlobalManagerInitOptions): Promise<void>;
|
|
61
40
|
whenLoaded(): Promise<void>;
|
|
62
|
-
private
|
|
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 {
|
|
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
|
-
*
|
|
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
|
-
|
|
12
|
+
cdnUrl: string;
|
|
28
13
|
/**
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
* @default true
|
|
14
|
+
* Fallback URL to resolve the modules from if `cdnUrl` fails
|
|
32
15
|
*/
|
|
33
|
-
|
|
16
|
+
fallbackCdnUrl?: string;
|
|
34
17
|
/**
|
|
35
|
-
*
|
|
18
|
+
* Preloads all components from the bundle.js module
|
|
36
19
|
*/
|
|
37
|
-
|
|
20
|
+
preload?: boolean;
|
|
38
21
|
/**
|
|
39
|
-
*
|
|
22
|
+
* Target version of the library to resolve
|
|
40
23
|
*
|
|
41
|
-
*
|
|
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
|
-
|
|
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
|
|
37
|
+
init(options: GlobalManagerInitOptions): Promise<void>;
|
|
59
38
|
whenLoaded(): Promise<void>;
|
|
60
|
-
private
|
|
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(
|
|
43
|
-
|
|
44
|
-
|
|
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 (
|
|
53
|
+
if (fallbackImportPath !== null) {
|
|
54
54
|
try {
|
|
55
|
-
|
|
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
|
|
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(
|
|
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
|
|
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(
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
17
|
-
|
|
16
|
+
cdnUrl: "",
|
|
17
|
+
fallbackCdnUrl: "",
|
|
18
18
|
preload: false,
|
|
19
19
|
loadPromise: createDeferredPromise()
|
|
20
20
|
};
|