合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
## @babel/parser 包括两个函数 * parse - 返回文件 * parseExpression - 返回表达式 ![](https://img.kancloud.cn/cb/ae/cbae86f3943652add3a0ade9ce7aa614_1472x714.png) 🌰 解析 typescript ``` require("@babel/parser").parse("code", { sourceType: "module", // 解析 es module 语法 plugins: [ "jsx", "typescript" ] // 指定jsx、typescript 插件来解析对应的语法 }); ``` ## @babel/traverse * 遍历和修改 AST ``` // 进入 FunctionDeclaration 节点时调用 traverse(ast, { FunctionDeclaration: { enter(path, state) {} } }) ``` ## @babel/types * 遍历 AST 的过程中创建一些 AST 和判断 AST 的类型 * 一个个的创建然后组装 ``` t.isIfStatement(node, opts); // 判断 t.ifStatement(test, consequent, alternate); // 创建 ``` ## @babel/template * 批量创建 AST 节点 * template.expression、template.statement、template.statements 方法,明确创建的AST的类型 * ## @babel/generator * 生成目标代码字符串 ## @babel/code-frame * 打印错误位置的代码 ## @babel/core * 基于上述api完成整个编译流程,从源码到目标代码,生成 sourcemap。