prettier-plugin-sort-re-exports
Version:
A prettier plugin that sorts the re-exports statements.
18 lines (17 loc) • 649 B
JavaScript
import { parse } from "@babel/parser";
import { chunkReExports } from "./_internal/chunkReExports.js";
import { getSortedCode } from "./_internal/getSortedCode.js";
export function preprocessor(code) {
const ast = parse(code, {
sourceType: "module",
plugins: ["typescript"],
});
const reExportNodes = ast.program.body.filter((node) => (node.type === "ExportNamedDeclaration" ||
node.type === "ExportAllDeclaration") &&
node.source);
if (reExportNodes.length <= 1) {
return code;
}
const reExportChunks = chunkReExports(reExportNodes);
return getSortedCode(code, reExportChunks);
}