Skip to content

Commit

Permalink
fix: esbuild error
Browse files Browse the repository at this point in the history
  • Loading branch information
hemengke1997 committed Nov 26, 2022
1 parent 43491be commit 8c1de9e
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 26 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion playground/ssr/publicTypescript/custom-manifest.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"a": "/a.9df57b6c.js"
"ssr": "/ssr.820d1dd3.js"
}
2 changes: 1 addition & 1 deletion playground/ssr/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 3 additions & 10 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, ViteDevServer } from 'vite'
import type { PluginOption, ResolvedConfig } from 'vite'
import { normalizePath } from 'vite'
import fg from 'fast-glob'
import type { BuildOptions } from 'esbuild'
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down
35 changes: 22 additions & 13 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 8c1de9e

Please sign in to comment.