Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(build): close rollup bundle directly #11460

Merged
merged 2 commits into from
Jan 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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