Jacob Lowe

jsla - Module of the Month: Babel

by Jacob Lowe on

Module of the month was a small series at jsla in which people of the community would give talks about a specific module in the nodejs ecosystem. This months talk was about Babel.

Transcripts (auto-generate)

  • I would be talking about a Babel or
  • Babel depends on where you're at and how
  • you call it but essentially Babel is a
  • trance compiler that will take next
  • generation JavaScript and turn it into
  • readable es5 JavaScript which means that
  • you can then run it on node or on a
  • browser at least some of the more modern
  • browsers there's still some that you
  • can't run it on and I say next
  • generation because it's not only es6 but
  • actually it's also es7 because like es6
  • now has been like finalized and we're
  • going to start seeing it roll into some
  • of the newer browsers usually the ones
  • that we use but es7 is a lot of
  • proposals and whatnot but a lot of the
  • people behind babel are you know at the
  • forefront of this so they're writing
  • these things out so that they can start
  • playing with these different proposals
  • but they're behind experimental flags so
  • don't worry you don't have to mess with
  • them if you don't want you so here's a
  • website there's like a ton of good
  • instructions on here on how to use this
  • thing I'm going to show you a few
  • examples of how to use this thing that
  • hopefully will apply to something that
  • you can actually start using so the
  • first thing is that it's a COI tool but
  • you can also use it programmatically
  • with in JavaScript I'm going to be using
  • the CLI tool but pretty much the options
  • come across the same exact way so first
  • off we need to start writing some es6
  • code so let me write so those classes in
  • es6 oh I'm gonna lie to class I'm just
  • going to make a class foo and it's going
  • to need a constructor and as you can see
  • this doesn't look like JavaScript the
  • stuff that we write this is totally
  • different and actually what I want to do
  • is pass them like options to this but
  • I'm also going to give it like a default
  • argument so that way it's always an
  • object I can count on it being that and
  • we'll just console.log that out just to
  • say just to show you that this is
  • working so type of
  • options and we're also going to do like
  • a basic like greet function part create
  • method we're going to use we're going to
  • pass an argument of name and then we're
  • going to return a hello name as you can
  • see there i'm actually using string
  • interpolation nu te s six so now we want
  • to actually do something with this so
  • I'm just going to say Const foo is equal
  • to new foo I'm going to pass anything to
  • it so that way we can see default
  • arguments working and then we're going
  • to say console.log and then food 3gs la
  • now let's say I want to run this on node
  • I'm a no developer I just want to run an
  • es6 file so babel comes with not only
  • the Babel command which as you can see
  • it will do nothing if I don't pass
  • anything but also Babel node which is
  • one's kind of straightforward
  • essentially you're running Babel inside
  • of node or you're essentially doing a
  • wrapper around node that will then
  • transform all of your code into es6 or
  • transfer all of your ESX code into es5
  • so it can be run on node so all I'm
  • going to do is point it towards this
  • file which is in Babel node directory
  • let's see and then index or is it mean
  • what is it mean
  • yeah ah sorry about that sorry guys I'm
  • terrible at spelling and then I got to
  • fix the other one again alright cool so
  • we got the type of object so actually
  • didn't pass anything there the default
  • arguments worked in hello JSL a witch
  • did string interpolation but as you can
  • see I didn't it didn't show any of the
  • compilation it did it on the fly while I
  • was running inside of node let's say
  • you're not lucky to use note at your
  • your work or any of your projects you
  • just write client apps so you need to be
  • able to trans compile this which is the
  • regular Babel command so what I'm going
  • to do is I'm going to take my contents
  • in this file and I'm going to go over
  • into this file here we're just gonna
  • paste over what's here so we already
  • know a console logs out object and hello
  • j SLA so what I'm going to do is I'm
  • going to run instead of Babel node I'm
  • just going to run Babel and then I'm
  • going to pass it my file which was in
  • extra j/s now as you can see it just
  • output it to the standard out what I'm
  • going to do is I'm actually just going
  • to copy this right now and it's readable
  • to a certain extent there's some crazy
  • stuff in here and I'm going to take this
  • over into my browser so you can see this
  • is just es5 code and we have the console
  • log object in hello jla so we trans
  • compiled es6 code and we're now running
  • it inside of a browser granted you don't
  • want to copy and paste it from your
  • standard out and then paste it into your
  • browser's console that's not logical so
  • well you can deal with Babel is you can
  • actually output it to a file so I'm just
  • going to do it out to a file called
  • compiled without the dash
  • it's going to be dash oh so da show is
  • output or you can do dash dash out push
  • output file its going to run so it
  • compiled it and we'll just run that on
  • node with regular note command and then
  • compilation and then compiled and as you
  • can see it's the same thing so that ran
  • a lot faster I don't know if you noticed
  • with the last time I ran it ran pretty
  • slow because it was compiling the files
  • and then running them so let's say
  • you're like using something like a
  • puddle electron project well you don't
  • really have access to node so you can't
  • really like be like oh I'm going to use
  • babel node to run it in that instance
  • you don't really have a way to you
  • besides there's this thing called a
  • Babel register which will allow you to
  • register all of the required statements
  • inside of node and pretty much hook into
  • that and then compile those things while
  • you're essentially requiring them in of
  • course it's a little bit slower you're
  • doing compilation but it does work so i
  • have this required transform slightly
  • es6 file here it's just another foo
  • class i'm doing some more stuff with it
  • passing some arguments to it and pulling
  • things out but also have this index file
  • so this is es5 it's actually a little
  • bit of es6 just the cost but i'm running
  • in I ojs so I can actually use those
  • type of things and as you can see like
  • I'm just using required it's it looks
  • like basic codes that maybe you work
  • with on a day-to-day basis except i have
  • require Babel and then / register so
  • what I can do with this is actually now
  • I can run node and then the required
  • transform and then the index file and as
  • you can see it console logged all my
  • stuff out we can go back and verify that
  • those are the right things but actually
  • I'm able to run es6 code in files that I
  • require so the base the only thing that
  • sucks about this is that the base file
  • your entry point file has to be es5 you
  • can try to es6 inside of it because
  • essentially we're just overriding the
  • requires statement so that's really cool
  • so you're able to now use it inside of
  • node you're able to use it inside of a
  • client app there's actually things for
  • doing like if you were to use browserify
  • there's babe laughs I there's gold Babel
  • they have plugins for everything it's
  • really cool it's like production ready
  • there's a ton of people already using a
  • Babel and it's you know Yahoo's using it
  • I actually have a list here because they
  • have it all over their website
  • cloudflare Netflix Airbnb is actually
  • using some really cool projects ghost
  • Adam the editor which I would have been
  • editing the stuff on so it's really cool
  • I suggest you know even if you don't
  • really like doing trance compiling stuff
  • you want to play around with the new
  • version of the language download Babel
  • and just play around with it and see if
  • you can actually make something thank
  • you