javascript - 就像我们在 babelTypes 中一样,将 JavaScript 代码转换为 AST 表示
问题描述
每次想修改都得写t.importDeclaration([t.importDefaultSpecifier(t.identifier(`${importcomponentName}`))], t.stringLiteral(`../components/${importcomponentName}`))
它仅用于导入语句。例如。,如果我想生成一个完整的组件,我必须编写一个冗长的代码,在一个文件中太长并且耗时。我们有什么捷径可以通过一些递归或库或任何工具来做到这一点吗?
解决方案
Babel 提供@babel/template
了这种类型的东西,所以你可以替换
const decl = t.importDeclaration(
[t.importDefaultSpecifier(t.identifier(`${importcomponentName}`))],
t.stringLiteral(`../components/${importcomponentName}`)
);
和
const decl = template.ast`
import ${importcomponentName} from "${`../components/${importcomponentName}`}";
`;
推荐阅读
- c# - 将二进制文件从 MemoryStream 保存到网络路径会间歇性地保存 0 字节文件
- c++ - 多线程 UDP 服务器中 io_context 或 std::cout 的奇怪行为
- python - 如何在 Kivy 中使用创建圆形或圆形按钮。Kv 和不使用。.kv 文件
- c# - 如何在弹出窗口中单击按钮?(木偶锋利)
- c# - 无法安装 VS2019 软件并抛出“Microsoft.VisualCpp.Redist.14”错误
- java - 在 Jenkins 中运行构建时,我收到“java/lang/OutOfMemoryError”
- javascript - 我可以打开 cy.wait() 超时而不是断言失败吗?
- c - Linux 多播数据包在 4 分钟后干涸
- python - 在 PyQt 的第二个线程中打开子对话框的正确方法是什么?
- java - 如何为给定的 maven 或 gradle 项目生成已弃用的库列表?