vue.jsvitevitepress

Trying to build a lib working with VitePress, Vite build error: failed to resolve import "@siteData"


I'm trying to build a plugin(?) for VitePress, using Vite as the building tool. When I import and use it locally, it works fine. However, when I build the lib, errors occurred:

[vite]: Rollup failed to resolve import "@siteData" from ".../node_modules/.pnpm/vitepress@1.4.2_@.../node_modules/vitepress/dist/client/app/data.js".

Reproduction


Solution

  • VitePress useData API relies on importing @siteData, which is a virtual file provided by VitePress Vite plugin. Therefore, it should be left as-is, VitePress core can handle it.

    Also, being a peer dependency, vitepress should be omitted from the bundle.

    vite.config.ts:

     import vue from '@vitejs/plugin-vue';
     import { defineConfig } from 'vite';
    
     export default defineConfig({
       build: {
         lib: {
           entry: 'src/index.ts',
           formats: ['es'],
         },
         rollupOptions: {
    -      external: ['vue'],
    +      external: ['@siteData', 'vitepress', 'vue'],
         },
         target: 'esnext',
       },
       plugins: [vue()],
     });