@miyaoka/vite-plugin-doc-block

Vite plugin to strip custom blocks from Vue SFC
6,898
Install
npm install @miyaoka/vite-plugin-doc-block
Latest Version:0.0.3
Vue:3
License:MIT
Last Updated:Oct 22, 2026
Links: GitHub  ·  npm
Maintainer: miyaoka

@miyaoka/vite-plugin-doc-block

npm version npm downloads license

Vite plugin to strip <doc> custom blocks from Vue SFC.

Write documentation directly in your Vue components. The plugin removes them during build, so they don't affect bundle size.

Install

pnpm add -D @miyaoka/vite-plugin-doc-block

Usage

// vite.config.ts
import { docBlockPlugin } from '@miyaoka/vite-plugin-doc-block'
import vue from '@vitejs/plugin-vue'
import { defineConfig } from 'vite'

export default defineConfig({
  plugins: [docBlockPlugin(), vue()],
})

Note: Place docBlockPlugin() before other Vue-related plugins. This plugin must run before vite-plugin-vue-inspector (used by vite-plugin-vue-devtools), which would error when parsing HTML-like strings (e.g. <RouterView>) inside <doc> blocks.

Then you can write <doc> blocks in your Vue components:

<doc lang="md">
# MyComponent

This component displays user information.

## Props

- `userId` - The user ID to display

## Usage

Used in the user profile page.
</doc>

<script setup lang="ts">
const props = defineProps<{ userId: string }>()
</script>

<template>
  <div>{{ user.name }}</div>
</template>

The <doc> block will be removed during build.

Why

  • Colocation: Keep documentation next to the code it describes
  • Zero runtime cost: Stripped at build time
  • IDE friendly: Documentation is visible when editing the component

Why not a simpler approach?

A common approach to ignore custom blocks is to return an empty module:

// This does NOT work with vite-plugin-vue-inspector
const vueDocsPlugin = {
  name: 'vue-docs',
  transform(_code, id) {
    if (!/vue&type=doc/.test(id))
      return
    return `export default ''`
  },
}

However, this doesn't solve the vite-plugin-vue-inspector issue. The inspector parses the SFC before this transform runs, and HTML-like strings inside <doc> blocks (e.g. <RouterView>) cause parsing errors.

This plugin uses enforce: 'pre' to run before the inspector and removes the content from the SFC source directly, preventing the error.

Dependency scan

Vite's dependency scanner does not run the normal plugin pipeline — it reads .vue files from disk and extracts <script> blocks with a regex. A literal <script> string inside a doc block (even in a markdown code span) would be misdetected as an opening tag and break the scan, skipping dependency pre-bundling.

To prevent this, the plugin also injects a scan-only plugin via optimizeDeps.rolldownOptions.plugins, which parses SFCs containing doc blocks with the real SFC parser and exposes each script block as its own module, mirroring Vite's built-in scan loader.

Note: This scan fix relies on Vite 8's rolldown-based scanner. On Vite 5–7 (esbuild-based scan) the option is ignored, so the scan issue remains there.

License

MIT

Related Packages

vite-plugin-vue-tailwind-auto-reference

Automatically @reference your Tailwind CSS file in Vue SFC blocks.

51,792 34
vite-plugin-vue-i18n-sfc-auto-import

Enable component-scoped i18n with Option API when using vue-i18n in composition...

1,835 1
@vue-sfc-i18n/vite-plugin

Vite plugin for transforming Vue SFC i18n custom blocks at build time

0 0
@vitejs/plugin-vue

The official plugin for Vue SFC support in Vite.

333,357,380 686
vite-plugin-checker

Vite plugin that runs TypeScript type checker on a separate process.

133,975,562 1,242

Version History

Version Released License
0.0.3 MIT
0.0.2 MIT