javascriptangularecmascript-5es5-compatiblity

How to convert ES6 code to ES5 code on Run Time of Angular App


I am getting one angular module details from API. The module is in ES6 i want to convert that module code into ES5 syntax.

API Used for Module Response

i have tried import { transform } from 'babel-core'; after installing the babel run time. but it is throwing error.

load() {
    fetch(url) // making the get request for loading the module response
      .then(response => response.text())
      .then(source => {
// i want to convert this response to es5 standard. right now it is in es6
      });
  }

My expectation is to transpile es6 to es5 code, on run time of angular component function.


Solution

  • Do:

    npm install --save-dev @babel/core @babel/preset-env
    

    and then:

    import * as babel  from '@babel/core';
    
    load() {
        fetch(url)
          .then(response => response.text())
          .then((source) => {
             return babel.transform(source, {
               presets: ['@babel/preset-env']
             });
          });
      }