Skip to content

Commit 0585f1a

Browse files
committed
fix: Use file basename in vue block imports
fixes vuejs#181 fixes vuejs#187
1 parent 758e330 commit 0585f1a

File tree

2 files changed

+53
-27
lines changed

2 files changed

+53
-27
lines changed

src/index.ts

+7-14
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import {
22
createVueFilter,
33
createVuePartRequest,
44
parseVuePartRequest,
5-
resolveVuePart
5+
resolveVuePart,
6+
isVuePartRequest
67
} from './utils'
78
import {
89
createDefaultCompiler,
@@ -63,7 +64,9 @@ export default function VuePlugin(opts: VuePluginOptions = {}): Plugin {
6364
return {
6465
name: 'vue.delegate',
6566

66-
resolveId(id) {
67+
resolveId(id, importer) {
68+
if (!isVuePartRequest(id)) return
69+
id = path.resolve(path.dirname(importer), id)
6770
const ref = parseVuePartRequest(id)
6871
if (ref) {
6972
const element = resolveVuePart(descriptors, ref)
@@ -116,21 +119,11 @@ export default function VuePlugin(opts: VuePluginOptions = {}): Plugin {
116119
)
117120

118121
if (input.template.errors && input.template.errors.length) {
119-
console.error(
120-
'> Errors: ' +
121-
path.relative(process.cwd(), filename) +
122-
'\n' +
123-
input.template.errors.map((error: string) => ' - ' + error).join('\n')
124-
)
122+
input.template.errors.map((error: Error) => this.error(error))
125123
}
126124

127125
if (input.template.tips && input.template.tips.length) {
128-
console.log(
129-
'> Tips: ' +
130-
path.relative(process.cwd(), filename) +
131-
'\n' +
132-
input.template.tips.map((tip: string) => ' - ' + tip).join('\n')
133-
)
126+
input.template.tips.map((message: string) => this.warn({ message }))
134127
}
135128
}
136129

src/utils.ts

+46-13
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
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'
37
import queryString from 'querystring'
8+
import * as path from 'path'
49

510
const GET_QUERY = /\.vue(\.[a-z]+?)?\?(.+)$/i
6-
const PARAM_NAME = 'rollup_plugin_vue'
11+
const PARAM_NAME = 'rollup-plugin-vue'
712

813
export 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

6186
createVuePartRequest.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

Comments
 (0)