flowbite-react
Version:
Official React components built for Flowbite and Tailwind CSS
55 lines (52 loc) • 1.84 kB
JavaScript
import { parse } from '@typescript-eslint/typescript-estree';
import * as recast from 'recast';
import { addImport } from './add-import.js';
function updateBuildConfig({
content,
pluginName,
pluginImportPath
}) {
const ast = recast.parse(content, {
parser: {
parse: (source) => parse(source, {
loc: true,
range: true,
tokens: true,
comment: true
})
}
});
const b = recast.types.builders;
recast.types.visit(ast, {
visitCallExpression(path) {
const { node } = path;
if (node.callee.type === "Identifier" && node.callee.name === "build" && node.arguments.length > 0 && node.arguments[0].type === "ObjectExpression") {
const buildOptions = node.arguments[0];
let pluginsProperty = buildOptions.properties.find(
(p) => p.type === "Property" && (p.key.type === "Identifier" && p.key.name === "plugins" || p.key.type === "Literal" && p.key.value === "plugins")
);
if (!pluginsProperty) {
pluginsProperty = b.property("init", b.identifier("plugins"), b.arrayExpression([b.identifier(pluginName)]));
buildOptions.properties.push(pluginsProperty);
} else if (pluginsProperty.value.type === "ArrayExpression") {
const hasPlugin = pluginsProperty.value.elements.some(
(el) => el?.type === "Identifier" && el.name === pluginName
);
if (!hasPlugin) {
pluginsProperty.value.elements.push(b.identifier(pluginName));
}
}
}
return false;
}
});
let modifiedCode = recast.print(ast).code;
modifiedCode = addImport({
content: modifiedCode,
importName: pluginName,
importPath: pluginImportPath
});
return modifiedCode;
}
export { updateBuildConfig };
//# sourceMappingURL=update-build-config.js.map