resource-preloader 2.0.3 → 2.0.5
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/README.md +4 -3
- package/dist/resource-preloader.amd.js +385 -302
- package/dist/resource-preloader.amd.js.map +1 -1
- package/dist/resource-preloader.amd.min.js +1 -1
- package/dist/resource-preloader.amd.min.js.map +1 -1
- package/dist/resource-preloader.cjs.js +314 -231
- package/dist/resource-preloader.cjs.js.map +1 -1
- package/dist/resource-preloader.cjs.min.js +1 -1
- package/dist/resource-preloader.cjs.min.js.map +1 -1
- package/dist/resource-preloader.esm.js +314 -231
- package/dist/resource-preloader.esm.js.map +1 -1
- package/dist/resource-preloader.esm.min.js +1 -1
- package/dist/resource-preloader.esm.min.js.map +1 -1
- package/dist/resource-preloader.umd.js +388 -305
- package/dist/resource-preloader.umd.js.map +1 -1
- package/dist/resource-preloader.umd.min.js +1 -1
- package/dist/resource-preloader.umd.min.js.map +1 -1
- package/package.json +4 -3
- package/src/checker.js +53 -0
- package/src/config.js +14 -15
- package/src/index.js +5 -5
- package/src/loader.js +143 -85
- package/src/other.js +4 -4
- package/src/scheduler.js +128 -157
- package/types/index.d.ts +22 -10
|
@@ -2,31 +2,32 @@
|
|
|
2
2
|
|
|
3
3
|
// 默认公共配置
|
|
4
4
|
const defaultConfig = {
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
timeout: 1500, // 默认超时时间1s
|
|
6
|
+
retry: 0, // 默认不重试
|
|
7
7
|
};
|
|
8
8
|
|
|
9
|
+
/** @var {GlobalConfig} */
|
|
9
10
|
let globalConfig = { ...defaultConfig };
|
|
10
11
|
|
|
11
12
|
/**
|
|
12
13
|
* 配置公共配置的方法(导出)
|
|
13
|
-
* @param {
|
|
14
|
-
* @returns {
|
|
14
|
+
* @param {GlobalConfig} config - 需覆盖的公共配置
|
|
15
|
+
* @returns {GlobalConfig} 合并后的最终全局配置
|
|
15
16
|
*/
|
|
16
17
|
function setGlobalConfig(config) {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
18
|
+
if (typeof config !== "object" || config === null) {
|
|
19
|
+
throw new Error("配置参数必须是一个非空对象");
|
|
20
|
+
}
|
|
21
|
+
globalConfig = { ...globalConfig, ...config };
|
|
22
|
+
return { ...globalConfig };
|
|
22
23
|
}
|
|
23
24
|
|
|
24
25
|
/**
|
|
25
26
|
* 获取当前全局配置
|
|
26
|
-
* @returns {
|
|
27
|
+
* @returns {GlobalConfig} 全局配置
|
|
27
28
|
*/
|
|
28
29
|
function getGlobalConfig() {
|
|
29
|
-
|
|
30
|
+
return { ...globalConfig };
|
|
30
31
|
}
|
|
31
32
|
|
|
32
33
|
// 加载器映射表(存储内置+自定义加载器)
|
|
@@ -35,101 +36,161 @@ const loaderMap = new Map();
|
|
|
35
36
|
/**
|
|
36
37
|
* 通用资源加载Promise封装
|
|
37
38
|
* @param {string} url - 资源地址
|
|
38
|
-
* @param {
|
|
39
|
+
* @param {string} type - 具体的资源加载处理函数
|
|
39
40
|
* @param {number} timeout - 超时时间
|
|
40
|
-
* @returns {Promise<
|
|
41
|
+
* @returns {Promise<LoadResult>} 加载结果Promise
|
|
41
42
|
*/
|
|
42
|
-
function wrapLoadPromise(url,
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
43
|
+
function wrapLoadPromise(url, type, timeout) {
|
|
44
|
+
return new Promise(function (resolve, reject) {
|
|
45
|
+
// 获取加载器处理函数
|
|
46
|
+
const handler = getLoader(type);
|
|
47
|
+
// 超时处理
|
|
48
|
+
const timeoutTimer = setTimeout(() => {
|
|
49
|
+
reject(new Error(`Resource ${url} load timeout (${timeout}ms)`));
|
|
50
|
+
}, timeout);
|
|
51
|
+
|
|
52
|
+
// 执行具体加载逻辑
|
|
53
|
+
handler(url)
|
|
54
|
+
.then(function (result) {
|
|
55
|
+
clearTimeout(timeoutTimer);
|
|
56
|
+
resolve({
|
|
57
|
+
url,
|
|
58
|
+
success: true,
|
|
59
|
+
data: result,
|
|
60
|
+
error: null,
|
|
61
|
+
});
|
|
62
|
+
})
|
|
63
|
+
.catch(function (error) {
|
|
64
|
+
clearTimeout(timeoutTimer);
|
|
65
|
+
reject({
|
|
66
|
+
url,
|
|
67
|
+
success: false,
|
|
68
|
+
data: null,
|
|
69
|
+
error,
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
});
|
|
70
73
|
}
|
|
71
74
|
|
|
75
|
+
/** 初始化内置加载器 **/
|
|
72
76
|
// --------------- 内置JS加载器 ---------------
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
77
|
+
loaderMap.set(
|
|
78
|
+
"js",
|
|
79
|
+
/**
|
|
80
|
+
* JS加载器
|
|
81
|
+
* @param {string} url
|
|
82
|
+
* @returns {Promise<HTMLScriptElement|void>}
|
|
83
|
+
*/
|
|
84
|
+
function (url) {
|
|
85
|
+
const script = document.createElement("script");
|
|
86
|
+
return new Promise(function (resolve, reject) {
|
|
87
|
+
script.type = "text/javascript";
|
|
88
|
+
script.src = url;
|
|
89
|
+
script.async = false; // 保持加载顺序(不开启异步)
|
|
90
|
+
script.dataset.flag = "resource-preloader";
|
|
91
|
+
|
|
92
|
+
script.addEventListener("load", function (e) {
|
|
93
|
+
resolve(script);
|
|
94
|
+
});
|
|
95
|
+
script.addEventListener("error", function () {
|
|
96
|
+
reject(new Error(`JS resource ${url} load failed`));
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
document.head.appendChild(script);
|
|
100
|
+
}).finally(function () {
|
|
101
|
+
// 加载完成后移除标签,避免污染DOM
|
|
102
|
+
document.head.removeChild(script);
|
|
91
103
|
});
|
|
92
|
-
}
|
|
93
|
-
|
|
104
|
+
},
|
|
105
|
+
);
|
|
94
106
|
// --------------- 内置CSS加载器 ---------------
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
107
|
+
loaderMap.set(
|
|
108
|
+
"css",
|
|
109
|
+
/**
|
|
110
|
+
* CSS加载器
|
|
111
|
+
* @param {string} url
|
|
112
|
+
* @returns {Promise<HTMLLinkElement>}
|
|
113
|
+
*/
|
|
114
|
+
function (url) {
|
|
115
|
+
return new Promise(function (resolve, reject) {
|
|
116
|
+
const link = document.createElement("link");
|
|
117
|
+
link.rel = "stylesheet";
|
|
118
|
+
link.href = url;
|
|
119
|
+
link.dataset.flag = "resource-preloader";
|
|
120
|
+
|
|
121
|
+
link.addEventListener("load", function () {
|
|
122
|
+
resolve(link);
|
|
123
|
+
});
|
|
124
|
+
link.addEventListener("error", function () {
|
|
125
|
+
reject(new Error(`CSS resource ${url} load failed`));
|
|
126
|
+
document.head.removeChild(link);
|
|
127
|
+
});
|
|
128
|
+
document.head.appendChild(link);
|
|
110
129
|
});
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
//
|
|
114
|
-
loaderMap.set(
|
|
115
|
-
|
|
130
|
+
},
|
|
131
|
+
);
|
|
132
|
+
// --------------- 内置IMG加载器 ---------------
|
|
133
|
+
loaderMap.set(
|
|
134
|
+
"img",
|
|
135
|
+
/**
|
|
136
|
+
* 图片加载器
|
|
137
|
+
* @param url
|
|
138
|
+
* @returns {Promise<HTMLImageElement>}
|
|
139
|
+
*/
|
|
140
|
+
function (url) {
|
|
141
|
+
const img = new Image();
|
|
142
|
+
img.dataset.flag = "resource-preloader";
|
|
143
|
+
return new Promise(function (resolve, reject) {
|
|
144
|
+
img.addEventListener("load", function () {
|
|
145
|
+
resolve(img);
|
|
146
|
+
});
|
|
147
|
+
img.addEventListener("error", function () {
|
|
148
|
+
reject(new Error(`IMG resource ${url} load failed`));
|
|
149
|
+
});
|
|
150
|
+
// img 标签不需要添加到DOM树
|
|
151
|
+
img.src = url;
|
|
152
|
+
});
|
|
153
|
+
},
|
|
154
|
+
);
|
|
155
|
+
// --------------- 内置JSON加载器 ---------------
|
|
156
|
+
loaderMap.set(
|
|
157
|
+
"json",
|
|
158
|
+
/**
|
|
159
|
+
* JSON加载器
|
|
160
|
+
* @param {string} url
|
|
161
|
+
* @returns {Promise<Object|Array|null|undefined>}
|
|
162
|
+
*/
|
|
163
|
+
function (url) {
|
|
164
|
+
return fetch(url)
|
|
165
|
+
.then(function (res) {
|
|
166
|
+
if (!res.ok) {
|
|
167
|
+
throw new Error(`JSON resource ${url} load failed`);
|
|
168
|
+
}
|
|
169
|
+
return res.json();
|
|
170
|
+
})
|
|
171
|
+
.catch(function (error) {
|
|
172
|
+
throw new Error(`JSON resource ${url} load failed: ${error.message}`);
|
|
173
|
+
});
|
|
174
|
+
},
|
|
175
|
+
);
|
|
116
176
|
|
|
117
177
|
/**
|
|
118
|
-
*
|
|
178
|
+
* 注册自定义加载器的方法
|
|
119
179
|
* @param {string} type - 资源类型(唯一标识)
|
|
120
|
-
* @param {
|
|
180
|
+
* @param {LoaderHandler} handler - 加载处理函数,接收url参数,返回Promise
|
|
121
181
|
*/
|
|
122
182
|
function registerLoader(type, handler) {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
}
|
|
132
|
-
|
|
183
|
+
if (typeof type !== "string" || type.trim() === "") {
|
|
184
|
+
throw new Error("资源类型必须是非空字符串");
|
|
185
|
+
}
|
|
186
|
+
const _ = Object.prototype.toString.call(type).slice(8, -1);
|
|
187
|
+
if (["Function", "AsyncFunction"].includes(_)) {
|
|
188
|
+
throw new Error("加载器必须是一个函数返回Promise对象或一个异步函数");
|
|
189
|
+
}
|
|
190
|
+
if (loaderMap.has(type)) {
|
|
191
|
+
console.warn(`类型为${type}的加载器已存在,将被覆盖`);
|
|
192
|
+
}
|
|
193
|
+
loaderMap.set(type, handler);
|
|
133
194
|
}
|
|
134
195
|
|
|
135
196
|
/**
|
|
@@ -138,135 +199,146 @@ function registerLoader(type, handler) {
|
|
|
138
199
|
* @returns {Function} 加载器处理函数
|
|
139
200
|
*/
|
|
140
201
|
function getLoader(type) {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
202
|
+
if (!loaderMap.has(type)) {
|
|
203
|
+
throw new Error(`未找到${type}类型的加载器,请先注册自定义加载器`);
|
|
204
|
+
}
|
|
205
|
+
return loaderMap.get(type);
|
|
145
206
|
}
|
|
146
|
-
// 导出wrapLoadPromise,避免冗余
|
|
147
207
|
|
|
148
208
|
/**
|
|
149
|
-
*
|
|
150
|
-
* @param {Array} configList - 完整配置数组
|
|
151
|
-
* @param {number} currentId - 当前资源ID
|
|
152
|
-
* @param {Set} visited - 已访问的资源ID集合
|
|
153
|
-
* @param {Set} visiting - 正在访问的资源ID集合(用于检测环)
|
|
209
|
+
* 预检测所有资源的循环依赖
|
|
210
|
+
* @param {Array<FullResourceConfig>} configList - 完整配置数组
|
|
154
211
|
*/
|
|
155
|
-
function
|
|
156
|
-
|
|
212
|
+
function checkDependencies (configList) {
|
|
213
|
+
// 预构建配置映射,O(n) 时间复杂度
|
|
214
|
+
const configMap = new Map(configList.map((config) => [config.name, config]));
|
|
215
|
+
|
|
216
|
+
const visited = new Set();
|
|
217
|
+
const visiting = new Set(); // 全局共享 visiting 集合
|
|
218
|
+
|
|
219
|
+
for (const config of configList) {
|
|
220
|
+
if (!visited.has(config.name)) {
|
|
221
|
+
detectCycle(configMap, config.name);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* 检测循环依赖
|
|
226
|
+
* @param {Map} configMap - 预构建的配置映射
|
|
227
|
+
* @param {string|number} currentId - 当前资源ID
|
|
228
|
+
*/
|
|
229
|
+
function detectCycle(configMap, currentId) {
|
|
230
|
+
const currentConfig = configMap.get(currentId);
|
|
157
231
|
if (!currentConfig) {
|
|
158
|
-
|
|
232
|
+
throw new Error(`未找到ID为${currentId}的资源配置`);
|
|
159
233
|
}
|
|
160
234
|
|
|
161
235
|
// 若正在访问中,说明存在循环依赖
|
|
162
236
|
if (visiting.has(currentId)) {
|
|
163
|
-
|
|
237
|
+
throw new Error(
|
|
238
|
+
`检测到循环依赖:${Array.from(visiting).join(" -> ")} -> ${currentId}`,
|
|
239
|
+
);
|
|
164
240
|
}
|
|
165
241
|
|
|
166
|
-
//
|
|
242
|
+
// 若已访问过,直接返回
|
|
167
243
|
if (visited.has(currentId)) {
|
|
168
|
-
|
|
244
|
+
return;
|
|
169
245
|
}
|
|
170
246
|
|
|
171
247
|
// 标记为正在访问
|
|
172
248
|
visiting.add(currentId);
|
|
173
|
-
|
|
249
|
+
|
|
250
|
+
// 递归检测依赖项
|
|
174
251
|
const dependencies = currentConfig.dependencies || [];
|
|
175
252
|
for (const depId of dependencies) {
|
|
176
|
-
|
|
253
|
+
detectCycle(configMap, depId);
|
|
177
254
|
}
|
|
178
|
-
|
|
255
|
+
|
|
256
|
+
// 标记为已访问
|
|
179
257
|
visiting.delete(currentId);
|
|
180
258
|
visited.add(currentId);
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
/**
|
|
184
|
-
* 预检测所有资源的循环依赖(配置字段改为dependencies)
|
|
185
|
-
* @param {Array} configList - 完整配置数组
|
|
186
|
-
*/
|
|
187
|
-
function checkAllCycles(configList) {
|
|
188
|
-
const visited = new Set();
|
|
189
|
-
for (const config of configList) {
|
|
190
|
-
detectCycle(configList, config.name, visited, new Set());
|
|
191
|
-
}
|
|
259
|
+
}
|
|
192
260
|
}
|
|
193
261
|
|
|
194
262
|
/**
|
|
195
263
|
* 递归加载单个资源及其所有依赖(保证依赖先加载,配置字段改为dependencies)
|
|
196
|
-
* @param {string|number}
|
|
264
|
+
* @param {string|number} name - 资源ID
|
|
197
265
|
* @param {Array} configList - 完整配置数组
|
|
198
266
|
* @param {number} timeout - 超时时间
|
|
199
267
|
* @param {Map} loadedMap - 已加载资源的结果缓存
|
|
268
|
+
* @returns {Promise<ResourceLoadResult>} 加载结果
|
|
200
269
|
*/
|
|
201
|
-
async function loadResourceWithDeps(
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
270
|
+
async function loadResourceWithDeps(name, configList, timeout, loadedMap) {
|
|
271
|
+
// 若已加载,直接返回缓存结果
|
|
272
|
+
if (loadedMap.has(name)) {
|
|
273
|
+
return loadedMap.get(name);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
const currentConfig = configList.find((item) => item.name === name);
|
|
277
|
+
if (!currentConfig) {
|
|
278
|
+
throw new Error(`未找到name为${name}的资源配置`);
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
// 1. 先加载所有依赖(改为dependencies)
|
|
282
|
+
const dependencies = currentConfig.dependencies || [];
|
|
283
|
+
for (const _ of dependencies) {
|
|
284
|
+
await loadResourceWithDeps(_, configList, timeout, loadedMap);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
// 2. 再加载当前资源的urls(顺序加载,一个成功即可)
|
|
288
|
+
const { urls, type } = currentConfig;
|
|
289
|
+
let loadResult = null;
|
|
290
|
+
let status = "success";
|
|
291
|
+
let error = null;
|
|
292
|
+
|
|
293
|
+
try {
|
|
294
|
+
loadResult = await loadUrlsInOrder(name, urls, type, timeout);
|
|
295
|
+
} catch (err) {
|
|
296
|
+
status = "failed";
|
|
297
|
+
error = err;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
// 3. 缓存加载结果
|
|
301
|
+
const result = {
|
|
302
|
+
name,
|
|
303
|
+
config: currentConfig,
|
|
304
|
+
result: loadResult,
|
|
305
|
+
status,
|
|
306
|
+
error,
|
|
307
|
+
};
|
|
308
|
+
loadedMap.set(name, result);
|
|
309
|
+
return result;
|
|
241
310
|
}
|
|
242
311
|
|
|
243
312
|
/**
|
|
244
313
|
* 按顺序加载urls,只需要一个成功即可
|
|
245
|
-
* @param {
|
|
314
|
+
* @param {string} name - 资源名
|
|
315
|
+
* @param {Array<string>} urls - 资源地址数组
|
|
246
316
|
* @param {string} type - 资源类型
|
|
247
317
|
* @param {number} timeout - 超时时间
|
|
248
|
-
* @returns {Promise<
|
|
318
|
+
* @returns {Promise<LoadResult>} 成功的加载结果
|
|
249
319
|
*/
|
|
250
|
-
async function loadUrlsInOrder(urls, type, timeout) {
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
320
|
+
async function loadUrlsInOrder(name, urls, type, timeout) {
|
|
321
|
+
if (!Array.isArray(urls) || urls.length === 0) {
|
|
322
|
+
throw new Error("urls必须是非空数组");
|
|
323
|
+
}
|
|
324
|
+
const errors = [];
|
|
325
|
+
|
|
326
|
+
// 按顺序遍历urls,直到有一个加载成功
|
|
327
|
+
for (const url of urls) {
|
|
328
|
+
try {
|
|
329
|
+
const ret = await wrapLoadPromise(url, type, timeout);
|
|
330
|
+
if (errors.length) {
|
|
331
|
+
console.warn(`加载${name}资源时的一些错误信息:`, errors);
|
|
332
|
+
}
|
|
333
|
+
return ret;
|
|
334
|
+
} catch (error) {
|
|
335
|
+
errors.push({ url, error });
|
|
266
336
|
}
|
|
337
|
+
}
|
|
267
338
|
|
|
268
|
-
|
|
269
|
-
|
|
339
|
+
// 所有url都加载失败,抛出最后一个错误
|
|
340
|
+
console.warn(`所有${name}资源加载错误信息`, errors);
|
|
341
|
+
throw new Error(`所有${name}资源加载失败,错误内容看上面`);
|
|
270
342
|
}
|
|
271
343
|
|
|
272
344
|
/**
|
|
@@ -275,55 +347,66 @@ async function loadUrlsInOrder(urls, type, timeout) {
|
|
|
275
347
|
* @returns {Promise<ResourceLoadResult[]>} 按配置定义顺序的加载结果数组
|
|
276
348
|
*/
|
|
277
349
|
async function resourcePreloader(configList) {
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
return {
|
|
297
|
-
name: url.pathname,
|
|
298
|
-
urls: item,
|
|
299
|
-
type: url.pathname.split('.').pop() || 'js',
|
|
300
|
-
};
|
|
301
|
-
} else {
|
|
302
|
-
return item;
|
|
350
|
+
if (!Array.isArray(configList) || configList.length === 0) {
|
|
351
|
+
throw new Error("配置数组必须是非空数组");
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
// 将 string[]|string 形式处理成 { name, urls, type } 形式 同时处理 urls:string 为 urls:[string]
|
|
355
|
+
/** @type {Array<FullResourceConfig>} */
|
|
356
|
+
const configs = configList
|
|
357
|
+
.map(function(item) {
|
|
358
|
+
if (typeof item === "string") {
|
|
359
|
+
const url = new URL(item, window.location.href);
|
|
360
|
+
return {
|
|
361
|
+
name: url.pathname,
|
|
362
|
+
urls: [item],
|
|
363
|
+
type: url.pathname.split(".").pop() || "js",
|
|
364
|
+
};
|
|
365
|
+
} else if (Array.isArray(item)) {
|
|
366
|
+
if (!item.length) {
|
|
367
|
+
return;
|
|
303
368
|
}
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
const loadedMap = new Map();
|
|
314
|
-
|
|
315
|
-
// 4. 按配置定义的顺序,并行加载同层级无依赖资源(保证依赖先加载,结果保留原始顺序)
|
|
316
|
-
const rawOrderPromises = configList.map(async (config) => {
|
|
317
|
-
return await loadResourceWithDeps(config.name, configList, globalTimeout, loadedMap);
|
|
318
|
-
});
|
|
319
|
-
|
|
320
|
-
// 5. 等待所有资源加载完成,返回原始配置顺序的结果
|
|
321
|
-
return Promise.all(rawOrderPromises).then(function (result) {
|
|
322
|
-
if (result.find(item => item.error)) {
|
|
323
|
-
return Promise.reject(result);
|
|
369
|
+
const url = new URL(item[0], window.location.href);
|
|
370
|
+
return {
|
|
371
|
+
name: url.pathname,
|
|
372
|
+
urls: item,
|
|
373
|
+
type: url.pathname.split(".").pop() || "js",
|
|
374
|
+
};
|
|
375
|
+
} else {
|
|
376
|
+
if (typeof item.urls === "string") {
|
|
377
|
+
item.urls = [item.urls];
|
|
324
378
|
}
|
|
325
|
-
return
|
|
326
|
-
|
|
379
|
+
return item;
|
|
380
|
+
}
|
|
381
|
+
})
|
|
382
|
+
.filter(Boolean);
|
|
383
|
+
|
|
384
|
+
// 1. 获取全局配置
|
|
385
|
+
const { timeout: globalTimeout } = getGlobalConfig();
|
|
386
|
+
|
|
387
|
+
// 2. 预检测所有循环依赖(防止无限递归)
|
|
388
|
+
checkDependencies(configs);
|
|
389
|
+
|
|
390
|
+
// 3. 初始化缓存(存储已加载资源结果,避免重复加载)
|
|
391
|
+
const loadedMap = new Map();
|
|
392
|
+
|
|
393
|
+
// 4. 按配置定义的顺序,并行加载同层级无依赖资源(保证依赖先加载,结果保留原始顺序)
|
|
394
|
+
const rawOrderPromises = configs.map(async function (config) {
|
|
395
|
+
return await loadResourceWithDeps(
|
|
396
|
+
config.name,
|
|
397
|
+
configs,
|
|
398
|
+
globalTimeout,
|
|
399
|
+
loadedMap,
|
|
400
|
+
);
|
|
401
|
+
});
|
|
402
|
+
|
|
403
|
+
// 5. 等待所有资源加载完成,返回原始配置顺序的结果
|
|
404
|
+
return Promise.all(rawOrderPromises).then(function (result) {
|
|
405
|
+
if (result.find((item) => item.error)) {
|
|
406
|
+
return Promise.reject(result);
|
|
407
|
+
}
|
|
408
|
+
return result;
|
|
409
|
+
});
|
|
327
410
|
}
|
|
328
411
|
|
|
329
412
|
resourcePreloader.registerLoader = registerLoader;
|