1
- // @ts -check
2
- const babel = require ( '@babel/core' )
3
- const jsx = require ( '@vue/babel-plugin-jsx' )
4
- const importMeta = require ( '@babel/plugin-syntax-import-meta' )
5
- const { createFilter, normalizePath } = require ( '@rollup/pluginutils' )
6
- const { createHash } = require ( 'crypto' )
7
- const path = require ( 'path' )
1
+ import { createHash } from 'crypto'
2
+ import path from 'path'
3
+ import type { types } from '@babel/core'
4
+ import babel from '@babel/core'
5
+ import jsx from '@vue/babel-plugin-jsx'
6
+ // @ts -expect-error missing type
7
+ import importMeta from '@babel/plugin-syntax-import-meta'
8
+ import { createFilter , normalizePath } from '@rollup/pluginutils'
9
+ import type { ComponentOptions } from 'vue'
10
+ import type { Plugin } from 'vite'
11
+ import type { Options } from './types'
12
+
13
+ export * from './types'
8
14
9
15
const ssrRegisterHelperId = '/__vue-jsx-ssr-register-helper'
10
16
const ssrRegisterHelperCode =
@@ -14,10 +20,8 @@ const ssrRegisterHelperCode =
14
20
/**
15
21
* This function is serialized with toString() and evaluated as a virtual
16
22
* module during SSR
17
- * @param {import('vue').ComponentOptions } comp
18
- * @param {string } filename
19
23
*/
20
- function ssrRegisterHelper ( comp , filename ) {
24
+ function ssrRegisterHelper ( comp : ComponentOptions , filename : string ) {
21
25
const setup = comp . setup
22
26
comp . setup = ( props , ctx ) => {
23
27
// @ts -ignore
@@ -29,17 +33,7 @@ function ssrRegisterHelper(comp, filename) {
29
33
}
30
34
}
31
35
32
- /**
33
- * @typedef { import('@rollup/pluginutils').FilterPattern } FilterPattern
34
- * @typedef { { include?: FilterPattern, exclude?: FilterPattern, babelPlugins?: any[] } } CommonOptions
35
- */
36
-
37
- /**
38
- *
39
- * @param {import('@vue/babel-plugin-jsx').VueJSXPluginOptions & CommonOptions } options
40
- * @returns {import('vite').Plugin }
41
- */
42
- function vueJsxPlugin ( options = { } ) {
36
+ function vueJsxPlugin ( options : Options = { } ) : Plugin {
43
37
let root = ''
44
38
let needHmr = false
45
39
let needSourceMap = true
@@ -110,31 +104,28 @@ function vueJsxPlugin(options = {}) {
110
104
sourceMaps : needSourceMap ,
111
105
sourceFileName : id ,
112
106
configFile : false
113
- } )
107
+ } ) !
114
108
115
109
if ( ! ssr && ! needHmr ) {
110
+ if ( ! result . code ) return
116
111
return {
117
112
code : result . code ,
118
113
map : result . map
119
114
}
120
115
}
121
116
117
+ interface HotComponent {
118
+ local : string
119
+ exported : string
120
+ id : string
121
+ }
122
+
122
123
// check for hmr injection
123
- /**
124
- * @type {{ name: string }[] }
125
- */
126
- const declaredComponents = [ ]
127
- /**
128
- * @type {{
129
- * local: string,
130
- * exported: string,
131
- * id: string,
132
- * }[] }
133
- */
134
- const hotComponents = [ ]
124
+ const declaredComponents : { name : string } [ ] = [ ]
125
+ const hotComponents : HotComponent [ ] = [ ]
135
126
let hasDefault = false
136
127
137
- for ( const node of result . ast . program . body ) {
128
+ for ( const node of result . ast ! . program . body ) {
138
129
if ( node . type === 'VariableDeclaration' ) {
139
130
const names = parseComponentDecls ( node , code )
140
131
if ( names . length ) {
@@ -204,7 +195,7 @@ function vueJsxPlugin(options = {}) {
204
195
if ( hotComponents . length ) {
205
196
if ( hasDefault && ( needHmr || ssr ) ) {
206
197
result . code =
207
- result . code . replace (
198
+ result . code ! . replace (
208
199
/ e x p o r t d e f a u l t d e f i n e C o m p o n e n t / g,
209
200
`const __default__ = defineComponent`
210
201
) + `\nexport default __default__`
@@ -239,6 +230,7 @@ function vueJsxPlugin(options = {}) {
239
230
}
240
231
}
241
232
233
+ if ( ! result . code ) return
242
234
return {
243
235
code : result . code ,
244
236
map : result . map
@@ -248,11 +240,7 @@ function vueJsxPlugin(options = {}) {
248
240
}
249
241
}
250
242
251
- /**
252
- * @param {import('@babel/core').types.VariableDeclaration } node
253
- * @param {string } source
254
- */
255
- function parseComponentDecls ( node , source ) {
243
+ function parseComponentDecls ( node : types . VariableDeclaration , source : string ) {
256
244
const names = [ ]
257
245
for ( const decl of node . declarations ) {
258
246
if ( decl . id . type === 'Identifier' && isDefineComponentCall ( decl . init ) ) {
@@ -264,10 +252,7 @@ function parseComponentDecls(node, source) {
264
252
return names
265
253
}
266
254
267
- /**
268
- * @param {import('@babel/core').types.Node } node
269
- */
270
- function isDefineComponentCall ( node ) {
255
+ function isDefineComponentCall ( node ?: types . Node | null ) {
271
256
return (
272
257
node &&
273
258
node . type === 'CallExpression' &&
@@ -276,13 +261,8 @@ function isDefineComponentCall(node) {
276
261
)
277
262
}
278
263
279
- /**
280
- * @param {string } text
281
- * @returns {string }
282
- */
283
- function getHash ( text ) {
264
+ function getHash ( text : string ) {
284
265
return createHash ( 'sha256' ) . update ( text ) . digest ( 'hex' ) . substring ( 0 , 8 )
285
266
}
286
267
287
- module . exports = vueJsxPlugin
288
- vueJsxPlugin . default = vueJsxPlugin
268
+ export default vueJsxPlugin
0 commit comments