@@ -3,63 +3,42 @@ import hash from 'hash-sum';
3
3
import * as path from 'path' ;
4
4
import * as loaderUtils from 'loader-utils' ;
5
5
import * as t from '@babel/types' ;
6
- import traverse from '@babel/traverse' ;
7
- import { File } from '@babel/types'
8
6
import { parse } from '@babel/parser' ;
9
7
import { isDefineComponentCall , parseComponentDecls } from './utils' ;
10
8
11
- const hasJSX = ( file : File ) => {
12
- let fileHasJSX = false ;
13
- traverse ( file , {
14
- JSXElement ( path ) {
15
- fileHasJSX = true ;
16
- path . stop ( ) ;
17
- } ,
18
- JSXFragment ( path ) {
19
- fileHasJSX = true ;
20
- path . stop ( ) ;
21
- } ,
22
- } ) ;
23
-
24
- return fileHasJSX ;
25
- } ;
26
-
27
9
export default function loader (
28
10
this : webpack . loader . LoaderContext ,
29
11
source : string ,
30
12
) {
31
13
const loaderContext = this ;
32
14
loaderContext . cacheable ?.( ) ;
33
15
34
- const isDev = loaderContext . mode === 'development' ;
35
-
36
- if ( ! isDev ) {
16
+ if ( ! ( loaderContext . mode === 'development' ) ) {
37
17
return source ;
38
18
}
39
19
20
+ const webpackRemainingChain = loaderUtils . getRemainingRequest ( loaderContext ) . split ( '!' ) ;
21
+ const fullPath = webpackRemainingChain [ webpackRemainingChain . length - 1 ] ;
22
+ const filename = path . relative ( process . cwd ( ) , fullPath ) ;
23
+
40
24
const file = parse ( source , { sourceType : 'module' , plugins : [ 'jsx' , 'typescript' ] } ) ;
41
25
42
- if ( ! hasJSX ( file ) ) {
26
+ if ( ! ( filename . endsWith ( '.jsx' ) || filename . endsWith ( '.tsx' ) ) ) {
43
27
return source ;
44
28
}
45
29
46
- const webpackRemainingChain = loaderUtils . getRemainingRequest ( loaderContext ) . split ( '!' ) ;
47
- const fullPath = webpackRemainingChain [ webpackRemainingChain . length - 1 ] ;
48
- const filename = path . relative ( process . cwd ( ) , fullPath ) ;
49
-
50
30
const declaredComponents : { name : string } [ ] = [ ] ;
51
31
const hotComponents : {
52
32
local : string ;
53
33
id : string ;
54
34
} [ ] = [ ] ;
55
- let defaultIdentifier : t . Identifier | null = null ;
35
+ let hasDefault = false ;
56
36
57
- traverse ( file , {
58
- VariableDeclaration ( nodePath ) {
59
- declaredComponents . push ( ...parseComponentDecls ( nodePath . node ) ) ;
60
- } ,
61
- ExportNamedDeclaration ( nodePath ) {
62
- const { specifiers = [ ] , declaration } = nodePath . node ;
37
+ for ( const node of file . program . body ) {
38
+ if ( t . isVariableDeclaration ( node ) ) {
39
+ declaredComponents . push ( ...parseComponentDecls ( node ) ) ;
40
+ } else if ( t . isExportNamedDeclaration ( node ) ) {
41
+ const { specifiers = [ ] , declaration } = node ;
63
42
if ( t . isVariableDeclaration ( declaration ) ) {
64
43
hotComponents . push ( ...parseComponentDecls ( declaration ) . map ( ( { name } ) => ( {
65
44
local : name ,
@@ -77,33 +56,30 @@ export default function loader(
77
56
}
78
57
}
79
58
}
80
- } ,
81
- ExportDefaultDeclaration ( nodePath ) {
82
- const { declaration } = nodePath . node ;
59
+ } else if ( t . isExportDefaultDeclaration ( node ) ) {
60
+ const { declaration } = node ;
83
61
if ( t . isIdentifier ( declaration ) ) {
84
62
if ( declaredComponents . find ( d => d . name === declaration . name ) ) {
85
63
hotComponents . push ( {
86
- local : declaration . name ,
64
+ local : '__default__' ,
87
65
id : hash ( `${ filename } -default` )
88
66
} )
89
67
}
90
68
} else if ( isDefineComponentCall ( declaration ) ) {
91
- defaultIdentifier = nodePath . scope . generateUidIdentifier ( 'default' )
92
69
hotComponents . push ( {
93
- local : defaultIdentifier . name ,
70
+ local : '__default__' ,
94
71
id : hash ( `${ filename } -default` )
95
72
} ) ;
96
73
}
97
74
}
98
- } ) ;
75
+ }
99
76
100
77
if ( hotComponents . length ) {
101
- if ( defaultIdentifier ) {
102
- const { name } = defaultIdentifier as t . Identifier ;
78
+ if ( hasDefault ) {
103
79
source = source . replace (
104
80
/ 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,
105
- `const ${ name } = defineComponent`
106
- ) + `\nexport default ${ name } `
81
+ `const __default__ = defineComponent`
82
+ ) + `\nexport default __default__ `
107
83
}
108
84
109
85
let callbackCode = '' ;
0 commit comments