Skip to content Skip to sidebar Skip to footer

Babel Js -- Simply Convert Javascript String To Es5 (using Nodejs)

I've been googling a lot and found a lot of half-answers or answers that don't address my question, so: I'm just trying to take an input string, that string being regular-source ja

Solution 1:

I'm not certain if this is what you've expected.

I have this simple project structure:

-- package.json-- index.js-- .babelrc

index.js

let babel = require('@babel/core')

// some es6 codelet code = `
    let a = () => { console.log('hello') }
`
babel.transform(
    code,
    {
        babelrc: true,
        filename: '.babelrc'
    },
    function(err, result) {
        console.log(result.code)
    }
)

and this is the console output:

D:\Documents\code\test>node index.js"use strict";

var a = functiona() {
  console.log('hello');
};

D:\Documents\code\test>

and my .babelrc & package.json files in case you're interested.

{
    "presets": [
        "@babel/preset-env"
    ]
}

I just followed along the usage guide. In this case, polyfill and cli are not required.

{
  "name": "test",
  "version": "1.0.0",
  "dependencies": {
    "@babel/polyfill": "^7.2.5"
  },
  "devDependencies": {
    "@babel/cli": "^7.2.3",
    "@babel/core": "^7.3.4",
    "@babel/preset-env": "^7.3.4"
  }
}

Post a Comment for "Babel Js -- Simply Convert Javascript String To Es5 (using Nodejs)"