vite-add-cdn-script 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +1 -1
- package/dist/index.js +44 -38
- package/dist/index.umd.cjs +5 -5
- package/index.d.ts +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
package/dist/index.js
CHANGED
@@ -1,46 +1,52 @@
|
|
1
|
-
import
|
2
|
-
import
|
3
|
-
const
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
jsdelivr: "
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
cdnjs: "/",
|
12
|
-
jsdelivr: "@"
|
13
|
-
}, p = {
|
1
|
+
import v from "node:path";
|
2
|
+
import h from "node:fs";
|
3
|
+
const u = {
|
4
|
+
jsdelivr: "cdn.jsdelivr.net/npm",
|
5
|
+
unpkg: "unpkg.com"
|
6
|
+
}, m = {
|
7
|
+
jsdelivr: "@",
|
8
|
+
unpkg: "@"
|
9
|
+
}, x = {
|
10
|
+
// react
|
14
11
|
react: "umd/react.production.min.js",
|
15
12
|
"react-dom": "umd/react-dom.production.min.js",
|
16
|
-
"
|
13
|
+
"@remix-run/router": "dist/router.umd.min.js",
|
14
|
+
"react-router": "dist/umd/react-router.production.min.js",
|
15
|
+
"react-router-dom": "dist/umd/react-router-dom.production.min.js",
|
17
16
|
mobx: "dist/mobx.umd.production.min.js",
|
18
|
-
"mobx-react": "
|
19
|
-
|
20
|
-
|
17
|
+
"mobx-react": "dist/mobxreact.umd.production.min.js",
|
18
|
+
// vue
|
19
|
+
vue: "dist/vue.global.min.js",
|
20
|
+
"vue-router": "dist/vue-router.global.min.js",
|
21
|
+
// tool
|
22
|
+
dayjs: "dayjs.min.js",
|
23
|
+
moment: "moment.min.js",
|
24
|
+
lodash: "lodash.min.js"
|
21
25
|
};
|
22
|
-
function
|
26
|
+
function D(p) {
|
23
27
|
const {
|
24
|
-
protocol:
|
25
|
-
customScript:
|
26
|
-
retryTimes: f =
|
27
|
-
defaultCdns: s = ["
|
28
|
-
|
29
|
-
|
28
|
+
protocol: l = "https",
|
29
|
+
customScript: o = {},
|
30
|
+
retryTimes: f = 1,
|
31
|
+
defaultCdns: s = ["jsdelivr", "unpkg"],
|
32
|
+
customFilepath: j = {}
|
33
|
+
} = p;
|
34
|
+
let a;
|
35
|
+
const c = { ...x, ...j };
|
30
36
|
return {
|
31
37
|
name: "vite-add-cdn-script",
|
32
38
|
enforce: "pre",
|
33
39
|
apply: "build",
|
34
40
|
config(e) {
|
35
|
-
|
41
|
+
a = e;
|
36
42
|
},
|
37
43
|
transformIndexHtml(e) {
|
38
|
-
const
|
44
|
+
const g = v.resolve(process.cwd(), "package.json");
|
39
45
|
try {
|
40
|
-
const r = s[0],
|
46
|
+
const r = s[0], b = h.readFileSync(g, "utf-8"), i = JSON.parse(b), O = a.build.rollupOptions.external;
|
41
47
|
let n = "" + `<script>
|
42
|
-
const separators = JSON.parse('${JSON.stringify(
|
43
|
-
const cdnUrlObj = JSON.parse('${JSON.stringify(
|
48
|
+
const separators = JSON.parse('${JSON.stringify(m)}');
|
49
|
+
const cdnUrlObj = JSON.parse('${JSON.stringify(u)}');
|
44
50
|
const defaultCdns = JSON.parse('${JSON.stringify(s)}');
|
45
51
|
function errorCDN(e) {
|
46
52
|
const nextCur = parseInt(e.getAttribute("data-cur")) + 1;
|
@@ -63,15 +69,14 @@ function h(l) {
|
|
63
69
|
e.remove();
|
64
70
|
}
|
65
71
|
<\/script>`;
|
66
|
-
return
|
67
|
-
if (
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
n += `<script src="${O}" type="text/javascript" onerror="errorCDN(this)" data-cur="0" data-key="${t}" data-filename="${i}"><\/script>
|
72
|
+
return O.forEach((t) => {
|
73
|
+
if (o[t])
|
74
|
+
n += o[t];
|
75
|
+
else {
|
76
|
+
const d = (i.dependencies[t] ? m[r] + i.dependencies[t].replace("^", "") : "") + "/" + (c[t] ? c[t] : `dist/${t}.min.js`), N = `${l}://${u[r]}/${t}${d}`;
|
77
|
+
n += `<script src="${N}" type="text/javascript" onerror="errorCDN(this)" data-cur="0" data-key="${t}" data-filename="${d}"><\/script>
|
73
78
|
`;
|
74
|
-
|
79
|
+
}
|
75
80
|
}), e = e.replace("</head>", `${n}</head>`), e;
|
76
81
|
} catch (r) {
|
77
82
|
console.error("获取dependencies出错:", r);
|
@@ -80,5 +85,6 @@ function h(l) {
|
|
80
85
|
};
|
81
86
|
}
|
82
87
|
export {
|
83
|
-
|
88
|
+
D as default,
|
89
|
+
x as npmProObj
|
84
90
|
};
|
package/dist/index.umd.cjs
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
-
(function(
|
1
|
+
(function(e,r){typeof exports=="object"&&typeof module<"u"?r(exports,require("node:path"),require("node:fs")):typeof define=="function"&&define.amd?define(["exports","node:path","node:fs"],r):(e=typeof globalThis<"u"?globalThis:e||self,r(e.index={},e.path,e.fs))})(this,function(e,r,j){"use strict";const c={jsdelivr:"cdn.jsdelivr.net/npm",unpkg:"unpkg.com"},a={jsdelivr:"@",unpkg:"@"},i={react:"umd/react.production.min.js","react-dom":"umd/react-dom.production.min.js","@remix-run/router":"dist/router.umd.min.js","react-router":"dist/umd/react-router.production.min.js","react-router-dom":"dist/umd/react-router-dom.production.min.js",mobx:"dist/mobx.umd.production.min.js","mobx-react":"dist/mobxreact.umd.production.min.js",vue:"dist/vue.global.min.js","vue-router":"dist/vue-router.global.min.js",dayjs:"dayjs.min.js",moment:"moment.min.js",lodash:"lodash.min.js"};function b(g){const{protocol:O="https",customScript:d={},retryTimes:h=1,defaultCdns:u=["jsdelivr","unpkg"],customFilepath:N={}}=g;let p;const m={...i,...N};return{name:"vite-add-cdn-script",enforce:"pre",apply:"build",config(n){p=n},transformIndexHtml(n){const v=r.resolve(process.cwd(),"package.json");try{const o=u[0],S=j.readFileSync(v,"utf-8"),l=JSON.parse(S),x=p.build.rollupOptions.external;let s=""+`<script>
|
2
2
|
const separators = JSON.parse('${JSON.stringify(a)}');
|
3
|
-
const cdnUrlObj = JSON.parse('${JSON.stringify(
|
3
|
+
const cdnUrlObj = JSON.parse('${JSON.stringify(c)}');
|
4
4
|
const defaultCdns = JSON.parse('${JSON.stringify(u)}');
|
5
5
|
function errorCDN(e) {
|
6
6
|
const nextCur = parseInt(e.getAttribute("data-cur")) + 1;
|
7
|
-
if(nextCur>=${
|
7
|
+
if(nextCur>=${h}){return;}
|
8
8
|
const filename = e.getAttribute("data-filename");
|
9
9
|
const key = e.getAttribute("data-key");
|
10
10
|
const urlName = defaultCdns[nextCur]
|
@@ -22,5 +22,5 @@
|
|
22
22
|
document.head.appendChild(cdnDOM);
|
23
23
|
e.remove();
|
24
24
|
}
|
25
|
-
<\/script>`;return
|
26
|
-
`}}),n=n.replace("</head>",`${
|
25
|
+
<\/script>`;return x.forEach(t=>{if(d[t])s+=d[t];else{const f=(l.dependencies[t]?a[o]+l.dependencies[t].replace("^",""):"")+"/"+(m[t]?m[t]:`dist/${t}.min.js`),y=`${O}://${c[o]}/${t}${f}`;s+=`<script src="${y}" type="text/javascript" onerror="errorCDN(this)" data-cur="0" data-key="${t}" data-filename="${f}"><\/script>
|
26
|
+
`}}),n=n.replace("</head>",`${s}</head>`),n}catch(o){console.error("获取dependencies出错:",o)}}}}e.default=b,e.npmProObj=i,Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
|
package/index.d.ts
CHANGED