chore(vite): add missing types and move out utils (#710)

This commit is contained in:
Anthony Fu 2021-10-11 20:29:35 +08:00 committed by GitHub
parent a4e903e55e
commit 2f5938c7b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 18 deletions

View File

@ -1,6 +1,6 @@
import { builtinModules } from 'module' import { builtinModules } from 'module'
import { createHash } from 'crypto'
import * as vite from 'vite' import * as vite from 'vite'
import { hashId, uniq } from './utils'
interface TransformChunk { interface TransformChunk {
id: string, id: string,
@ -68,10 +68,10 @@ async function transformRequestRecursive (viteServer: vite.ViteDevServer, id, pa
return Object.values(chunks) return Object.values(chunks)
} }
export async function bundleRequest (viteServer: vite.ViteDevServer, entryURL) { export async function bundleRequest (viteServer: vite.ViteDevServer, entryURL: string) {
const chunks = await transformRequestRecursive(viteServer, entryURL) const chunks = await transformRequestRecursive(viteServer, entryURL)
const listIds = ids => ids.map(id => `// - ${id} (${hashId(id)})`).join('\n') const listIds = (ids: string[]) => ids.map(id => `// - ${id} (${hashId(id)})`).join('\n')
const chunksCode = chunks.map(chunk => ` const chunksCode = chunks.map(chunk => `
// -------------------- // --------------------
// Request: ${chunk.id} // Request: ${chunk.id}
@ -167,18 +167,3 @@ async function __instantiateModule__(url, urlStack) {
return { code } return { code }
} }
function hashId (id: string) {
return '$id_' + hash(id)
}
function hash (input: string, length = 8) {
return createHash('sha256')
.update(input)
.digest('hex')
.substr(0, length)
}
export function uniq<T> (arr: T[]): T[] {
return Array.from(new Set(arr))
}

View File

@ -0,0 +1,16 @@
import { createHash } from 'crypto'
export function hashId (id: string) {
return '$id_' + hash(id)
}
export function hash (input: string, length = 8) {
return createHash('sha256')
.update(input)
.digest('hex')
.substr(0, length)
}
export function uniq<T> (arr: T[]): T[] {
return Array.from(new Set(arr))
}