Skip to content

Commit

Permalink
update vite plugin to preserve special file
Browse files Browse the repository at this point in the history
  • Loading branch information
usual2970 committed Oct 23, 2024
1 parent bac0049 commit bff18a7
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 2 deletions.
62 changes: 62 additions & 0 deletions ui/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"zod": "^3.23.8"
},
"devDependencies": {
"@types/fs-extra": "^11.0.4",
"@types/node": "^22.0.0",
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
Expand All @@ -62,6 +63,7 @@
"eslint-plugin-prettier": "^5.2.1",
"eslint-plugin-react-hooks": "^4.6.2",
"eslint-plugin-react-refresh": "^0.4.7",
"fs-extra": "^11.2.0",
"postcss": "^8.4.40",
"prettier": "^3.3.3",
"tailwindcss": "^3.4.7",
Expand Down
33 changes: 31 additions & 2 deletions ui/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,38 @@
import path from "path";
import react from "@vitejs/plugin-react";
import { defineConfig } from "vite";
import { defineConfig, Plugin } from "vite";

import fs from "fs-extra";

const preserveFilesPlugin = (filesToPreserve: string[]): Plugin => {
return {
name: "preserve-files",
apply: "build",
buildStart() {
// 在构建开始时将要保留的文件或目录移动到临时位置
filesToPreserve.forEach((file) => {
const srcPath = path.resolve(__dirname, file);
const tempPath = path.resolve(__dirname, `temp_${file}`);
if (fs.existsSync(srcPath)) {
fs.moveSync(srcPath, tempPath, { overwrite: true });
}
});
},
closeBundle() {
// 在构建完成后将临时位置的文件或目录移回原来的位置
filesToPreserve.forEach((file) => {
const srcPath = path.resolve(__dirname, file);
const tempPath = path.resolve(__dirname, `temp_${file}`);
if (fs.existsSync(tempPath)) {
fs.moveSync(tempPath, srcPath, { overwrite: true });
}
});
},
};
};

export default defineConfig({
plugins: [react()],
plugins: [react(), preserveFilesPlugin(["dist/.gitkeep"])],
resolve: {
alias: {
"@": path.resolve(__dirname, "./src"),
Expand Down

0 comments on commit bff18a7

Please sign in to comment.