vite-add-cdn-script 0.0.9 → 0.0.11
Sign up to get free protection for your applications and to get access to all the features.
- package/LICENSE +165 -0
- package/README.md +88 -89
- package/dist/index.js +415 -249
- package/dist/index.umd.cjs +7 -7
- package/index.d.ts +5 -6
- package/package.json +64 -59
package/dist/index.js
CHANGED
@@ -1,22 +1,23 @@
|
|
1
|
-
var
|
2
|
-
var
|
3
|
-
var
|
4
|
-
import
|
5
|
-
import
|
6
|
-
import
|
7
|
-
|
1
|
+
var q = Object.defineProperty;
|
2
|
+
var G = (r, t, e) => t in r ? q(r, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : r[t] = e;
|
3
|
+
var g = (r, t, e) => (G(r, typeof t != "symbol" ? t + "" : t, e), e);
|
4
|
+
import A from "node:path";
|
5
|
+
import E from "node:fs";
|
6
|
+
import O from "node-fetch";
|
7
|
+
import V from "semver";
|
8
|
+
class B {
|
8
9
|
constructor() {
|
9
|
-
|
10
|
-
|
11
|
-
this.cdnCachePath =
|
10
|
+
g(this, "cdnCache", {});
|
11
|
+
g(this, "cdnCachePath", "");
|
12
|
+
this.cdnCachePath = A.resolve(process.cwd(), "./.cdn-cache.json");
|
12
13
|
}
|
13
14
|
// 初始化cdn缓存
|
14
15
|
async init() {
|
15
16
|
try {
|
16
|
-
const t = await
|
17
|
+
const t = await E.readFileSync(this.cdnCachePath, "utf-8");
|
17
18
|
this.cdnCache = JSON.parse(t);
|
18
19
|
} catch {
|
19
|
-
console.log("cdn缓存文件不存在,创建缓存文件"), this.cdnCache = {}, await
|
20
|
+
console.log("cdn缓存文件不存在,创建缓存文件"), this.cdnCache = {}, await E.writeFileSync(this.cdnCachePath, "", "utf-8");
|
20
21
|
}
|
21
22
|
}
|
22
23
|
/**
|
@@ -25,8 +26,8 @@ class L {
|
|
25
26
|
* @param version 版本
|
26
27
|
*/
|
27
28
|
getCdnCache(t, e) {
|
28
|
-
var
|
29
|
-
return (
|
29
|
+
var s;
|
30
|
+
return (s = this.cdnCache[t]) == null ? void 0 : s[e];
|
30
31
|
}
|
31
32
|
/**
|
32
33
|
* 设置cdn缓存
|
@@ -34,25 +35,45 @@ class L {
|
|
34
35
|
* @param version 版本
|
35
36
|
* @param urls 地址列表
|
36
37
|
*/
|
37
|
-
setCdnCache(t, e,
|
38
|
-
this.cdnCache[t] ? this.cdnCache[t][e] =
|
39
|
-
[e]:
|
38
|
+
setCdnCache(t, e, s) {
|
39
|
+
this.cdnCache[t] ? this.cdnCache[t][e] = s : this.cdnCache[t] = {
|
40
|
+
[e]: s
|
40
41
|
};
|
41
42
|
}
|
42
43
|
/**
|
43
44
|
* 更新cdn缓存
|
44
45
|
*/
|
45
46
|
async save() {
|
46
|
-
await
|
47
|
+
await E.writeFileSync(this.cdnCachePath, JSON.stringify(this.cdnCache), "utf-8");
|
48
|
+
}
|
49
|
+
}
|
50
|
+
let U;
|
51
|
+
const z = async () => (U || (U = new B(), await U.init()), U);
|
52
|
+
class u extends Error {
|
53
|
+
constructor(t) {
|
54
|
+
super(t), this.name = "NetworkError";
|
55
|
+
}
|
56
|
+
}
|
57
|
+
class P extends u {
|
58
|
+
constructor({ packageName: t, version: e, cdn: s }) {
|
59
|
+
super(`${s} ${t}@${e} 网络请求失败`), this.name = "PackageNetworkError";
|
60
|
+
}
|
61
|
+
}
|
62
|
+
class m extends Error {
|
63
|
+
constructor({ packageName: t, version: e, cdn: s }) {
|
64
|
+
super(`${s}上没有${t}@${e}的版本`), this.name = "NoVersionError";
|
65
|
+
}
|
66
|
+
}
|
67
|
+
class K extends Error {
|
68
|
+
constructor({ packageName: t, version: e, cdn: s }) {
|
69
|
+
super(`在 ${s} 中找不到 ${t}@${e} 文件,请检查包名或版本号`), this.name = "GetFileListError";
|
47
70
|
}
|
48
71
|
}
|
49
|
-
let y;
|
50
|
-
const I = async () => (y || (y = new L(), await y.init()), y);
|
51
72
|
class N {
|
52
73
|
constructor(t) {
|
53
|
-
|
54
|
-
|
55
|
-
|
74
|
+
g(this, "_max");
|
75
|
+
g(this, "_count");
|
76
|
+
g(this, "_taskQueue");
|
56
77
|
this._max = t || 5, this._count = 0, this._taskQueue = [];
|
57
78
|
}
|
58
79
|
/**
|
@@ -62,9 +83,9 @@ class N {
|
|
62
83
|
* @returns {Promise<any>} 返回一个promise
|
63
84
|
*/
|
64
85
|
call(t, ...e) {
|
65
|
-
return new Promise((
|
66
|
-
const
|
67
|
-
this._count >= this._max ? this._taskQueue.push(
|
86
|
+
return new Promise((s, n) => {
|
87
|
+
const o = this._createTask(t, e, s, n);
|
88
|
+
this._count >= this._max ? this._taskQueue.push(o) : o();
|
68
89
|
});
|
69
90
|
}
|
70
91
|
/**
|
@@ -76,138 +97,217 @@ class N {
|
|
76
97
|
* @returns {Function} 返回一个任务函数
|
77
98
|
* @private
|
78
99
|
*/
|
79
|
-
_createTask(t, e,
|
100
|
+
_createTask(t, e, s, n) {
|
80
101
|
return () => {
|
81
|
-
t(...e).then(
|
102
|
+
t(...e).then(s).catch(n).finally(() => {
|
82
103
|
this._count--, this._taskQueue.length && this._taskQueue.shift()();
|
83
104
|
}), this._count++;
|
84
105
|
};
|
85
106
|
}
|
86
107
|
}
|
87
|
-
const
|
108
|
+
const _ = new N(5), J = async (r) => {
|
109
|
+
try {
|
110
|
+
const t = await O(r, { method: "HEAD", redirect: "manual" });
|
111
|
+
return t.status >= 300 && t.status < 400 ? await J(t.headers.get("location") || "") : r;
|
112
|
+
} catch (t) {
|
113
|
+
throw Promise.reject(new u(t.message));
|
114
|
+
}
|
115
|
+
}, W = {
|
88
116
|
//get请求封装
|
89
|
-
get:
|
117
|
+
get: async (r) => {
|
90
118
|
try {
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
119
|
+
const t = await O(r);
|
120
|
+
if (t.ok) {
|
121
|
+
const e = t.headers.get("content-type"), s = await t.text();
|
122
|
+
return e && e.includes("application/json") ? JSON.parse(s) : s;
|
123
|
+
} else
|
124
|
+
throw new u(`请求失败,状态码:${t.status}`);
|
125
|
+
} catch (t) {
|
126
|
+
throw new u(t.message);
|
127
|
+
}
|
128
|
+
}
|
129
|
+
}, $ = {
|
130
|
+
get: _.call.bind(_, W.get)
|
131
|
+
}, X = async (r, t) => {
|
132
|
+
try {
|
133
|
+
const e = await $.get(`https://api.bootcdn.cn/libraries/${r}`);
|
134
|
+
if (e.length === 0)
|
135
|
+
throw new m({
|
136
|
+
packageName: r,
|
137
|
+
version: t,
|
138
|
+
cdn: "bootcdn"
|
139
|
+
});
|
140
|
+
const s = e[0], o = s.assets.reverse().find((c) => {
|
141
|
+
if (V.satisfies(c.version, t))
|
142
|
+
return !0;
|
143
|
+
});
|
144
|
+
if (!o)
|
145
|
+
throw new m({
|
146
|
+
packageName: r,
|
147
|
+
version: t,
|
148
|
+
cdn: "bootcdn"
|
149
|
+
});
|
150
|
+
return { fileList: o.files.map((c) => ({
|
151
|
+
name: "/" + c
|
152
|
+
})), recommendFileName: s.filename, version: o.version };
|
153
|
+
} catch (e) {
|
154
|
+
throw e instanceof u ? new P({
|
155
|
+
packageName: r,
|
156
|
+
version: t,
|
157
|
+
cdn: "unpkg"
|
158
|
+
}) : e;
|
159
|
+
}
|
160
|
+
}, Y = (r, t, e) => `https://cdn.bootcdn.net/ajax/libs/${r}/${t}${e}`, Z = {
|
161
|
+
getFileList: X,
|
162
|
+
getUrl: Y
|
163
|
+
};
|
164
|
+
async function M(r, t, e = !1) {
|
165
|
+
try {
|
166
|
+
if (!e && t.match(/^\D/)) {
|
167
|
+
const n = await et(r, t);
|
168
|
+
for (let o of n)
|
169
|
+
if (V.satisfies(o, t))
|
170
|
+
return M(r, o, !0);
|
171
|
+
throw new m({
|
172
|
+
packageName: r,
|
173
|
+
version: t,
|
174
|
+
cdn: "cdnjs"
|
100
175
|
});
|
101
|
-
} catch (c) {
|
102
|
-
e == null || e(c), s(c);
|
103
176
|
}
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
}
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
)
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
177
|
+
const s = await $.get(`https://api.cdnjs.com/libraries/${r}/${t}`);
|
178
|
+
if (s.error)
|
179
|
+
throw new m({
|
180
|
+
packageName: r,
|
181
|
+
version: t,
|
182
|
+
cdn: "cdnjs"
|
183
|
+
});
|
184
|
+
return {
|
185
|
+
fileList: s.rawFiles.map((n) => ({
|
186
|
+
name: "/" + n
|
187
|
+
})),
|
188
|
+
version: t
|
189
|
+
};
|
190
|
+
} catch (s) {
|
191
|
+
throw s instanceof u ? new P({
|
192
|
+
packageName: r,
|
193
|
+
version: t,
|
194
|
+
cdn: "unpkg"
|
195
|
+
}) : s;
|
196
|
+
}
|
197
|
+
}
|
198
|
+
const tt = (r, t, e) => `https://cdnjs.cloudflare.com/ajax/libs/${r}/${t}${e}`, et = async (r, t) => {
|
199
|
+
try {
|
200
|
+
return (await $.get(`https://api.cdnjs.com/libraries/${r}?fields=versions`)).versions;
|
201
|
+
} catch (e) {
|
202
|
+
throw e instanceof u ? new P({
|
203
|
+
packageName: r,
|
204
|
+
version: t,
|
205
|
+
cdn: "unpkg"
|
206
|
+
}) : e;
|
207
|
+
}
|
208
|
+
}, rt = {
|
209
|
+
getFileList: M,
|
210
|
+
getUrl: tt
|
211
|
+
}, I = (r, t = "") => r.reduce((e, s) => (s.type === "file" ? e.push({ name: `${t}/${s.name}` }) : s.files && e.push(...I(s.files, `${t}/${s.name}`)), e), []);
|
212
|
+
async function Q(r, t, e = !1) {
|
213
|
+
try {
|
214
|
+
if (!e && t.match(/^\D/)) {
|
215
|
+
const n = await nt(r, t);
|
216
|
+
if (typeof n == "string")
|
217
|
+
return Q(r, n, !0);
|
218
|
+
throw new m({
|
219
|
+
packageName: r,
|
220
|
+
version: t,
|
221
|
+
cdn: "jsdelivr"
|
222
|
+
});
|
223
|
+
}
|
224
|
+
const s = await $.get(`https://data.jsdelivr.com/v1/packages/npm/${r}@${t}`);
|
225
|
+
if (s.status)
|
226
|
+
throw new m({
|
227
|
+
packageName: r,
|
228
|
+
version: t,
|
229
|
+
cdn: "jsdelivr"
|
230
|
+
});
|
231
|
+
return { fileList: I(s.files), version: t };
|
232
|
+
} catch (s) {
|
233
|
+
throw s instanceof u ? new P({
|
234
|
+
packageName: r,
|
235
|
+
version: t,
|
236
|
+
cdn: "unpkg"
|
237
|
+
}) : s;
|
238
|
+
}
|
239
|
+
}
|
240
|
+
const st = (r, t, e) => `https://cdn.jsdelivr.net/npm/${r}@${t}${e}`, nt = async (r, t) => {
|
241
|
+
try {
|
242
|
+
return (await $.get(
|
243
|
+
`https://data.jsdelivr.com/v1/packages/npm/${r}/resolved?specifier=${t}`
|
244
|
+
)).version;
|
245
|
+
} catch (e) {
|
246
|
+
throw e instanceof u ? new P({
|
247
|
+
packageName: r,
|
248
|
+
version: t,
|
249
|
+
cdn: "unpkg"
|
250
|
+
}) : e;
|
251
|
+
}
|
252
|
+
}, ct = {
|
253
|
+
getFileList: Q,
|
172
254
|
// 拼接url
|
173
|
-
getUrl:
|
174
|
-
},
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
}
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
}
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
}
|
196
|
-
|
197
|
-
return
|
198
|
-
}
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
255
|
+
getUrl: st
|
256
|
+
}, T = (r) => r.reduce((t, e) => (e.type === "file" ? t.push({ name: e.path }) : e.files && t.push(...T(e.files)), t), []);
|
257
|
+
async function ot(r, t) {
|
258
|
+
var e;
|
259
|
+
try {
|
260
|
+
const n = (e = (await J(`https://unpkg.com/${r}@${t}/?meta`)).match(new RegExp("(?<=@)\\d+\\.\\d+\\.\\d+(?=\\/\\?meta)"))) == null ? void 0 : e[0];
|
261
|
+
if (n) {
|
262
|
+
const o = await $.get(`https://unpkg.com/${r}@${n}/?meta`);
|
263
|
+
return { fileList: T(o.files || []), version: n };
|
264
|
+
} else
|
265
|
+
throw new m({
|
266
|
+
packageName: r,
|
267
|
+
version: t,
|
268
|
+
cdn: "unpkg"
|
269
|
+
});
|
270
|
+
} catch (s) {
|
271
|
+
throw s instanceof u ? new P({
|
272
|
+
packageName: r,
|
273
|
+
version: t,
|
274
|
+
cdn: "unpkg"
|
275
|
+
}) : s;
|
276
|
+
}
|
277
|
+
}
|
278
|
+
function it(r, t, e) {
|
279
|
+
return `https://unpkg.com/${r}@${t}${e}`;
|
280
|
+
}
|
281
|
+
const at = {
|
282
|
+
getFileList: ot,
|
283
|
+
getUrl: it
|
284
|
+
}, lt = async (r) => {
|
285
|
+
try {
|
286
|
+
const t = /^(https?:\/\/.*\d+\.\d+\.\d+\/).+?\.js$/;
|
287
|
+
if (t.test(r)) {
|
288
|
+
const e = r.replace(t, (s, n) => `${n}package.json`);
|
289
|
+
return await $.get(e);
|
290
|
+
} else
|
291
|
+
throw new Error(`${r} 不是正确的url`);
|
292
|
+
} catch (t) {
|
293
|
+
throw t;
|
294
|
+
}
|
295
|
+
}, ut = (r, t) => {
|
296
|
+
var e, s;
|
297
|
+
return ((e = r.dependencies) == null ? void 0 : e[t]) || ((s = r.devDependencies) == null ? void 0 : s[t]);
|
298
|
+
}, dt = async (r, t, e) => {
|
299
|
+
if (!t.match(/\d+(.\d+)?(.\d+)?/))
|
300
|
+
throw new Error(`${r} version ${t} is not valid`);
|
301
|
+
const n = await D[e].getFileList(r, t), o = ht(n, r);
|
302
|
+
if (!o)
|
303
|
+
throw new K({
|
304
|
+
packageName: r,
|
305
|
+
version: t,
|
306
|
+
cdn: e
|
307
|
+
});
|
308
|
+
return D[e].getUrl(r, n.version, o);
|
309
|
+
}, ht = ({ fileList: r }, t) => {
|
310
|
+
var o, a;
|
211
311
|
let e = [
|
212
312
|
`umd/${t}.production.min.js`,
|
213
313
|
/umd\/.+?\.production\.min\.js$/,
|
@@ -225,153 +325,218 @@ const U = new N(5), R = {
|
|
225
325
|
/\.min\.js$/,
|
226
326
|
/\.js$/
|
227
327
|
];
|
228
|
-
const
|
229
|
-
let
|
230
|
-
for (let
|
231
|
-
if (
|
328
|
+
const s = ["runtime", "compiler", ".esm", ".cjs", "development"].filter((c) => !t.includes(c));
|
329
|
+
let n = "";
|
330
|
+
for (let c of e)
|
331
|
+
if (c instanceof RegExp ? n = ((o = r.find((i) => c.test(i.name) && !s.some((f) => i.name.includes(f)))) == null ? void 0 : o.name) || "" : n = ((a = r.find((i) => i.name.includes(c) && !s.some((f) => i.name.includes(f)))) == null ? void 0 : a.name) || "", n)
|
232
332
|
break;
|
233
|
-
return
|
234
|
-
},
|
235
|
-
jsdelivr:
|
236
|
-
bootcdn:
|
237
|
-
cdnjs:
|
238
|
-
unpkg:
|
333
|
+
return n;
|
334
|
+
}, D = {
|
335
|
+
jsdelivr: ct,
|
336
|
+
bootcdn: Z,
|
337
|
+
cdnjs: rt,
|
338
|
+
unpkg: at
|
239
339
|
};
|
240
|
-
function
|
241
|
-
const e =
|
242
|
-
for (; e.length <
|
340
|
+
function ft(r, t) {
|
341
|
+
const e = r.replace(/^\D/, "").split("."), s = t.replace(/^\D/, "").split("."), n = Math.max(e.length, s.length);
|
342
|
+
for (; e.length < n; )
|
243
343
|
e.push("0");
|
244
|
-
for (;
|
245
|
-
|
246
|
-
for (let
|
247
|
-
const
|
248
|
-
if (
|
344
|
+
for (; s.length < n; )
|
345
|
+
s.push("0");
|
346
|
+
for (let o = 0; o < n; o++) {
|
347
|
+
const a = parseInt(e[o], 10), c = parseInt(s[o], 10);
|
348
|
+
if (a > c)
|
249
349
|
return 1;
|
250
|
-
if (
|
350
|
+
if (a < c)
|
251
351
|
return -1;
|
252
352
|
}
|
253
353
|
return 0;
|
254
354
|
}
|
255
|
-
function
|
355
|
+
function pt(r, t) {
|
256
356
|
for (let e in t)
|
257
|
-
Object.prototype.hasOwnProperty.call(t, e) && (
|
258
|
-
return
|
357
|
+
Object.prototype.hasOwnProperty.call(t, e) && (r[e] ? ft(r[e], t[e]) === -1 && (r[e] = t[e]) : r[e] = t[e]);
|
358
|
+
return r;
|
359
|
+
}
|
360
|
+
class wt {
|
361
|
+
constructor() {
|
362
|
+
// 打印记录
|
363
|
+
g(this, "logList", []);
|
364
|
+
}
|
365
|
+
// 打印方法
|
366
|
+
log(t) {
|
367
|
+
this.logList.push({
|
368
|
+
type: "log",
|
369
|
+
message: t
|
370
|
+
});
|
371
|
+
}
|
372
|
+
warn(t) {
|
373
|
+
this.logList.push({
|
374
|
+
type: "warn",
|
375
|
+
message: t
|
376
|
+
});
|
377
|
+
}
|
378
|
+
error(t) {
|
379
|
+
this.logList.push({
|
380
|
+
type: "error",
|
381
|
+
message: t
|
382
|
+
});
|
383
|
+
}
|
384
|
+
info(t) {
|
385
|
+
this.logList.push({
|
386
|
+
type: "info",
|
387
|
+
message: t
|
388
|
+
});
|
389
|
+
}
|
390
|
+
// 打印全部
|
391
|
+
consoleAll() {
|
392
|
+
this.logList.forEach((t) => {
|
393
|
+
console[t.type](`${H} ${t.message}`);
|
394
|
+
});
|
395
|
+
}
|
396
|
+
// 清除打印记录
|
397
|
+
clear() {
|
398
|
+
this.logList = [];
|
399
|
+
}
|
259
400
|
}
|
260
|
-
|
261
|
-
|
262
|
-
|
401
|
+
const H = "vite-add-cdn-script", b = new wt();
|
402
|
+
async function R({
|
403
|
+
external: r,
|
404
|
+
packageData: t,
|
405
|
+
customScript: e,
|
406
|
+
defaultCdns: s
|
407
|
+
}) {
|
408
|
+
let n = [], o = !1;
|
409
|
+
const a = await z();
|
263
410
|
return await Promise.all(
|
264
|
-
|
265
|
-
const
|
266
|
-
if (e[
|
411
|
+
r.map(async (c) => {
|
412
|
+
const i = ut(t, c);
|
413
|
+
if (e[c])
|
267
414
|
return {
|
268
415
|
urls: [],
|
269
|
-
key:
|
416
|
+
key: c
|
270
417
|
};
|
271
|
-
|
272
|
-
|
273
|
-
return s.push(o), {
|
418
|
+
if (!i)
|
419
|
+
return n.push(c), {
|
274
420
|
urls: [],
|
275
|
-
key:
|
421
|
+
key: c
|
276
422
|
};
|
277
|
-
|
423
|
+
const f = a.getCdnCache(c, i);
|
424
|
+
if (f)
|
278
425
|
return {
|
279
|
-
urls:
|
280
|
-
key:
|
426
|
+
urls: f,
|
427
|
+
key: c
|
281
428
|
};
|
282
429
|
{
|
283
|
-
|
284
|
-
const d =
|
285
|
-
|
286
|
-
|
287
|
-
)
|
288
|
-
|
430
|
+
o = !0, console.log(`从网络获取${c}${i}的cdn地址`);
|
431
|
+
const d = await Promise.allSettled(
|
432
|
+
s.map(async (j) => await dt(c, i, j))
|
433
|
+
).then((j) => j.filter((l) => {
|
434
|
+
if (l.status === "fulfilled")
|
435
|
+
return l.value, !0;
|
436
|
+
b.warn(l.reason.toString());
|
437
|
+
}).map((l) => l.value));
|
438
|
+
if (d.length === 0)
|
439
|
+
throw new Error(`获取${c} ${i}的cdn地址失败`);
|
440
|
+
const y = {
|
441
|
+
urls: d,
|
442
|
+
key: c
|
289
443
|
};
|
290
|
-
return
|
444
|
+
return a.setCdnCache(c, i, y.urls), y;
|
291
445
|
}
|
292
446
|
})
|
293
|
-
).then((
|
294
|
-
urls:
|
295
|
-
noVersionPackages:
|
447
|
+
).then((c) => (o && a.save(), {
|
448
|
+
urls: c,
|
449
|
+
noVersionPackages: n
|
296
450
|
}));
|
297
451
|
}
|
298
|
-
function
|
299
|
-
const { customScript: t = {},
|
300
|
-
let s
|
301
|
-
s || (s = r.length - 1);
|
302
|
-
let c;
|
452
|
+
function Pt(r) {
|
453
|
+
const { customScript: t = {}, defaultCdns: e = ["jsdelivr", "unpkg"] } = r;
|
454
|
+
let s;
|
303
455
|
return {
|
304
|
-
name:
|
456
|
+
name: H,
|
305
457
|
enforce: "pre",
|
306
458
|
apply: "build",
|
307
|
-
config(
|
308
|
-
|
459
|
+
config(n) {
|
460
|
+
s = n;
|
309
461
|
},
|
310
|
-
async transformIndexHtml(
|
311
|
-
|
462
|
+
async transformIndexHtml(n) {
|
463
|
+
var a, c;
|
464
|
+
if (!e || e.length === 0)
|
312
465
|
throw new Error("defaultCdns不能为空");
|
313
|
-
const o =
|
466
|
+
const o = A.resolve(process.cwd(), "package.json");
|
314
467
|
try {
|
315
|
-
const
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
468
|
+
const i = E.readFileSync(o, "utf-8"), f = JSON.parse(i), d = (c = (a = s.build) == null ? void 0 : a.rollupOptions) == null ? void 0 : c.external;
|
469
|
+
if (!d)
|
470
|
+
return n;
|
471
|
+
let y = [];
|
472
|
+
if (typeof d == "string")
|
473
|
+
y = [d];
|
474
|
+
else if (Array.isArray(d))
|
475
|
+
y = d.filter((w) => typeof w == "string");
|
476
|
+
else if (typeof d == "object")
|
477
|
+
return n;
|
478
|
+
const j = {};
|
479
|
+
let l = "";
|
480
|
+
const { urls: S, noVersionPackages: F } = await R({
|
481
|
+
external: y,
|
482
|
+
packageData: f,
|
320
483
|
customScript: t,
|
321
|
-
defaultCdns:
|
484
|
+
defaultCdns: e
|
322
485
|
});
|
323
|
-
if (
|
324
|
-
const
|
325
|
-
await Promise.
|
326
|
-
|
327
|
-
if (!
|
486
|
+
if (F.length > 0) {
|
487
|
+
const w = { dependencies: {} };
|
488
|
+
await Promise.allSettled(
|
489
|
+
S.map(async (h) => {
|
490
|
+
if (!h)
|
328
491
|
return;
|
329
|
-
const { key:
|
330
|
-
if (!
|
492
|
+
const { key: C, urls: v } = h, k = t[C] || v[0];
|
493
|
+
if (!k)
|
331
494
|
return;
|
332
|
-
const
|
333
|
-
|
495
|
+
const x = await lt(k);
|
496
|
+
pt(w.dependencies, x.dependencies);
|
334
497
|
})
|
335
|
-
).
|
336
|
-
|
498
|
+
).then((h) => {
|
499
|
+
h.forEach((C) => {
|
500
|
+
C.status === "rejected" && b.warn(C.reason.toString());
|
501
|
+
});
|
337
502
|
});
|
338
|
-
const { urls:
|
339
|
-
external:
|
340
|
-
packageData:
|
503
|
+
const { urls: L, noVersionPackages: p } = await R({
|
504
|
+
external: F,
|
505
|
+
packageData: w,
|
341
506
|
customScript: t,
|
342
|
-
defaultCdns:
|
507
|
+
defaultCdns: e
|
343
508
|
});
|
344
|
-
if (
|
345
|
-
var
|
346
|
-
if (!
|
509
|
+
if (L.map((h) => {
|
510
|
+
var k;
|
511
|
+
if (!h)
|
347
512
|
return;
|
348
|
-
const { urls:
|
349
|
-
(
|
350
|
-
}),
|
351
|
-
throw console.error(`找不到${
|
513
|
+
const { urls: C, key: v } = h;
|
514
|
+
(k = S.find((x) => (x == null ? void 0 : x.key) === v)) == null || k.urls.push(...C);
|
515
|
+
}), p.length > 0)
|
516
|
+
throw console.error(`找不到${p.join(",")}的版本`), new Error(`找不到${p.join(",")}的版本`);
|
352
517
|
}
|
353
|
-
return
|
354
|
-
if (!
|
518
|
+
return b.consoleAll(), S.forEach((w) => {
|
519
|
+
if (!w)
|
355
520
|
return;
|
356
|
-
const { urls:
|
357
|
-
if (t[
|
358
|
-
|
521
|
+
const { urls: L, key: p } = w;
|
522
|
+
if (t[p])
|
523
|
+
l += t[p];
|
359
524
|
else {
|
360
|
-
|
361
|
-
const
|
362
|
-
|
525
|
+
j[p] = L;
|
526
|
+
const h = L[0];
|
527
|
+
l += `<script src="${h}" type="text/javascript" crossorigin="anonymous" onerror="errorCDN(this)" data-cur="0" data-key="${p}"><\/script>
|
363
528
|
`;
|
364
529
|
}
|
365
|
-
}),
|
530
|
+
}), l = `<script>
|
366
531
|
function errorCDN(e) {
|
367
|
-
const packNameUrl = JSON.parse('${JSON.stringify(
|
532
|
+
const packNameUrl = JSON.parse('${JSON.stringify(j)}');
|
368
533
|
const nextCur = parseInt(e.getAttribute("data-cur")) + 1;
|
369
|
-
if(nextCur>${e}){return;}
|
370
534
|
|
371
535
|
const key = e.getAttribute("data-key");
|
372
|
-
|
536
|
+
const curPackNameUrl = packNameUrl[key]
|
537
|
+
if(nextCur>=curPackNameUrl.length){return;}
|
373
538
|
// 新的cdn链接
|
374
|
-
const url =
|
539
|
+
const url = curPackNameUrl[nextCur]
|
375
540
|
// 克隆原标签
|
376
541
|
const tagName = e.tagName
|
377
542
|
const cdnDOM = document.createElement(tagName);
|
@@ -384,13 +549,14 @@ function tt(n) {
|
|
384
549
|
document.head.appendChild(cdnDOM);
|
385
550
|
e.remove();
|
386
551
|
}
|
387
|
-
<\/script>` +
|
388
|
-
} catch (
|
389
|
-
console.error("
|
552
|
+
<\/script>` + l, n = n.replace("</head>", `${l}</head>`), n;
|
553
|
+
} catch (i) {
|
554
|
+
b.consoleAll(), console.error("vite-add-cdn-script error:", i.message), process.exit(1);
|
390
555
|
}
|
391
556
|
}
|
392
557
|
};
|
393
558
|
}
|
394
559
|
export {
|
395
|
-
|
560
|
+
Pt as default,
|
561
|
+
H as libName
|
396
562
|
};
|