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'
37import queryString from 'querystring'
8+ import * as path from 'path'
49
510const GET_QUERY = / \. v u e ( \. [ a - z ] + ?) ? \? ( .+ ) $ / i
6- const PARAM_NAME = 'rollup_plugin_vue '
11+ const PARAM_NAME = 'rollup-plugin-vue '
712
813export interface VuePartRequest {
9- filename : string ,
14+ filename : string
1015 meta : VuePartRequestMeta
1116}
1217
@@ -24,7 +29,10 @@ export interface VuePartRequestCreator {
2429 }
2530}
2631
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 {
2836 const filter = createFilter ( include || '**/*.vue' , exclude )
2937
3038 return id => filter ( id )
@@ -37,7 +45,15 @@ export function getVueMetaFromQuery(id: string): VuePartRequestMeta | null {
3745 const query = queryString . parse ( match [ 2 ] )
3846
3947 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
4157 }
4258 }
4359
@@ -48,14 +64,23 @@ export function isVuePartRequest(id: string): boolean {
4864 return getVueMetaFromQuery ( id ) !== null
4965}
5066
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 => {
5273 lang = lang || createVuePartRequest . defaultLang [ type ]
5374
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 ( '.' )
5782
58- return `${ filename } .${ lang } ?${ queryString . stringify ( query ) } `
83+ return `${ path . basename ( filename ) } .${ lang } ?${ queryString . stringify ( query ) } `
5984} ) as VuePartRequestCreator
6085
6186createVuePartRequest . defaultLang = {
@@ -78,15 +103,23 @@ export function parseVuePartRequest(id: string): VuePartRequest | undefined {
78103 }
79104}
80105
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 {
82110 const descriptor = descriptors . get ( filename )
83111
84112 if ( ! descriptor ) throw Error ( 'File not processed yet, ' + filename )
85113
86114 const blocks = descriptor [ meta . type ]
87115 const block = Array . isArray ( blocks ) ? blocks [ meta . index as number ] : blocks
88116
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+ )
90123
91124 return block
92125}
0 commit comments