vite-plugin-zip-file 1.0.2 → 1.0.3
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/lib/index.cjs +1 -1
- package/lib/index.mjs +20 -20
- package/package.json +1 -1
- package/src/utils/vite-plugin-zip-flie.js +7 -6
- package/vite.config.js +3 -3
package/lib/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const
|
|
1
|
+
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const g=require("path"),j=require("fs"),q=require("node:module"),m=e=>e&&typeof e=="object"&&"default"in e?e:{default:e},u=m(g),i=m(j),b=q.createRequire(typeof document>"u"?new(require("url")).URL("file:"+__filename).href:document.currentScript&&document.currentScript.src||new URL("index.cjs",document.baseURI).href),l=u.default.sep,w=(e={folderPath:null,outPath:null,zipName:null})=>{let{folderPath:r,outPath:s,zipName:t}=e;if(!r||!s)throw new Error("config.folderPath and config.outPath is required.");r=u.default.resolve(r),s=u.default.resolve(s),t=t||r.split(l).pop()+".zip";const y=()=>{const S=b("jszip"),f=new S,d=function(n,o,c=""){const v=i.default.readdirSync(o);c+=o.split(l).pop()+l,v.forEach(h=>{const a=u.default.join(o,l,h);i.default.statSync(a).isDirectory()?d(n,a,c):n.file(c+h,i.default.readFileSync(a))})},p=(n=t)=>{const o=u.default.join(s,l+n);i.default.existsSync(o)&&i.default.unlinkSync(o)},_=function(){d(f,r),f.generateAsync({type:"nodebuffer",compression:"DEFLATE",compressionOptions:{level:9}}).then(n=>{p(t),i.default.writeFileSync(u.default.join(s,l,t),n)})};p(t),_()};return{name:"vite-plugin-zip-file",apply:"build",closeBundle(){y()}}};exports.viteZip=w;
|
package/lib/index.mjs
CHANGED
|
@@ -1,42 +1,42 @@
|
|
|
1
|
-
import
|
|
1
|
+
import r from "path";
|
|
2
2
|
import n from "fs";
|
|
3
|
-
import { createRequire as
|
|
4
|
-
const
|
|
5
|
-
let { folderPath:
|
|
6
|
-
if (!
|
|
3
|
+
import { createRequire as z } from "node:module";
|
|
4
|
+
const P = z(import.meta.url), i = r.sep, E = (d = { folderPath: null, outPath: null, zipName: null }) => {
|
|
5
|
+
let { folderPath: s, outPath: p, zipName: e } = d;
|
|
6
|
+
if (!s || !p)
|
|
7
7
|
throw new Error("config.folderPath and config.outPath is required.");
|
|
8
|
-
|
|
9
|
-
const
|
|
10
|
-
const y =
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
const
|
|
14
|
-
n.statSync(
|
|
8
|
+
s = r.resolve(s), p = r.resolve(p), e = e || s.split(i).pop() + ".zip";
|
|
9
|
+
const h = () => {
|
|
10
|
+
const y = P("jszip"), a = new y(), f = function(o, t, c = "") {
|
|
11
|
+
const v = n.readdirSync(t);
|
|
12
|
+
c += t.split(i).pop() + i, v.forEach((m) => {
|
|
13
|
+
const l = r.join(t, i, m);
|
|
14
|
+
n.statSync(l).isDirectory() ? f(o, l, c) : o.file(c + m, n.readFileSync(l));
|
|
15
15
|
});
|
|
16
|
-
},
|
|
17
|
-
const t =
|
|
16
|
+
}, u = (o = e) => {
|
|
17
|
+
const t = r.join(p, i + o);
|
|
18
18
|
n.existsSync(t) && n.unlinkSync(t);
|
|
19
|
-
},
|
|
20
|
-
f(
|
|
19
|
+
}, S = function() {
|
|
20
|
+
f(a, s), a.generateAsync({
|
|
21
21
|
type: "nodebuffer",
|
|
22
22
|
compression: "DEFLATE",
|
|
23
23
|
compressionOptions: {
|
|
24
24
|
level: 9
|
|
25
25
|
}
|
|
26
26
|
}).then((o) => {
|
|
27
|
-
|
|
27
|
+
u(e), n.writeFileSync(r.join(p, i, e), o);
|
|
28
28
|
});
|
|
29
29
|
};
|
|
30
|
-
|
|
30
|
+
u(e), S();
|
|
31
31
|
};
|
|
32
32
|
return {
|
|
33
33
|
name: "vite-plugin-zip-file",
|
|
34
34
|
apply: "build",
|
|
35
35
|
closeBundle() {
|
|
36
|
-
|
|
36
|
+
h();
|
|
37
37
|
}
|
|
38
38
|
};
|
|
39
39
|
};
|
|
40
40
|
export {
|
|
41
|
-
|
|
41
|
+
E as viteZip
|
|
42
42
|
};
|
package/package.json
CHANGED
|
@@ -8,6 +8,7 @@ import path from 'path';
|
|
|
8
8
|
import fs from 'fs';
|
|
9
9
|
import { createRequire } from 'node:module'
|
|
10
10
|
const requireds = createRequire(import.meta.url);
|
|
11
|
+
const pathSep = path.sep;
|
|
11
12
|
|
|
12
13
|
export const viteZip = (config = { folderPath: null, outPath: null, zipName: null }) => {
|
|
13
14
|
let { folderPath, outPath, zipName } = config;
|
|
@@ -16,7 +17,7 @@ export const viteZip = (config = { folderPath: null, outPath: null, zipName: nul
|
|
|
16
17
|
}
|
|
17
18
|
folderPath = path.resolve(folderPath);
|
|
18
19
|
outPath = path.resolve(outPath);
|
|
19
|
-
zipName = zipName? zipName: folderPath.split(
|
|
20
|
+
zipName = zipName? zipName: folderPath.split(pathSep).pop() + '.zip';
|
|
20
21
|
const makeZip = () => {
|
|
21
22
|
const JSZip = requireds('jszip');
|
|
22
23
|
const zip = new JSZip();
|
|
@@ -25,9 +26,9 @@ export const viteZip = (config = { folderPath: null, outPath: null, zipName: nul
|
|
|
25
26
|
const readDir = function (zip, dirPath, fileDir = '') {
|
|
26
27
|
// 读取组件下的根文件目录
|
|
27
28
|
const files = fs.readdirSync(dirPath);
|
|
28
|
-
fileDir += dirPath.split(
|
|
29
|
+
fileDir += dirPath.split(pathSep).pop() + pathSep;
|
|
29
30
|
files.forEach(fileName => {
|
|
30
|
-
const fillPath = path.join(dirPath,
|
|
31
|
+
const fillPath = path.join(dirPath, pathSep, fileName)
|
|
31
32
|
const file = fs.statSync(fillPath);
|
|
32
33
|
// 如果是文件夹的话需要递归遍历下面的子文件
|
|
33
34
|
if (file.isDirectory()) {
|
|
@@ -41,7 +42,7 @@ export const viteZip = (config = { folderPath: null, outPath: null, zipName: nul
|
|
|
41
42
|
}
|
|
42
43
|
|
|
43
44
|
const removeZip = (name = zipName) => {
|
|
44
|
-
const dest = path.join(outPath,
|
|
45
|
+
const dest = path.join(outPath, pathSep + name)
|
|
45
46
|
if (fs.existsSync(dest)) {
|
|
46
47
|
fs.unlinkSync(dest)
|
|
47
48
|
}
|
|
@@ -58,7 +59,7 @@ export const viteZip = (config = { folderPath: null, outPath: null, zipName: nul
|
|
|
58
59
|
}
|
|
59
60
|
}).then(content => {
|
|
60
61
|
removeZip(zipName)
|
|
61
|
-
fs.writeFileSync(path.join(outPath,
|
|
62
|
+
fs.writeFileSync(path.join(outPath,pathSep ,zipName), content);
|
|
62
63
|
});
|
|
63
64
|
}
|
|
64
65
|
|
|
@@ -72,4 +73,4 @@ export const viteZip = (config = { folderPath: null, outPath: null, zipName: nul
|
|
|
72
73
|
makeZip();
|
|
73
74
|
}
|
|
74
75
|
}
|
|
75
|
-
}
|
|
76
|
+
}
|
package/vite.config.js
CHANGED
|
@@ -9,13 +9,13 @@
|
|
|
9
9
|
import { defineConfig } from 'vite'
|
|
10
10
|
import path from 'path';
|
|
11
11
|
import { fileURLToPath } from 'url';
|
|
12
|
-
// import {
|
|
13
|
-
import {
|
|
12
|
+
// import { viteZip } from './src/utils/vite-plugin-zip-flie';
|
|
13
|
+
import { viteZip } from './lib/index.mjs';
|
|
14
14
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
15
15
|
export default defineConfig({
|
|
16
16
|
publicDir: false,
|
|
17
17
|
plugins: [
|
|
18
|
-
|
|
18
|
+
viteZip({
|
|
19
19
|
folderPath: path.resolve(__dirname, 'dist'),
|
|
20
20
|
outPath: path.resolve(__dirname)
|
|
21
21
|
})
|