Skip to content

Commit

Permalink
fix(hmr): wait build then reload
Browse files Browse the repository at this point in the history
  • Loading branch information
hemengke1997 committed Nov 8, 2022
1 parent e0c3b73 commit 5e4d834
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export function publicTypescript(options: VitePluginOptions): PluginOption {
})
) {
const code = await ctx.read()
build({
await build({
...opts,
filePath: ctx.file,
publicDir: config.publicDir,
Expand Down
21 changes: 11 additions & 10 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { createHash } from 'node:crypto'
import path from 'node:path'
import type { WebSocketServer } from 'vite'
import { normalizePath, transformWithEsbuild } from 'vite'

import fg from 'fast-glob'
import fs from 'fs-extra'
import type { VitePluginOptions } from '..'
Expand All @@ -24,14 +23,16 @@ export async function build(options: BuildOptions) {
const { filePath, publicDir, transformOptions, outputDir } = options
const code = options.code || fs.readFileSync(filePath, 'utf-8')
const fileName = path.basename(filePath, path.extname(filePath))
await transformWithEsbuild(code, fileName, {
loader: 'ts',
format: 'esm',
minify: true,
platform: 'browser',
sourcemap: false,
...transformOptions,
}).then(async (res) => {
try {
const res = await transformWithEsbuild(code, fileName, {
loader: 'ts',
format: 'esm',
minify: true,
platform: 'browser',
sourcemap: false,
...transformOptions,
})

await deleteOldFiles({
...options,
publicDir,
Expand All @@ -40,7 +41,7 @@ export async function build(options: BuildOptions) {
})

await addJsFile({ ...options, code: res.code, fileName })
})
} catch {}
}

type TDeleteFile = {
Expand Down

0 comments on commit 5e4d834

Please sign in to comment.