import { defineConfig, loadEnv, type UserConfig } from 'vite' import react from '@vitejs/plugin-react' import path from 'node:path' import { cmsLivePlugin } from './vite/live' import { cmsMockPlugin } from './vite/mock' export default defineConfig(({ command, mode }): UserConfig => { const env = { ...loadEnv(mode, process.cwd(), ''), ...process.env } as Record const editorSrc = path.resolve(env.CMS_EDITOR_SRC || '../ecommerce/backoffice/web/packages/cms-editor/src') const common: UserConfig = { plugins: [react()], resolve: { alias: { '@k6n/cms-editor': path.join(editorSrc, 'index.ts') }, // The shared package imports tiptap from its own location; keep a single copy. dedupe: ['react', 'react-dom', '@tiptap/core', '@tiptap/pm'], }, server: { port: 5173, strictPort: true /* the issuer's CORS allowlist names localhost:5173 */, fs: { allow: [process.cwd(), path.resolve(editorSrc, '..')] } }, test: { environment: 'jsdom', environmentOptions: { jsdom: { pretendToBeVisual: true } }, include: ['test/**/*.test.{ts,tsx}', 'src/**/*.test.{ts,tsx}', 'mock/**/*.test.ts'], setupFiles: ['test/setup.ts'], }, } as UserConfig if (command === 'build') { // The artifact served at /editor/onpage-editor.js. One self-contained file; // only loaded when the preview cookie is present (see src/main.ts). return { ...common, define: { 'process.env.NODE_ENV': '"production"' }, build: { outDir: 'dist', lib: { entry: 'src/main.ts', formats: ['es'], fileName: () => 'onpage-editor.js' }, sourcemap: true, cssCodeSplit: false, }, } } const mock = (env.CMS_MODE || 'live') === 'mock' return { ...common, plugins: [...(common.plugins ?? []), mock ? cmsMockPlugin() : cmsLivePlugin(env)], } })