How to use babel to transform es6 to es5 programmatically? - javascript

I have a function which runs string as javascript code through eval(). It works fine if the string is es5 but doesn't work for es6. I know babel can transport es6 to es5 but most of them use cases are done in compile stage. How can I use babel programmatically?

Babel has an API.
I assume you can do something like this:
eval(babel.transform(code, options).code)
However I would strongly reconsider that! First, eval is usually a very, very dangerous thing, and next babel is huge. You don't want to deliver that to a browser if you don't have to.

Related

Is there a place to test if code is ES5 compliant / safe?

Sometimes I'd like to know if the code I'm writing is ES5 compliant/safe or not.
Example: this would fail because of the arrow function.
() => "something";
I know Babel could take care of this. But sometimes I would like to test some cases.
Is there a place where I could do this?
Try ESlint, https://eslint.org/demo. You can disable advanced rules and stick to the basics.
For example, with an arrow function on ES5: https://eslint.org/demo#eyJ0ZXh0IjoiKCkgPT4gXCJzb21ldGhpbmdcIjsiLCJvcHRpb25zIjp7InBhcnNlck9wdGlvbnMiOnsiZWNtYVZlcnNpb24iOjUsInNvdXJjZVR5cGUiOiJzY3JpcHQiLCJlY21hRmVhdHVyZXMiOnt9fSwicnVsZXMiOnt9LCJlbnYiOnt9fX0=
It could also be automated as part of a build/ci script.

nightwatch-cucumber ES6 --- nightwatch JS ES5

I've read that nightwatch does not support ES6. Fair enough.
However, the documentation for nightwatch-cucumber
Looks like it uses ES6 (arrow functions) on the steps it defines as examples.
My question is: Is it possible to use ES6 on the cucumber steps and ES5 on the page objects for nightwatch? Or should I stick to ES5 for everything?
Yes I was able to write my cucumber steps in ES6 and my page objects in ES5. However it might be better to stick to ES5 to have consistency + I did notice I would get some strange failures (rarely) when using:
Given('Go to Site', () => client.url('https://google.com'));

How to get rid of editor’s error for object.key

I have the following code that basically gets some JSON data, looks for the keys with "servergenre", and saves the results in an array.
This is a follow up of this question.
let result = [];
Object.keys(data).forEach( key => {
if(/servergenre/.test(key)){
result.push(data[key])
}
});
Even though the code is working correctly, in some editors it raises syntactic errors:
"key": unsolvable variable or type key
"=>": expression expected
"if( / server...": formal parameter name expected
")){": , expected
"});": statement expected
Here is an image to show you where the errors are:
As I said the code is working fine, I just need it to be fixed or another approach to get rid of the errors.
Furthermore, many compressors and minifiers do not support this bit of code. So I can’t minify it.
Thanks in advance.
ES2015, formerly known as ES6, is a more recent version of JavaScript, which introduces features such as the => syntax for functions you are using.
Not all features of ES2015 are fully supported in all browsers, so many people who use it pass it through a compiler (“transpiler”) first to convert it to ES5, which is supported by all browsers. Babel is one such transpiler. But if you are only targeting newer browsers, that would explain why the => syntax is working for you.
You just need to change your editor settings to understand that syntax. Exactly how you do that depends on what text editor you are using. It could be that your editor's built-in JavaScript mode doesn't know how to read ES2015 syntax, and you need to either upgrade your editor or install a third-party plugin that provides an updated error-checker. Or it could be that your editor supports both ES5 and ES2015, and it thinks you are trying to write your project only in ES5. In this case you just need to go to the settings and tell your editor that you mean for this project to use ES2015 (or ES2016, which is currently the most recent version).
Fat arrows are ES6 syntax. If that causes trouble, just write good old ES5 :
let result = [];
Object.keys(data).forEach( function(key) {
if(/servergenre/.test(key)){
result.push(data[key])
}
});

Does a python-esque placeholder exist in the Javascript language?

I am experienced in Python, but relatively new to the Javascript language. I know in Python, if I want to substitute a variable into a string (for instance) I could do it this way:
s = "%s:%s" % (mins, secs)
However, I can't find an equivalent way to substitute values in Javascript. Does something like this exist, or am I going about this the wrong way?
Not available in ES5, but it is available in ES6:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
Keep in mind this might not work right in the browser across the board-- you'll probably need to use something like Babel to transpile your code to ES5.

Destructuring in ES6. Should I worry?

Experimenting with destructuring and found that the same code works on stackoverflow and not Codepen (toy gets "undefined"): http://codepen.io/tsalexey544/pen/VjWxmm?editors=0010#
What does it mean? should I worry when using destructuring in my projects?
let obj = {
species: "Cat",
// toy: "ball",
}
function whatDoTheyDo ({species, toy = "ball"}) {
return `The ${species} playes with a ${toy}`
}
document.write(whatDoTheyDo(obj));
You just need to set the Preprocessor to babel in CodePen, otherwise it will use standard ES5, where destructuring is not supported.
If you want to use ES6/ES7 features you have to "transpile" your code back to ES5 using certain tools such as Babel. Some browsers already support some ES6 features, but full support is still somehow spotty.
Edit - To answer your question: YES, you should worry about serving valid ES5 code, since ES6 is not yet fully supported. At the very minimum you should feed your code to Babel and publish the resulting code, but I strongly suggest looking it Webpack and going for a full toolchain

Categories

Resources