1- import Macros from '@vue-jsx-vapor/macros/raw'
1+ import macros from '@vue-jsx-vapor/macros/raw'
2+ import { transformJsxDirective } from '@vue-macros/jsx-directive/api'
23import { createFilter , normalizePath } from 'unplugin-utils'
34import { transformVueJsxVapor } from './core'
4- import { registerHMR } from './core/hmr'
5+ import { injectHMRAndSSR } from './core/hmr'
56import runtimeCode from './core/runtime?raw'
7+ import { ssrRegisterHelperCode , ssrRegisterHelperId } from './core/ssr'
8+ import { transformVueJsx } from './core/vue-jsx'
69import type { Options } from './options'
710import type { UnpluginOptions } from 'unplugin'
811
912const plugin = ( options : Options = { } ) : UnpluginOptions [ ] => {
1013 const transformInclude = createFilter (
11- options ?. include || / \. [ c m ] ? [ j t ] s x ? $ / ,
12- options ?. exclude ,
14+ options ?. include || / \. [ c m ] ? [ j t ] s x $ / ,
15+ options ?. exclude || / n o d e _ m o d u l e s / ,
1316 )
17+ let root = ''
1418 let needHMR = false
19+ let needSourceMap = false
1520 return [
1621 {
1722 name : 'vue-jsx-vapor' ,
@@ -33,34 +38,62 @@ const plugin = (options: Options = {}): UnpluginOptions[] => {
3338 }
3439 } ,
3540 configResolved ( config ) {
41+ root = config . root
3642 needHMR = config . command === 'serve'
43+ needSourceMap = config . command === 'serve' || ! ! config . build . sourcemap
3744 } ,
3845 } ,
3946 resolveId ( id ) {
47+ if ( id === ssrRegisterHelperId ) return id
4048 if ( normalizePath ( id ) === 'vue-jsx-vapor/runtime' ) return id
4149 } ,
4250 loadInclude ( id ) {
51+ if ( id === ssrRegisterHelperId ) return true
4352 return normalizePath ( id ) === 'vue-jsx-vapor/runtime'
4453 } ,
4554 load ( id ) {
55+ if ( id === ssrRegisterHelperId ) return ssrRegisterHelperCode
4656 if ( normalizePath ( id ) === 'vue-jsx-vapor/runtime' ) return runtimeCode
4757 } ,
4858 transformInclude,
49- transform ( code , id ) {
50- const result = transformVueJsxVapor ( code , id , options )
59+ transform ( code , id , opt ?: { ssr ?: boolean } ) {
60+ const result = transformVueJsxVapor ( code , id , options , needSourceMap )
5161 if ( result ?. code ) {
52- needHMR && registerHMR ( result , id )
62+ ; ( needHMR || opt ?. ssr ) &&
63+ injectHMRAndSSR ( result , id , { ssr : opt ?. ssr , root } )
5364 return {
5465 code : result . code ,
5566 map : result . map ,
5667 }
5768 }
5869 } ,
5970 } ,
71+ {
72+ name : '@vue-macros/jsx-directive' ,
73+ transformInclude,
74+ transform ( code , id , opt ?: { ssr ?: boolean } ) {
75+ if ( options . interop || opt ?. ssr ) {
76+ return transformJsxDirective ( code , id , {
77+ lib : 'vue' ,
78+ prefix : 'v-' ,
79+ version : 3.6 ,
80+ } )
81+ }
82+ } ,
83+ } ,
84+ {
85+ name : '@vitejs/plugin-vue-jsx' ,
86+ transformInclude,
87+ transform ( code , id , opt ?: { ssr ?: boolean } ) {
88+ if ( options . interop || opt ?. ssr ) {
89+ return transformVueJsx ( code , id , needSourceMap )
90+ }
91+ } ,
92+ } ,
6093 ...( options . macros === false
6194 ? [ ]
6295 : options . macros
63- ? [ Macros ( options . macros === true ? undefined : options . macros ) ]
96+ ? [ macros ( options . macros === true ? undefined : options . macros ) ]
6497 : [ ] ) ,
6598 ]
6699}
0 commit comments