From 255bfe11a79e57f7fabe20e2febb43f2da731b47 Mon Sep 17 00:00:00 2001 From: Daniel Nalborczyk Date: Thu, 30 Dec 2021 13:34:25 -0500 Subject: [PATCH 1/2] refactor: convert utils.transform to async function --- src/utils/transform.ts | 47 ++++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/src/utils/transform.ts b/src/utils/transform.ts index 5821d636a07..80724be94dc 100644 --- a/src/utils/transform.ts +++ b/src/utils/transform.ts @@ -21,7 +21,7 @@ import { decodedSourcemap } from './decodedSourcemap'; import { augmentCodeLocation, errNoTransformMapOrAstWithoutCode } from './error'; import { throwPluginError } from './pluginUtils'; -export default function transform( +export default async function transform( source: SourceDescription, module: Module, pluginDriver: PluginDriver, @@ -37,7 +37,7 @@ export default function transform( const emittedFiles: EmittedFile[] = []; let customTransformCache = false; const useCustomTransformCache = () => (customTransformCache = true); - let curPlugin: Plugin; + let curPlugin: Plugin | undefined; const curSource: string = source.code; function transformReducer( @@ -77,8 +77,10 @@ export default function transform( return code; } - return pluginDriver - .hookReduceArg0( + let code: string; + + try { + code = await pluginDriver.hookReduceArg0( 'transform', [curSource, id], transformReducer, @@ -149,23 +151,24 @@ export default function transform( } }; } - ) - .catch(err => throwPluginError(err, curPlugin.name, { hook: 'transform', id })) - .then(code => { - if (!customTransformCache) { - // files emitted by a transform hook need to be emitted again if the hook is skipped - if (emittedFiles.length) module.transformFiles = emittedFiles; - } + ); + } catch (err: any) { + throwPluginError(err, curPlugin?.name ?? 'Unknown plugin', { hook: 'transform', id }); + } + + if (!customTransformCache) { + // files emitted by a transform hook need to be emitted again if the hook is skipped + if (emittedFiles.length) module.transformFiles = emittedFiles; + } - return { - ast, - code, - customTransformCache, - meta: module.info.meta, - originalCode, - originalSourcemap, - sourcemapChain, - transformDependencies - }; - }); + return { + ast, + code, + customTransformCache, + meta: module.info.meta, + originalCode, + originalSourcemap, + sourcemapChain, + transformDependencies + }; } From 885682343cee95fda9d748c6ae603caef6760af3 Mon Sep 17 00:00:00 2001 From: Daniel Nalborczyk Date: Fri, 31 Dec 2021 11:59:27 -0500 Subject: [PATCH 2/2] initialize plugin name --- src/utils/transform.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/utils/transform.ts b/src/utils/transform.ts index 80724be94dc..16ea7de1319 100644 --- a/src/utils/transform.ts +++ b/src/utils/transform.ts @@ -37,7 +37,7 @@ export default async function transform( const emittedFiles: EmittedFile[] = []; let customTransformCache = false; const useCustomTransformCache = () => (customTransformCache = true); - let curPlugin: Plugin | undefined; + let pluginName = ''; const curSource: string = source.code; function transformReducer( @@ -85,7 +85,7 @@ export default async function transform( [curSource, id], transformReducer, (pluginContext, plugin): TransformPluginContext => { - curPlugin = plugin; + pluginName = plugin.name; return { ...pluginContext, addWatchFile(id: string) { @@ -153,7 +153,7 @@ export default async function transform( } ); } catch (err: any) { - throwPluginError(err, curPlugin?.name ?? 'Unknown plugin', { hook: 'transform', id }); + throwPluginError(err, pluginName, { hook: 'transform', id }); } if (!customTransformCache) {