From a802828f66282205c7f61718f2f399eea0173c4d Mon Sep 17 00:00:00 2001 From: Bjorn Lu Date: Thu, 5 Jan 2023 00:00:26 +0800 Subject: [PATCH] refactor(build): close rollup bundle directly (#11460) --- packages/vite/src/node/build.ts | 36 +++++---------------------------- 1 file changed, 5 insertions(+), 31 deletions(-) diff --git a/packages/vite/src/node/build.ts b/packages/vite/src/node/build.ts index 8337f083aaa7a3..13e00598435bed 100644 --- a/packages/vite/src/node/build.ts +++ b/packages/vite/src/node/build.ts @@ -421,36 +421,12 @@ export async function resolveBuildPlugins(config: ResolvedConfig): Promise<{ } } -/** - * Track parallel build calls and only stop the esbuild service when all - * builds are done. (#1098) - */ -let parallelCallCounts = 0 -// we use a separate counter to track since the call may error before the -// bundle is even pushed. -const parallelBuilds: RollupBuild[] = [] - /** * Bundles the app for production. * Returns a Promise containing the build result. */ export async function build( inlineConfig: InlineConfig = {}, -): Promise { - parallelCallCounts++ - try { - return await doBuild(inlineConfig) - } finally { - parallelCallCounts-- - if (parallelCallCounts <= 0) { - await Promise.all(parallelBuilds.map((bundle) => bundle.close())) - parallelBuilds.length = 0 - } - } -} - -async function doBuild( - inlineConfig: InlineConfig = {}, ): Promise { const config = await resolveConfig( inlineConfig, @@ -544,6 +520,7 @@ async function doBuild( config.logger.error(msg, { error: e }) } + let bundle: RollupBuild | undefined try { const buildOutputOptions = (output: OutputOptions = {}): OutputOptions => { // @ts-expect-error See https://github.com/vitejs/vite/issues/5812#issuecomment-984345618 @@ -652,12 +629,7 @@ async function doBuild( // write or generate files with rollup const { rollup } = await import('rollup') - const bundle = await rollup(rollupOptions) - parallelBuilds.push(bundle) - - const generate = (output: OutputOptions = {}) => { - return bundle[options.write ? 'write' : 'generate'](output) - } + bundle = await rollup(rollupOptions) if (options.write) { prepareOutDir(outDirs, options.emptyOutDir, config) @@ -665,12 +637,14 @@ async function doBuild( const res = [] for (const output of normalizedOutputs) { - res.push(await generate(output)) + res.push(await bundle[options.write ? 'write' : 'generate'](output)) } return Array.isArray(outputs) ? res : res[0] } catch (e) { outputBuildError(e) throw e + } finally { + if (bundle) bundle.close() } }