@babel/cli是babel自带的命令行集成工具,本文对其常见的使用api进行了总结。很多情况下我们开发人员看着官网介绍也可以很快的使用该功能,但是为什么还要记忆这些呢。
除了使用时可以快速实现意外,还有两点比较重要的
一:通过总结可以加深记忆,有利于于新知识的接收
二:形成知识网络,单个的知识是孤立的,网状知识是有迹可循的
安装
使用npm安装到本地,可以全局安装,也可以使用npx
npm install --save-dev @babel/core @babel/cli
语法
babel test.js --参数
功能分类
按功能划分为以下几类:
1,输出类,包括文件夹的名称,文件的名称,输出的格式,是否编译等等
--out-file | 输出文件名称 | npx babel script.js --out-file script-compiled.js |
--watch | 文件监控 | npx babel script.js --watch --out-file script-compiled.js |
--source-maps | 生成.js.map文件 | npx babel script.js --out-file script-compiled.js --source-maps |
--source-maps inline | 在生成的文件中插入source.map注释 | npx babel script.js --out-file script-compiled.js --source-maps inline |
--out-dir | 输出文件夹 | npx babel src --out-dir lib |
--copy-files | 复制文件 | npx babel src --out-dir lib --copy-files |
< | 通过stdin导入文件 | npx babel --out-file script-compiled.js < script.js |
--out-file-extension | 指定扩展名称 | babel src/ lib/ --out-file-extension .mjs |
2,插件和预设,指定编译代码时的插件或者预设
--plugins= | 指定plugins | npx babel script.js --out-file script-compiled.js --plugins=@babel/proposal-class-properties,@babel/transform-modules-amd |
--presets= | 指定presets | npx babel script.js --out-file script-compiled.js --presets=@babel/preset-env,@babel/flow |
--config-file | 指定configPath | npx babel --config-file /path/to/my/babel.config.json --out-dir dist ./src |
3,忽略文件
--ignore | 忽略文件 | npx babel src --out-dir lib --ignore "src/**/*.spec.js","src/**/*.test.js" |
--no-copy-ignored | 不拷贝忽略文件 | npx babel src --out-dir lib --copy-files --no-copy-ignored |
--no-babelrc | 忽略.babelrc | npx babel --no-babelrc script.js --out-file script-compiled.js --presets=es2015,react |
参考: