1
- import type { UnpluginFactory } from 'unplugin' ;
2
- import { createUnplugin } from 'unplugin ' ;
3
- import type { Options } from '../types' ;
4
- import { filter_ID } from './filter_ID ' ;
1
+ import { type UnpluginFactory , createUnplugin } from 'unplugin' ;
2
+ import { createFilter } from '@rollup/pluginutils ' ;
3
+ import { type ResolvedOptions , type Options } from '../types' ;
4
+ import { TRACE_ID } from './constants ' ;
5
5
import { transform } from './transform' ;
6
+ import { parse_ID } from './parse_ID' ;
6
7
7
8
export const unpluginFactory : UnpluginFactory < Options > = ( options = { } ) => {
8
9
if ( process . env . NODE_ENV !== 'development' ) {
@@ -12,23 +13,44 @@ export const unpluginFactory: UnpluginFactory<Options> = (options = {}) => {
12
13
}
13
14
14
15
const opts = resolveOptions ( options ) ;
16
+ const filter = createFilter ( opts . include , opts . exclude ) ;
15
17
16
18
return {
17
19
name : 'unplugin-vue-source' ,
18
20
enforce : 'pre' ,
19
- transformInclude : filter_ID ,
21
+ transformInclude ( id ) {
22
+ if ( filter ( id ) ) {
23
+ const parsed = parse_ID ( id ) ;
24
+
25
+ if ( parsed . isSfc ) {
26
+ const { query } = parsed ;
27
+ // vue cli | vue-loader
28
+ if ( query . type === 'template' ) {
29
+ return true ;
30
+ }
31
+ return (
32
+ // vite-plugin-vue
33
+ ! query [ TRACE_ID ] &&
34
+ // rollup-plugin-vue
35
+ ! query [ 'rollup-plugin-vue' ]
36
+ ) ;
37
+ }
38
+
39
+ return true ;
40
+ }
41
+ } ,
20
42
transform ( code , id ) {
21
43
return transform ( code , id , opts ) ;
22
44
} ,
23
45
} ;
24
46
} ;
25
47
26
- function resolveOptions ( options : Options ) : Required < Options > {
27
- const { root = process . cwd ( ) , sourceMap = false } = options ;
28
-
48
+ function resolveOptions ( opts : Options ) : ResolvedOptions {
29
49
return {
30
- root,
31
- sourceMap,
50
+ root : opts . root ?? process . cwd ( ) ,
51
+ sourceMap : opts . sourceMap ?? false ,
52
+ include : opts . include ?? '**/*.{vue,jsx.tsx}' ,
53
+ exclude : opts . exclude ?? 'node_modules/**' ,
32
54
} ;
33
55
}
34
56
0 commit comments