Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you change the file names output from babel-cli in directory mode?

Background:

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:

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

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.

Question:

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"

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.

like image 939
Gideon Pyzer Avatar asked Apr 12 '17 12:04

Gideon Pyzer


People also ask

What is the use of Babel CLI?

Babel comes with a built-in CLI which can be used to compile files from the command line. In addition, various entry point scripts live in the top-level package at @babel/cli/bin . There is a shell-executable utility script, babel-external-helpers.

What is Babelrc?

The . babelrc file is your local configuration for your code in your project. Generally you would put it in the root of your application repo. It will affect all files that Babel processes that are in the same directory or in sibling directories of the . babelrc .

How do you set up Babel?

Simply add a "scripts" field to your package. json and put the babel command inside there as build . This will run Babel the same way as before and the output will be present in lib directory, only now we are using a local copy. Alternatively, you can reference the babel cli inside of node_modules .

How do I know if I have Babel?

You can also check the version of babel-cli by finding the babel-cli folder in node_modules and looking at the version property of the package. json that is at the base of that folder. If babel-cli was installed globally via -g flag of npm install , you could check the version by executing command babel --version .


1 Answers

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

like image 142
adamduncan Avatar answered Sep 27 '22 19:09

adamduncan