1
- import type { Position } from '@vue/compiler-dom' ;
2
1
import type { UnpluginFactory } from 'unplugin' ;
3
2
import { createUnplugin } from 'unplugin' ;
4
- import MagicString from 'magic-string' ;
5
3
import type { Options } from '../types' ;
6
- import { TRACE_ID } from './constants' ;
7
4
import { filter_ID } from './filter_ID' ;
8
- import { parse_ID } from './parse_ID' ;
9
- import { transform_SFC } from './transform_SFC' ;
10
- import { transform_JSX } from './transform_JSX' ;
5
+ import { transform } from './transform' ;
11
6
12
7
export const unpluginFactory : UnpluginFactory < Options > = ( options = { } ) => {
13
8
if ( process . env . NODE_ENV !== 'development' ) {
@@ -16,36 +11,25 @@ export const unpluginFactory: UnpluginFactory<Options> = (options = {}) => {
16
11
} ;
17
12
}
18
13
19
- const { root = process . cwd ( ) , sourceMap = false } = options ;
14
+ const opts = resolveOptions ( options ) ;
20
15
21
16
return {
22
17
name : 'unplugin-vue-source' ,
23
18
enforce : 'pre' ,
24
19
transformInclude : filter_ID ,
25
20
transform ( code , id ) {
26
- const s = new MagicString ( code ) ;
27
-
28
- const parsed = parse_ID ( id , root ) ;
29
- if ( parsed . isSfc ) {
30
- transform_SFC ( code , replace ) ;
31
- } else if ( parsed . isJsx ) {
32
- transform_JSX ( code , replace , parsed ) ;
33
- }
34
-
35
- function replace ( pos : Position ) {
36
- const { offset, line, column } = pos ;
37
- s . prependLeft (
38
- offset ,
39
- ` ${ TRACE_ID } ="${ parsed . file } :${ line } :${ column } "` ,
40
- ) ;
41
- }
42
-
43
- return {
44
- code : s . toString ( ) ,
45
- map : sourceMap ? s . generateMap ( ) : null ,
46
- } ;
21
+ return transform ( code , id , opts ) ;
47
22
} ,
48
23
} ;
49
24
} ;
50
25
26
+ function resolveOptions ( options : Options ) : Required < Options > {
27
+ const { root = process . cwd ( ) , sourceMap = false } = options ;
28
+
29
+ return {
30
+ root,
31
+ sourceMap,
32
+ } ;
33
+ }
34
+
51
35
export default /* #__PURE__ */ createUnplugin ( unpluginFactory ) ;
0 commit comments