Skip to content

Commit

Permalink
fix(plugin-vue): ensure id on descriptor
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Dec 18, 2020
1 parent 7680773 commit fe1848c
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 42 deletions.
20 changes: 11 additions & 9 deletions packages/plugin-vue/src/handleHotUpdate.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import fs from 'fs'
import _debug from 'debug'
import { parse, SFCBlock, SFCDescriptor } from '@vue/compiler-sfc'
import { SFCBlock, SFCDescriptor } from '@vue/compiler-sfc'
import {
createDescriptor,
getDescriptor,
setDescriptor,
setPrevDescriptor
} from './utils/descriptorCache'
import { getResolvedScript, setResolvedScript } from './script'
import { ModuleNode } from 'vite'
import { ModuleNode, ViteDevServer } from 'vite'

const debug = _debug('vite:hmr')

Expand All @@ -16,7 +16,8 @@ const debug = _debug('vite:hmr')
*/
export async function handleHotUpdate(
file: string,
modules: ModuleNode[]
modules: ModuleNode[],
server: ViteDevServer
): Promise<ModuleNode[] | void> {
if (!file.endsWith('.vue')) {
return
Expand All @@ -34,12 +35,13 @@ export async function handleHotUpdate(
content = fs.readFileSync(file, 'utf-8')
}

const { descriptor } = parse(content, {
filename: file,
sourceMap: true
})
setDescriptor(file, descriptor)
setPrevDescriptor(file, prevDescriptor)
const { descriptor } = createDescriptor(
file,
content,
server.config.root,
false
)

let needRerender = false
const affectedModules = new Set<ModuleNode | undefined>()
Expand Down
39 changes: 9 additions & 30 deletions packages/plugin-vue/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
import hash from 'hash-sum'
import path from 'path'
import qs from 'querystring'
import {
parse,
rewriteDefault,
SFCBlock,
SFCDescriptor
} from '@vue/compiler-sfc'
import { rewriteDefault, SFCBlock, SFCDescriptor } from '@vue/compiler-sfc'
import { ResolvedOptions } from '.'
import { getPrevDescriptor, setDescriptor } from './utils/descriptorCache'
import { createDescriptor, getPrevDescriptor } from './utils/descriptorCache'
import { PluginContext, TransformPluginContext } from 'rollup'
import { resolveScript } from './script'
import { transformTemplateInMain } from './template'
Expand All @@ -26,23 +19,13 @@ export async function transformMain(

// prev descriptor is only set and used for hmr
const prevDescriptor = getPrevDescriptor(filename)
const { descriptor, errors } = parse(code, {
sourceMap: true,
filename
})

// set the id on the descriptor
const shortFilePath = path
.relative(root, filename)
.replace(/^(\.\.[\/\\])+/, '')
.replace(/\\/g, '/')

descriptor.id = hash(
isProduction ? shortFilePath + '\n' + code : shortFilePath
const { descriptor, errors } = createDescriptor(
filename,
code,
root,
isProduction
)

setDescriptor(filename, descriptor)

if (errors.length) {
errors.forEach((error) =>
pluginContext.error(createRollupError(filename, error))
Expand Down Expand Up @@ -100,13 +83,9 @@ export async function transformMain(
`_sfc_main.__scopeId = ${JSON.stringify(`data-v-${descriptor.id}`)}`
)
}
if (!isProduction) {
output.push(`_sfc_main.__file = ${JSON.stringify(shortFilePath)}`)
} else if (devServer) {
if (devServer) {
// expose filename during serve for devtools to pickup
output.push(
`_sfc_main.__file = ${JSON.stringify(path.basename(shortFilePath))}`
)
output.push(`_sfc_main.__file = ${JSON.stringify(filename)}`)
}
output.push('export default _sfc_main')

Expand Down
25 changes: 22 additions & 3 deletions packages/plugin-vue/src/utils/descriptorCache.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
import { SFCDescriptor } from '@vue/compiler-sfc'
import path from 'path'
import slash from 'slash'
import hash from 'hash-sum'
import { parse, SFCDescriptor } from '@vue/compiler-sfc'

const cache = new Map<string, SFCDescriptor>()
const prevCache = new Map<string, SFCDescriptor | undefined>()

export function setDescriptor(filename: string, entry: SFCDescriptor) {
cache.set(filename, entry)
export function createDescriptor(
filename: string,
source: string,
root: string,
isProduction: boolean | undefined
) {
const { descriptor, errors } = parse(source, {
filename,
sourceMap: true
})

// ensure the path is normalized in a way that is consistent inside
// project (relative to root) and on different systems.
const normalizedPath = slash(path.normalize(path.relative(root, filename)))
descriptor.id = hash(normalizedPath + (isProduction ? source : ''))

cache.set(filename, descriptor)
return { descriptor, errors }
}

export function getPrevDescriptor(filename: string) {
Expand Down
17 changes: 17 additions & 0 deletions packages/plugin-vue/src/utils/genId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import path from 'path'
import slash from 'slash'
import hash from 'hash-sum'
import { SFCDescriptor } from '@vue/compiler-sfc'

export function genScopeId(
descriptor: SFCDescriptor,
root: string,
isProduction = false
) {
// ensure the path is normalized in a way that is consistent inside
// project (relative to root) and on different systems.
const normalizedPath = slash(
path.normalize(path.relative(root, descriptor.filename))
)
return hash(normalizedPath + (isProduction ? descriptor.source : ''))
}

0 comments on commit fe1848c

Please sign in to comment.