Skip to content

Commit

Permalink
perf: use hash to replace createHash (#460)
Browse files Browse the repository at this point in the history
  • Loading branch information
btea authored Oct 18, 2024
1 parent 0fc9cd0 commit de88394
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
13 changes: 11 additions & 2 deletions packages/plugin-vue-jsx/src/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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
13 changes: 11 additions & 2 deletions packages/plugin-vue/src/utils/descriptorCache.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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)
}

0 comments on commit de88394

Please sign in to comment.