165 lines
5.4 KiB
TypeScript
165 lines
5.4 KiB
TypeScript
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 * as child_process from 'child_process';
|
|
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(`</kuc-([a-zA-Z0-9-]+)(?![0-9-])>`, 'g'), (match, p1) => `</kuc-${p1}-${formattedVersion}>`)
|
|
.replace(new RegExp(`<kuc-([a-zA-Z0-9-]+)(?![0-9-])([^>]*)>`, 'g'), (match, p1, p2) => {
|
|
usedComponent[p1] = true;
|
|
return `<kuc-${p1}-${formattedVersion}${p2}>`
|
|
});
|
|
if (Object.keys(usedComponent).length) {
|
|
let importScript = '<script lang="ts">'
|
|
Object.keys(usedComponent).forEach((key) => {
|
|
const keyPascal = key.split('-')
|
|
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
|
|
.join('');
|
|
if (key === 'multi-choice') {
|
|
key = 'multichoice';
|
|
}
|
|
importScript += `import * as Kuc${keyPascal} from "kintone-ui-component/lib/${key}";`
|
|
});
|
|
importScript += '</script>';
|
|
res = importScript + res;
|
|
}
|
|
|
|
return res;
|
|
}
|
|
}
|
|
};
|
|
}
|
|
|
|
// Function to build IIFE versions of desktop and mobile scripts
|
|
function buildIIFEVersions() {
|
|
return {
|
|
name: 'iife-builder',
|
|
buildStart() {
|
|
console.log('Building IIFE versions...');
|
|
|
|
try {
|
|
// Build desktop IIFE
|
|
console.log('Building desktop IIFE...');
|
|
child_process.execSync('npx vite build --config vite.desktop.config.ts', { stdio: 'inherit' });
|
|
|
|
// Build mobile IIFE
|
|
console.log('Building mobile IIFE...');
|
|
child_process.execSync('npx vite build --config vite.mobile.config.ts', { stdio: 'inherit' });
|
|
|
|
console.log('IIFE builds completed successfully.');
|
|
} catch (error) {
|
|
console.error('Error building IIFE versions:', error);
|
|
throw error;
|
|
}
|
|
},
|
|
generateBundle(options, bundle) {
|
|
// Copy and overwrite the main dist/src/js files with IIFE versions
|
|
const desktopIIFEPath = path.resolve(__dirname, 'dist-temp/desktop/desktop.js');
|
|
const mobileIIFEPath = path.resolve(__dirname, 'dist-temp/mobile/mobile.js');
|
|
const destDir = path.resolve(__dirname, 'dist/src/js');
|
|
|
|
if (fs.existsSync(desktopIIFEPath)) {
|
|
fs.mkdirSync(destDir, { recursive: true });
|
|
fs.copyFileSync(desktopIIFEPath, path.join(destDir, 'desktop.js'));
|
|
console.log('Copied desktop.js to dist/src/js/');
|
|
}
|
|
|
|
if (fs.existsSync(mobileIIFEPath)) {
|
|
fs.mkdirSync(destDir, { recursive: true });
|
|
fs.copyFileSync(mobileIIFEPath, path.join(destDir, 'mobile.js'));
|
|
console.log('Copied mobile.js to dist/src/js/');
|
|
}
|
|
|
|
// Clean up dist-temp directory
|
|
if (fs.existsSync(path.resolve(__dirname, 'dist-temp'))) {
|
|
fs.rmSync(path.resolve(__dirname, 'dist-temp'), { recursive: true, force: true });
|
|
console.log('Cleaned up dist-temp directory');
|
|
}
|
|
}
|
|
};
|
|
}
|
|
|
|
// https://vite.dev/config/
|
|
export default defineConfig({
|
|
plugins: [
|
|
buildIIFEVersions(),
|
|
vue({
|
|
template: {
|
|
compilerOptions: {
|
|
isCustomElement: (tag) => tag.startsWith("kuc-"),
|
|
}
|
|
}
|
|
}),
|
|
Components(),
|
|
copy({
|
|
targets: [
|
|
{ src: 'dist/index.html', dest: 'dist/src/html', rename: 'config.html' },
|
|
{ src: 'src/manifest.json', dest: 'dist/src' },
|
|
{ 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' // 指定在何时复制文件
|
|
}),
|
|
replaceKucTagsPlugin()
|
|
],
|
|
resolve: {
|
|
alias: {
|
|
'@': path.resolve(__dirname, 'src'),
|
|
},
|
|
},
|
|
build: {
|
|
rollupOptions: {
|
|
input: {
|
|
config: path.resolve(__dirname, 'index.html'),
|
|
},
|
|
output: {
|
|
entryFileNames: (chunkInfo) => {
|
|
return 'src/js/[name].js'; // 默认处理为 JS 文件
|
|
},
|
|
assetFileNames: 'src/[ext]/[name].[ext]',
|
|
},
|
|
},
|
|
sourcemap: 'inline',
|
|
},
|
|
define: {
|
|
KUC_VERSION: JSON.stringify(dottedVersion),
|
|
KUC_VERSION_DASHED: JSON.stringify(formattedVersion)
|
|
}
|
|
})
|