Skip to content

Commit

Permalink
Package zip
Browse files Browse the repository at this point in the history
  • Loading branch information
HeHang0 committed Nov 2, 2023
1 parent 31bff08 commit deb5b01
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 31 deletions.
3 changes: 2 additions & 1 deletion web/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ env.d.ts
*.log
.vscode
PingFang-SC-Regular.otf
tsconfig.tsbuildinfo
tsconfig.tsbuildinfo
web.zip
3 changes: 3 additions & 0 deletions web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"scripts": {
"start": "vite",
"build": "run-p type-check build-only",
"build:zip": "cross-env BUILD_ZIP=1 run-p type-check build-only",
"preview": "vite preview",
"build-only": "vite build",
"type-check": "vue-tsc --noEmit"
Expand All @@ -31,6 +32,8 @@
"@types/qrcode": "^1.5.4",
"@vitejs/plugin-vue": "*",
"@vue/tsconfig": "*",
"cross-env": "^7.0.3",
"jszip": "^3.10.1",
"npm-run-all": "*",
"typescript": "*",
"unplugin-auto-import": "*",
Expand Down
8 changes: 1 addition & 7 deletions web/src/stores/play.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import { defineStore } from 'pinia';
import * as api from '../utils/api/api';
import {
Music,
MusicType,
PlayStatus,
Playlist,
SortType
} from '../utils/type';
import { Music, PlayStatus, Playlist, SortType } from '../utils/type';
import { musicOperate } from '../utils/http';
import { StorageKey, storage } from '../utils/storage';
import { generateGuid, getRandomInt } from '../utils/utils';
Expand Down
2 changes: 1 addition & 1 deletion web/tsconfig.node.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
"moduleResolution": "bundler",
"allowSyntheticDefaultImports": true
},
"include": ["vite.config.ts"]
"include": ["vite.config.ts", "zip.ts"]
}
35 changes: 13 additions & 22 deletions web/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,22 @@ import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers';

import { ZipPlugin } from './zip';

const plugins = [
vue(),
AutoImport({
resolvers: [ElementPlusResolver()]
}),
Components({
resolvers: [ElementPlusResolver()]
})
];
process.env.BUILD_ZIP === '1' && plugins.push(ZipPlugin());
// https://vitejs.dev/config/
export default defineConfig({
base: './',
build: {
rollupOptions: {
output: {
// manualChunks(id, meta) {
// if (path.resolve(id).includes(path.resolve('./src/views'))) {
// return 'views';
// } else if (path.resolve(id).includes('node_modules')) {
// return 'node_modules';
// }
// }
}
}
},
plugins: [
vue(),
AutoImport({
resolvers: [ElementPlusResolver()]
}),
Components({
resolvers: [ElementPlusResolver()]
})
],
plugins,
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url))
Expand Down
59 changes: 59 additions & 0 deletions web/zip.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import path from 'path';
import fs from 'fs';
import JSZip from 'jszip';

export const ZipPlugin = function (
fileName: string = 'web',
output: string = null
) {
if (!output) output = path.resolve('dist');
fileName += '.zip';
const makeZip = function () {
const zip = new JSZip();
const distPath = path.resolve(output);
const readDir = function (allFolder: JSZip, dirPath: string) {
const files = fs.readdirSync(dirPath);
files.forEach(fileName => {
const fillPath = path.join(dirPath, './', fileName);
const file = fs.statSync(fillPath);
if (file.isDirectory()) {
const dirZip = allFolder.folder(fileName);
readDir(dirZip, fillPath);
} else {
allFolder.file(fileName, fs.readFileSync(fillPath));
}
});
};
const removeExistedZip = () => {
const dest = path.join(distPath, './' + fileName);
if (fs.existsSync(dest)) {
fs.unlinkSync(dest);
}
};
const zipDir = function () {
readDir(zip, distPath);
zip
.generateAsync({
type: 'nodebuffer',
compression: 'DEFLATE',
compressionOptions: {
level: 9
}
})
.then(content => {
const dest = path.join(distPath, '../' + fileName);
removeExistedZip();
fs.writeFileSync(dest, content);
});
};
removeExistedZip();
zipDir();
};
return {
name: 'vite-plugin-auto-zip',
apply: 'build',
closeBundle() {
makeZip();
}
};
};

0 comments on commit deb5b01

Please sign in to comment.