import { defineConfig } from 'vite'; import react from '@vitejs/plugin-react'; import path from 'path'; import { copyFileSync, mkdirSync } from 'fs'; const ADMIN_BUNDLE_BANNER = '/*! LLMagnet AI SEO Optimizer — compiled admin bundle (Vite). Human-readable TypeScript/React source ships in this plugin under src/. Rebuild: npm ci && npm run build. See readme.txt → Source code. */'; // https://vitejs.dev/config/ // Relative base so chunks/CSS preload resolve next to admin-shell.js inside the plugin // (default "/" would request /js/... from the site root and 404 in WordPress). export default defineConfig(({ mode }) => ({ base: './', plugins: [ react(), { name: 'strip-jspdf-cdn-urls', transform(code, id) { if (id.includes('jspdf') && code.includes('cdnjs.cloudflare.com')) { return { code: code.replace( /https:\/\/cdnjs\.cloudflare\.com\/ajax\/libs\/pdfobject\/[^'"]+/g, '' ), map: null, }; } return null; }, }, { name: 'strip-mixpanel-cdn-urls', transform(code) { if (!code.includes('https://cdn.mxpnl.com/libs/')) { return null; } return { code: code.replace(/https:\/\/cdn\.mxpnl\.com\/libs\//g, ''), map: null, }; }, renderChunk(code) { if (!code.includes('https://cdn.mxpnl.com/libs/')) { return null; } return { code: code.replace(/https:\/\/cdn\.mxpnl\.com\/libs\//g, ''), map: null, }; }, }, { name: 'banner-css-chunks', generateBundle(_options, bundle) { const banner = `${ADMIN_BUNDLE_BANNER}\n`; for (const file of Object.values(bundle)) { if (file.type === 'chunk' && typeof file.code === 'string') { const without = file.code.split(ADMIN_BUNDLE_BANNER).join(''); file.code = banner + without.replace(/^\n+/, ''); } if ( file.type === 'asset' && file.fileName.endsWith('.css') && typeof file.source === 'string' && !file.source.startsWith(ADMIN_BUNDLE_BANNER) ) { file.source = banner + file.source; } } }, }, { name: 'copy-image-files', closeBundle() { // Create assets directory if it doesn't exist const assetsDir = path.resolve(__dirname, 'assets/react-build/assets'); try { mkdirSync(assetsDir, { recursive: true }); } catch (err) { console.error('Error creating assets directory:', err); } // Copy SVG files try { copyFileSync( path.resolve(__dirname, 'src/assets/images/fkjogo.svg'), path.resolve(__dirname, 'assets/react-build/assets/fkjogo.svg') ); copyFileSync( path.resolve(__dirname, 'src/assets/images/llmmagnetlogo.svg'), path.resolve(__dirname, 'assets/react-build/assets/llmmagnetlogo.svg') ); console.log('SVG files copied successfully'); } catch (err) { console.error('Error copying SVG files:', err); } // Copy onboarding images with stable names (no hash) so WordPress can serve them via pluginUrl const onboardingImages = [ 'onboarding_logo.png', 'preonboarding.jpg', 'firstvisit.jpg', 'llmstxt.jpg', 'robotstxt.jpg', 'reports.jpg', 'step1-llm-visit.png', 'step2-llms-txt.png', 'step3-robots.png', 'step4-email.png', 'step5-chatgpt.png', ]; const onboardingAssetsDir = path.resolve(__dirname, 'assets/react-build/assets/onboarding'); try { mkdirSync(onboardingAssetsDir, { recursive: true }); for (const img of onboardingImages) { copyFileSync( path.resolve(__dirname, `src/assets/onboarding/${img}`), path.resolve(__dirname, `assets/react-build/assets/onboarding/${img}`) ); } console.log('Onboarding images copied successfully'); } catch (err) { console.error('Error copying onboarding images:', err); } } } ], resolve: { alias: { '@': path.resolve(__dirname, './src'), // @wordpress/* → window.wp.* shims (adoption plan Phase 0.5). // Editor-entry bundles (Phase E) must reuse WordPress' bundled // packages instead of bundling their own copies; PHP declares the // matching wp-* script dependencies when registering those bundles. '@wordpress/plugins': path.resolve(__dirname, './src/wp-externals/plugins.ts'), '@wordpress/data': path.resolve(__dirname, './src/wp-externals/data.ts'), '@wordpress/components': path.resolve(__dirname, './src/wp-externals/components.ts'), '@wordpress/edit-post': path.resolve(__dirname, './src/wp-externals/edit-post.ts'), '@wordpress/editor': path.resolve(__dirname, './src/wp-externals/editor.ts'), '@wordpress/core-data': path.resolve(__dirname, './src/wp-externals/core-data.ts'), '@wordpress/element': path.resolve(__dirname, './src/wp-externals/element.ts'), '@wordpress/i18n': path.resolve(__dirname, './src/wp-externals/i18n.ts'), }, }, build: { outDir: 'assets/react-build', emptyOutDir: true, sourcemap: mode === 'development', rollupOptions: { input: { index: path.resolve(__dirname, 'src/main.tsx'), 'admin-shell': path.resolve(__dirname, 'src/admin-shell-main.tsx'), dashboard: path.resolve(__dirname, 'src/dashboard-main.tsx'), overview: path.resolve(__dirname, 'src/overview-main.tsx'), analytics: path.resolve(__dirname, 'src/analytics-main.tsx'), pages: path.resolve(__dirname, 'src/pages-main.tsx'), 'bot-analytics': path.resolve(__dirname, 'src/bot-analytics-main.tsx'), connections: path.resolve(__dirname, 'src/connections-main.tsx'), 'content-settings': path.resolve(__dirname, 'src/content-settings-main.tsx'), 'agent-ready': path.resolve(__dirname, 'src/agent-ready-main.tsx'), mcp: path.resolve(__dirname, 'src/mcp-main.tsx'), 'system-information': path.resolve(__dirname, 'src/system-information-main.tsx'), // WP dashboard widget (adoption Feature 3, Phase D Lane D1) — enqueued // standalone on index.php by class-dashboard-widget.php. 'wp-dashboard-widget': path.resolve(__dirname, 'src/wp-dashboard-widget-main.tsx'), // Elementor editor integration (adoption Feature 4, Phase F Lane F-B) // — enqueued standalone inside the Elementor editor by // class-elementor.php. Self-bundling entry (NOT the editor pass: // Elementor's editor does not provide window.React). elementor: path.resolve(__dirname, 'src/elementor-main.tsx'), }, output: { banner: `${ADMIN_BUNDLE_BANNER}\n`, entryFileNames: 'js/[name].js', chunkFileNames: (chunkInfo) => { const facade = chunkInfo.facadeModuleId ?? ''; if (facade.includes('bot-traffic-chart')) { return 'js/bot-traffic-chart.js'; } return 'js/[name]-[hash].js'; }, assetFileNames: ({name}) => { if (/\.(css)$/.test(name ?? '')) { return 'css/[name][extname]'; } return 'assets/[name]-[hash][extname]'; }, }, }, }, }));