Vue 2 plugin for Vite that provides Vue 2 single file component support in Vite build tool
npx @tessl/cli install tessl/npm-vitejs--plugin-vue2@2.3.0@vitejs/plugin-vue2 is a Vite plugin that provides Vue 2 Single File Component (SFC) support for Vite build tool. It compiles Vue 2 templates, handles asset URL transformations, and provides comprehensive options for configuring the Vue 2 compiler including script, template, and style compilation settings.
npm install @vitejs/plugin-vue2import vue from '@vitejs/plugin-vue2';
import type { Options, VueQuery } from '@vitejs/plugin-vue2';
import { parseVueRequest } from '@vitejs/plugin-vue2';For CommonJS:
const vue = require('@vitejs/plugin-vue2');
const { parseVueRequest } = require('@vitejs/plugin-vue2');import vue from '@vitejs/plugin-vue2';
export default {
plugins: [
vue({
include: /\.vue$/,
template: {
transformAssetUrls: {
img: ['src'],
video: ['src', 'poster']
}
}
})
]
}@vitejs/plugin-vue2 is built around several key components:
Creates a Vite plugin instance configured for Vue 2 SFC processing with customizable compilation options.
/**
* Creates a Vite plugin for Vue 2 Single File Component support
* @param rawOptions - Configuration options for the plugin
* @returns Vite Plugin instance with Vue 2 SFC processing capabilities
*/
function vue(rawOptions?: Options): Plugin;
interface Options {
/** Files to include for processing (default: /\.vue$/) */
include?: string | RegExp | (string | RegExp)[];
/** Files to exclude from processing */
exclude?: string | RegExp | (string | RegExp)[];
/** Production mode flag (auto-detected if not specified) */
isProduction?: boolean;
/** Script compilation options */
script?: Partial<Pick<SFCScriptCompileOptions, 'babelParserPlugins'>>;
/** Template compilation options */
template?: Partial<Pick<SFCTemplateCompileOptions,
| 'compiler'
| 'compilerOptions'
| 'preprocessOptions'
| 'transpileOptions'
| 'transformAssetUrls'
| 'transformAssetUrlsOptions'
>>;
/** Style compilation options */
style?: Partial<Pick<SFCStyleCompileOptions, 'trim'>>;
/** Custom Vue compiler instance (defaults to auto-resolved vue/compiler-sfc) */
compiler?: typeof _compiler;
}
interface ResolvedOptions extends Options {
/** Resolved Vue compiler instance (required) */
compiler: typeof _compiler;
/** Project root directory */
root: string;
/** Source map generation flag */
sourceMap: boolean;
/** CSS dev source map flag */
cssDevSourcemap: boolean;
/** Vite dev server instance */
devServer?: ViteDevServer;
/** Vue devtools support flag */
devToolsEnabled?: boolean;
}Utility function for parsing Vue-specific request URLs to extract filename and query parameters used internally by the plugin.
/**
* Parses Vue request URLs to extract filename and query parameters
* @param id - Request URL string
* @returns Object containing filename and parsed query parameters
*/
function parseVueRequest(id: string): {
filename: string;
query: VueQuery;
};
interface VueQuery {
/** Vue file processing flag */
vue?: boolean;
/** Source path reference for external file blocks */
src?: string;
/** Block type for SFC processing */
type?: 'script' | 'template' | 'style' | 'custom';
/** Block index for multiple blocks of same type */
index?: number;
/** Language/preprocessor identifier */
lang?: string;
/** Raw content flag bypassing transformations */
raw?: boolean;
/** Scoped CSS processing flag */
scoped?: boolean;
}For TypeScript users working with Vue SFC components, these types from vue/compiler-sfc are commonly used:
// Import Vue SFC types for advanced usage
import type {
SFCDescriptor,
SFCBlock,
SFCScriptBlock,
SFCTemplateBlock,
SFCStyleBlock,
SFCScriptCompileOptions,
SFCTemplateCompileOptions,
SFCStyleCompileOptions
} from 'vue/compiler-sfc';import vue from '@vitejs/plugin-vue2';
export default {
plugins: [vue()]
}import vue from '@vitejs/plugin-vue2';
export default {
plugins: [
vue({
include: [/\.vue$/, /\.md$/],
exclude: /node_modules/,
template: {
compilerOptions: {
whitespace: 'condense'
},
transformAssetUrls: {
video: ['src', 'poster'],
source: ['src'],
img: ['src'],
image: ['xlink:href', 'href'],
use: ['xlink:href', 'href']
}
},
script: {
babelParserPlugins: ['decorators-legacy']
}
})
]
}import vue from '@vitejs/plugin-vue2';
const vueI18nPlugin = {
name: 'vue-i18n',
transform(code, id) {
if (!/vue&type=i18n/.test(id)) {
return;
}
if (/\.ya?ml$/.test(id)) {
code = JSON.stringify(require('js-yaml').load(code.trim()));
}
return `export default Comp => {
Comp.i18n = ${code}
}`;
}
};
export default {
plugins: [vue(), vueI18nPlugin]
}The plugin automatically transforms asset URLs in Vue templates to ES module imports:
Template:
<template>
<img src="../assets/logo.png" />
<video poster="../assets/thumbnail.jpg">
<source src="../assets/video.mp4" />
</video>
</template>Transforms to:
<script>
import _imports_0 from '../assets/logo.png';
import _imports_1 from '../assets/thumbnail.jpg';
import _imports_2 from '../assets/video.mp4';
</script>
<template>
<img :src="_imports_0" />
<video :poster="_imports_1">
<source :src="_imports_2" />
</video>
</template>The plugin fully supports TypeScript in Vue components:
<template>
<div>{{ greeting }}</div>
</template>
<script lang="ts">
import { defineComponent } from 'vue';
interface User {
name: string;
age: number;
}
export default defineComponent({
data(): { user: User } {
return {
user: { name: 'Alice', age: 25 }
};
},
computed: {
greeting(): string {
return `Hello, ${this.user.name}!`;
}
}
});
</script><template>
<div class="container">
<p class="text">Scoped styles</p>
</div>
</template>
<style scoped>
.container {
background: #f0f0f0;
}
.text {
color: #333;
}
</style><template>
<div :class="$style.container">
<p :class="$style.text">CSS Modules</p>
</div>
</template>
<style module>
.container {
background: #f0f0f0;
}
.text {
color: #333;
}
</style>The plugin provides seamless HMR support during development:
Note: Both Vue and Vite must be installed as peer dependencies. The plugin will automatically resolve the Vue compiler from your project's dependencies.
npm install vue@^2.7.0 vite@^5.0.0 @vitejs/plugin-vue2