Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3943111

Browse files
authored
refactor: convert utils.transform to async function (#4333)
* refactor: convert utils.transform to async function * initialize plugin name
1 parent ba7fc53 commit 3943111

File tree

1 file changed

+26
-23
lines changed

1 file changed

+26
-23
lines changed

src/utils/transform.ts

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { decodedSourcemap } from './decodedSourcemap';
2121
import { augmentCodeLocation, errNoTransformMapOrAstWithoutCode } from './error';
2222
import { throwPluginError } from './pluginUtils';
2323

24-
export default function transform(
24+
export default async function transform(
2525
source: SourceDescription,
2626
module: Module,
2727
pluginDriver: PluginDriver,
@@ -37,7 +37,7 @@ export default function transform(
3737
const emittedFiles: EmittedFile[] = [];
3838
let customTransformCache = false;
3939
const useCustomTransformCache = () => (customTransformCache = true);
40-
let curPlugin: Plugin;
40+
let pluginName = '';
4141
const curSource: string = source.code;
4242

4343
function transformReducer(
@@ -77,13 +77,15 @@ export default function transform(
7777
return code;
7878
}
7979

80-
return pluginDriver
81-
.hookReduceArg0(
80+
let code: string;
81+
82+
try {
83+
code = await pluginDriver.hookReduceArg0(
8284
'transform',
8385
[curSource, id],
8486
transformReducer,
8587
(pluginContext, plugin): TransformPluginContext => {
86-
curPlugin = plugin;
88+
pluginName = plugin.name;
8789
return {
8890
...pluginContext,
8991
addWatchFile(id: string) {
@@ -149,23 +151,24 @@ export default function transform(
149151
}
150152
};
151153
}
152-
)
153-
.catch(err => throwPluginError(err, curPlugin.name, { hook: 'transform', id }))
154-
.then(code => {
155-
if (!customTransformCache) {
156-
// files emitted by a transform hook need to be emitted again if the hook is skipped
157-
if (emittedFiles.length) module.transformFiles = emittedFiles;
158-
}
154+
);
155+
} catch (err: any) {
156+
throwPluginError(err, pluginName, { hook: 'transform', id });
157+
}
158+
159+
if (!customTransformCache) {
160+
// files emitted by a transform hook need to be emitted again if the hook is skipped
161+
if (emittedFiles.length) module.transformFiles = emittedFiles;
162+
}
159163

160-
return {
161-
ast,
162-
code,
163-
customTransformCache,
164-
meta: module.info.meta,
165-
originalCode,
166-
originalSourcemap,
167-
sourcemapChain,
168-
transformDependencies
169-
};
170-
});
164+
return {
165+
ast,
166+
code,
167+
customTransformCache,
168+
meta: module.info.meta,
169+
originalCode,
170+
originalSourcemap,
171+
sourcemapChain,
172+
transformDependencies
173+
};
171174
}

0 commit comments

Comments
 (0)