import { fileURLToPath } from 'node:url' import { openapi } from '@dify/openapi-content-mapper/vite' import { configDefaults, defineConfig, lazyPlugins } from 'vite-plus' import { playwright } from 'vite-plus/test/browser-playwright' import { customI18nHmrPlugin } from './plugins/vite/custom-i18n-hmr.ts' import { i18nAnalysisPlugin } from './plugins/vite/i18n-analysis.ts' import { getRootClientInjectTarget } from './plugins/vite/inject-target.ts' import { nextStaticImageTestPlugin } from './plugins/vite/next-static-image-test.ts' const projectRoot = fileURLToPath(new URL('.', import.meta.url)) const isCI = !!process.env.CI const rootClientInjectTarget = getRootClientInjectTarget(projectRoot) const browserTestPattern = '{app,features}/**/*.browser.spec.{ts,tsx}' export default defineConfig(({ command, mode, isPreview }) => { const isTest = mode === 'test' const isStorybook = process.env.STORYBOOK === 'true' || process.argv.some((arg) => arg.toLowerCase().includes('storybook')) return { build: { // Match package.json's browserslist; these targets support native top-level await. target: ['chrome111', 'edge111', 'firefox128', 'safari16.4', 'ios16.4'], }, plugins: lazyPlugins(async () => { const { default: react } = await import('@vitejs/plugin-react') if (isTest) return [openapi(), nextStaticImageTestPlugin({ projectRoot }), react()] if (isStorybook) return [openapi(), react()] const [{ default: tailwindcss }, { default: vinext }, { default: Inspect }] = await Promise.all([ import('@tailwindcss/vite'), import('vinext'), import('vite-plugin-inspect'), ]) const inspector = command === 'serve' && isPreview !== true ? (await import('code-inspector-plugin')).codeInspectorPlugin({ bundler: 'vite', }) : undefined return [ openapi(), i18nAnalysisPlugin({ adapters: [ { module: 'i18n/lib.client.ts', exportName: 'useTranslation', namespaceArgument: 0 }, { module: 'i18n/lib.server.ts', exportName: 'useTranslation', namespaceArgument: 0, implementationFunctions: ['getI18nConfig'], }, { module: 'i18n/server.ts', exportName: 'getTranslation', namespaceArgument: 1 }, { module: 'app/route-metadata.ts', exportName: 'getRouteMetadata', namespaceArgument: 0, selectorArgument: 1, }, ], }), Inspect(), inspector, tailwindcss(), react(), vinext({ react: false }), customI18nHmrPlugin({ injectTarget: rootClientInjectTarget }), // reactGrabOpenFilePlugin({ // injectTarget: rootClientInjectTarget, // projectRoot, // }), ] }), resolve: { tsconfigPaths: true, alias: [{ find: '~@', replacement: projectRoot }], }, // vinext related config ...(!isTest && !isStorybook ? { optimizeDeps: { // Keep skipToken shared by the unbundled React Query and oRPC clients. exclude: ['@tanstack/react-query', '@tanstack/query-core'], }, server: { port: 3000, }, } : {}), // Vitest config test: { // Vitest 5 stores Browser Mode failure screenshots as attachments; keep them beside the traces. attachmentsDir: './.vitest-browser/attachments', coverage: { provider: 'v8', // Include authored sources even when no test imports them. include: ['**/*.{js,jsx,ts,tsx,mjs,mts,cjs,cts}'], reporter: isCI ? ['json', 'json-summary'] : ['text', 'html', 'json', 'json-summary'], exclude: [ // Test infrastructure and examples do not belong in the source denominator. '**/{__tests__,__mocks__}/**', '**/*.{spec,test,stories}.*', 'test/**', 'vitest*.setup.ts', // Declarations, generated sources, tool configuration, and build output. '**/*.d.{ts,mts,cts}', '**/*.{generated,gen}.*', 'types/doc-paths.ts', '*.config.*', '.*/**', '**/dist/**', '{coverage,build,out,storybook-static}/**', // Keep public/embed.js, but omit its minified copy and vendored workers/editor. 'public/**/*.min.{js,mjs}', 'public/vs/**', ], }, projects: [ { extends: true, test: { name: 'unit', pool: 'threads', environment: 'happy-dom', globals: true, setupFiles: ['./vitest.setup.ts'], exclude: [...configDefaults.exclude, browserTestPattern], }, }, { extends: true, define: { 'process.env': '{}', }, plugins: lazyPlugins(async () => { const { default: tailwindcss } = await import('@tailwindcss/vite') return [tailwindcss()] }), optimizeDeps: { include: [ '@base-ui/react/fieldset', '@base-ui/react/number-field', '@base-ui/react/slider', 'vite-plus/test/browser', 'dayjs/plugin/relativeTime', 'react-textarea-autosize', ], }, test: { name: 'browser', globals: true, setupFiles: ['./vitest.browser.setup.ts'], include: [browserTestPattern], browser: { enabled: true, provider: playwright(), instances: [{ browser: 'chromium' }], headless: true, screenshotFailures: true, trace: { mode: 'retain-on-failure', tracesDir: './.vitest-browser/traces', }, }, }, }, ], }, } })