From 471cc9e060d721215337c88d9c7030d6bb991555 Mon Sep 17 00:00:00 2001 From: Bjorn Lu Date: Thu, 12 May 2022 02:47:03 +0800 Subject: [PATCH] refactor: use node hash (#7975) --- packages/plugin-vue-jsx/index.js | 18 +++++++++++++----- packages/plugin-vue-jsx/package.json | 3 +-- packages/plugin-vue/package.json | 1 - .../plugin-vue/src/utils/descriptorCache.ts | 8 ++++++-- 4 files changed, 20 insertions(+), 10 deletions(-) diff --git a/packages/plugin-vue-jsx/index.js b/packages/plugin-vue-jsx/index.js index 929f4fb4..41fcb911 100644 --- a/packages/plugin-vue-jsx/index.js +++ b/packages/plugin-vue-jsx/index.js @@ -3,7 +3,7 @@ const babel = require('@babel/core') const jsx = require('@vue/babel-plugin-jsx') const importMeta = require('@babel/plugin-syntax-import-meta') const { createFilter, normalizePath } = require('@rollup/pluginutils') -const hash = require('hash-sum') +const { createHash } = require('crypto') const path = require('path') const ssrRegisterHelperId = '/__vue-jsx-ssr-register-helper' @@ -152,7 +152,7 @@ function vueJsxPlugin(options = {}) { ({ name }) => ({ local: name, exported: name, - id: hash(id + name) + id: getHash(id + name) }) ) ) @@ -169,7 +169,7 @@ function vueJsxPlugin(options = {}) { hotComponents.push({ local: spec.local.name, exported: spec.exported.name, - id: hash(id + spec.exported.name) + id: getHash(id + spec.exported.name) }) } } @@ -187,7 +187,7 @@ function vueJsxPlugin(options = {}) { hotComponents.push({ local: node.declaration.name, exported: 'default', - id: hash(id + 'default') + id: getHash(id + 'default') }) } } else if (isDefineComponentCall(node.declaration)) { @@ -195,7 +195,7 @@ function vueJsxPlugin(options = {}) { hotComponents.push({ local: '__default__', exported: 'default', - id: hash(id + 'default') + id: getHash(id + 'default') }) } } @@ -276,5 +276,13 @@ function isDefineComponentCall(node) { ) } +/** + * @param {string} text + * @returns {string} + */ +function getHash(text) { + return createHash('sha256').update(text).digest('hex').substring(0, 8) +} + module.exports = vueJsxPlugin vueJsxPlugin.default = vueJsxPlugin diff --git a/packages/plugin-vue-jsx/package.json b/packages/plugin-vue-jsx/package.json index 11e83bac..f62a6cd9 100644 --- a/packages/plugin-vue-jsx/package.json +++ b/packages/plugin-vue-jsx/package.json @@ -26,8 +26,7 @@ "@babel/plugin-syntax-import-meta": "^7.10.4", "@babel/plugin-transform-typescript": "^7.16.8", "@rollup/pluginutils": "^4.2.1", - "@vue/babel-plugin-jsx": "^1.1.1", - "hash-sum": "^2.0.0" + "@vue/babel-plugin-jsx": "^1.1.1" }, "peerDependencies": { "vite": "^2.0.0", diff --git a/packages/plugin-vue/package.json b/packages/plugin-vue/package.json index 5ee997fe..22ce5ad5 100644 --- a/packages/plugin-vue/package.json +++ b/packages/plugin-vue/package.json @@ -37,7 +37,6 @@ "devDependencies": { "@rollup/pluginutils": "^4.2.1", "debug": "^4.3.4", - "hash-sum": "^2.0.0", "rollup": "^2.72.1", "slash": "^4.0.0", "source-map": "^0.6.1", diff --git a/packages/plugin-vue/src/utils/descriptorCache.ts b/packages/plugin-vue/src/utils/descriptorCache.ts index 5143a613..890c817a 100644 --- a/packages/plugin-vue/src/utils/descriptorCache.ts +++ b/packages/plugin-vue/src/utils/descriptorCache.ts @@ -1,7 +1,7 @@ import fs from 'fs' import path from 'path' +import { createHash } from 'crypto' import slash from 'slash' -import hash from 'hash-sum' import type { CompilerError, SFCDescriptor } from 'vue/compiler-sfc' import type { ResolvedOptions, VueQuery } from '..' @@ -27,7 +27,7 @@ export function createDescriptor( // ensure the path is normalized in a way that is consistent inside // project (relative to root) and on different systems. const normalizedPath = slash(path.normalize(path.relative(root, filename))) - descriptor.id = hash(normalizedPath + (isProduction ? source : '')) + descriptor.id = getHash(normalizedPath + (isProduction ? source : '')) cache.set(filename, descriptor) return { descriptor, errors } @@ -88,3 +88,7 @@ export function setSrcDescriptor( } cache.set(filename, entry) } + +function getHash(text: string): string { + return createHash('sha256').update(text).digest('hex').substring(0, 8) +}