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

Asked

Viewed 228 times

4

I have an HTML5 Cordova project and I need to hook before compiling to convert entire projects folder (www) to ES5 before compiling. It is possible?

Structure

cordova/
    hooks/
    node_modules/
    platforms/
    plugins/
    www/
    config.xml
    icon.png
    splash.png
  • You are in Node.js right? You can put the code of the file that compiles?

  • @Sergio The project is Cordova, not Ode. But I have Ode. I can post yes.

  • @Sergio Updated the question with the directories.

  • Utilise Rollup + Bublé, worth taking a look, in my case the compilation is much faster.

  • @Jonataswalker Da to convert directory or just file this Bumblé?

  • As you wish, directory or file (example), I personally use this design model.

Show 1 more comment

1 answer

2


I have something similar mounted on an app that I’m working on. Then I mix things from Browserify and Babel because I import modules I want to have on the client with npm. But only Babel version could be 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 more things into this array that I need to compile. You can link with .then() and call the Córdova compiler next if you are calling via command line.

In my briefcase I’m running this module in root/lib, the original files are in root/js and I’m compiling all the .js for root/public/js.

Dependencies are:

"babel-cli": "^6.4.5",
"babel-preset-es2015": "^6.3.13",
  • good! How to implement this in a Cordova hook to transpire only in Platforms/Ios/Assets?

  • @ropbla9 do not know well the internal mechanisms of Cordova, I know that has a central "hook" when compiling but I do not dominate. That would be a good new question that would be interesting to see answered :) Take a look here: https://cordova.apache.org/docs/en/latest/guide/appdev/hooks/#configxml and you could try putting on config.xml one <platform name="ios"> and define there which script to run.

  • About Babel, I can’t use it for the CLI Couldn't find preset "es2015" relative to directory. Could open private?

  • @ropbla9 you’re right, forgot, I just joined the dependencies.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.