Every time I want to modify, I have to write t.importDeclaration([t.importDefaultSpecifier(t.identifier(`${importcomponentName}`))], t.stringLiteral(`../components/${importcomponentName}`))
It is just for an import statement. Eg. , if I want to generate a whole component, I have to write a lengthy code that is too long in a file and time-consuming. Do we have any short way to do this by some recursion or library or any tool?
Advertisement
Answer
Babel provides @babel/template
for this type of thing so you could replace
JavaScript
x
5
1
const decl = t.importDeclaration(
2
[t.importDefaultSpecifier(t.identifier(`${importcomponentName}`))],
3
t.stringLiteral(`../components/${importcomponentName}`)
4
);
5
with
JavaScript
1
4
1
const decl = template.ast`
2
import ${importcomponentName} from "${`../components/${importcomponentName}`}";
3
`;
4