diff --git a/src/Bundle.ts b/src/Bundle.ts index 9578f303698..b1a5ad1bf57 100644 --- a/src/Bundle.ts +++ b/src/Bundle.ts @@ -31,7 +31,6 @@ export default class Bundle { private readonly graph: Graph ) {} - // TODO Lukas make this one nicer by structuring it async generate(isWrite: boolean): Promise { timeStart('GENERATE', 1); const outputBundle: OutputBundleWithPlaceholders = Object.create(null); @@ -45,36 +44,17 @@ export default class Bundle { timeStart('generate chunks', 2); const chunks = await this.generateChunks(); - timeEnd('generate chunks', 2); - - timeStart('render modules', 2); if (chunks.length > 1) { validateOptionsForMultiChunkOutput(this.outputOptions); } - const addons = await createAddons(this.outputOptions, this.pluginDriver); - for (const chunk of chunks) { - chunk.generateExports(); - } const inputBase = commondir(getAbsoluteEntryModulePaths(chunks)); - for (const chunk of chunks) { - chunk.preRender(this.outputOptions, inputBase, this.pluginDriver); - } - timeEnd('render modules', 2); + timeEnd('generate chunks', 2); - this.assignChunkIds(chunks, inputBase, addons, outputBundle); - assignChunksToBundle(chunks, outputBundle); + timeStart('render modules', 2); + this.prerenderChunks(chunks, inputBase); + timeEnd('render modules', 2); - await Promise.all( - chunks.map(chunk => { - const outputChunk = outputBundle[chunk.id!] as OutputChunk; - return chunk - .render(this.outputOptions, addons, outputChunk, this.pluginDriver) - .then(rendered => { - outputChunk.code = rendered.code; - outputChunk.map = rendered.map; - }); - }) - ); + await this.addFinalizedChunksToBundle(chunks, inputBase, outputBundle); } catch (error) { await this.pluginDriver.hookParallel('renderError', [error]); throw error; @@ -84,23 +64,38 @@ export default class Bundle { outputBundle as OutputBundle, isWrite ]); - for (const key of Object.keys(outputBundle)) { - const file = outputBundle[key] as any; - if (!file.type) { - warnDeprecation( - 'A plugin is directly adding properties to the bundle object in the "generateBundle" hook. This is deprecated and will be removed in a future Rollup version, please use "this.emitFile" instead.', - true, - this.inputOptions - ); - file.type = 'asset'; - } - } - this.pluginDriver.finaliseAssets(); + this.finaliseAssets(outputBundle); timeEnd('GENERATE', 1); return outputBundle as OutputBundle; } + private async addFinalizedChunksToBundle( + chunks: Chunk[], + inputBase: string, + outputBundle: OutputBundleWithPlaceholders + ): Promise { + const addons = await createAddons(this.outputOptions, this.pluginDriver); + this.assignChunkIds(chunks, inputBase, addons, outputBundle); + for (const chunk of chunks) { + const chunkDescription = (outputBundle[ + chunk.id! + ] = chunk.getPrerenderedChunk() as OutputChunk); + chunkDescription.fileName = chunk.id!; + } + await Promise.all( + chunks.map(chunk => { + const outputChunk = outputBundle[chunk.id!] as OutputChunk; + return chunk + .render(this.outputOptions, addons, outputChunk, this.pluginDriver) + .then(rendered => { + outputChunk.code = rendered.code; + outputChunk.map = rendered.map; + }); + }) + ); + } + private async addManualChunks( manualChunks: Record ): Promise> { @@ -170,6 +165,21 @@ export default class Bundle { return manualChunkAliasByEntry; } + private finaliseAssets(outputBundle: OutputBundleWithPlaceholders): void { + for (const key of Object.keys(outputBundle)) { + const file = outputBundle[key] as any; + if (!file.type) { + warnDeprecation( + 'A plugin is directly adding properties to the bundle object in the "generateBundle" hook. This is deprecated and will be removed in a future Rollup version, please use "this.emitFile" instead.', + true, + this.inputOptions + ); + file.type = 'asset'; + } + } + this.pluginDriver.finaliseAssets(); + } + private async generateChunks(): Promise { const { manualChunks } = this.outputOptions; const manualChunkAliasByEntry = @@ -211,6 +221,15 @@ export default class Bundle { } return [...chunks, ...facades]; } + + private prerenderChunks(chunks: Chunk[], inputBase: string): void { + for (const chunk of chunks) { + chunk.generateExports(); + } + for (const chunk of chunks) { + chunk.preRender(this.outputOptions, inputBase, this.pluginDriver); + } + } } function getAbsoluteEntryModulePaths(chunks: Chunk[]): string[] { @@ -245,19 +264,6 @@ function validateOptionsForMultiChunkOutput(outputOptions: NormalizedOutputOptio }); } -function assignChunksToBundle( - chunks: Chunk[], - outputBundle: OutputBundleWithPlaceholders -): OutputBundle { - for (const chunk of chunks) { - const chunkdDescription = (outputBundle[ - chunk.id! - ] = chunk.getPrerenderedChunk() as OutputChunk); - chunkdDescription.fileName = chunk.id!; - } - return outputBundle as OutputBundle; -} - function getIncludedModules(modulesById: Map): Module[] { return [...modulesById.values()].filter( module =>