How to compile the entire project folder for ES5 using babel.js?

4

I have an HTML5 Cordova project and I need to make a hook before compiling to convert entire projects folder (www) to ES5 before compiling. Is it possible?

Structure

cordova/
    hooks/
    node_modules/
    platforms/
    plugins/
    www/
    config.xml
    icon.png
    splash.png
    
asked by anonymous 09.08.2016 / 23:10

1 answer

2

I have something like that mounted on an app I'm working on. Then I mix things from Browserify and Babel because I import modules that I want to have in the client with npm. But the only Babel version might look like this:

"use strict";
var exec = require('child_process').exec;

function babel(){
    return new Promise(resolve, reject){
        exec('babel --presets es2015 js --out-dir public/js', {
            cwd: __dirname + '/../'
        }, (err) => {
            if (err) reject(err);
            else resolve();
        });
    });
}

module.exports = Promise.all([babel]);

I use Promise.all because I put in this array more things that I need to compile. You can chain with .then() and call the cordoba compiler next if you are calling via command line.

In my folder I'm running this module in root/lib , the original files are in root/js and I'm compiling all files from .js to root/public/js .

Dependencies are:

"babel-cli": "^6.4.5",
"babel-preset-es2015": "^6.3.13",
    
10.08.2016 / 00:45