Repository: storybook
Issue: [Bug]: Storybook 7, `storybook build` fails with `Entry points must be inside Vite root directory`
Labels: bug,help wanted,compatibility with other tools,has workaround,builder-vite,sev:S3

### Describe the bug

At work I have tried to upgrade to Storybook 7. Altough I was able to fix the HMR issue that the storybook-server-channel url is fixed to localhost with a patch, I still am unable to build at all.

I am using vite with the following configuration for my application

```js
import react from '@vitejs/plugin-react';
import browserslistToEsbuild from 'browserslist-to-esbuild';
import * as dotenv from 'dotenv';
import { defineConfig } from 'vite';
import { chunkSplitPlugin } from 'vite-plugin-chunk-split';
import symfonyPlugin from 'vite-plugin-symfony';
import viteRestart from 'vite-plugin-restart';
import fs from 'fs';
import i18nHotReload from './assets/vite-plugins/i18n-hot-reload';

dotenv.config();

export default defineConfig(({ command }) => ({
  plugins: [
    chunkSplitPlugin({
      customChunk: ({ file }) => {
        // Extract each node_modules package into a separate chunk in "js/npm/(user-)package".
        // Regex taken from: https://github.com/npm/validate-npm-package-name/blob/main/lib/index.js#L3
        const match = file.match(/node_modules\/(?:@([^\/]+?)[\/])?([^\/]+)/);
        if (match) {
          const name = match[1] ? `${match[1]}-${match[2]}` : match[2];
          return `npm/${name}`;
        }
        return null;
      },
    }),
    react(),
    symfonyPlugin({
      viteDevServerHostname: 'localhost',
    }),
    viteRestart({
      reload: ['templates/**/*'],
    }),
    i18nHotReload(),
  ],
  base: '/build/',
  build: {
    target: browserslistToEsbuild(),
    outDir: './public/build',
    rollupOptions: {
      input: {
        main: './assets/main.tsx',
      },
      output: {
        assetFileNames: (assetInfo) => {
          // Place asset in each corresponding folder "build/{img,font,etc.}/*".
          const info = assetInfo.name.split('.');
          let extType = info[info.length - 1];
          if (/png|jpe?g|svg|gif|tiff|bmp|ico/i.test(extType)) {
            extType = 'img';
          } else if (/woff2?|otf|ttf|eot/.test(extType)) {
            extType = 'font';
          }
          return `${extType}/[name].[hash][extname]`;
        },
        chunkFileNames: 'js/[name].[hash].js',
        entryFileNames: 'js/[name].[hash].js',
      },
    },
    manifest: true,
  },
  server: {
    host: '0.0.0.0',
    port: 3000,
    strictPort: true,
    https: {
      // Get certificates if we are not building (CI has no certificates)
      key: command === 'build' ? null : fs.readFileSync('docker/run/certificates/key.pem'),
      cert: command === 'build' ? null : fs.readFileSync('docker/run/certificates/cert.pem'),
    },
    watch: {
      ignored: ['**/.idea/**', '**/tests/**', '**/var/**', '**/vendor/**'],
    },
    origin: `https://${process.env.APP_URL}:3000`,
  },
  // Needed for antd
  css: {
    preprocessorOptions: {
      less: {
        javascriptEnabled: true,
      },
    },
  },
}));
```

and here is my `.storybook/main.ts`

```ts
import type { StorybookConfig } from '@storybook/react-vite';
import * as dotenv from 'dotenv';
import {mergeConfig} from 'vite';

dotenv.config({path: '../.env'});

const config: StorybookConfig = {
  stories: [
    '../assets/**/*.mdx',
    '../assets/**/*.stories.@(js|jsx|ts|tsx)',
  ],
  addons: [
    '@storybook/addon-links',
    '@storybook/addon-essentials',
    '@storybook/addon-interactions',
  ],
  framework: '@storybook/react-vite',
  docs: {
    autodocs: 'tag',
  },
  async viteFinal(config, options) {
    if (options.configType === 'PRODUCTION') {
      // TODO: While "rendering chunks", Vite will error out with "Entry points must be inside Vite root directory"
      return mergeConfig(config, {
        base: '/storybook/',
      });
    }
    return mergeConfig(config, {
      server: {
        watch: {
          ignored: ['**/.idea/**', '**/tests/**', '**/var/**', '**/vendor/**'],
        },
      },
    });
  },
};

export default config;
```

All my stories are in `assets/stories/*`. I moved them from `src/` where the init script put them.

The full output of the build:

```text
www-data@b4a1870105af:/app$ yarn build:storybook
yarn run v1.22.19
$ storybook build --output-dir ./public/storybook
@storybook/cli v7.0.0-rc.10

info => Cleaning outputDir: /public/storybook
info => Loading presets
info => Building manager..
info => Manager built (1.82 s)
info => Copying static files: /app/node_modules/@storybook/manager/static at /app/public/storybook/sb-common-assets
vite v4.2.1 building for production...

./sb-common-assets/fonts.css doesn't exist at build time, it will remain unchanged to be resolved at runtime
transforming (2334) node_modules/rc-virtual-list/es/Item.jsUse of eval in "node_modules/telejson/dist/index.mjs" is strongly discouraged as it poses security risks and may cause issues with minification.
Use of eval in "node_modules/telejson/dist/index.mjs" is strongly discouraged as it poses security risks and may cause issues with minification.
✓ 2341 modules transformed.
Generated an empty chunk: "npm/copy-to-clipboard".
Generated an empty chunk: "npm/html-parse-stringify".
Generated an empty chunk: "npm/json2mq".
Generated an empty chunk: "npm/moment".
Generated an empty chunk: "npm/shallowequal".
Generated an empty chunk: "npm/string-convert".
Generated an empty chunk: "npm/toggle-selection".
Generated an empty chunk: "npm/void-elements".
rendering chunks (181)...Entry points must be inside Vite root directory
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
```

I also had a console log for the `config` param passed to `viteFinal` which looked like this

```text
{
  plugins: [
    {
      name: 'storybook:react-docgen-plugin',
      enforce: 'pre',
      transform: [AsyncFunction: transform]
    },
    {
      name: 'vite-plugin-chunk-split',
      config: [Function: config],
      renderChunk: [Function: renderChunk]
    },
    [
      {
        name: 'vite:react-babel',
        enforce: 'pre',
        config: [Function: config],
        configResolved: [Function: configResolved],
        transform: [AsyncFunction: transform]
      },
      {
        name: 'vite:react-refresh',
        enforce: 'pre',
        config: [Function: config],
        resolveId: [Function: resolveId],
        load: [Function: load],
        transformIndexHtml: [Function: transformIndexHtml]
      },
      {
        name: 'vite:react-jsx',
        enforce: 'pre',
        config: [Function: config],
        resolveId: [Function: resolveId],
        load: [Function: load]
      }
    ],
    {
      name: 'symfony',
      enforce: 'post',
      config: [Function: config],
      configResolved: [Function: configResolved],
      configureServer: [Function: configureServer],
      renderChunk: [AsyncFunction: renderChunk],
      generateBundle: [Function: generateBundle]
    },
    {
      name: 'vite-plugin-restart:0',
      apply: 'serve',
      config: [Function: config],
      configResolved: [Function: configResolved],
      configureServer: [Function: configureServer]
    },
    {
      name: 'i18n-hot-reload',
      handleHotUpdate: [Function: handleHotUpdate]
    },
    {
      name: 'storybook:code-generator-plugin',
      enforce: 'pre',
      configureServer: [Function: configureServer],
      config: [Function: config],
      configResolved: [Function: configResolved],
      resolveId: [Function: resolveId],
      load: [AsyncFunction: load],
      transformIndexHtml: [AsyncFunction: transformIndexHtml]
    },
    {
      name: 'unplugin-csf',
      transformInclude: [Function: transformInclude],
      transform: [Function (anonymous)],
      vite: { enforce: 'pre' },
      enforce: 'pre'
    },
    {
      name: 'storybook:mdx-plugin',
      enforce: 'pre',
      transform: [AsyncFunction: transform]
    },
    {
      name: 'storybook:inject-export-order-plugin',
      enforce: 'post',
      transform: [AsyncFunction: transform]
    },
    {
      name: 'storybook:strip-hmr-boundary-plugin',
      enforce: 'post',
      transform: [AsyncFunction: transform]
    },
    {
      name: 'storybook:allow-storybook-dir',
      enforce: 'post',
      config: [Function: config]
    },
    {
      name: 'storybook:external-globals-plugin',
      enforce: 'post',
      config: [AsyncFunction: config],
      transform: [AsyncFunction: transform]
    },
    {
      name: 'vite:react-docgen-typescript',
      transform: [AsyncFunction: transform]
    }
  ],
  base: './',
  server: {
    host: '0.0.0.0',
    port: 3000,
    strictPort: true,
    https: { key: null, cert: null },
    watch: {
      ignored: [ '**/.idea/**', '**/tests/**', '**/var/**', '**/vendor/**' ]
    },
    origin: 'https://<my-custom-url>.test:3000'
  },
  css: { preprocessorOptions: { less: { javascriptEnabled: true } } },
  configFile: false,
  cacheDir: 'node_modules/.cache/.vite-storybook',
  root: '/app',
  resolve: {
    preserveSymlinks: false,
    alias: {
      assert: '/app/node_modules/browser-assert/lib/assert.js',
      '@storybook/react-dom-shim': '@storybook/react-dom-shim/dist/react-18'
    }
  },
  envPrefix: [ 'VITE_', 'STORYBOOK_' ],
  build: {
    outDir: '/app/public/storybook',
    emptyOutDir: false,
    sourcemap: true,
    rollupOptions: { external: [ './sb-preview/runtime.mjs' ] }
  }
}
```

I don't know if i have misconfigured something or whether there is actually an issue, which is why I'm here.

### To Reproduce

Can't quite explain simply ran `npx sb@next init --builder=vite` and added my custom config

### System

```shell
Environment Info:

  System:
    OS: Linux 5.10 Debian GNU/Linux 10 (buster) 10 (buster)
    CPU: (2) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
  Binaries:
    Node: 18.15.0 - /usr/bin/node
    Yarn: 1.22.19 - /usr/bin/yarn
    npm: 9.5.0 - /usr/bin/npm
  npmPackages:
    @storybook/addon-essentials: ^7.0.0-rc.10 => 7.0.0-rc.10
    @storybook/addon-interactions: ^7.0.0-rc.10 => 7.0.0-rc.10
    @storybook/addon-links: ^7.0.0-rc.10 => 7.0.0-rc.10
    @storybook/blocks: ^7.0.0-rc.10 => 7.0.0-rc.10
    @storybook/react: ^7.0.0-rc.10 => 7.0.0-rc.10
    @storybook/react-vite: ^7.0.0-rc.10 => 7.0.0-rc.10
    @storybook/testing-library: ^0.0.14-next.1 => 0.0.14-next.1
```


### Additional context

_No response_