A Vite plugin that provides TypeScript generic syntax support for Vue Styled Components, similar to React styled-components.
npm install @vue-styled-components/pluginA Vite plugin that provides TypeScript generic syntax support for Vue Styled Components, similar to React styled-components.
> [!IMPORTANT]
> The current plugin is not fully mature and may not completely support complex TypeScript types.
- Allows using the styled.tag syntax instead of the original required styled('tag', { props }) syntax
- Automatically transforms at compile time with no runtime performance impact
``bashnpm
npm install @vue-styled-components/plugin --save-dev
Usage
$3
`ts
// vite.config.ts
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import vueStyled from '@vue-styled-components/plugin'export default defineConfig({
plugins: [
// Make sure to use it before vue and vueJsx plugins
vueStyled(),
vue(),
vueJsx(),
],
})
`$3
`ts
vueStyled({
// Included file extensions, default is ['.vue', '.tsx', '.jsx', '.ts', '.js']
include: ['.vue', '.tsx', '.jsx', '.ts', '.js'],
// Excluded file paths, default is ['node_modules']
exclude: ['node_modules'],
// Enable debug mode
debug: false,
// Log level: 'error' | 'warn' | 'info' | 'debug' | 'none'
logLevel: 'error',
// Enable type caching for better performance
enableCache: true,
})
`Examples
$3
`tsx
import styled from '@vue-styled-components/core'interface IconProps {
color?: string
size?: number
}
// Using the new generic syntax
const Icon = styled.span
${props.size || 16}px};// Equivalent to the original syntax
const IconOriginal = styled('span', {
color: String,
size: Number,
})
${props.size || 16}px};
`How It Works
The plugin intercepts source code during the compilation phase, uses an AST parser to analyze the code, searches for
styled.tag patterns, and transforms them into the styled('tag', Props)` format supported by Vue Styled Components.Alpha-2.0