diff --git a/src/node/build/index.ts b/src/node/build/index.ts index aee8387..58f8580 100644 --- a/src/node/build/index.ts +++ b/src/node/build/index.ts @@ -1,6 +1,5 @@ import createDebug from 'debug' -import { type BuildResult, type Plugin, build as esbuild } from 'esbuild' -import { resolveToEsbuildTarget } from 'esbuild-plugin-browserslist' +import { type BuildResult, type Plugin } from 'esbuild' import path from 'node:path' import colors from 'picocolors' import { globalConfig } from '../global-config' @@ -62,6 +61,7 @@ export async function esbuildTypescript(buildOptions: IBuildOptions) { if (enableBabel) { const { default: browserslist } = await import('browserslist') babelTarget = browserslist.loadConfig({ path: viteConfig.root }) || [] + const { resolveToEsbuildTarget } = await import('esbuild-plugin-browserslist') esbuildTarget = resolveToEsbuildTarget(browserslist(babelTarget), { printUnknownTargets: false }) } @@ -83,7 +83,8 @@ export async function esbuildTypescript(buildOptions: IBuildOptions) { let res: BuildResult try { - res = await esbuild({ + const { build } = await import('esbuild') + res = await build({ bundle: true, define, entryPoints: [filePath], diff --git a/src/node/helper/file-watcher.ts b/src/node/helper/file-watcher.ts index 8a26416..0105ef2 100644 --- a/src/node/helper/file-watcher.ts +++ b/src/node/helper/file-watcher.ts @@ -1,7 +1,6 @@ import createDebug from 'debug' import path from 'node:path' import colors from 'picocolors' -import Watcher from 'watcher' import { build } from '../build' import { globalConfig } from '../global-config' import { type HmrFile } from './server' @@ -39,8 +38,9 @@ async function handleFileChange(filePath: string, cb?: () => void) { handleFileAdded(filePath, cb) } -export function initWatcher(cb: (file: HmrFile) => void) { +export async function initWatcher(cb: (file: HmrFile) => void) { try { + const { default: Watcher } = await import('watcher') const watcher = new Watcher(globalConfig.get('absInputDir'), { debounce: 0, ignoreInitial: true, diff --git a/src/node/index.ts b/src/node/index.ts index 06cdc0d..02177df 100644 --- a/src/node/index.ts +++ b/src/node/index.ts @@ -126,12 +126,12 @@ export default function publicTypescript(options: VPPTPluginOptions = {}) { await setupManifestCache(viteConfig, opts) }, - configureServer(server) { + async configureServer(server) { const { ws } = server globalConfig.set('viteDevServer', server) - initWatcher((file) => reloadPage(ws, file)) + await initWatcher((file) => reloadPage(ws, file)) }, async buildStart() { const manifestPath = manifestCache.manifestPath