问题
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.
回答1:
There are build tools like grunt-babel, which allow you to modify the extension:
files: [{
expand: true,
cwd: 'test/',
src: ['*.test.js'],
dest: 'test/',
ext: '.test.es5.js'
}]
This is a solution. However, I didn't want to rely on using grunt for a small project.
I could create a node script that does what the grunt plugin does, which would be simple, but I think the babel-cli package should naively support this. I will look into the possibility of adding to it.
In the meantime, I have a workaround. I renamed my test files to use the.es6 extension, and then I force babel-cli to look only at that particular extension. The output will be .js for the ES5 code.
babel test --extensions=".es6" --out-dir test
It doesn't allow me to rename the script name or even provide a different extension, but it helps me distinguish between the two versions in the same directory.
来源:https://stackoverflow.com/questions/43369099/how-do-you-change-the-file-names-output-from-babel-cli-in-directory-mode