Skip to content

Commit

Permalink
perf: improve speed
Browse files Browse the repository at this point in the history
1. Replace hash algorithm `sha256` with `md5`

2. Skip write file if hash is same
  • Loading branch information
hemengke1997 committed Mar 28, 2023
1 parent 38c109a commit 740b038
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
10 changes: 8 additions & 2 deletions __test__/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import path from 'path'
import { describe, expect, it, test } from 'vitest'
import { eq, isPublicTypescript, linebreak, setEol } from '../src/utils'
import { getContentHash } from '../src/utils/build'
import { eq, extractHashFromFileName, getContentHash, isPublicTypescript, linebreak, setEol } from '../src/utils'
import { getGlobalConfig, setGlobalConfig } from '../src/utils/globalConfig'

describe('vite-plugin-public-typescript', () => {
Expand Down Expand Up @@ -72,4 +71,11 @@ describe('vite-plugin-public-typescript', () => {
setGlobalConfig({ config: { publicDir: 'public' }, inputDir: 'publicTypescript' })
expect(() => getGlobalConfig()).not.toThrowError()
})

test('should extract hash', () => {
const hash1 = extractHashFromFileName('dir/hello.1234.js', 4)
const hash2 = extractHashFromFileName('hello.1234', 4)
expect(hash1).toBe('1234')
expect(hash2).toBe('1234')
})
})
12 changes: 1 addition & 11 deletions src/utils/build.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { createHash } from 'crypto'
import path from 'path'
import type { ResolvedConfig } from 'vite'
import { normalizePath } from 'vite'
Expand All @@ -10,16 +9,7 @@ import type { VPPTPluginOptions } from '..'
import { name } from '../../package.json'
import { getGlobalConfig } from './globalConfig'
import { assert } from './assert'
import { debug, writeFile } from '.'

export function getContentHash(chunk: string | Uint8Array | undefined, hash?: VPPTPluginOptions['hash']) {
if (!chunk) {
return ''
}
const hashLen = typeof hash === 'number' ? hash : 8

return createHash('sha256').update(chunk).digest('hex').substring(0, hashLen)
}
import { debug, getContentHash, writeFile } from '.'

const noSideEffectsPlugin: Plugin = {
name: 'no-side-effects',
Expand Down
26 changes: 26 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import path from 'path'
import { createHash } from 'crypto'
import type { WebSocketServer } from 'vite'
import { normalizePath } from 'vite'
import fs from 'fs-extra'
import createDebug from 'debug'
import { name as PKGNAME } from '../../package.json'
import type { VPPTPluginOptions } from '..'

export const debug = createDebug(PKGNAME)

Expand Down Expand Up @@ -80,6 +82,11 @@ export function writeFile(filename: string, content: string): void {
const newContent = setEol(content)

if (fs.existsSync(filename)) {
if (extractHashFromFileName(filename)) {
// if filename has hash, skip write file
debug('skip writeFile, filename has hash')
return
}
// Read content first
// if content is same, skip write file
const oldContent = fs.readFileSync(filename, 'utf-8')
Expand All @@ -93,3 +100,22 @@ export function writeFile(filename: string, content: string): void {

debug('writeFile success:', filename)
}

const HASH_LEN = 8
export function getContentHash(chunk: string | Uint8Array | undefined, hash?: VPPTPluginOptions['hash']) {
if (!chunk) {
return ''
}
const hashLen = typeof hash === 'number' ? hash : HASH_LEN

return createHash('md5').update(chunk).digest('hex').substring(0, hashLen)
}

export function extractHashFromFileName(filename: string, hashLen = HASH_LEN) {
const regex = new RegExp(`\\.([\\w\\d]{${hashLen}})\\.?`)
const match = filename.match(regex)
if (match) {
return match[1]
}
return ''
}

0 comments on commit 740b038

Please sign in to comment.