From de883946224767bddb89ec93cd491888df228479 Mon Sep 17 00:00:00 2001 From: btea <2356281422@qq.com> Date: Fri, 18 Oct 2024 12:59:23 +0800 Subject: [PATCH] perf: use `hash` to replace `createHash` (#460) --- packages/plugin-vue-jsx/src/index.ts | 13 +++++++++++-- packages/plugin-vue/src/utils/descriptorCache.ts | 13 +++++++++++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/packages/plugin-vue-jsx/src/index.ts b/packages/plugin-vue-jsx/src/index.ts index 08834b37..ef96326c 100644 --- a/packages/plugin-vue-jsx/src/index.ts +++ b/packages/plugin-vue-jsx/src/index.ts @@ -1,4 +1,4 @@ -import { createHash } from 'node:crypto' +import crypto from 'node:crypto' import path from 'node:path' import type { types } from '@babel/core' import * as babel from '@babel/core' @@ -293,8 +293,17 @@ function isDefineComponentCall( ) } +const hash = + // eslint-disable-next-line n/no-unsupported-features/node-builtins -- crypto.hash is supported in Node 21.7.0+, 20.12.0+ + crypto.hash ?? + (( + algorithm: string, + data: crypto.BinaryLike, + outputEncoding: crypto.BinaryToTextEncoding, + ) => crypto.createHash(algorithm).update(data).digest(outputEncoding)) + function getHash(text: string) { - return createHash('sha256').update(text).digest('hex').substring(0, 8) + return hash('sha256', text, 'hex').substring(0, 8) } export default vueJsxPlugin diff --git a/packages/plugin-vue/src/utils/descriptorCache.ts b/packages/plugin-vue/src/utils/descriptorCache.ts index 9930d268..5a9e7282 100644 --- a/packages/plugin-vue/src/utils/descriptorCache.ts +++ b/packages/plugin-vue/src/utils/descriptorCache.ts @@ -1,6 +1,6 @@ import fs from 'node:fs' import path from 'node:path' -import { createHash } from 'node:crypto' +import crypto from 'node:crypto' import type { CompilerError, SFCDescriptor } from 'vue/compiler-sfc' import { normalizePath } from 'vite' import type { ResolvedOptions, VueQuery } from '../index' @@ -120,6 +120,15 @@ export function setSrcDescriptor( cache.set(filename, entry) } +const hash = + // eslint-disable-next-line n/no-unsupported-features/node-builtins -- crypto.hash is supported in Node 21.7.0+, 20.12.0+ + crypto.hash ?? + (( + algorithm: string, + data: crypto.BinaryLike, + outputEncoding: crypto.BinaryToTextEncoding, + ) => crypto.createHash(algorithm).update(data).digest(outputEncoding)) + function getHash(text: string): string { - return createHash('sha256').update(text).digest('hex').substring(0, 8) + return hash('sha256', text, 'hex').substring(0, 8) }