This might be a bug in Babel CLI (I'm using 6.24.1), but it appears that Babel doesn't apply .babelrc
when the source data is being piped to it.
Example:
Given this source file, foo.js
:
const foo = 10;
Good: This command does what you think it should, with a .babelrc
in the same directory with the es2015
preset in it:
babel foo.js --out-file foo.classic.js
This results in foo.classic.js
, with properly transpiled content:
"use strict";
var foo = 10;
Bad: This command does not do what you expect:
cat foo.js | babel --out-file foo2.classic.js
This results in foo2.classic.js
, with unchanged content:
const foo = 10;
Good: This command does what you expect, so it's not the actual configuration at fault:
cat foo.js | babel --presets es2015 --out-file foo3.classic.js
Result in foo3.classic.js
:
"use strict";
var foo = 10;
Analysis: In the piping cases, Babel is obviously reading the data from the pipe and passing it out (because the output file gets created), but it appears that Babel completely ignores the .babelrc
when it gets the data from the pipe.
For reference, this is the .babelrc
that is supposed to be applied:
{
"presets": [ "es2015" ]
}
Why use pipes?
For what it's worth, you might wonder, "Why pipe input to Babel? Why not just use files?"
Well, for one thing, piping is a supported feature, so it should "just work."
But in my case, the source files being passed to Babel are themselves the result of a code-generation chain that produces valid ES6 code as output. It would be nice not to have to have the code generator output a ~temp.js
intermediate file and then have to have it be deleted afterward; it would be far better if the pipes just worked as they're supposed to.
The Question:
Is this a bug? If it is, does anyone know of a better workaround than just emitting a file named ~temp.js
, passing that to Babel, and then deleting it?
This is currently expected behavior, but we've talked about some alternatives in https://github.com/babel/babel/pull/5384.
See my comment here for some info. As mentioned in there, the current fix for you would be to do
cat foo.js | babel --filename foo.js --out-file foo2.classic.js
to tell Babel the filename, so it knows where to search for the .babelrc
file.