unbag
Version:
一个专门用来开发npm工具的包
39 lines • 1.23 kB
JavaScript
import { defineTransformActionTask, TransformActionTaskOutFileType } from "../index.mjs";
import { transformFileAsync } from "@babel/core";
const TransformActionTaskBabel = options => {
const {
babel: babelOptions,
extMapping
} = options;
return defineTransformActionTask(async params => {
const {
inputDir,
filePaths
} = params;
const matchedExtnames = Object.keys(extMapping);
const filePathsFiltered = filePaths.filter(filePath => {
if (matchedExtnames.includes(filePath.extname)) {
return true;
}
return false;
});
const outputFiles = await Promise.all(filePathsFiltered.map(async filePath => {
const absoluteFilePath = filePath.toAbsolutePath({
rel: inputDir
});
const jsFile = await transformFileAsync(absoluteFilePath.content, {
...babelOptions
});
const targetExtName = extMapping[filePath.extname];
return {
from: filePath,
to: filePath.replaceExtname(targetExtName),
type: TransformActionTaskOutFileType.Transformed,
content: jsFile?.code || "",
sourcemap: void 0
};
}));
return outputFiles;
});
};
export { TransformActionTaskBabel };