vite-plugin-zip-file 1.0.1 → 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/README.md +3 -3
- package/lib/index.cjs +1 -0
- package/lib/index.mjs +42 -0
- package/package.json +3 -3
- package/plugin.config.js +1 -1
- package/src/utils/vite-plugin-zip-flie.js +7 -6
- package/vite.config.js +3 -3
- package/lib/index.cjs.js +0 -1
- package/lib/index.es.js +0 -42
package/README.md
CHANGED
|
@@ -25,8 +25,8 @@ npm install vite-plugin-zip-file --save-dev
|
|
|
25
25
|
|
|
26
26
|
# Usage
|
|
27
27
|
```javascript
|
|
28
|
-
import { defineConfig } from 'vite'
|
|
29
|
-
import {
|
|
28
|
+
import { defineConfig } from 'vite';
|
|
29
|
+
import { viteZip } from 'vite-plugin-zip-file';
|
|
30
30
|
import path from 'path';
|
|
31
31
|
import { fileURLToPath } from 'url';
|
|
32
32
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
@@ -34,7 +34,7 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
|
34
34
|
// https://vitejs.dev/config/
|
|
35
35
|
export default defineConfig({
|
|
36
36
|
plugins: [
|
|
37
|
-
|
|
37
|
+
viteZip({
|
|
38
38
|
folderPath: path.resolve(__dirname, 'dist'),
|
|
39
39
|
outPath: path.resolve(__dirname),
|
|
40
40
|
zipName: 'Test.zip'
|
package/lib/index.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import r from "path";
|
|
2
|
+
import n from "fs";
|
|
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
|
+
throw new Error("config.folderPath and config.outPath is required.");
|
|
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
|
+
});
|
|
16
|
+
}, u = (o = e) => {
|
|
17
|
+
const t = r.join(p, i + o);
|
|
18
|
+
n.existsSync(t) && n.unlinkSync(t);
|
|
19
|
+
}, S = function() {
|
|
20
|
+
f(a, s), a.generateAsync({
|
|
21
|
+
type: "nodebuffer",
|
|
22
|
+
compression: "DEFLATE",
|
|
23
|
+
compressionOptions: {
|
|
24
|
+
level: 9
|
|
25
|
+
}
|
|
26
|
+
}).then((o) => {
|
|
27
|
+
u(e), n.writeFileSync(r.join(p, i, e), o);
|
|
28
|
+
});
|
|
29
|
+
};
|
|
30
|
+
u(e), S();
|
|
31
|
+
};
|
|
32
|
+
return {
|
|
33
|
+
name: "vite-plugin-zip-file",
|
|
34
|
+
apply: "build",
|
|
35
|
+
closeBundle() {
|
|
36
|
+
h();
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
export {
|
|
41
|
+
E as viteZip
|
|
42
|
+
};
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vite-plugin-zip-file",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "zip files at build time.",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"main": "lib/index.cjs
|
|
7
|
-
"module": "lib/index.
|
|
6
|
+
"main": "lib/index.cjs",
|
|
7
|
+
"module": "lib/index.mjs",
|
|
8
8
|
"scripts": {
|
|
9
9
|
"dev": "vite",
|
|
10
10
|
"build": "vite build",
|
package/plugin.config.js
CHANGED
|
@@ -15,7 +15,7 @@ export default defineConfig({
|
|
|
15
15
|
lib: {
|
|
16
16
|
entry: path.resolve(__dirname, 'src/utils/vite-plugin-zip-flie.js'),
|
|
17
17
|
name: 'index',
|
|
18
|
-
fileName: (format) =>
|
|
18
|
+
fileName: (format) => format === 'cjs'? 'index.cjs': 'index.mjs',
|
|
19
19
|
formats: ['cjs', 'es']
|
|
20
20
|
},
|
|
21
21
|
rollupOptions: {
|
|
@@ -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
|
})
|
package/lib/index.cjs.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const v=require("path"),j=require("fs"),g=require("node:module"),m=e=>e&&typeof e=="object"&&"default"in e?e:{default:e},r=m(v),i=m(j),q=g.createRequire(typeof document>"u"?new(require("url")).URL("file:"+__filename).href:document.currentScript&&document.currentScript.src||new URL("index.cjs.js",document.baseURI).href),b=(e={folderPath:null,outPath:null,zipName:null})=>{let{folderPath:l,outPath:u,zipName:t}=e;if(!l||!u)throw new Error("config.folderPath and config.outPath is required.");l=r.default.resolve(l),u=r.default.resolve(u),t=t||l.split("/").pop()+".zip";const y=()=>{const h=q("jszip"),f=new h,a=function(n,o,s=""){const _=i.default.readdirSync(o);s+=o.split("/").pop()+"/",_.forEach(p=>{const c=r.default.join(o,"/",p);i.default.statSync(c).isDirectory()?a(n,c,s):n.file(s+p,i.default.readFileSync(c))})},d=(n=t)=>{const o=r.default.join(u,"/"+n);i.default.existsSync(o)&&i.default.unlinkSync(o)},S=function(){a(f,l),f.generateAsync({type:"nodebuffer",compression:"DEFLATE",compressionOptions:{level:9}}).then(n=>{d(t),i.default.writeFileSync(r.default.join(u,"/",t),n)})};d(t),S()};return{name:"vite-plugin-zip-file",apply:"build",closeBundle(){y()}}};exports.viteZip=b;
|
package/lib/index.es.js
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import s from "path";
|
|
2
|
-
import n from "fs";
|
|
3
|
-
import { createRequire as v } from "node:module";
|
|
4
|
-
const z = v(import.meta.url), w = (m = { folderPath: null, outPath: null, zipName: null }) => {
|
|
5
|
-
let { folderPath: i, outPath: r, zipName: e } = m;
|
|
6
|
-
if (!i || !r)
|
|
7
|
-
throw new Error("config.folderPath and config.outPath is required.");
|
|
8
|
-
i = s.resolve(i), r = s.resolve(r), e = e || i.split("/").pop() + ".zip";
|
|
9
|
-
const d = () => {
|
|
10
|
-
const y = z("jszip"), p = new y(), f = function(o, t, l = "") {
|
|
11
|
-
const S = n.readdirSync(t);
|
|
12
|
-
l += t.split("/").pop() + "/", S.forEach((u) => {
|
|
13
|
-
const c = s.join(t, "/", u);
|
|
14
|
-
n.statSync(c).isDirectory() ? f(o, c, l) : o.file(l + u, n.readFileSync(c));
|
|
15
|
-
});
|
|
16
|
-
}, a = (o = e) => {
|
|
17
|
-
const t = s.join(r, "/" + o);
|
|
18
|
-
n.existsSync(t) && n.unlinkSync(t);
|
|
19
|
-
}, h = function() {
|
|
20
|
-
f(p, i), p.generateAsync({
|
|
21
|
-
type: "nodebuffer",
|
|
22
|
-
compression: "DEFLATE",
|
|
23
|
-
compressionOptions: {
|
|
24
|
-
level: 9
|
|
25
|
-
}
|
|
26
|
-
}).then((o) => {
|
|
27
|
-
a(e), n.writeFileSync(s.join(r, "/", e), o);
|
|
28
|
-
});
|
|
29
|
-
};
|
|
30
|
-
a(e), h();
|
|
31
|
-
};
|
|
32
|
-
return {
|
|
33
|
-
name: "vite-plugin-zip-file",
|
|
34
|
-
apply: "build",
|
|
35
|
-
closeBundle() {
|
|
36
|
-
d();
|
|
37
|
-
}
|
|
38
|
-
};
|
|
39
|
-
};
|
|
40
|
-
export {
|
|
41
|
-
w as viteZip
|
|
42
|
-
};
|