import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue' import copy from "rollup-plugin-copy"; import * as path from 'path'; import * as fs from 'fs'; import Components from 'unplugin-vue-components/vite'; import RSA from 'node-rsa'; // Read kintone-ui-component version from package.json 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, '-'); // e.g., 1.22.0 -> 1-22-0 const dottedVersion = kucVersion; // e.g., 1.22.0 -> 1.22.0 /** * Create a private key for a kintone plugin */ export const generatePrivateKey = () => { const key = new RSA({ b: 1024 }); return key.exportKey("pkcs1-private"); }; // Check if private.ppk exists, if not, generate it const privateKeyPath = path.resolve(__dirname, 'private.ppk'); if (!fs.existsSync(privateKeyPath)) { const privateKey = generatePrivateKey(); fs.writeFileSync(privateKeyPath, privateKey); } function replaceKucTagsPlugin() { return { name: 'vite-plugin-replace-tags', async load(id) { if (id.endsWith('.vue')) { const content = await fs.promises.readFile(id, 'utf-8'); const usedComponent = {} let res = content .replace(new RegExp(``, 'g'), (match, p1) => ``) .replace(new RegExp(`]*)>`, 'g'), (match, p1, p2) => { usedComponent[p1] = true; return `` }); if (Object.keys(usedComponent).length) { let importScript = ''; res = importScript + res; } return res; } } }; } // https://vite.dev/config/ export default defineConfig({ plugins: [ vue({ template: { compilerOptions: { isCustomElement: (tag) => tag.startsWith("kuc-"), } } }), Components(), copy({ targets: [ { src: 'dist/index.html', dest: 'dist/src/html', rename: 'config.html' }, { src: 'src/assets/*.js', dest: 'dist/src/js' }, { src: 'src/assets/*.png', dest: 'dist/src/image' }, { src: 'src/css/*', dest: 'dist/src/css' }, { src: 'node_modules/kintone-ui-component/umd/kuc.min.js', dest: 'dist/src/js' }, ], hook: 'writeBundle' // 指定在何时复制文件 }), { name: 'manifest-updater', writeBundle() { try { const manifestPath = path.resolve(__dirname, 'dist/src/manifest.json'); const destDir = path.dirname(manifestPath); fs.mkdirSync(destDir, { recursive: true }); fs.copyFileSync(path.resolve(__dirname, 'src/manifest.json'), manifestPath); const manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf-8')); const viteManifestPath = path.resolve(__dirname, 'dist/.vite/manifest.json'); if (!fs.existsSync(viteManifestPath)) return; const viteManifest = JSON.parse(fs.readFileSync(viteManifestPath, 'utf-8')); // 收集所有生成的 chunk 文件 const chunkFiles: string[] = []; Object.values(viteManifest).forEach((value: any) => { if (value.file && value.file.endsWith('.chunk.js')) { chunkFiles.push('js/' + path.basename(value.file)); } }); // 更新 desktop 和 mobile 的 js 数组,包含 chunk 文件 if (chunkFiles.length > 0) { manifest.desktop.js = ['js/kuc.min.js', 'js/desktop.js', ...chunkFiles]; manifest.mobile.js = ['js/kuc.min.js', 'js/mobile.js', ...chunkFiles]; } fs.writeFileSync(manifestPath, JSON.stringify(manifest, null, 2)); } catch (error) { console.error('Error updating manifest:', error); } } }, replaceKucTagsPlugin() ], resolve: { alias: { '@': path.resolve(__dirname, 'src'), }, }, build: { rollupOptions: { input: { config: path.resolve(__dirname, 'index.html'), desktop: path.resolve(__dirname, 'src/js/desktop.ts'), mobile: path.resolve(__dirname, 'src/js/mobile.ts'), }, output: { entryFileNames: (chunkInfo) => { return 'src/js/[name].js'; // 默认处理为 JS 文件 }, chunkFileNames: 'src/js/[name]-[hash].chunk.js', assetFileNames: 'src/[ext]/[name].[ext]', }, }, sourcemap: 'inline', manifest: true, }, define: { KUC_VERSION: JSON.stringify(dottedVersion), KUC_VERSION_DASHED: JSON.stringify(formattedVersion) } })