Skip to content

Commit

Permalink
chore(vite): add missing types and move out utils (nuxt#710)
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu authored Oct 11, 2021
1 parent a4e903e commit 2f5938c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
21 changes: 3 additions & 18 deletions packages/vite/src/dev-bundler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { builtinModules } from 'module'
import { createHash } from 'crypto'
import * as vite from 'vite'
import { hashId, uniq } from './utils'

interface TransformChunk {
id: string,
Expand Down Expand Up @@ -68,10 +68,10 @@ async function transformRequestRecursive (viteServer: vite.ViteDevServer, id, pa
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 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 => `
// --------------------
// Request: ${chunk.id}
Expand Down Expand Up @@ -167,18 +167,3 @@ async function __instantiateModule__(url, urlStack) {

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))
}
16 changes: 16 additions & 0 deletions packages/vite/src/utils.ts
Original file line number Diff line number Diff line change
@@ -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))
}

0 comments on commit 2f5938c

Please sign in to comment.