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: convert utils.transform to async function #4333

Merged
merged 2 commits into from Jan 4, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 25 additions & 22 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 curPlugin: Plugin | undefined;
const curSource: string = source.code;

function transformReducer(
Expand Down Expand Up @@ -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,
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, 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
};
}