Skip to content

Commit de88394

Browse files
authored
perf: use hash to replace createHash (#460)
1 parent 0fc9cd0 commit de88394

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

packages/plugin-vue-jsx/src/index.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createHash } from 'node:crypto'
1+
import crypto from 'node:crypto'
22
import path from 'node:path'
33
import type { types } from '@babel/core'
44
import * as babel from '@babel/core'
@@ -293,8 +293,17 @@ function isDefineComponentCall(
293293
)
294294
}
295295

296+
const hash =
297+
// eslint-disable-next-line n/no-unsupported-features/node-builtins -- crypto.hash is supported in Node 21.7.0+, 20.12.0+
298+
crypto.hash ??
299+
((
300+
algorithm: string,
301+
data: crypto.BinaryLike,
302+
outputEncoding: crypto.BinaryToTextEncoding,
303+
) => crypto.createHash(algorithm).update(data).digest(outputEncoding))
304+
296305
function getHash(text: string) {
297-
return createHash('sha256').update(text).digest('hex').substring(0, 8)
306+
return hash('sha256', text, 'hex').substring(0, 8)
298307
}
299308

300309
export default vueJsxPlugin

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

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import fs from 'node:fs'
22
import path from 'node:path'
3-
import { createHash } from 'node:crypto'
3+
import crypto from 'node:crypto'
44
import type { CompilerError, SFCDescriptor } from 'vue/compiler-sfc'
55
import { normalizePath } from 'vite'
66
import type { ResolvedOptions, VueQuery } from '../index'
@@ -120,6 +120,15 @@ export function setSrcDescriptor(
120120
cache.set(filename, entry)
121121
}
122122

123+
const hash =
124+
// eslint-disable-next-line n/no-unsupported-features/node-builtins -- crypto.hash is supported in Node 21.7.0+, 20.12.0+
125+
crypto.hash ??
126+
((
127+
algorithm: string,
128+
data: crypto.BinaryLike,
129+
outputEncoding: crypto.BinaryToTextEncoding,
130+
) => crypto.createHash(algorithm).update(data).digest(outputEncoding))
131+
123132
function getHash(text: string): string {
124-
return createHash('sha256').update(text).digest('hex').substring(0, 8)
133+
return hash('sha256', text, 'hex').substring(0, 8)
125134
}

0 commit comments

Comments
 (0)