Skip to content

Commit

Permalink
perf: code optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
hemengke1997 committed Mar 25, 2023
1 parent 211980d commit 3a0fb44
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
30 changes: 22 additions & 8 deletions __test__/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import path from 'path'
import { describe, expect, test } from 'vitest'
import { eol, eq, isPublicTypescript, linebreak } from '../src/utils'
import { describe, expect, it, test } from 'vitest'
import { eq, isPublicTypescript, linebreak, setEol } from '../src/utils'
import { getContentHash } from '../src/utils/build'
import { getGlobalConfig, setGlobalConfig } from '../src/utils/globalConfig'

describe('vite-plugin-public-typescript', () => {
test('should hash stable', () => {
const code = 'export const t = { hello: "world" }'
const a = getContentHash(code)
const b = getContentHash(code)
it('should return true when filePath is a public typescript file', () => {
const filePath = 'src/foo/bar.ts'
const root = 'src'
const inputDir = 'foo'
expect(isPublicTypescript({ filePath, root, inputDir })).toBe(true)
})

expect(a).toBe(b)
it('should return false when filePath is not a public typescript file', () => {
const filePath = 'src/foo/bar.js'
const root = 'src'
const inputDir = 'foo'
expect(isPublicTypescript({ filePath, root, inputDir })).toBe(false)
})

test('should be typescript file', () => {
Expand All @@ -35,7 +41,7 @@ describe('vite-plugin-public-typescript', () => {

test('should add eol', () => {
const json = JSON.stringify({ a: 'b' }, null, 2)
const eolJson = eol(json)
const eolJson = setEol(json)
expect(eolJson).toEqual(`{${linebreak} "a": "b"${linebreak}}${linebreak}`)
})

Expand All @@ -49,6 +55,14 @@ describe('vite-plugin-public-typescript', () => {
expect(eq({ a: 1 }, { a: 2 })).toBe(false)
})

test('should hash stable', () => {
const code = 'export const t = { hello: "world" }'
const a = getContentHash(code)
const b = getContentHash(code)

expect(a).toBe(b)
})

test('should getGlobalConfig throw error', () => {
expect(() => getGlobalConfig()).toThrowError('init')
})
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import glob from 'tiny-glob'
import type { BuildOptions } from 'esbuild'
import Watcher from 'watcher'
import fs from 'fs-extra'
import { debug, eq, isEmptyObject, isPublicTypescript, reloadPage, ts } from './utils'
import { TS_EXT, debug, eq, isEmptyObject, isPublicTypescript, reloadPage } from './utils'
import { build, deleteOldJsFile, esbuildTypescript } from './utils/build'
import { ManifestCache } from './utils/manifestCache'
import { getGlobalConfig, setGlobalConfig } from './utils/globalConfig'
Expand Down Expand Up @@ -97,7 +97,7 @@ export function publicTypescript(options: VPPTPluginOptions = {}) {

fs.ensureDirSync(getInputDir())

const filesGlob = await glob(getInputDir(`/*${ts}`), {
const filesGlob = await glob(getInputDir(`/*${TS_EXT}`), {
cwd: config.root,
absolute: true,
})
Expand Down
16 changes: 10 additions & 6 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import type { WebSocketServer } from 'vite'
import { normalizePath } from 'vite'
import fs from 'fs-extra'
import createDebug from 'debug'
import { name } from '../../package.json'
import { name as PKGNAME } from '../../package.json'

export const debug = createDebug(name)
export const debug = createDebug(PKGNAME)

export const ts = '.ts'
export const TS_EXT = '.ts'

export function reloadPage(ws: WebSocketServer) {
ws.send({
Expand All @@ -18,7 +18,11 @@ export function reloadPage(ws: WebSocketServer) {

export function isPublicTypescript(args: { filePath: string; inputDir: string; root: string }) {
const { filePath, root, inputDir } = args
return path.extname(filePath) === ts && normalizePath(filePath).includes(normalizePath(path.resolve(root, inputDir)))

return (
path.extname(filePath) === TS_EXT &&
normalizePath(path.resolve(root, inputDir)).endsWith(normalizePath(path.dirname(filePath)))
)
}

export function isWindows() {
Expand All @@ -34,7 +38,7 @@ export function detectLastLine(string: string) {
}

const newline = /\r\n|\r|\n/g
export function eol(text: string) {
export function setEol(text: string) {
if (!detectLastLine(text)) {
text += linebreak
}
Expand Down Expand Up @@ -73,7 +77,7 @@ export function writeFile(filename: string, content: string): void {
fs.mkdirSync(dir, { recursive: true })
}

const newContent = eol(content)
const newContent = setEol(content)

if (fs.existsSync(filename)) {
// Read content first
Expand Down

0 comments on commit 3a0fb44

Please sign in to comment.