vite-plugin-zip-file 1.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 shawn.wu
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,44 @@
1
+ # vite-plugin-zip-file
2
+ Zip files at build time.
3
+
4
+ # Install
5
+
6
+ ```
7
+ yarn add vite-plugin-zip-file --dev
8
+ ```
9
+
10
+ or
11
+
12
+ ```
13
+ npm install vite-plugin-zip-file --save-dev
14
+ ```
15
+
16
+ # Optons
17
+
18
+
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 |
24
+
25
+
26
+ # Usage
27
+ ```
28
+ import { defineConfig } from 'vite'
29
+ import { vitePluginZip } from 'vite-plugin-zip'
30
+ import path from 'path';
31
+ import { fileURLToPath } from 'url';
32
+ const __dirname = path.dirname(fileURLToPath(import.meta.url));
33
+
34
+ // https://vitejs.dev/config/
35
+ export default defineConfig({
36
+ plugins: [
37
+ vitePluginZip({
38
+ folderPath: path.resolve(__dirname, 'dist'),
39
+ outPath: path.resolve(__dirname),
40
+ zipName: 'Test.zip'
41
+ })
42
+ ]
43
+ })
44
+ ```
package/index.html ADDED
@@ -0,0 +1,20 @@
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>
@@ -0,0 +1 @@
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;
@@ -0,0 +1,42 @@
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
+ };
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "vite-plugin-zip-file",
3
+ "version": "1.0.0",
4
+ "description": "zip files at build time.",
5
+ "type": "module",
6
+ "main": "lib/index.cjs.js",
7
+ "module": "lib/index.es.js",
8
+ "scripts": {
9
+ "dev": "vite",
10
+ "build": "vite build",
11
+ "build-d": "node --inspect-brk=9229 ./node_modules/vite/dist/node/cli.js build",
12
+ "build-plugin": "vite --config plugin.config.js build"
13
+ },
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git+ssh://git@github.com/Ssis53/vite-plugin-zip.git"
17
+ },
18
+ "keywords": [
19
+ "vite",
20
+ "zip",
21
+ "compress"
22
+ ],
23
+ "author": "Shawn Wu",
24
+ "license": "MIT",
25
+ "bugs": {
26
+ "url": "https://github.com/Ssis53/vite-plugin-zip/issues"
27
+ },
28
+ "homepage": "https://github.com/Ssis53/vite-plugin-zip#readme",
29
+ "peerDependencies": {
30
+ "vite": "^3.0.7"
31
+ },
32
+ "dependencies": {
33
+ "jszip": "^3.10.1"
34
+ },
35
+ "devDependencies": {
36
+ "@rollup/plugin-node-resolve": "^13.3.0"
37
+ }
38
+ }
@@ -0,0 +1,26 @@
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) => `index.${format}.js`,
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 ADDED
@@ -0,0 +1,8 @@
1
+ /*
2
+ * @Author: xiangfu.wu
3
+ * @Date: 2022-08-18 16:37:47
4
+ * @Description: 🚀
5
+ * @FilePath: /vite-plugin-zip-file/src/main.js
6
+ */
7
+
8
+ document.querySelector('#app').innerHTML = 'vite-plugin-zip';
@@ -0,0 +1,75 @@
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 ADDED
@@ -0,0 +1,28 @@
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
+