添加项目说明图片, 修改打包oss配置

This commit is contained in:
xie7654
2025-07-05 09:59:05 +08:00
parent 361004918f
commit e14730bee7
18 changed files with 657 additions and 106 deletions

View File

@@ -17,3 +17,12 @@ VITE_INJECT_APP_LOADING=true
# 打包后是否生成dist.zip
VITE_ARCHIVER=true
# 阿里云oss配置
VITE_OSS_ENABLED=false
VITE_OSS_REGION=
VITE_OSS_ACCESS_KEY_ID=
VITE_OSS_ACCESS_KEY_SECRET=
VITE_OSS_BUCKET=
VITE_OSS_PREFIX=
VITE_OSS_DELETE_LOCAL=

View File

@@ -47,5 +47,8 @@
"pinia": "catalog:",
"vue": "catalog:",
"vue-router": "catalog:"
},
"devDependencies": {
"ali-oss": "^6.23.0"
}
}

View File

@@ -0,0 +1,77 @@
import fs from 'node:fs';
import path from 'node:path';
import OSS from 'ali-oss';
export default function vitePluginOss(options = {}) {
const {
enabled = false,
region,
accessKeyId,
accessKeySecret,
bucket,
prefix = '',
deleteLocal = false,
} = options;
if (!enabled) {
return {
name: 'vite-plugin-oss',
apply: 'build',
closeBundle() {
console.log('OSS upload disabled');
},
};
}
return {
name: 'vite-plugin-oss',
apply: 'build',
async closeBundle() {
console.log('Starting OSS upload...');
const client = new OSS({
region,
accessKeyId,
accessKeySecret,
bucket,
});
const distPath = path.resolve(process.cwd(), 'dist');
if (!fs.existsSync(distPath)) {
console.error('Dist folder not found');
return;
}
try {
await uploadDirectory(client, distPath, prefix);
console.log('OSS upload completed successfully');
if (deleteLocal) {
fs.rmSync(distPath, { recursive: true, force: true });
console.log('Local dist folder deleted');
}
} catch (error) {
console.error('OSS upload failed:', error);
}
},
};
}
async function uploadDirectory(client, localPath, prefix) {
const files = fs.readdirSync(localPath);
for (const file of files) {
const localFilePath = path.join(localPath, file);
const stats = fs.statSync(localFilePath);
if (stats.isDirectory()) {
await uploadDirectory(client, localFilePath, `${prefix}${file}/`);
} else {
const ossKey = `${prefix}${file}`;
await client.put(ossKey, localFilePath);
console.log(`Uploaded: ${ossKey}`);
}
}
}

View File

@@ -1,13 +1,17 @@
import * as console from 'node:console';
import { defineConfig } from '@vben/vite-config';
import { loadEnv } from 'vite';
import * as console from "node:console";
import vitePluginOss from './plugins/vite-plugin-oss.mjs';
export default defineConfig(async ({ mode }) => {
// eslint-disable-next-line n/prefer-global/process
const env = loadEnv(mode, process.cwd());
// 这样获取
const backendUrl = env.VITE_BACKEND_URL;
console.log(backendUrl)
console.log(backendUrl);
return {
application: {},
vite: {
@@ -21,6 +25,17 @@ export default defineConfig(async ({ mode }) => {
},
},
},
plugins: [
vitePluginOss({
enabled: env.VITE_OSS_ENABLED === 'true',
region: env.VITE_OSS_REGION,
accessKeyId: env.VITE_OSS_ACCESS_KEY_ID,
accessKeySecret: env.VITE_OSS_ACCESS_KEY_SECRET,
bucket: env.VITE_OSS_BUCKET,
prefix: env.VITE_OSS_PREFIX || '',
deleteLocal: env.VITE_OSS_DELETE_LOCAL === 'true',
}),
],
},
};
});