From c22e216b1c6d8bbce3cbb4376d82ce15ce149433 Mon Sep 17 00:00:00 2001 From: Evan You Date: Wed, 25 May 2022 10:24:08 +0800 Subject: [PATCH] fix: avoid using native crypto fix #25 --- package.json | 1 + pnpm-lock.yaml | 6 ++++++ src/transform.ts | 12 +++--------- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index b6e0d0cf..bde50df7 100644 --- a/package.json +++ b/package.json @@ -40,6 +40,7 @@ "@types/node": "^16.11.12", "@vitejs/plugin-vue": "^1.9.0", "codemirror": "^5.62.3", + "hash-sum": "^2.0.0", "rimraf": "^3.0.2", "sucrase": "^3.20.1", "typescript": "^4.5.4", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 5ea94f13..8490e135 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -7,6 +7,7 @@ specifiers: '@types/node': ^16.11.12 '@vitejs/plugin-vue': ^1.9.0 codemirror: ^5.62.3 + hash-sum: ^2.0.0 rimraf: ^3.0.2 sucrase: ^3.20.1 typescript: ^4.5.4 @@ -21,6 +22,7 @@ devDependencies: '@types/node': 16.11.36 '@vitejs/plugin-vue': 1.10.2_vite@2.9.9 codemirror: 5.65.3 + hash-sum: 2.0.0 rimraf: 3.0.2 sucrase: 3.21.0 typescript: 4.6.4 @@ -606,6 +608,10 @@ packages: function-bind: 1.1.1 dev: true + /hash-sum/2.0.0: + resolution: {integrity: sha512-WdZTbAByD+pHfl/g9QSsBIIwy8IT+EsPiKDs0KNX+zSHhdDLFKdZu0BQHljvO+0QI/BasbMSUa8wYNCZTvhslg==} + dev: true + /import-lazy/4.0.0: resolution: {integrity: sha512-rKtvo6a868b5Hu3heneU+L4yEQ4jYKLtjpnPeUdK7h0yzXGmyBTypknlkCvHFBqfX9YlorEiMM6Dnq/5atfHkw==} engines: {node: '>=8'} diff --git a/src/transform.ts b/src/transform.ts index 6bcdb6db..d508797f 100644 --- a/src/transform.ts +++ b/src/transform.ts @@ -7,6 +7,8 @@ import { CompilerOptions } from 'vue/compiler-sfc' import { transform } from 'sucrase' +// @ts-ignore +import hashId from 'hash-sum' export const COMP_IDENTIFIER = `__sfc__` @@ -48,7 +50,7 @@ export async function compileFile( return } - const id = await hashId(filename) + const id = hashId(filename) const { errors, descriptor } = store.compiler.parse(code, { filename, sourceMap: true @@ -308,11 +310,3 @@ async function doCompileTemplate( return code } - -async function hashId(filename: string) { - const msgUint8 = new TextEncoder().encode(filename) // encode as (utf-8) Uint8Array - const hashBuffer = await crypto.subtle.digest('SHA-256', msgUint8) // hash the message - const hashArray = Array.from(new Uint8Array(hashBuffer)) // convert buffer to byte array - const hashHex = hashArray.map((b) => b.toString(16).padStart(2, '0')).join('') // convert bytes to hex string - return hashHex.slice(0, 8) -}