From ad9c8244f81eda6515e6e6fe5ccf5852888ddd65 Mon Sep 17 00:00:00 2001 From: khai96_ Date: Mon, 12 Aug 2019 11:30:04 +0700 Subject: [PATCH 1/6] Minor refactoring: Remove one try-catch --- src/rollup/index.ts | 326 +++++++++++++++++++++----------------------- 1 file changed, 159 insertions(+), 167 deletions(-) diff --git a/src/rollup/index.ts b/src/rollup/index.ts index bc11bab656b..1f8798e1604 100644 --- a/src/rollup/index.ts +++ b/src/rollup/index.ts @@ -174,184 +174,176 @@ function assignChunksToBundle( return outputBundle as OutputBundle; } -export default function rollup(rawInputOptions: GenericConfigObject): Promise { - try { - const inputOptions = getInputOptions(rawInputOptions); - initialiseTimers(inputOptions); - - const graph = new Graph(inputOptions, curWatcher); - curWatcher = undefined as any; - - // remove the cache option from the memory after graph creation (cache is not used anymore) - const useCache = rawInputOptions.cache !== false; - delete inputOptions.cache; - delete rawInputOptions.cache; - - timeStart('BUILD', 1); - - return graph.pluginDriver - .hookParallel('buildStart', [inputOptions]) - .then(() => - graph.build( - inputOptions.input as string | string[] | Record, - inputOptions.manualChunks, - inputOptions.inlineDynamicImports as boolean - ) +export default async function rollup(rawInputOptions: GenericConfigObject): Promise { + const inputOptions = getInputOptions(rawInputOptions); + initialiseTimers(inputOptions); + + const graph = new Graph(inputOptions, curWatcher); + curWatcher = undefined as any; + + // remove the cache option from the memory after graph creation (cache is not used anymore) + const useCache = rawInputOptions.cache !== false; + delete inputOptions.cache; + delete rawInputOptions.cache; + + timeStart('BUILD', 1); + + return graph.pluginDriver + .hookParallel('buildStart', [inputOptions]) + .then(() => + graph.build( + inputOptions.input as string | string[] | Record, + inputOptions.manualChunks, + inputOptions.inlineDynamicImports as boolean ) - .then( - chunks => graph.pluginDriver.hookParallel('buildEnd', []).then(() => chunks), - err => - graph.pluginDriver.hookParallel('buildEnd', [err]).then(() => { - throw err; - }) - ) - .then(chunks => { - timeEnd('BUILD', 1); - - // ensure we only do one optimization pass per build - let optimized = false; - - function getOutputOptions(rawOutputOptions: GenericConfigObject) { - return normalizeOutputOptions( - inputOptions as GenericConfigObject, - rawOutputOptions, - chunks.length > 1, - graph.pluginDriver - ); - } + ) + .then( + chunks => graph.pluginDriver.hookParallel('buildEnd', []).then(() => chunks), + err => + graph.pluginDriver.hookParallel('buildEnd', [err]).then(() => { + throw err; + }) + ) + .then(chunks => { + timeEnd('BUILD', 1); + + // ensure we only do one optimization pass per build + let optimized = false; + + function getOutputOptions(rawOutputOptions: GenericConfigObject) { + return normalizeOutputOptions( + inputOptions as GenericConfigObject, + rawOutputOptions, + chunks.length > 1, + graph.pluginDriver + ); + } - async function generate( - outputOptions: OutputOptions, - isWrite: boolean - ): Promise { - timeStart('GENERATE', 1); - - const assetFileNames = outputOptions.assetFileNames || 'assets/[name]-[hash][extname]'; - const outputBundleWithPlaceholders: OutputBundleWithPlaceholders = Object.create(null); - let outputBundle; - const inputBase = commondir(getAbsoluteEntryModulePaths(chunks)); - graph.pluginDriver.startOutput(outputBundleWithPlaceholders, assetFileNames); - - try { - await graph.pluginDriver.hookParallel('renderStart', []); - const addons = await createAddons(graph, outputOptions); - for (const chunk of chunks) { - if (!inputOptions.preserveModules) chunk.generateInternalExports(outputOptions); - if (chunk.facadeModule && chunk.facadeModule.isEntryPoint) - chunk.exportMode = getExportMode(chunk, outputOptions); - } - for (const chunk of chunks) { - chunk.preRender(outputOptions, inputBase); - } - if (!optimized && inputOptions.experimentalOptimizeChunks) { - optimizeChunks( - chunks, - outputOptions, - inputOptions.chunkGroupingSize as number, - inputBase - ); - optimized = true; - } - assignChunkIds( + async function generate( + outputOptions: OutputOptions, + isWrite: boolean + ): Promise { + timeStart('GENERATE', 1); + + const assetFileNames = outputOptions.assetFileNames || 'assets/[name]-[hash][extname]'; + const outputBundleWithPlaceholders: OutputBundleWithPlaceholders = Object.create(null); + let outputBundle; + const inputBase = commondir(getAbsoluteEntryModulePaths(chunks)); + graph.pluginDriver.startOutput(outputBundleWithPlaceholders, assetFileNames); + + try { + await graph.pluginDriver.hookParallel('renderStart', []); + const addons = await createAddons(graph, outputOptions); + for (const chunk of chunks) { + if (!inputOptions.preserveModules) chunk.generateInternalExports(outputOptions); + if (chunk.facadeModule && chunk.facadeModule.isEntryPoint) + chunk.exportMode = getExportMode(chunk, outputOptions); + } + for (const chunk of chunks) { + chunk.preRender(outputOptions, inputBase); + } + if (!optimized && inputOptions.experimentalOptimizeChunks) { + optimizeChunks( chunks, - inputOptions, outputOptions, - inputBase, - addons, - outputBundleWithPlaceholders - ); - outputBundle = assignChunksToBundle(chunks, outputBundleWithPlaceholders); - - await Promise.all( - chunks.map(chunk => { - const outputChunk = outputBundleWithPlaceholders[chunk.id as string] as OutputChunk; - return chunk.render(outputOptions, addons, outputChunk).then(rendered => { - outputChunk.code = rendered.code; - outputChunk.map = rendered.map; - - return graph.pluginDriver.hookParallel('ongenerate', [ - { bundle: outputChunk, ...outputOptions }, - outputChunk - ]); - }); - }) + inputOptions.chunkGroupingSize as number, + inputBase ); - } catch (error) { - await graph.pluginDriver.hookParallel('renderError', [error]); - throw error; + optimized = true; } - await graph.pluginDriver.hookSeq('generateBundle', [ + assignChunkIds( + chunks, + inputOptions, outputOptions, - outputBundle, - isWrite - ]); - graph.pluginDriver.finaliseAssets(); - - timeEnd('GENERATE', 1); - return outputBundle; + inputBase, + addons, + outputBundleWithPlaceholders + ); + outputBundle = assignChunksToBundle(chunks, outputBundleWithPlaceholders); + + await Promise.all( + chunks.map(chunk => { + const outputChunk = outputBundleWithPlaceholders[chunk.id as string] as OutputChunk; + return chunk.render(outputOptions, addons, outputChunk).then(rendered => { + outputChunk.code = rendered.code; + outputChunk.map = rendered.map; + + return graph.pluginDriver.hookParallel('ongenerate', [ + { bundle: outputChunk, ...outputOptions }, + outputChunk + ]); + }); + }) + ); + } catch (error) { + await graph.pluginDriver.hookParallel('renderError', [error]); + throw error; } + await graph.pluginDriver.hookSeq('generateBundle', [outputOptions, outputBundle, isWrite]); + graph.pluginDriver.finaliseAssets(); - const cache = useCache ? graph.getCache() : undefined; - const result: RollupBuild = { - cache: cache as RollupCache, - generate: ((rawOutputOptions: GenericConfigObject) => { - const promise = generate(getOutputOptions(rawOutputOptions), false).then(result => - createOutput(result) - ); - Object.defineProperty(promise, 'code', throwAsyncGenerateError); - Object.defineProperty(promise, 'map', throwAsyncGenerateError); - return promise; - }) as any, - watchFiles: Object.keys(graph.watchFiles), - write: ((rawOutputOptions: GenericConfigObject) => { - const outputOptions = getOutputOptions(rawOutputOptions); - if (!outputOptions.dir && !outputOptions.file) { - error({ - code: 'MISSING_OPTION', - message: 'You must specify "output.file" or "output.dir" for the build.' - }); + timeEnd('GENERATE', 1); + return outputBundle; + } + + const cache = useCache ? graph.getCache() : undefined; + const result: RollupBuild = { + cache: cache as RollupCache, + generate: ((rawOutputOptions: GenericConfigObject) => { + const promise = generate(getOutputOptions(rawOutputOptions), false).then(result => + createOutput(result) + ); + Object.defineProperty(promise, 'code', throwAsyncGenerateError); + Object.defineProperty(promise, 'map', throwAsyncGenerateError); + return promise; + }) as any, + watchFiles: Object.keys(graph.watchFiles), + write: ((rawOutputOptions: GenericConfigObject) => { + const outputOptions = getOutputOptions(rawOutputOptions); + if (!outputOptions.dir && !outputOptions.file) { + error({ + code: 'MISSING_OPTION', + message: 'You must specify "output.file" or "output.dir" for the build.' + }); + } + return generate(outputOptions, true).then(bundle => { + let chunkCnt = 0; + for (const fileName of Object.keys(bundle)) { + const file = bundle[fileName]; + if ((file as OutputAsset).isAsset) continue; + chunkCnt++; + if (chunkCnt > 1) break; } - return generate(outputOptions, true).then(bundle => { - let chunkCnt = 0; - for (const fileName of Object.keys(bundle)) { - const file = bundle[fileName]; - if ((file as OutputAsset).isAsset) continue; - chunkCnt++; - if (chunkCnt > 1) break; - } - if (chunkCnt > 1) { - if (outputOptions.sourcemapFile) - error({ - code: 'INVALID_OPTION', - message: '"output.sourcemapFile" is only supported for single-file builds.' - }); - if (typeof outputOptions.file === 'string') - error({ - code: 'INVALID_OPTION', - message: - 'When building multiple chunks, the "output.dir" option must be used, not "output.file".' + - (typeof inputOptions.input !== 'string' || - inputOptions.inlineDynamicImports === true - ? '' - : ' To inline dynamic imports, set the "inlineDynamicImports" option.') - }); - } - return Promise.all( - Object.keys(bundle).map(chunkId => - writeOutputFile(graph, result, bundle[chunkId], outputOptions) - ) + if (chunkCnt > 1) { + if (outputOptions.sourcemapFile) + error({ + code: 'INVALID_OPTION', + message: '"output.sourcemapFile" is only supported for single-file builds.' + }); + if (typeof outputOptions.file === 'string') + error({ + code: 'INVALID_OPTION', + message: + 'When building multiple chunks, the "output.dir" option must be used, not "output.file".' + + (typeof inputOptions.input !== 'string' || + inputOptions.inlineDynamicImports === true + ? '' + : ' To inline dynamic imports, set the "inlineDynamicImports" option.') + }); + } + return Promise.all( + Object.keys(bundle).map(chunkId => + writeOutputFile(graph, result, bundle[chunkId], outputOptions) ) - .then(() => graph.pluginDriver.hookParallel('writeBundle', [bundle])) - .then(() => createOutput(bundle)); - }); - }) as any - }; - if (inputOptions.perf === true) result.getTimings = getTimings; - return result; - }); - } catch (err) { - return Promise.reject(err); - } + ) + .then(() => graph.pluginDriver.hookParallel('writeBundle', [bundle])) + .then(() => createOutput(bundle)); + }); + }) as any + }; + if (inputOptions.perf === true) result.getTimings = getTimings; + return result; + }); } enum SortingFileType { From 2b06ebced769c998a720a5d5dc1213c182947b8e Mon Sep 17 00:00:00 2001 From: khai96_ Date: Mon, 12 Aug 2019 12:38:21 +0700 Subject: [PATCH 2/6] Use async control flow in place of .then() chain --- src/rollup/index.ts | 289 +++++++++++++++++++++----------------------- 1 file changed, 139 insertions(+), 150 deletions(-) diff --git a/src/rollup/index.ts b/src/rollup/index.ts index 1f8798e1604..a9fbacc01fd 100644 --- a/src/rollup/index.ts +++ b/src/rollup/index.ts @@ -188,162 +188,151 @@ export default async function rollup(rawInputOptions: GenericConfigObject): Prom timeStart('BUILD', 1); - return graph.pluginDriver - .hookParallel('buildStart', [inputOptions]) - .then(() => - graph.build( - inputOptions.input as string | string[] | Record, - inputOptions.manualChunks, - inputOptions.inlineDynamicImports as boolean - ) - ) - .then( - chunks => graph.pluginDriver.hookParallel('buildEnd', []).then(() => chunks), - err => - graph.pluginDriver.hookParallel('buildEnd', [err]).then(() => { - throw err; - }) - ) - .then(chunks => { - timeEnd('BUILD', 1); - - // ensure we only do one optimization pass per build - let optimized = false; - - function getOutputOptions(rawOutputOptions: GenericConfigObject) { - return normalizeOutputOptions( - inputOptions as GenericConfigObject, - rawOutputOptions, - chunks.length > 1, - graph.pluginDriver - ); - } + await graph.pluginDriver.hookParallel('buildStart', [inputOptions]); - async function generate( - outputOptions: OutputOptions, - isWrite: boolean - ): Promise { - timeStart('GENERATE', 1); - - const assetFileNames = outputOptions.assetFileNames || 'assets/[name]-[hash][extname]'; - const outputBundleWithPlaceholders: OutputBundleWithPlaceholders = Object.create(null); - let outputBundle; - const inputBase = commondir(getAbsoluteEntryModulePaths(chunks)); - graph.pluginDriver.startOutput(outputBundleWithPlaceholders, assetFileNames); - - try { - await graph.pluginDriver.hookParallel('renderStart', []); - const addons = await createAddons(graph, outputOptions); - for (const chunk of chunks) { - if (!inputOptions.preserveModules) chunk.generateInternalExports(outputOptions); - if (chunk.facadeModule && chunk.facadeModule.isEntryPoint) - chunk.exportMode = getExportMode(chunk, outputOptions); - } - for (const chunk of chunks) { - chunk.preRender(outputOptions, inputBase); - } - if (!optimized && inputOptions.experimentalOptimizeChunks) { - optimizeChunks( - chunks, - outputOptions, - inputOptions.chunkGroupingSize as number, - inputBase - ); - optimized = true; - } - assignChunkIds( - chunks, - inputOptions, - outputOptions, - inputBase, - addons, - outputBundleWithPlaceholders - ); - outputBundle = assignChunksToBundle(chunks, outputBundleWithPlaceholders); - - await Promise.all( - chunks.map(chunk => { - const outputChunk = outputBundleWithPlaceholders[chunk.id as string] as OutputChunk; - return chunk.render(outputOptions, addons, outputChunk).then(rendered => { - outputChunk.code = rendered.code; - outputChunk.map = rendered.map; - - return graph.pluginDriver.hookParallel('ongenerate', [ - { bundle: outputChunk, ...outputOptions }, - outputChunk - ]); - }); - }) - ); - } catch (error) { - await graph.pluginDriver.hookParallel('renderError', [error]); - throw error; - } - await graph.pluginDriver.hookSeq('generateBundle', [outputOptions, outputBundle, isWrite]); - graph.pluginDriver.finaliseAssets(); + const chunks = await graph.build( + inputOptions.input as string | string[] | Record, + inputOptions.manualChunks, + inputOptions.inlineDynamicImports as boolean + ); + + try { + await graph.pluginDriver.hookParallel('buildEnd', []); + } catch (err) { + await graph.pluginDriver.hookParallel('buildEnd', [err]); + throw err; + } + + timeEnd('BUILD', 1); + + // ensure we only do one optimization pass per build + let optimized = false; + + function getOutputOptions(rawOutputOptions: GenericConfigObject) { + return normalizeOutputOptions( + inputOptions as GenericConfigObject, + rawOutputOptions, + chunks.length > 1, + graph.pluginDriver + ); + } - timeEnd('GENERATE', 1); - return outputBundle; + async function generate(outputOptions: OutputOptions, isWrite: boolean): Promise { + timeStart('GENERATE', 1); + + const assetFileNames = outputOptions.assetFileNames || 'assets/[name]-[hash][extname]'; + const outputBundleWithPlaceholders: OutputBundleWithPlaceholders = Object.create(null); + let outputBundle; + const inputBase = commondir(getAbsoluteEntryModulePaths(chunks)); + graph.pluginDriver.startOutput(outputBundleWithPlaceholders, assetFileNames); + + try { + await graph.pluginDriver.hookParallel('renderStart', []); + const addons = await createAddons(graph, outputOptions); + for (const chunk of chunks) { + if (!inputOptions.preserveModules) chunk.generateInternalExports(outputOptions); + if (chunk.facadeModule && chunk.facadeModule.isEntryPoint) + chunk.exportMode = getExportMode(chunk, outputOptions); + } + for (const chunk of chunks) { + chunk.preRender(outputOptions, inputBase); + } + if (!optimized && inputOptions.experimentalOptimizeChunks) { + optimizeChunks(chunks, outputOptions, inputOptions.chunkGroupingSize as number, inputBase); + optimized = true; } + assignChunkIds( + chunks, + inputOptions, + outputOptions, + inputBase, + addons, + outputBundleWithPlaceholders + ); + outputBundle = assignChunksToBundle(chunks, outputBundleWithPlaceholders); + + await Promise.all( + chunks.map(chunk => { + const outputChunk = outputBundleWithPlaceholders[chunk.id as string] as OutputChunk; + return chunk.render(outputOptions, addons, outputChunk).then(rendered => { + outputChunk.code = rendered.code; + outputChunk.map = rendered.map; + + return graph.pluginDriver.hookParallel('ongenerate', [ + { bundle: outputChunk, ...outputOptions }, + outputChunk + ]); + }); + }) + ); + } catch (error) { + await graph.pluginDriver.hookParallel('renderError', [error]); + throw error; + } + await graph.pluginDriver.hookSeq('generateBundle', [outputOptions, outputBundle, isWrite]); + graph.pluginDriver.finaliseAssets(); + + timeEnd('GENERATE', 1); + return outputBundle; + } - const cache = useCache ? graph.getCache() : undefined; - const result: RollupBuild = { - cache: cache as RollupCache, - generate: ((rawOutputOptions: GenericConfigObject) => { - const promise = generate(getOutputOptions(rawOutputOptions), false).then(result => - createOutput(result) - ); - Object.defineProperty(promise, 'code', throwAsyncGenerateError); - Object.defineProperty(promise, 'map', throwAsyncGenerateError); - return promise; - }) as any, - watchFiles: Object.keys(graph.watchFiles), - write: ((rawOutputOptions: GenericConfigObject) => { - const outputOptions = getOutputOptions(rawOutputOptions); - if (!outputOptions.dir && !outputOptions.file) { + const cache = useCache ? graph.getCache() : undefined; + const result: RollupBuild = { + cache: cache as RollupCache, + generate: ((rawOutputOptions: GenericConfigObject) => { + const promise = generate(getOutputOptions(rawOutputOptions), false).then(result => + createOutput(result) + ); + Object.defineProperty(promise, 'code', throwAsyncGenerateError); + Object.defineProperty(promise, 'map', throwAsyncGenerateError); + return promise; + }) as any, + watchFiles: Object.keys(graph.watchFiles), + write: ((rawOutputOptions: GenericConfigObject) => { + const outputOptions = getOutputOptions(rawOutputOptions); + if (!outputOptions.dir && !outputOptions.file) { + error({ + code: 'MISSING_OPTION', + message: 'You must specify "output.file" or "output.dir" for the build.' + }); + } + return generate(outputOptions, true).then(bundle => { + let chunkCnt = 0; + for (const fileName of Object.keys(bundle)) { + const file = bundle[fileName]; + if ((file as OutputAsset).isAsset) continue; + chunkCnt++; + if (chunkCnt > 1) break; + } + if (chunkCnt > 1) { + if (outputOptions.sourcemapFile) error({ - code: 'MISSING_OPTION', - message: 'You must specify "output.file" or "output.dir" for the build.' + code: 'INVALID_OPTION', + message: '"output.sourcemapFile" is only supported for single-file builds.' }); - } - return generate(outputOptions, true).then(bundle => { - let chunkCnt = 0; - for (const fileName of Object.keys(bundle)) { - const file = bundle[fileName]; - if ((file as OutputAsset).isAsset) continue; - chunkCnt++; - if (chunkCnt > 1) break; - } - if (chunkCnt > 1) { - if (outputOptions.sourcemapFile) - error({ - code: 'INVALID_OPTION', - message: '"output.sourcemapFile" is only supported for single-file builds.' - }); - if (typeof outputOptions.file === 'string') - error({ - code: 'INVALID_OPTION', - message: - 'When building multiple chunks, the "output.dir" option must be used, not "output.file".' + - (typeof inputOptions.input !== 'string' || - inputOptions.inlineDynamicImports === true - ? '' - : ' To inline dynamic imports, set the "inlineDynamicImports" option.') - }); - } - return Promise.all( - Object.keys(bundle).map(chunkId => - writeOutputFile(graph, result, bundle[chunkId], outputOptions) - ) - ) - .then(() => graph.pluginDriver.hookParallel('writeBundle', [bundle])) - .then(() => createOutput(bundle)); - }); - }) as any - }; - if (inputOptions.perf === true) result.getTimings = getTimings; - return result; - }); + if (typeof outputOptions.file === 'string') + error({ + code: 'INVALID_OPTION', + message: + 'When building multiple chunks, the "output.dir" option must be used, not "output.file".' + + (typeof inputOptions.input !== 'string' || + inputOptions.inlineDynamicImports === true + ? '' + : ' To inline dynamic imports, set the "inlineDynamicImports" option.') + }); + } + return Promise.all( + Object.keys(bundle).map(chunkId => + writeOutputFile(graph, result, bundle[chunkId], outputOptions) + ) + ) + .then(() => graph.pluginDriver.hookParallel('writeBundle', [bundle])) + .then(() => createOutput(bundle)); + }); + }) as any + }; + if (inputOptions.perf === true) result.getTimings = getTimings; + return result; } enum SortingFileType { From c783380e82c72c2379f173498c87c0cbc15e7b8a Mon Sep 17 00:00:00 2001 From: khai96_ Date: Mon, 12 Aug 2019 12:52:02 +0700 Subject: [PATCH 3/6] Restore some .then()s to fix the tests --- src/rollup/index.ts | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/src/rollup/index.ts b/src/rollup/index.ts index a9fbacc01fd..3cac975a380 100644 --- a/src/rollup/index.ts +++ b/src/rollup/index.ts @@ -190,18 +190,22 @@ export default async function rollup(rawInputOptions: GenericConfigObject): Prom await graph.pluginDriver.hookParallel('buildStart', [inputOptions]); - const chunks = await graph.build( - inputOptions.input as string | string[] | Record, - inputOptions.manualChunks, - inputOptions.inlineDynamicImports as boolean - ); - - try { - await graph.pluginDriver.hookParallel('buildEnd', []); - } catch (err) { - await graph.pluginDriver.hookParallel('buildEnd', [err]); - throw err; - } + const chunks = await graph + .build( + inputOptions.input as string | string[] | Record, + inputOptions.manualChunks, + inputOptions.inlineDynamicImports as boolean + ) + .then( + async chunks => { + await graph.pluginDriver.hookParallel('buildEnd', []); + return chunks; + }, + async err => { + await graph.pluginDriver.hookParallel('buildEnd', [err]); + throw err; + } + ); timeEnd('BUILD', 1); From 7fe47774716e344f21f93737bf5cef7b5eee743c Mon Sep 17 00:00:00 2001 From: khai96_ Date: Mon, 12 Aug 2019 13:14:29 +0700 Subject: [PATCH 4/6] Pass err from 'buildStart' hook to 'buildEnd' --- src/rollup/index.ts | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/rollup/index.ts b/src/rollup/index.ts index 3cac975a380..3a8f4c0c1b1 100644 --- a/src/rollup/index.ts +++ b/src/rollup/index.ts @@ -188,13 +188,14 @@ export default async function rollup(rawInputOptions: GenericConfigObject): Prom timeStart('BUILD', 1); - await graph.pluginDriver.hookParallel('buildStart', [inputOptions]); - - const chunks = await graph - .build( - inputOptions.input as string | string[] | Record, - inputOptions.manualChunks, - inputOptions.inlineDynamicImports as boolean + const chunks = await graph.pluginDriver + .hookParallel('buildStart', [inputOptions]) + .then(() => + graph.build( + inputOptions.input as string | string[] | Record, + inputOptions.manualChunks, + inputOptions.inlineDynamicImports as boolean + ) ) .then( async chunks => { From 4fcacdfc8fa4d57120278190cecd78aa96cc0bee Mon Sep 17 00:00:00 2001 From: khai96_ Date: Tue, 13 Aug 2019 09:09:41 +0700 Subject: [PATCH 5/6] Convert another promise chain into async try/catch --- src/rollup/index.ts | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/src/rollup/index.ts b/src/rollup/index.ts index 3a8f4c0c1b1..2c20f2eef33 100644 --- a/src/rollup/index.ts +++ b/src/rollup/index.ts @@ -188,25 +188,21 @@ export default async function rollup(rawInputOptions: GenericConfigObject): Prom timeStart('BUILD', 1); - const chunks = await graph.pluginDriver - .hookParallel('buildStart', [inputOptions]) - .then(() => - graph.build( - inputOptions.input as string | string[] | Record, - inputOptions.manualChunks, - inputOptions.inlineDynamicImports as boolean - ) - ) - .then( - async chunks => { - await graph.pluginDriver.hookParallel('buildEnd', []); - return chunks; - }, - async err => { - await graph.pluginDriver.hookParallel('buildEnd', [err]); - throw err; - } + let chunks: Chunk[]; + + try { + await graph.pluginDriver.hookParallel('buildStart', [inputOptions]); + chunks = await graph.build( + inputOptions.input as string | string[] | Record, + inputOptions.manualChunks, + inputOptions.inlineDynamicImports as boolean ); + } catch (err) { + await graph.pluginDriver.hookParallel('buildEnd', [err]); + throw err; + } + + await graph.pluginDriver.hookParallel('buildEnd', []); timeEnd('BUILD', 1); From 4aaf4161a2628ad48c423359a24e1951b3a78075 Mon Sep 17 00:00:00 2001 From: khai96_ Date: Tue, 13 Aug 2019 19:51:24 +0700 Subject: [PATCH 6/6] Convert another promise chain to async control flow --- src/rollup/index.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/rollup/index.ts b/src/rollup/index.ts index 2c20f2eef33..4f0246b0f54 100644 --- a/src/rollup/index.ts +++ b/src/rollup/index.ts @@ -297,7 +297,7 @@ export default async function rollup(rawInputOptions: GenericConfigObject): Prom message: 'You must specify "output.file" or "output.dir" for the build.' }); } - return generate(outputOptions, true).then(bundle => { + return generate(outputOptions, true).then(async bundle => { let chunkCnt = 0; for (const fileName of Object.keys(bundle)) { const file = bundle[fileName]; @@ -322,13 +322,13 @@ export default async function rollup(rawInputOptions: GenericConfigObject): Prom : ' To inline dynamic imports, set the "inlineDynamicImports" option.') }); } - return Promise.all( + await Promise.all( Object.keys(bundle).map(chunkId => writeOutputFile(graph, result, bundle[chunkId], outputOptions) ) - ) - .then(() => graph.pluginDriver.hookParallel('writeBundle', [bundle])) - .then(() => createOutput(bundle)); + ); + await graph.pluginDriver.hookParallel('writeBundle', [bundle]); + return createOutput(bundle); }); }) as any };