refactoring

This commit is contained in:
2025-10-17 14:34:45 +08:00
parent 99649dc960
commit cdfee67960
8 changed files with 118 additions and 89 deletions

View File

@@ -5,20 +5,17 @@ import * as path from 'path';
import * as fs from 'fs';
import * as child_process from 'child_process';
import Components from 'unplugin-vue-components/vite';
import RSA from 'node-rsa';
import { generatePrivateKey } from './scripts/privateKey';
const packageJson = require('./package.json');
// 从 package.json 读取 kintone-ui-component 版本信息
const packageJson = JSON.parse(fs.readFileSync(path.resolve(__dirname, 'package.json'), 'utf-8'));
const kucVersion = packageJson.dependencies['kintone-ui-component'];
const formattedVersion = kucVersion.replace(/\./g, '-'); // 例如1.22.0 -> 1-22-0
/**
* 生成 kintone 插件的私钥
*/
export const generatePrivateKey = () => {
const key = new RSA({ b: 1024 });
return key.exportKey('pkcs1-private');
};
// 判断是否为生产环境构建
const isProd = process.env.BUILD_MODE === 'production';
const buildMode = isProd ? 'production' : 'development';
console.log(`Building IIFE versions in ${buildMode} mode...`);
// 检查 private.ppk 是否存在,如果不存在则生成
const privateKeyPath = path.resolve(__dirname, 'private.ppk');
@@ -82,11 +79,11 @@ function buildIIFEVersions() {
try {
// Build desktop IIFE
console.log('Building desktop IIFE...');
child_process.execSync('npx vite build --config vite.desktop.config.ts', { stdio: 'inherit' });
child_process.execSync('npx vite build --config vite.iife.config.ts', { stdio: 'inherit', env: { ...process.env, PLATFORM: 'desktop' } });
// Build mobile IIFE
console.log('Building mobile IIFE...');
child_process.execSync('npx vite build --config vite.mobile.config.ts', { stdio: 'inherit' });
child_process.execSync('npx vite build --config vite.iife.config.ts', { stdio: 'inherit', env: { ...process.env, PLATFORM: 'mobile' } });
console.log('IIFE builds completed successfully.');
} catch (error) {
@@ -165,7 +162,22 @@ export default defineConfig({
assetFileNames: 'src/[ext]/[name].[ext]', // 设置资源文件输出路径
},
},
sourcemap: 'inline', // 生成内联 sourcemap 用于调试
// sourcemap: false
sourcemap: isProd ? false : 'inline', // 生产环境关闭 sourcemap,开发环境使用 inline
minify: isProd ? 'terser' : false, // 生产环境使用 terser 混淆,开发环境不混淆
...(isProd && {
terserOptions: {
compress: {
drop_console: true, // 移除 console
drop_debugger: true, // 移除 debugger
pure_funcs: ['console.log'], // 移除 console.log
},
mangle: {
toplevel: true, // 混淆顶级作用域的变量名
},
format: {
comments: false, // 移除注释
},
},
}),
},
});