Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vite separate config file resolve problem #4460

Closed
4 tasks done
lavitalite opened this issue Jan 2, 2025 · 1 comment
Closed
4 tasks done

vite separate config file resolve problem #4460

lavitalite opened this issue Jan 2, 2025 · 1 comment
Labels
bug: pending triage Maybe a bug, waiting for confirmation

Comments

@lavitalite
Copy link

lavitalite commented Jan 2, 2025

Describe the bug

separate vite config file is not resolve as expected

Reproduction

// .vitepress/config.ts

export default defineConfig({
  srcDir: 'content',
  base: BASE_PATH,
  outDir: 'dist',
  cacheDir: 'cache',
  markdown: {
    config: (md) => {
      md.use(groupIconMdPlugin)
    }
  },
  themeConfig: {
    // theme-level options
  }
})

running DEBUG=vite:config,vite:resolve,vite:debug-alias vitepress dev

debug info showing vitepress config as expect

  vite:config   vitepress: {
  vite:config     root: '/Users/ayao/repos/vita-ui/packages/docs',
  vite:config     srcDir: '/Users/ayao/repos/vita-ui/packages/docs/content',

but separate vite config is not respect(i expect it to be /Users/ayao/repos/vita-ui/packages/docs`

which is where the script is running and the separate config file vite.config.ts root field is config like that
root: __dirname,

  vite:config bundled config file loaded in 34.09ms +0ms
  vite:config no config file found. +53ms
  vite:config using resolved config: {
  vite:config   root: '/Users/ayao/repos/vita-ui/packages/docs/content',
  vite:config   base: '/wiki/',
  vite:config   cacheDir: '/Users/ayao/repos/vita-ui/packages/docs/cache',
  vite:config   plugins: [

my viteconfig as following

import { defineConfig, createLogger } from 'vite'
import path, { resolve } from 'path'
import { fileURLToPath } from 'node:url'

import Components from 'unplugin-vue-components/vite'
import AutoImport from 'unplugin-auto-import/vite'
import Inspect from 'vite-plugin-inspect'
import { groupIconVitePlugin, localIconLoader } from 'vitepress-plugin-group-icons'


const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const logger = createLogger();

export default defineConfig({
  optimizeDeps: {
    exclude: [
      'vitepress',
    ],
  },
  logLevel: 'info',
  clearScreen: false,
  resolve: {
    alias: {
      "@utils": resolve(__dirname, "./utils"),
      "@": resolve(__dirname, "./content"),
      "@oss": resolve(__dirname, "./oss"),
    }
  },
  plugins: [
    {
      name: 'vite:debug-alias',
      configResolved(config) {
        debugger
        logger.info(`Alias config: ${JSON.stringify(config.resolve.alias)}`);
      },
      resolveId(id) {
        if (id.startsWith('@utils')) {
          logger.info(`Resolving @utils path: ${id}`);
        }
      },
    },
    Inspect({
      build: true,
      dev: true,
      outputDir: '.vite-inspect',
    }),
    Components({
      dirs: [
        "components",
      ],
      include: [
        /\.vue$/,
        /\.vue\?vue/,
      ],
      dts: "components.d.ts"
    }),
    AutoImport({
      imports: ["vue", "@vueuse/core"],
      dirs: ["composables"],
      vueTemplate: true,
      dts: "auto-imports.d.ts"
    }),
    groupIconVitePlugin({
      customIcon: {
        'docker': 'vscode-icons:file-type-docker2',
        'k8s': localIconLoader(import.meta.url, './assets/devicon--kubernetes.svg'),
        'dir': localIconLoader(import.meta.url, './assets/mingcute--directory-line.svg'),
        'bnf': localIconLoader(import.meta.url, './assets/file-icons--bnf.svg'),
      },
    }),

  ],
})

Expected behavior

all my plugin didn't register in merge config

 plugins: [
  vite:config     'vite:optimized-deps',
  vite:config     'vite:watch-package-data',
  vite:config     'vite:pre-alias',
,,,
 ]
 ~  tree -L 2
├── content
│   └── index.md
├── logs
│   └── debug.log
└── vite.config.ts
├── .vitepress
│   └── config.ts

one thing to know that the separte vite config is respect if config srcDir to cwd(meaning leaving empty with default)
the separate vite config will be respect and all the plugin is register successfully
but i prefer flat structure, wish to pull all my markdown file in content direcotry, so i was hoping this issue could be resolve

System Info

~  npx envinfo --system --npmPackages vitepress --binaries --browsers

  System:
    OS: macOS 14.4
    CPU: (8) arm64 Apple M1
    Memory: 90.00 MB / 8.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.18.1 - ~/n/bin/node
    npm: 10.8.2 - ~/n/bin/npm
    pnpm: 9.15.2 - ~/n/bin/pnpm
    Watchman: 2024.12.02.00 - /opt/homebrew/bin/watchman
  Browsers:
    Chrome Canary: 133.0.6933.0
    Safari: 17.4
  npmPackages:
    vitepress: catalog: => 1.5.0

Additional context

No response

Validations

@lavitalite lavitalite added the bug: pending triage Maybe a bug, waiting for confirmation label Jan 2, 2025
@brc-dd
Copy link
Member

brc-dd commented Jan 2, 2025

vite.config.ts should be inside srcDir for auto-discovery to work.

You can customize vite.configFile in vitepress settings if you want to keep it outside:

// .vitepress/config.ts

export default defineConfig({
  srcDir: 'content', // relative to root
  vite: {
    configFile: 'vite.config.ts' // relative to cwd
                                 // you can use fileURLToPath(import.meta.resolve('../vite.config.mts'))
                                 // if you want it to be relative to file
  }
})

@brc-dd brc-dd closed this as completed Jan 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug: pending triage Maybe a bug, waiting for confirmation
Projects
None yet
Development

No branches or pull requests

2 participants