diff --git a/src/js/mobile.ts b/src/js/mobile.ts index 8bba15a..c7d5d65 100644 --- a/src/js/mobile.ts +++ b/src/js/mobile.ts @@ -4,7 +4,7 @@ import { KintoneRestAPIClient } from '@kintone/rest-api-client'; (function (PLUGIN_ID) { kintone.events.on('mobile.app.record.index.show', () => { // App id - const appId = kintone.app.getId()?.toString(); + const appId = kintone.mobile.app.getId()?.toString(); if (!appId) return; // get setting diff --git a/vite.config.ts b/vite.config.ts index 725599e..640fdb5 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -3,6 +3,7 @@ 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'; @@ -63,9 +64,59 @@ function replaceKucTagsPlugin() { }; } +// 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: { @@ -77,6 +128,7 @@ export default defineConfig({ 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' }, @@ -84,40 +136,6 @@ export default defineConfig({ ], 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: { @@ -129,19 +147,15 @@ export default defineConfig({ 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), diff --git a/vite.desktop.config.ts b/vite.desktop.config.ts new file mode 100644 index 0000000..836a008 --- /dev/null +++ b/vite.desktop.config.ts @@ -0,0 +1,33 @@ +import * as path from 'path'; +import * as fs from 'fs'; + +// 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 dottedVersion = kucVersion; // e.g., 1.22.0 -> 1.22.0 + +export default { + build: { + lib: { + entry: path.resolve(__dirname, 'src/js/desktop.ts'), + name: 'DesktopPlugin', + formats: ['iife'], + fileName: () => 'desktop.js' + }, + rollupOptions: { + external: ['kintone'], + output: { + globals: { + kintone: 'kintone' + } + } + }, + sourcemap: false, + emptyOutDir: false, + outDir: path.resolve(__dirname, 'dist-temp/desktop') + }, + define: { + KUC_VERSION: JSON.stringify(dottedVersion), + KUC_VERSION_DASHED: JSON.stringify(dottedVersion.replace(/\./g, '-')) + } +} diff --git a/vite.mobile.config.ts b/vite.mobile.config.ts new file mode 100644 index 0000000..0f21d17 --- /dev/null +++ b/vite.mobile.config.ts @@ -0,0 +1,33 @@ +import * as path from 'path'; +import * as fs from 'fs'; + +// 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 dottedVersion = kucVersion; // e.g., 1.22.0 -> 1.22.0 + +export default { + build: { + lib: { + entry: path.resolve(__dirname, 'src/js/mobile.ts'), + name: 'MobilePlugin', + formats: ['iife'], + fileName: () => 'mobile.js' + }, + rollupOptions: { + external: ['kintone'], + output: { + globals: { + kintone: 'kintone' + } + } + }, + sourcemap: false, + emptyOutDir: false, + outDir: path.resolve(__dirname, 'dist-temp/mobile') + }, + define: { + KUC_VERSION: JSON.stringify(dottedVersion), + KUC_VERSION_DASHED: JSON.stringify(dottedVersion.replace(/\./g, '-')) + } +}