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 CHANGED
@@ -1,6 +1,6 @@
1
1
  # vite-add-cdn-script
2
2
 
3
- 这是一个在 vite.js 中使用公共 cdn 的库,包括了 unpkg, cdnjs, jsdelivr, staticfile 等多个 cdn 资源,如加载失败会自动切换下一个 cdn 进行加载。
3
+ 这是一个在 vite.js 中使用公共 cdn 的库,包括了 unpkg, jsdelivr 等多个 cdn 资源,如加载失败会自动切换下一个 cdn 进行加载。
4
4
 
5
5
  ## 开始
6
6
 
package/dist/index.js CHANGED
@@ -1,46 +1,52 @@
1
- import N from "node:path";
2
- import v from "node:fs";
3
- const d = {
4
- unpkg: "unpkg.com",
5
- staticfile: "cdn.staticfile.net",
6
- cdnjs: "cdnjs.cloudflare.com/ajax/libs",
7
- jsdelivr: "cdn.jsdelivr.net/npm"
8
- }, u = {
9
- unpkg: "@",
10
- staticfile: "/",
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
- "react-router-dom": "dist/react-router-dom.production.min.js",
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": "/dist/mobxreact.umd.production.min.js",
19
- vue: "/dist/vue.global.prod.js",
20
- "vue-router": "/dist/vue-router.global.prod.js"
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 h(l) {
26
+ function D(p) {
23
27
  const {
24
- protocol: m = "https",
25
- customScript: c = {},
26
- retryTimes: f = 3,
27
- defaultCdns: s = ["unpkg", "cdnjs", "jsdelivr", "staticfile"]
28
- } = l;
29
- let o;
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
- o = e;
41
+ a = e;
36
42
  },
37
43
  transformIndexHtml(e) {
38
- const j = N.resolve(process.cwd(), "package.json");
44
+ const g = v.resolve(process.cwd(), "package.json");
39
45
  try {
40
- const r = s[0], g = v.readFileSync(j, "utf-8"), a = JSON.parse(g), b = o.build.rollupOptions.external;
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(u)}');
43
- const cdnUrlObj = JSON.parse('${JSON.stringify(d)}');
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 Object.keys(a.dependencies).forEach((t) => {
67
- if (b.includes(t))
68
- if (c[t])
69
- n += c[t];
70
- else {
71
- const i = a.dependencies[t].replace("^", "") + "/" + (p[t] ? p[t] : `dist/${t}.min.js`), O = `${m}://${d[r]}/${t}${u[r]}${i}`;
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
- h as default
88
+ D as default,
89
+ x as npmProObj
84
90
  };
@@ -1,10 +1,10 @@
1
- (function(t,r){typeof exports=="object"&&typeof module<"u"?module.exports=r(require("node:path"),require("node:fs")):typeof define=="function"&&define.amd?define(["node:path","node:fs"],r):(t=typeof globalThis<"u"?globalThis:t||self,t.index=r(t.path,t.fs))})(this,function(t,r){"use strict";const o={unpkg:"unpkg.com",staticfile:"cdn.staticfile.net",cdnjs:"cdnjs.cloudflare.com/ajax/libs",jsdelivr:"cdn.jsdelivr.net/npm"},a={unpkg:"@",staticfile:"/",cdnjs:"/",jsdelivr:"@"},i={react:"umd/react.production.min.js","react-dom":"umd/react-dom.production.min.js","react-router-dom":"dist/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.prod.js","vue-router":"/dist/vue-router.global.prod.js"};function f(j){const{protocol:b="https",customScript:d={},retryTimes:g=3,defaultCdns:u=["unpkg","cdnjs","jsdelivr","staticfile"]}=j;let p;return{name:"vite-add-cdn-script",enforce:"pre",apply:"build",config(n){p=n},transformIndexHtml(n){const O=t.resolve(process.cwd(),"package.json");try{const s=u[0],N=r.readFileSync(O,"utf-8"),l=JSON.parse(N),h=p.build.rollupOptions.external;let c=""+`<script>
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(o)}');
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>=${g}){return;}
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 Object.keys(l.dependencies).forEach(e=>{if(h.includes(e))if(d[e])c+=d[e];else{const m=l.dependencies[e].replace("^","")+"/"+(i[e]?i[e]:`dist/${e}.min.js`),x=`${b}://${o[s]}/${e}${a[s]}${m}`;c+=`<script src="${x}" type="text/javascript" onerror="errorCDN(this)" data-cur="0" data-key="${e}" data-filename="${m}"><\/script>
26
- `}}),n=n.replace("</head>",`${c}</head>`),n}catch(s){console.error("获取dependencies出错:",s)}}}}return f});
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
@@ -1,6 +1,7 @@
1
1
  interface IOptions {
2
2
  protocol?: string;
3
3
  customScript?: { [key: string]: string };
4
+ customFilepath?: { [key: string]: string };
4
5
  retryTimes?: number;
5
6
  defaultCdns?: string[];
6
7
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-add-cdn-script",
3
- "version": "0.0.4",
3
+ "version": "0.0.5",
4
4
  "keywords": [
5
5
  "vite",
6
6
  "cdn",