Skip to content

Commit

Permalink
refactor(build): close rollup bundle directly (#11460)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy authored Jan 4, 2023
1 parent 4a12b89 commit a802828
Showing 1 changed file with 5 additions and 31 deletions.
36 changes: 5 additions & 31 deletions packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<RollupOutput | RollupOutput[] | RollupWatcher> {
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<RollupOutput | RollupOutput[] | RollupWatcher> {
const config = await resolveConfig(
inlineConfig,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -652,25 +629,22 @@ 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)
}

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()
}
}

Expand Down

0 comments on commit a802828

Please sign in to comment.