Skip to content

Commit 471cc9e

Browse files
authored
refactor: use node hash (#7975)
1 parent e42c759 commit 471cc9e

File tree

4 files changed

+20
-10
lines changed

4 files changed

+20
-10
lines changed

packages/plugin-vue-jsx/index.js

+13-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const babel = require('@babel/core')
33
const jsx = require('@vue/babel-plugin-jsx')
44
const importMeta = require('@babel/plugin-syntax-import-meta')
55
const { createFilter, normalizePath } = require('@rollup/pluginutils')
6-
const hash = require('hash-sum')
6+
const { createHash } = require('crypto')
77
const path = require('path')
88

99
const ssrRegisterHelperId = '/__vue-jsx-ssr-register-helper'
@@ -152,7 +152,7 @@ function vueJsxPlugin(options = {}) {
152152
({ name }) => ({
153153
local: name,
154154
exported: name,
155-
id: hash(id + name)
155+
id: getHash(id + name)
156156
})
157157
)
158158
)
@@ -169,7 +169,7 @@ function vueJsxPlugin(options = {}) {
169169
hotComponents.push({
170170
local: spec.local.name,
171171
exported: spec.exported.name,
172-
id: hash(id + spec.exported.name)
172+
id: getHash(id + spec.exported.name)
173173
})
174174
}
175175
}
@@ -187,15 +187,15 @@ function vueJsxPlugin(options = {}) {
187187
hotComponents.push({
188188
local: node.declaration.name,
189189
exported: 'default',
190-
id: hash(id + 'default')
190+
id: getHash(id + 'default')
191191
})
192192
}
193193
} else if (isDefineComponentCall(node.declaration)) {
194194
hasDefault = true
195195
hotComponents.push({
196196
local: '__default__',
197197
exported: 'default',
198-
id: hash(id + 'default')
198+
id: getHash(id + 'default')
199199
})
200200
}
201201
}
@@ -276,5 +276,13 @@ function isDefineComponentCall(node) {
276276
)
277277
}
278278

279+
/**
280+
* @param {string} text
281+
* @returns {string}
282+
*/
283+
function getHash(text) {
284+
return createHash('sha256').update(text).digest('hex').substring(0, 8)
285+
}
286+
279287
module.exports = vueJsxPlugin
280288
vueJsxPlugin.default = vueJsxPlugin

packages/plugin-vue-jsx/package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@
2626
"@babel/plugin-syntax-import-meta": "^7.10.4",
2727
"@babel/plugin-transform-typescript": "^7.16.8",
2828
"@rollup/pluginutils": "^4.2.1",
29-
"@vue/babel-plugin-jsx": "^1.1.1",
30-
"hash-sum": "^2.0.0"
29+
"@vue/babel-plugin-jsx": "^1.1.1"
3130
},
3231
"peerDependencies": {
3332
"vite": "^2.0.0",

packages/plugin-vue/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
"devDependencies": {
3838
"@rollup/pluginutils": "^4.2.1",
3939
"debug": "^4.3.4",
40-
"hash-sum": "^2.0.0",
4140
"rollup": "^2.72.1",
4241
"slash": "^4.0.0",
4342
"source-map": "^0.6.1",

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import fs from 'fs'
22
import path from 'path'
3+
import { createHash } from 'crypto'
34
import slash from 'slash'
4-
import hash from 'hash-sum'
55
import type { CompilerError, SFCDescriptor } from 'vue/compiler-sfc'
66
import type { ResolvedOptions, VueQuery } from '..'
77

@@ -27,7 +27,7 @@ export function createDescriptor(
2727
// ensure the path is normalized in a way that is consistent inside
2828
// project (relative to root) and on different systems.
2929
const normalizedPath = slash(path.normalize(path.relative(root, filename)))
30-
descriptor.id = hash(normalizedPath + (isProduction ? source : ''))
30+
descriptor.id = getHash(normalizedPath + (isProduction ? source : ''))
3131

3232
cache.set(filename, descriptor)
3333
return { descriptor, errors }
@@ -88,3 +88,7 @@ export function setSrcDescriptor(
8888
}
8989
cache.set(filename, entry)
9090
}
91+
92+
function getHash(text: string): string {
93+
return createHash('sha256').update(text).digest('hex').substring(0, 8)
94+
}

0 commit comments

Comments
 (0)