1
- import { SFCDescriptor , SFCBlock , SFCCustomBlock } from '@vue/component-compiler-utils'
2
- import { createFilter } from 'rollup-pluginutils'
1
+ import {
2
+ SFCDescriptor ,
3
+ SFCBlock ,
4
+ SFCCustomBlock
5
+ } from '@vue/component-compiler-utils'
6
+ import { createFilter } from 'rollup-pluginutils'
3
7
import queryString from 'querystring'
8
+ import * as path from 'path'
4
9
5
10
const GET_QUERY = / \. v u e ( \. [ a - z ] + ?) ? \? ( .+ ) $ / i
6
- const PARAM_NAME = 'rollup_plugin_vue '
11
+ const PARAM_NAME = 'rollup-plugin-vue '
7
12
8
13
export interface VuePartRequest {
9
- filename : string ,
14
+ filename : string
10
15
meta : VuePartRequestMeta
11
16
}
12
17
@@ -24,7 +29,10 @@ export interface VuePartRequestCreator {
24
29
}
25
30
}
26
31
27
- export function createVueFilter ( include : string | undefined , exclude : string | undefined ) : ( file : string ) => boolean {
32
+ export function createVueFilter (
33
+ include : string | undefined ,
34
+ exclude : string | undefined
35
+ ) : ( file : string ) => boolean {
28
36
const filter = createFilter ( include || '**/*.vue' , exclude )
29
37
30
38
return id => filter ( id )
@@ -37,7 +45,15 @@ export function getVueMetaFromQuery(id: string): VuePartRequestMeta | null {
37
45
const query = queryString . parse ( match [ 2 ] )
38
46
39
47
if ( PARAM_NAME in query ) {
40
- return JSON . parse ( query [ PARAM_NAME ] as string )
48
+ const data : string = ( Array . isArray ( query [ PARAM_NAME ] )
49
+ ? query [ PARAM_NAME ] [ 0 ]
50
+ : query [ PARAM_NAME ] ) as string
51
+
52
+ const [ type , index , lang ] = data . split ( '.' )
53
+
54
+ return ( lang
55
+ ? { type, lang, index : parseInt ( index ) } // styles.0.css
56
+ : { type, lang : index } ) as VuePartRequestMeta // script.js
41
57
}
42
58
}
43
59
@@ -48,14 +64,23 @@ export function isVuePartRequest(id: string): boolean {
48
64
return getVueMetaFromQuery ( id ) !== null
49
65
}
50
66
51
- export const createVuePartRequest : VuePartRequestCreator = ( ( filename : string , lang : string | undefined , type : string , index ?: number ) : string => {
67
+ export const createVuePartRequest : VuePartRequestCreator = ( (
68
+ filename : string ,
69
+ lang : string | undefined ,
70
+ type : string ,
71
+ index ?: number
72
+ ) : string => {
52
73
lang = lang || createVuePartRequest . defaultLang [ type ]
53
74
54
- const query = {
55
- [ PARAM_NAME ] : JSON . stringify ( { type, index, lang} )
56
- }
75
+ const match = GET_QUERY . exec ( filename )
76
+
77
+ const query = match ? queryString . parse ( match [ 2 ] ) : { }
78
+
79
+ query [ PARAM_NAME ] = [ type , index , lang ]
80
+ . filter ( it => it !== undefined )
81
+ . join ( '.' )
57
82
58
- return `${ filename } .${ lang } ?${ queryString . stringify ( query ) } `
83
+ return `${ path . basename ( filename ) } .${ lang } ?${ queryString . stringify ( query ) } `
59
84
} ) as VuePartRequestCreator
60
85
61
86
createVuePartRequest . defaultLang = {
@@ -78,15 +103,23 @@ export function parseVuePartRequest(id: string): VuePartRequest | undefined {
78
103
}
79
104
}
80
105
81
- export function resolveVuePart ( descriptors : Map < string , SFCDescriptor > , { filename, meta} : VuePartRequest ) : SFCBlock | SFCCustomBlock {
106
+ export function resolveVuePart (
107
+ descriptors : Map < string , SFCDescriptor > ,
108
+ { filename, meta } : VuePartRequest
109
+ ) : SFCBlock | SFCCustomBlock {
82
110
const descriptor = descriptors . get ( filename )
83
111
84
112
if ( ! descriptor ) throw Error ( 'File not processed yet, ' + filename )
85
113
86
114
const blocks = descriptor [ meta . type ]
87
115
const block = Array . isArray ( blocks ) ? blocks [ meta . index as number ] : blocks
88
116
89
- if ( ! block ) throw Error ( `Requested (type=${ meta . type } & index=${ meta . index } ) block not found in ${ filename } ` )
117
+ if ( ! block )
118
+ throw Error (
119
+ `Requested (type=${ meta . type } & index=${
120
+ meta . index
121
+ } ) block not found in ${ filename } `
122
+ )
90
123
91
124
return block
92
125
}
0 commit comments