remark-mdx-remove-esm
Version:
Remark plugin to remove import and/or export statements (mdxjsEsm)
60 lines • 1.96 kB
JavaScript
import { remove } from "unist-util-remove";
function isLiteral(node) {
return "value" in node;
}
function isMdxjsEsm(node) {
return node.type === "mdxjsEsm";
}
const RemarkMdxRemoveEsm = (options) => {
// normalize everything into a clean array
const raw = Array.isArray(options) ? options : [options];
// filter for valid specifiers
const active = raw.filter((o) => o === "import" || o === "export");
const hasImport = active.includes("import");
const hasExport = active.includes("export");
if (options === undefined || (hasImport && hasExport))
return removeAllEsm();
if (hasExport)
return removeExports();
if (hasImport)
return removeImports();
function removeAllEsm() {
return (tree) => {
remove(tree, "mdxjsEsm");
};
}
function removeExports() {
return (tree) => {
remove(tree, (node) => {
// type predicate
if (!isLiteral(node))
return;
// type predicate
if (!isMdxjsEsm(node))
return;
const type = node.data?.estree?.body[0].type;
return (type === "ExportNamedDeclaration" ||
type === "ExportDefaultDeclaration" ||
type === "ExportAllDeclaration");
});
};
}
function removeImports() {
return (tree) => {
// remove import declarations
remove(tree, (node) => {
// type predicate
if (!isLiteral(node))
return;
// type predicate
if (!isMdxjsEsm(node))
return;
const type = node.data?.estree?.body[0].type;
return type === "ImportDeclaration";
});
};
}
return;
};
export default RemarkMdxRemoveEsm;
//# sourceMappingURL=index.js.map