Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transform ES5 code to ES6 at runtime

Tags:

There is an option to use babel API to transform javascript code from ecma script 5 to ecma script 6? I mean lets say I use the following cdn

https://cdnjs.cloudflare.com/ajax/libs/babel-core/6.1.19/browser.js

and provide a source like array or object with simple code of ES5 and it transform it to some array/object/string of ES6 code?

is it possible somehow to achieve this with babel or some other tool?

I mean to use some example from here for instance.. https://github.com/addyosmani/es6-equivalents-in-es5

if I put in source ES5 Code

[1, 2, 3].map(function(n) { return n * 2; }, this);

It converted to arrow function in ES6

[1, 2, 3].map(n => n * 2);

UPDATE

What I need is actually is to take ES5 code and change it to ES6 code, it can be via api

For example is I need API/open source that do something like this (my code is in the left side )

Link for example

like image 243
07_05_GuyT Avatar asked Jan 26 '16 15:01

07_05_GuyT


People also ask

What is the process of converting ES6 code to ES5 code called as?

A transpiler is a tool that translates between source codes at the same level of abstraction. Babel is a transpiler because it translates JavaScript ES6 to JavaScript ES5.

Should I compile to ES6 or ES5?

For best performance, you should serve ES6 code to browsers that support it, and only serve ES5 code to browsers that don't support ES6. If you need to statically host your code and serve a single version to all browsers, compile to ES5.

Is ES6 compatible with ES5?

As of now, there are no browsers that fully support the ES6 features; however, we can convert the ES6 code to the ES5 code by using the transpilation. There are two major compilers Babel and Traceur, which are used to convert the ES6 code to ES5 code as part of the build process.

Can Babel convert ES5 to ES6?

Is there any feature in Babel to transpile ES5 code into ES6? Any ES5 code is valid ES6 code. And Babel only transpiles newer features into older equivalent code, not the other way round (detecting patterns in ES5 that could be expressed differently in ES6). You should aim to write ES6 as the source.


2 Answers

The best idea is to go into the source code of Lebab

Open bin/file.js. Read all the lines to understand that script.

The interesting part is the following:

  var transformer = new Transformer({transformers: transformers});
  transformer.readFile(file[0]);
  transformer.applyTransformations();
  transformer.writeFile(program.outFile);

More specificaly transformer.applyTransformations();

Let's open /src/transformer.js

In this file I see some usefull functions :

  /**
   * Prepare an abstract syntax tree for given code in string
   *
   * @param string
   */
  read(string) {

    this.ast = astGenerator.read(string, this.options);

  }

So you can use the transformer with a string of code (not a file)

Now you can apply the transformations "ES5 to ES6"

  /**
   * Apply All transformations
   */
  applyTransformations() {

    for (let i = 0; i < this.transformations.length; i++) {
      let transformation = this.transformations[i];
      this.applyTransformation(transformation);

    }

And then, recast it into string

  out() {
    let result = recast.print(this.ast).code;

    if(this.options.formatter) {
      result = formatter.format(result, this.options.formatter);
    }

    return result;
  }

Summary

var transformer = new Transformer({});
transformer.read('var mySourceCode = "in ES5"');
transformer.applyTransformations();
console.log(transformer.out());

JSFiddle demo here

If you don't want all transformations, you can choose what you want with options:

var transformers = {
  classes: false,
  stringTemplates: false,
  arrowFunctions: true,
  let: false,
  defaultArguments: false,
  objectMethods: false,
  objectShorthands: false,
  noStrict: false,
  importCommonjs: false,
  exportCommonjs: false,
};

var transformer = new Transformer({transformers: transformers});

JSFiddle demo with options

like image 68
Hugeen Avatar answered Sep 22 '22 12:09

Hugeen


To change ES5 to ES6 you can use this https://www.npmjs.com/package/xto6

You have to install it

npm install -g xto6

And then just:

xto6 es5.js -o es6.js

There is also gulp plugin https://www.npmjs.com/package/gulp-xto6:

var gulp = require('gulp');
var xto6 = require('gulp-xto6');

gulp.task('default', function () {
  return gulp.src('path/to/fixtures/es5/*.js')
    .pipe(xto6())
    .pipe(gulp.dest('path/to/fixtures/es6/'));
});
like image 41
Krzysztof Sztompka Avatar answered Sep 21 '22 12:09

Krzysztof Sztompka