如何在目录模式下更改babel-cli输出的文件名? [英] How do you change the file names output from babel-cli in directory mode?

查看:236
本文介绍了如何在目录模式下更改babel-cli输出的文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:

我有一些用ES2015编写的测试,但是由于v2.1中QtWebKit中缺少ES2015支持,因此需要将其转换为ES5才能在PhantomJS中运行. 2.5 beta应该支持它.

I have some tests that are written in ES2015, but will need to be transpiled to ES5 to run in PhantomJS due to the lack of ES2015 support in QtWebKit in v2.1. The 2.5 beta should support it.

babel-cli:

我可以使用babel-cli将测试转换为ES5:

I can transpile a test to ES5 using babel-cli:

babel main.test.js --out-file main.test.es5.js

为了转译脚本目录,我可以使用:

In order to transpile a directory of scripts, I can use:

babel tests --out-dir compiled-tests

这会将转译的测试脚本输出到compiled-tests目录.如果我使用相同的目录,它将覆盖原始目录,因此我们不希望这样做.

This will output the transpiled test scripts to the compiled-tests directory. If I use the same directory, it will overwrite the original, so we don't want that.

问题:

是否有一种方法可以在目录模式下babel-cli修改文件名或扩展名,而不必循环浏览文件并使用babel.transformFileSync?

Is there a way to modify the file name or extension in the directory mode of babel-cli without having to loop through the files and using babel.transformFileSync?

例如,我期望按照以下方式做一些事情:

For example, I was expecting to do something along the lines of:

  babel tests --out-dir tests --outputExtension=".es5.js"

这需要a.jsb.js等并输出a.es5.jsb.es5.js等.

This would take a.js, b.js etc and output a.es5.js, b.es5.js, etc.

我还可以想象使用正则表达式来提取源文件名的一部分并将其与自定义内容结合在一起的选项.

I could also imagine the option of using regular expressions to extract part(s) of the source file name and combine that with something custom.

推荐答案

由于 babel-cli --out-file-extension标志:

Since v7.8.0 you can use babel-cli's --out-file-extension flag:

babel tests --out-dir tests --out-file-extension .es5.js

https://babeljs.io/docs/en/babel -cli#set-file-extensions

这篇关于如何在目录模式下更改babel-cli输出的文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆