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