@babel/cli
Babel 自带了一个内置的 CLI 命令行工具,可通过命令行编译文件。
此外,各种可直接调用脚本都存放在 @babel/cli/bin 中。一个可通过 shell 执行的实用脚本 - babel-external-helpers.js,以及 Babel cli 主脚本 babel.js。
安装
虽然你 可以 在你的计算机上将 Babel CLI 安装到全局环境中,但是更好的方式是 将 Babel CLI 安装到每个项目的 本地 目录下。
这主要有两个原因:
- 同一台计算机上的不同项目可能依赖不同版本的 Babel,并且你可以针对项目单独升级 Babel 的版本。
- 对你所用的环境没有隐性依赖, 这能让你的项目更易于迁移和设置。
我们可以通过运行以下命令在本地安装 Babel CLI :
- npm
- Yarn
- pnpm
- Bun
npm install --save-dev @babel/core @babel/cli
yarn add --dev @babel/core @babel/cli
pnpm add --save-dev @babel/core @babel/cli
bun add --dev @babel/core @babel/cli
如果不存在 package.json 文件,请在安装之前创建一个。这将确保能够使用 npx 命令。
安装完成后,你的 package.json 文件应当包括如下内容:
{
"devDependencies": {
+ "@babel/cli": "^7.0.0",
+ "@babel/core": "^7.0.0"
}
}
用法
在运行 npx babel 之前,请务必先安装 @babel/cli 和 @babel/core,否则 npx 会安装过时的 babel 6.x 版本。除了使用 npx 之外,你也可以将其放入 npm run script 中,或者直接使用相对路径 ./node_modules/.bin/babel 来运行 Babel。
npx babel script.js
打印帮助信息
npx babel --help
编译文件
编译 script.js 文件并 输出到标准输出设备(stdout)。
npx babel script.js
# output...
如果你希望 输出到文件 ,可以使用 --out-file 或 -o 参数。
npx babel script.js --out-file script-compiled.js
要在 每次文件修改后 编译该文件,请使用 --watch 或 -w 参数:
npx babel script.js --watch --out-file script-compiled.js
编译并输出源码映射表(Source Maps)
从 v7.19.3 版本开始,如果未指定此参数,@babel/cli 将遵循 配置文件 中的设置。
如果你希望输出 源码映射表文件(source map file) ,你可以使用
--source-maps 或 -s 参数。
npx babel script.js --out-file script-compiled.js --source-maps
或者,如果你希望使用 内联源码映射表(inline source maps),请使用 --source-maps inline 参数。
npx babel script.js --out-file script-compiled.js --source-maps inline
编译整个目录
编译整个 src 目录下的文件并输出到 lib 目录,输出目录可以通过 --out-dir 或 -d 指定。这不会覆盖 lib 目录下的任何其他文件或目录。
npx babel src --out-dir lib
编译整个 src 目录下的文件并将输出合并为一个文件。
npx babel src --out-file script-compiled.js
包含 TypeScript 文件的目录
使用 --extensions 选项来指定在编译整个 src 目录时,Babel 应处理哪些文件扩展名。默认的 --extensions 可通过 Babel.DEFAULT_EXTENSIONS 获取。
npx babel src --out-dir lib \
--extensions .ts,.js,.tsx,.jsx,.cjs,.mjs \
--presets=@babel/preset-typescript,@babel/preset-env,@babel/preset-react
忽略某些文件
忽略规范和测试文件
npx babel src --out-dir lib --ignore "src/**/*.spec.js","src/**/*.test.js"
复制文件
复制不需要编译的文件
npx babel src --out-dir lib --copy-files
如果你不想复制被忽略的 JavaScript 文件,请执行以下操作:
History
| 版本 | 变化 |
|---|---|
| v7.8.0 | 添加了 --copy-ignored 选项 |
| v7.8.4 | 将 copyeIgnored 参数的默认值更改为 true,可以通过 --no-copy-ignored 来禁用该选项。 |
npx babel src --out-dir lib --copy-files --no-copy-ignored
通过管道输入文件
通过 stdin 和管道(pipe)将文件内容传递给 babel 命令,并将编译结果输出到 script-compiled.js 文件
npx babel --out-file script-compiled.js < script.js
使用插件
通过 --plugins 参数指定要在编译过程中所使用的插件。
npx babel script.js --out-file script-compiled.js --plugins=@babel/transform-class-properties,@babel/transform-modules-amd
使用 Preset
通过 --presets 参数来指定要在编译过程中所使用的 preset。
npx babel script.js --out-file script-compiled.js --presets=@babel/preset-env,@babel/flow
使用配置文件
忽略 .babelrc.json 或 .babelrc 文件
忽略项目中的 .babelrc 或 .babelrc.json 配置文件,并通过命令行(cli)参数来设置定制化的构建流程
npx babel --no-babelrc script.js --out-file script-compiled.js --presets=@babel/preset-env,@babel/preset-react
自定义配置文件的路径
npx babel --config-file /path/to/my/babel.config.json --out-dir dist ./src
See here for more information about config files.
设置文件扩展名
添加于: v7.8.0
默认情况下,转译后的文件将使用 .js 作为扩展名。
你可以使用 --out-file-extension 参数来控制输出文件的扩展名:
npx babel src --out-dir lib --out-file-extension .mjs
你也可以使用 --keep-file-extension 参数来保留输入文件的原始扩展名:
npx babel src-with-mjs-and-cjs --out-dir lib --keep-file-extension
注意, --keep-file-extension 和 --out-file-extension 不能一起使用。
输出级别
History
| 版本 | 变化 |
|---|---|
| v7.7.0 | 添加了 --silent 选项 |
# Log everything to stdout
npx babel script.js --out-file script-compiled.js --verbose
# Mute the "Successfully compiled..." log
npx babel script.js --out-file script-compiled.js --silent
高阶用法
还有很多参数可以使用,请参阅 options、babel --help 和其他章节以获取更多信息。