Skip to content

Commit

Permalink
fix: 🐛 esbuild error The service is no longer running
Browse files Browse the repository at this point in the history
  • Loading branch information
hemengke1997 committed Nov 25, 2022
1 parent 5772e6a commit 76a3d20
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
12 changes: 9 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from 'node:path'
import type { PluginOption, ResolvedConfig } from 'vite'
import type { PluginOption, ResolvedConfig, ViteDevServer } from 'vite'
import { normalizePath } from 'vite'
import fg from 'fast-glob'
import type { BuildOptions } from 'esbuild'
Expand Down Expand Up @@ -58,8 +58,14 @@ export function publicTypescript(options: VitePluginOptions): PluginOption {
let config: ResolvedConfig
let files: string[]
let buildLength = 0
let server: ViteDevServer
const cache = new ManifestCache()

// fix esbuild error: The service is no longer running
process.on('SIGINT', () => {
server.close()
})

return {
name: 'vite:public-typescript',
enforce: 'pre',
Expand All @@ -78,7 +84,6 @@ export function publicTypescript(options: VitePluginOptions): PluginOption {
buildStart() {
if (opts.ssrBuild || config.build.ssr) return
const outDir = config.publicDir

files.forEach((f) => {
build({
...opts,
Expand All @@ -89,7 +94,8 @@ export function publicTypescript(options: VitePluginOptions): PluginOption {
})
})
},
configureServer(server) {
configureServer(_server) {
server = _server
const { watcher, ws } = server
watcher.on('unlink', async (f) => {
// ts file deleted
Expand Down
4 changes: 2 additions & 2 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export async function deleteOldFiles(args: TDeleteFile) {
if (fs.existsSync(f)) {
// and modify manifest
cache.removeCache(fileName)
cache.writeCache(`${inputDir}/${manifestName}.json`)
cache.writeManifestJSON(`${inputDir}/${manifestName}.json`)
await fs.remove(f)
}
}
Expand Down Expand Up @@ -100,7 +100,7 @@ export async function addJsFile(args: TAddFile) {
// write cache
currentBuildTimes++
if (currentBuildTimes >= buildLength) {
cache.writeCache(`${inputDir}/${manifestName}.json`)
cache.writeManifestJSON(`${inputDir}/${manifestName}.json`)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/utils/manifestCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class ManifestCache {
return cacheMap
}

async writeCache(targetPath: string) {
async writeManifestJSON(targetPath: string) {
const cacheObj = Object.fromEntries(this.getAll())
const orderdCache: Record<string, string> = {}
Object.keys(cacheObj)
Expand Down

0 comments on commit 76a3d20

Please sign in to comment.