Skip to content

Commit

Permalink
refactor: use node hash (#7975)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed May 11, 2022
1 parent e42c759 commit 471cc9e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
18 changes: 13 additions & 5 deletions packages/plugin-vue-jsx/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -152,7 +152,7 @@ function vueJsxPlugin(options = {}) {
({ name }) => ({
local: name,
exported: name,
id: hash(id + name)
id: getHash(id + name)
})
)
)
Expand All @@ -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)
})
}
}
Expand All @@ -187,15 +187,15 @@ function vueJsxPlugin(options = {}) {
hotComponents.push({
local: node.declaration.name,
exported: 'default',
id: hash(id + 'default')
id: getHash(id + 'default')
})
}
} else if (isDefineComponentCall(node.declaration)) {
hasDefault = true
hotComponents.push({
local: '__default__',
exported: 'default',
id: hash(id + 'default')
id: getHash(id + 'default')
})
}
}
Expand Down Expand Up @@ -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
3 changes: 1 addition & 2 deletions packages/plugin-vue-jsx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 0 additions & 1 deletion packages/plugin-vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 6 additions & 2 deletions packages/plugin-vue/src/utils/descriptorCache.ts
Original file line number Diff line number Diff line change
@@ -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 '..'

Expand All @@ -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 }
Expand Down Expand Up @@ -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)
}

0 comments on commit 471cc9e

Please sign in to comment.