vite-plugin-zip-file 1.0.2 → 1.1.0
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 +14 -6
- package/lib/index.cjs +1 -1
- package/lib/index.mjs +28 -20
- package/package.json +9 -2
- package/index.html +0 -20
- package/plugin.config.js +0 -26
- package/src/main.js +0 -8
- package/src/utils/vite-plugin-zip-flie.js +0 -75
- package/vite.config.js +0 -28
package/README.md
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
# vite-plugin-zip-file
|
|
2
|
+
[](./LICENSE)
|
|
3
|
+
[](https://www.npmjs.com/package/vite-plugin-zip-file)
|
|
4
|
+
|
|
2
5
|
Zip files at build time.
|
|
3
6
|
|
|
4
7
|
# Install
|
|
@@ -16,11 +19,14 @@ npm install vite-plugin-zip-file --save-dev
|
|
|
16
19
|
# Optons
|
|
17
20
|
|
|
18
21
|
|
|
19
|
-
| Param | Types | Rquired | Default | Desc
|
|
20
|
-
| :--------- | ------------ | ------- | ------- |
|
|
21
|
-
| folderPath | String\|Path | true | - | Path to the compressed folder
|
|
22
|
-
| outPath | String\|Path | true | - | Compressed package output path
|
|
23
|
-
| zipName | String | false | dist | Package name
|
|
22
|
+
| Param | Types | Rquired | Default | Desc |
|
|
23
|
+
| :--------- | ------------ | ------- | ------- | ------------------------------------------------------------ |
|
|
24
|
+
| folderPath | String\|Path | true | - | Path to the compressed folder |
|
|
25
|
+
| outPath | String\|Path | true | - | Compressed package output path |
|
|
26
|
+
| zipName | String | false | dist | Package name |
|
|
27
|
+
| enabled | Boolean | false | true | This parameter is used to control whether the plugin is enabled. It is usually used to determine whether to compress files according to the environment |
|
|
28
|
+
|
|
29
|
+
|
|
24
30
|
|
|
25
31
|
|
|
26
32
|
# Usage
|
|
@@ -29,6 +35,7 @@ import { defineConfig } from 'vite';
|
|
|
29
35
|
import { viteZip } from 'vite-plugin-zip-file';
|
|
30
36
|
import path from 'path';
|
|
31
37
|
import { fileURLToPath } from 'url';
|
|
38
|
+
import { env } from 'node:process';
|
|
32
39
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
33
40
|
|
|
34
41
|
// https://vitejs.dev/config/
|
|
@@ -37,7 +44,8 @@ export default defineConfig({
|
|
|
37
44
|
viteZip({
|
|
38
45
|
folderPath: path.resolve(__dirname, 'dist'),
|
|
39
46
|
outPath: path.resolve(__dirname),
|
|
40
|
-
zipName: 'Test.zip'
|
|
47
|
+
zipName: 'Test.zip',
|
|
48
|
+
enabled: env.NODE_ENV === 'production'? true: false
|
|
41
49
|
})
|
|
42
50
|
]
|
|
43
51
|
})
|
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 j=require("path"),q=require("fs"),w=require("node:module"),y=e=>e&&typeof e=="object"&&"default"in e?e:{default:e},r=y(j),i=y(q),P=w.createRequire(typeof document>"u"?new(require("url")).URL("file:"+__filename).href:document.currentScript&&document.currentScript.src||new URL("index.cjs",document.baseURI).href),l=r.default.sep,Z={enabled:!0,folderPath:"",outPath:"",zipName:""},z=e=>{let S={...Z,...e},{enabled:s,folderPath:u,outPath:c,zipName:t}=S;if(s=Boolean(s),!u||!c)throw new Error("config.folderPath and config.outPath is required.");u=r.default.resolve(u),c=r.default.resolve(c),t=t||u.split(l).pop()+".zip";const _=()=>{const g=P("jszip"),d=new g,p=function(n,o,a=""){const b=i.default.readdirSync(o);a+=o.split(l).pop()+l,b.forEach(m=>{const f=r.default.join(o,l,m);i.default.statSync(f).isDirectory()?p(n,f,a):n.file(a+m,i.default.readFileSync(f))})},h=(n=t)=>{const o=r.default.join(c,l+n);i.default.existsSync(o)&&i.default.unlinkSync(o)},v=function(){p(d,u),d.generateAsync({type:"nodebuffer",compression:"DEFLATE",compressionOptions:{level:9}}).then(n=>{h(t),i.default.writeFileSync(r.default.join(c,l,t),n)})};h(t),v()};return{name:"vite-plugin-zip-file",apply:"build",closeBundle(){!s||_()}}};exports.viteZip=z;
|
package/lib/index.mjs
CHANGED
|
@@ -1,42 +1,50 @@
|
|
|
1
|
-
import
|
|
1
|
+
import r from "path";
|
|
2
2
|
import n from "fs";
|
|
3
|
-
import { createRequire as
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
import { createRequire as P } from "node:module";
|
|
4
|
+
const Z = P(import.meta.url), i = r.sep, b = {
|
|
5
|
+
enabled: !0,
|
|
6
|
+
folderPath: "",
|
|
7
|
+
outPath: "",
|
|
8
|
+
zipName: ""
|
|
9
|
+
}, F = (h) => {
|
|
10
|
+
let y = {
|
|
11
|
+
...b,
|
|
12
|
+
...h
|
|
13
|
+
}, { enabled: p, folderPath: s, outPath: c, zipName: e } = y;
|
|
14
|
+
if (p = Boolean(p), !s || !c)
|
|
7
15
|
throw new Error("config.folderPath and config.outPath is required.");
|
|
8
|
-
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
const
|
|
12
|
-
l += t.split(
|
|
13
|
-
const
|
|
14
|
-
n.statSync(
|
|
16
|
+
s = r.resolve(s), c = r.resolve(c), e = e || s.split(i).pop() + ".zip";
|
|
17
|
+
const S = () => {
|
|
18
|
+
const g = Z("jszip"), f = new g(), u = function(o, t, l = "") {
|
|
19
|
+
const z = n.readdirSync(t);
|
|
20
|
+
l += t.split(i).pop() + i, z.forEach((m) => {
|
|
21
|
+
const a = r.join(t, i, m);
|
|
22
|
+
n.statSync(a).isDirectory() ? u(o, a, l) : o.file(l + m, n.readFileSync(a));
|
|
15
23
|
});
|
|
16
|
-
},
|
|
17
|
-
const t =
|
|
24
|
+
}, d = (o = e) => {
|
|
25
|
+
const t = r.join(c, i + o);
|
|
18
26
|
n.existsSync(t) && n.unlinkSync(t);
|
|
19
|
-
},
|
|
20
|
-
f
|
|
27
|
+
}, v = function() {
|
|
28
|
+
u(f, s), f.generateAsync({
|
|
21
29
|
type: "nodebuffer",
|
|
22
30
|
compression: "DEFLATE",
|
|
23
31
|
compressionOptions: {
|
|
24
32
|
level: 9
|
|
25
33
|
}
|
|
26
34
|
}).then((o) => {
|
|
27
|
-
|
|
35
|
+
d(e), n.writeFileSync(r.join(c, i, e), o);
|
|
28
36
|
});
|
|
29
37
|
};
|
|
30
|
-
|
|
38
|
+
d(e), v();
|
|
31
39
|
};
|
|
32
40
|
return {
|
|
33
41
|
name: "vite-plugin-zip-file",
|
|
34
42
|
apply: "build",
|
|
35
43
|
closeBundle() {
|
|
36
|
-
|
|
44
|
+
!p || S();
|
|
37
45
|
}
|
|
38
46
|
};
|
|
39
47
|
};
|
|
40
48
|
export {
|
|
41
|
-
|
|
49
|
+
F as viteZip
|
|
42
50
|
};
|
package/package.json
CHANGED
|
@@ -1,9 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vite-plugin-zip-file",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "zip files at build time.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "lib/index.cjs",
|
|
7
|
+
"files": [
|
|
8
|
+
"lib/*",
|
|
9
|
+
"LICENSE",
|
|
10
|
+
"package.json",
|
|
11
|
+
"README.md"
|
|
12
|
+
],
|
|
7
13
|
"module": "lib/index.mjs",
|
|
8
14
|
"scripts": {
|
|
9
15
|
"dev": "vite",
|
|
@@ -30,7 +36,8 @@
|
|
|
30
36
|
"vite": "^3.0.7"
|
|
31
37
|
},
|
|
32
38
|
"dependencies": {
|
|
33
|
-
"jszip": "^3.10.1"
|
|
39
|
+
"jszip": "^3.10.1",
|
|
40
|
+
"vite-plugin-zip-file": "^1.0.3"
|
|
34
41
|
},
|
|
35
42
|
"devDependencies": {
|
|
36
43
|
"@rollup/plugin-node-resolve": "^13.3.0"
|
package/index.html
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
<!--
|
|
2
|
-
* @Author: xiangfu.wu
|
|
3
|
-
* @Date: 2022-08-18 16:24:33
|
|
4
|
-
* @Description: 🚀
|
|
5
|
-
* @FilePath: /vite-plugin-zip/index.html
|
|
6
|
-
-->
|
|
7
|
-
|
|
8
|
-
<!DOCTYPE html>
|
|
9
|
-
<html lang="en">
|
|
10
|
-
<head>
|
|
11
|
-
<meta charset="UTF-8">
|
|
12
|
-
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
|
13
|
-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
14
|
-
<title>Document</title>
|
|
15
|
-
</head>
|
|
16
|
-
<body>
|
|
17
|
-
<div id="app"></div>
|
|
18
|
-
<script type="module" src="/src/main.js"></script>
|
|
19
|
-
</body>
|
|
20
|
-
</html>
|
package/plugin.config.js
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* @Author: xiangfu.wu
|
|
3
|
-
* @Date: 2022-08-18 16:59:00
|
|
4
|
-
* @Description: 🚀
|
|
5
|
-
* @FilePath: /vite-plugin-zip-file/plugin.config.js
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import { defineConfig } from 'vite'
|
|
9
|
-
import path from 'path';
|
|
10
|
-
import { fileURLToPath } from 'url';
|
|
11
|
-
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
12
|
-
export default defineConfig({
|
|
13
|
-
publicDir: false,
|
|
14
|
-
build: {
|
|
15
|
-
lib: {
|
|
16
|
-
entry: path.resolve(__dirname, 'src/utils/vite-plugin-zip-flie.js'),
|
|
17
|
-
name: 'index',
|
|
18
|
-
fileName: (format) => format === 'cjs'? 'index.cjs': 'index.mjs',
|
|
19
|
-
formats: ['cjs', 'es']
|
|
20
|
-
},
|
|
21
|
-
rollupOptions: {
|
|
22
|
-
external: ['node:module', 'fs', 'path', 'url'] //忽略处理node内置依赖
|
|
23
|
-
},
|
|
24
|
-
outDir: "./lib"
|
|
25
|
-
}
|
|
26
|
-
})
|
package/src/main.js
DELETED
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* @Author: xiangfu.wu
|
|
3
|
-
* @Date: 2022-08-18 15:30:35
|
|
4
|
-
* @Description: 🚀
|
|
5
|
-
* @FilePath: /vite-plugin-zip-file/src/utils/vite-plugin-zip-flie.js
|
|
6
|
-
*/
|
|
7
|
-
import path from 'path';
|
|
8
|
-
import fs from 'fs';
|
|
9
|
-
import { createRequire } from 'node:module'
|
|
10
|
-
const requireds = createRequire(import.meta.url);
|
|
11
|
-
|
|
12
|
-
export const viteZip = (config = { folderPath: null, outPath: null, zipName: null }) => {
|
|
13
|
-
let { folderPath, outPath, zipName } = config;
|
|
14
|
-
if (!folderPath || !outPath) {
|
|
15
|
-
throw new Error('config.folderPath and config.outPath is required.');
|
|
16
|
-
}
|
|
17
|
-
folderPath = path.resolve(folderPath);
|
|
18
|
-
outPath = path.resolve(outPath);
|
|
19
|
-
zipName = zipName? zipName: folderPath.split('/').pop() + '.zip';
|
|
20
|
-
const makeZip = () => {
|
|
21
|
-
const JSZip = requireds('jszip');
|
|
22
|
-
const zip = new JSZip();
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
const readDir = function (zip, dirPath, fileDir = '') {
|
|
26
|
-
// 读取组件下的根文件目录
|
|
27
|
-
const files = fs.readdirSync(dirPath);
|
|
28
|
-
fileDir += dirPath.split('/').pop() + '/';
|
|
29
|
-
files.forEach(fileName => {
|
|
30
|
-
const fillPath = path.join(dirPath, "/", fileName)
|
|
31
|
-
const file = fs.statSync(fillPath);
|
|
32
|
-
// 如果是文件夹的话需要递归遍历下面的子文件
|
|
33
|
-
if (file.isDirectory()) {
|
|
34
|
-
// const dirZip = zip.folder(fileName);
|
|
35
|
-
readDir(zip, fillPath, fileDir);
|
|
36
|
-
} else {
|
|
37
|
-
// 读取每个文件为buffer存到zip中,带上文件夹,保证压缩后文件目录不变
|
|
38
|
-
zip.file(fileDir + fileName, fs.readFileSync(fillPath))
|
|
39
|
-
}
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const removeZip = (name = zipName) => {
|
|
44
|
-
const dest = path.join(outPath, '/' + name)
|
|
45
|
-
if (fs.existsSync(dest)) {
|
|
46
|
-
fs.unlinkSync(dest)
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
const doZip = function () {
|
|
51
|
-
|
|
52
|
-
readDir(zip, folderPath);
|
|
53
|
-
zip.generateAsync({
|
|
54
|
-
type: "nodebuffer", // 压缩类型
|
|
55
|
-
compression: "DEFLATE", // 压缩算法
|
|
56
|
-
compressionOptions: { // 压缩级别
|
|
57
|
-
level: 9
|
|
58
|
-
}
|
|
59
|
-
}).then(content => {
|
|
60
|
-
removeZip(zipName)
|
|
61
|
-
fs.writeFileSync(path.join(outPath,'/' ,zipName), content);
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
removeZip(zipName)
|
|
66
|
-
doZip()
|
|
67
|
-
};
|
|
68
|
-
return {
|
|
69
|
-
name: 'vite-plugin-zip-file',
|
|
70
|
-
apply: 'build',
|
|
71
|
-
closeBundle() {
|
|
72
|
-
makeZip();
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
}
|
package/vite.config.js
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* @Author: xiangfu.wu
|
|
3
|
-
* @Date: 2022-08-18 16:31:25
|
|
4
|
-
* @Description: 🚀
|
|
5
|
-
* @FilePath: /vite-plugin-zip-file/vite.config.js
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
import { defineConfig } from 'vite'
|
|
10
|
-
import path from 'path';
|
|
11
|
-
import { fileURLToPath } from 'url';
|
|
12
|
-
// import { vitePluginZip } from './src/utils/vite-plugin-zip-file.js';
|
|
13
|
-
import { vitePluginZip } from './lib/index.es.js';
|
|
14
|
-
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
15
|
-
export default defineConfig({
|
|
16
|
-
publicDir: false,
|
|
17
|
-
plugins: [
|
|
18
|
-
vitePluginZip({
|
|
19
|
-
folderPath: path.resolve(__dirname, 'dist'),
|
|
20
|
-
outPath: path.resolve(__dirname)
|
|
21
|
-
})
|
|
22
|
-
],
|
|
23
|
-
server: {
|
|
24
|
-
port: '4321'
|
|
25
|
-
}
|
|
26
|
-
})
|
|
27
|
-
|
|
28
|
-
|