@babel/cli
本页面由 PageTurner AI 翻译(测试版)。未经项目官方认可。 发现错误? 报告问题 →
Babel 自带内置命令行工具(CLI),可通过命令行编译文件。
此外,在 @babel/cli/bin 顶层包中包含多个入口脚本:包括可执行的 shell 工具脚本 babel-external-helpers.js 和主 Babel CLI 脚本 babel.js。
安装
虽然您_可以_全局安装 Babel CLI,但本地按项目安装是更好的实践。
这样做主要有两个原因:
-
同一机器上的不同项目可依赖不同版本的 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 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 文件,可使用 --source-maps 或 -s:
npx babel script.js --out-file script-compiled.js --source-maps
如需使用内联 source maps,请改用 --source-maps inline:
npx babel script.js --out-file script-compiled.js --source-maps inline
编译目录
使用 --out-dir 或 -d 编译整个 src 目录并输出到 lib 目录(不会覆盖 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
忽略文件
忽略 spec 文件和测试文件:
npx babel src --out-dir lib --ignore "src/**/*.spec.js","src/**/*.test.js"
复制文件
复制无需编译的文件:
npx babel src --out-dir lib --copy-files
如不希望复制被忽略的 JavaScript 文件:
History
| Version | Changes |
|---|---|
| v7.8.0 | Added --copy-ignored |
| v7.8.4 | Change copyeIgnored option default to true, it can be disabled by --no-copy-ignored |
npx babel src --out-dir lib --copy-files --no-copy-ignored
管道传输文件
通过 stdin 管道传输文件并输出到 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
使用预设
通过 --presets 选项指定编译时使用的预设
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
有关配置文件的更多信息,请参阅此处。
设置文件扩展名
新增于: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 不可同时使用。
高级用法
更多选项请参阅选项说明、执行 babel --help 命令及其他相关章节。