Skip to content

Commit

Permalink
refactor: convert utils.transform to async function (#4333)
Browse files Browse the repository at this point in the history
* refactor: convert utils.transform to async function

* initialize plugin name
  • Loading branch information
dnalborczyk committed Jan 4, 2022
1 parent ba7fc53 commit 3943111
Showing 1 changed file with 26 additions and 23 deletions.
49 changes: 26 additions & 23 deletions src/utils/transform.ts
Expand Up @@ -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,
Expand All @@ -37,7 +37,7 @@ export default function transform(
const emittedFiles: EmittedFile[] = [];
let customTransformCache = false;
const useCustomTransformCache = () => (customTransformCache = true);
let curPlugin: Plugin;
let pluginName = '';
const curSource: string = source.code;

function transformReducer(
Expand Down Expand Up @@ -77,13 +77,15 @@ export default function transform(
return code;
}

return pluginDriver
.hookReduceArg0(
let code: string;

try {
code = await pluginDriver.hookReduceArg0(
'transform',
[curSource, id],
transformReducer,
(pluginContext, plugin): TransformPluginContext => {
curPlugin = plugin;
pluginName = plugin.name;
return {
...pluginContext,
addWatchFile(id: string) {
Expand Down Expand Up @@ -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, pluginName, { 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
};
}

0 comments on commit 3943111

Please sign in to comment.