如何配置Babel Transpiler CLI遇错误时继续转译文件
如何让Babel CLI遇到语法错误时继续转译剩余文件?
要实现遇到单个文件语法错误时不终止整个转译进程,有两种可行的配置方式:
方式一:修改CLI命令参数
在原命令中添加--error-recovery参数,该参数会让Babel在遇到语法错误时跳过当前文件,继续处理目录中的其他文件:
npx babel <input_dir> --out-dir <output_dir> --extensions .ts --error-recovery
方式二:在Babel配置文件中全局设置
在babel.config.json的顶层添加"errorRecovery": true配置,这样无需每次命令行添加参数,默认启用错误恢复模式:
{ "errorRecovery": true, "presets": [ "@babel/preset-typescript", "@babel/preset-react" ], "plugins":[ "@babel/plugin-transform-class-properties", "@babel/plugin-transform-modules-commonjs", "@babel/plugin-transform-export-namespace-from", ["@babel/plugin-proposal-decorators",{ "decoratorsBeforeExport": true }], ["@babel/plugin-syntax-decorators",{ "decoratorsBeforeExport": true }], ["@babel/plugin-syntax-import-attributes" , { "deprecatedAssertSyntax": true }] ] }
无论使用哪种方式,Babel都会在遇到语法错误的文件时记录错误信息,但不会终止整个转译流程,剩余的正常文件会被正常转译输出。
内容的提问来源于stack exchange,提问作者Guy Hudara
相关产品推荐
相关产品推荐

