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'
2
3
import { createFilter , normalizePath } from 'unplugin-utils'
3
4
import { transformVueJsxVapor } from './core'
4
- import { registerHMR } from './core/hmr'
5
+ import { injectHMRAndSSR } from './core/hmr'
5
6
import runtimeCode from './core/runtime?raw'
7
+ import { ssrRegisterHelperCode , ssrRegisterHelperId } from './core/ssr'
8
+ import { transformVueJsx } from './core/vue-jsx'
6
9
import type { Options } from './options'
7
10
import type { UnpluginOptions } from 'unplugin'
8
11
9
12
const plugin = ( options : Options = { } ) : UnpluginOptions [ ] => {
10
13
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 / ,
13
16
)
17
+ let root = ''
14
18
let needHMR = false
19
+ let needSourceMap = false
15
20
return [
16
21
{
17
22
name : 'vue-jsx-vapor' ,
@@ -33,34 +38,62 @@ const plugin = (options: Options = {}): UnpluginOptions[] => {
33
38
}
34
39
} ,
35
40
configResolved ( config ) {
41
+ root = config . root
36
42
needHMR = config . command === 'serve'
43
+ needSourceMap = config . command === 'serve' || ! ! config . build . sourcemap
37
44
} ,
38
45
} ,
39
46
resolveId ( id ) {
47
+ if ( id === ssrRegisterHelperId ) return id
40
48
if ( normalizePath ( id ) === 'vue-jsx-vapor/runtime' ) return id
41
49
} ,
42
50
loadInclude ( id ) {
51
+ if ( id === ssrRegisterHelperId ) return true
43
52
return normalizePath ( id ) === 'vue-jsx-vapor/runtime'
44
53
} ,
45
54
load ( id ) {
55
+ if ( id === ssrRegisterHelperId ) return ssrRegisterHelperCode
46
56
if ( normalizePath ( id ) === 'vue-jsx-vapor/runtime' ) return runtimeCode
47
57
} ,
48
58
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 )
51
61
if ( result ?. code ) {
52
- needHMR && registerHMR ( result , id )
62
+ ; ( needHMR || opt ?. ssr ) &&
63
+ injectHMRAndSSR ( result , id , { ssr : opt ?. ssr , root } )
53
64
return {
54
65
code : result . code ,
55
66
map : result . map ,
56
67
}
57
68
}
58
69
} ,
59
70
} ,
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
+ } ,
60
93
...( options . macros === false
61
94
? [ ]
62
95
: options . macros
63
- ? [ Macros ( options . macros === true ? undefined : options . macros ) ]
96
+ ? [ macros ( options . macros === true ? undefined : options . macros ) ]
64
97
: [ ] ) ,
65
98
]
66
99
}
0 commit comments