Repository: strapi/strapi
File: packages/core/strapi/src/node/vite/config.ts (TypeScript)

Code before:
import path from 'node:path';
import readPkgUp from 'read-pkg-up';
import type { InlineConfig, UserConfig } from 'vite';
import browserslistToEsbuild from 'browserslist-to-esbuild';
import react from '@vitejs/plugin-react-swc';

import { getUserConfig } from '../core/config';
import { isDesignSystemLinked } from '../core/linked-packages';
import { loadStrapiMonorepo } from '../core/monorepo';
import { getMonorepoAliases } from '../core/aliases';
import type { BuildContext } from '../create-build-context';
import { buildFilesPlugin } from './plugins';

/**
 * Resolve module to package root for use in aliases. Ensures pnpm's strict
 * node_modules structure can resolve packages when bundling plugin chunks.
 */
const getModulePath = (mod: string) => {
  const modulePath = require.resolve(mod);
  const pkg = readPkgUp.sync({ cwd: path.dirname(modulePath) });
  return pkg ? path.dirname(pkg.path) : modulePath;
};

const resolveBaseConfig = async (ctx: BuildContext): Promise<InlineConfig> => {
  const target = browserslistToEsbuild(ctx.target);
  const isMonorepoExampleApp = (ctx.strapi as any).internal_config?.uuid === 'getstarted';
  const designSystemLinked = isDesignSystemLinked(ctx.cwd);

  return {
    root: ctx.cwd,
    base: ctx.basePath,
    build: {
      emptyOutDir: false, // Rely on CLI to do this
      outDir: ctx.distDir,
      target,
    },
    cacheDir: 'node_modules/.strapi/vite',
    configFile: false,
    define: {
      process: {},
      'process.env': JSON.stringify(ctx.env),
    },
    envPrefix: 'STRAPI_ADMIN_',

Review comment (refactor):
I think this is duplicated exactly in packages/core/strapi/src/node/webpack/config.ts. Maybe we could put it somewhere shared ?

Code after:
import type { InlineConfig, UserConfig } from 'vite';
import browserslistToEsbuild from 'browserslist-to-esbuild';
import react from '@vitejs/plugin-react-swc';

import { getUserConfig } from '../core/config';
import { getModulePath } from '../core/resolve-module';
import { isDesignSystemLinked } from '../core/linked-packages';
import { loadStrapiMonorepo } from '../core/monorepo';
import { getMonorepoAliases } from '../core/aliases';
import type { BuildContext } from '../create-build-context';
import { buildFilesPlugin } from './plugins';

const resolveBaseConfig = async (ctx: BuildContext): Promise<InlineConfig> => {
  const target = browserslistToEsbuild(ctx.target);
  const isMonorepoExampleApp = (ctx.strapi as any).internal_config?.uuid === 'getstarted';
  const designSystemLinked = isDesignSystemLinked();

  return {
    root: ctx.cwd,
    base: ctx.basePath,
    build: {
      emptyOutDir: false, // Rely on CLI to do this
      outDir: ctx.distDir,
      target,
    },
    cacheDir: 'node_modules/.strapi/vite',
    configFile: false,
    define: {
      process: {},
      'process.env': JSON.stringify(ctx.env),
    },
    envPrefix: 'STRAPI_ADMIN_',
    optimizeDeps: {
      // When design-system is linked (portal:, file:, yarn link), exclude from pre-bundling
      // so changes are reflected without clearing node_modules/.strapi/vite cache
      ...(designSystemLinked && { exclude: ['@strapi/design-system'] }),
      include: [
        // pre-bundle React dependencies to avoid React duplicates,
        // even if React dependencies are not direct dependencies
        // https://react.dev/warnings/invalid-hook-call-warning#duplicate-react
        'react',
        `react/jsx-runtime`,
        'react-dom/client',