diff --git a/playground/ssr/public/ssr.9df57b6c.js b/playground/ssr/public/ssr.820d1dd3.js similarity index 100% rename from playground/ssr/public/ssr.9df57b6c.js rename to playground/ssr/public/ssr.820d1dd3.js diff --git a/playground/ssr/publicTypescript/custom-manifest.json b/playground/ssr/publicTypescript/custom-manifest.json index dc07047..f2ef110 100644 --- a/playground/ssr/publicTypescript/custom-manifest.json +++ b/playground/ssr/publicTypescript/custom-manifest.json @@ -1,3 +1,3 @@ { - "a": "/a.9df57b6c.js" + "ssr": "/ssr.820d1dd3.js" } \ No newline at end of file diff --git a/playground/ssr/server.js b/playground/ssr/server.js index fa81f4a..2de1809 100644 --- a/playground/ssr/server.js +++ b/playground/ssr/server.js @@ -4,7 +4,7 @@ import manifest from './publicTypescript/custom-manifest.json' assert { type: 'j // Constants const isProduction = process.env.NODE_ENV === 'production' -const port = process.env.PORT || 3000 +const port = process.env.PORT || 4002 const base = process.env.BASE || '/' // Cached production assets diff --git a/src/index.ts b/src/index.ts index 3f97e50..2b9db89 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,5 @@ import path from 'node:path' -import type { PluginOption, ResolvedConfig, ViteDevServer } from 'vite' +import type { PluginOption, ResolvedConfig } from 'vite' import { normalizePath } from 'vite' import fg from 'fast-glob' import type { BuildOptions } from 'esbuild' @@ -58,17 +58,10 @@ 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', configResolved(c) { config = c @@ -94,8 +87,8 @@ export function publicTypescript(options: VitePluginOptions): PluginOption { }) }) }, - configureServer(_server) { - server = _server + + configureServer(server) { const { watcher, ws } = server watcher.on('unlink', async (f) => { // ts file deleted diff --git a/src/utils/index.ts b/src/utils/index.ts index 422a4f9..7788b9b 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -4,8 +4,10 @@ import type { WebSocketServer } from 'vite' import { normalizePath } from 'vite' import fg from 'fast-glob' import fs from 'fs-extra' +import type { BuildResult } from 'esbuild' import { build as esbuild } from 'esbuild' import type { VitePluginOptions } from '..' +import { name } from '../../package.json' import type { ManifestCache } from './manifestCache' export function getContentHash(chunk: string | Uint8Array) { @@ -23,20 +25,27 @@ export async function build(options: BuildOptions) { const { filePath, publicDir, esbuildOptions, outputDir } = options const fileName = path.basename(filePath, path.extname(filePath)) - const res = await esbuild({ - entryPoints: [filePath], - write: false, - platform: 'browser', - bundle: true, - format: 'iife', - sourcemap: false, - treeShaking: true, - splitting: false, - minify: true, - ...esbuildOptions, - }) - const code = res.outputFiles?.[0].text + let res: BuildResult + try { + res = await esbuild({ + entryPoints: [filePath], + write: false, + platform: 'browser', + bundle: true, + format: 'iife', + sourcemap: false, + treeShaking: true, + splitting: false, + minify: true, + ...esbuildOptions, + }) + } catch (e) { + console.error(`${name} esbuild error:`, e) + return + } + + const code = res!.outputFiles?.[0].text await deleteOldFiles({ ...options, diff --git a/tsup.config.ts b/tsup.config.ts index 42e3494..9f1f786 100644 --- a/tsup.config.ts +++ b/tsup.config.ts @@ -2,7 +2,7 @@ import { defineConfig } from 'tsup' export const tsup = defineConfig((option) => ({ entry: ['src/index.ts'], - dts: true, + dts: !option.watch, clean: true, format: ['cjs', 'esm'], minify: false,