Skip to content

Commit 26a8dc1

Browse files
committed
feat: support custom component id generator
1 parent de88394 commit 26a8dc1

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

packages/plugin-vue/src/index.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,23 @@ export interface Options {
138138
* - **default:** `false`
139139
*/
140140
prodHydrationMismatchDetails?: boolean
141+
/**
142+
* Customize the component ID generation strategy.
143+
* - `'filepath'`: hash the file path (relative to the project root)
144+
* - `'filepath-source'`: hash the file path and the source code
145+
* - `function`: custom function that takes the file path, source code,
146+
* whether in production mode, and the default hash function as arguments
147+
* - **default:** `'filepath'` in development, `'filepath-source'` in production
148+
*/
149+
componentIdGenerator?:
150+
| 'filepath'
151+
| 'filepath-source'
152+
| ((
153+
filepath: string,
154+
source: string,
155+
isProduction: boolean | undefined,
156+
getHash: (text: string) => string,
157+
) => string)
141158
}
142159

143160
/**

packages/plugin-vue/src/utils/descriptorCache.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,14 @@ const prevCache = new Map<string, SFCDescriptor | undefined>()
2222
export function createDescriptor(
2323
filename: string,
2424
source: string,
25-
{ root, isProduction, sourceMap, compiler, template }: ResolvedOptions,
25+
{
26+
root,
27+
isProduction,
28+
sourceMap,
29+
compiler,
30+
template,
31+
features,
32+
}: ResolvedOptions,
2633
hmr = false,
2734
): SFCParseResult {
2835
const { descriptor, errors } = compiler.parse(source, {
@@ -34,7 +41,23 @@ export function createDescriptor(
3441
// ensure the path is normalized in a way that is consistent inside
3542
// project (relative to root) and on different systems.
3643
const normalizedPath = normalizePath(path.relative(root, filename))
37-
descriptor.id = getHash(normalizedPath + (isProduction ? source : ''))
44+
45+
const componentIdGenerator = features?.componentIdGenerator
46+
if (componentIdGenerator === 'filepath') {
47+
descriptor.id = getHash(normalizedPath)
48+
} else if (componentIdGenerator === 'filepath-source') {
49+
descriptor.id = getHash(normalizedPath + source)
50+
} else if (typeof componentIdGenerator === 'function') {
51+
descriptor.id = componentIdGenerator(
52+
normalizedPath,
53+
source,
54+
isProduction,
55+
getHash,
56+
)
57+
} else {
58+
descriptor.id = getHash(normalizedPath + (isProduction ? source : ''))
59+
}
60+
3861
;(hmr ? hmrCache : cache).set(filename, descriptor)
3962
return { descriptor, errors }
4063
}

0 commit comments

Comments
 (0)