@@ -11,6 +11,7 @@ import {
1111 injectCssOnBuild ,
1212 injectCssOnServer ,
1313} from './inject'
14+ import type { ResolvedConfig } from 'vite'
1415import type { TMatchVariable } from './parser'
1516import type { Options } from './types'
1617
@@ -25,6 +26,7 @@ const unplugin = createUnplugin<Options>(
2526 const CSSFileModuleMap = preProcessCSS ( userOptions )
2627 const vbindVariableList = new Map < string , TMatchVariable > ( )
2728 let isScriptSetup = false
29+ let isServer = false
2830 return [
2931 {
3032 name : NAME ,
@@ -41,18 +43,27 @@ const unplugin = createUnplugin<Options>(
4143 const {
4244 vbindVariableListByPath,
4345 injectCSSContent,
44- } = getVBindVariableListByPath ( descriptor , id , CSSFileModuleMap , ! ! userOptions . server )
46+ } = getVBindVariableListByPath ( descriptor , id , CSSFileModuleMap , isServer )
4547 const variableName = getVariable ( descriptor )
4648 vbindVariableList . set ( id , matchVariable ( vbindVariableListByPath , variableName ) )
4749
48- if ( ! userOptions . server )
50+ if ( ! isServer )
4951 code = injectCssOnBuild ( code , injectCSSContent , descriptor )
5052 }
5153 return code
5254 } catch ( err : unknown ) {
5355 this . error ( `${ NAME } ${ err } ` )
5456 }
5557 } ,
58+ vite : {
59+ // Vite plugin
60+ configResolved ( config : ResolvedConfig ) {
61+ if ( userOptions . server !== undefined )
62+ isServer = userOptions . server
63+ else
64+ isServer = config . command === 'serve'
65+ } ,
66+ } ,
5667 } ,
5768 {
5869 name : `${ NAME } :inject` ,
@@ -61,7 +72,7 @@ const unplugin = createUnplugin<Options>(
6172 // ⭐TODO: 只支持 .vue ? jsx, tsx, js, ts ?
6273 try {
6374 // transform in dev
64- if ( userOptions . server ) {
75+ if ( isServer ) {
6576 if ( id . endsWith ( '.vue' ) ) {
6677 const injectRes = injectCSSVars ( code , vbindVariableList . get ( id ) , isScriptSetup )
6778 code = injectRes . code
0 commit comments