If you liked what you've learned so far, dive in!
Subscribe to get access to this tutorial plus
video, code and script downloads.
Ok, so do you love ES2015 yet? Man, I sure do. So... let's go use it immediately on our site! Oh wait.... didn't I mention that its features aren't supported by all browsers? Whoops. Yea, the latest version of Chrome supports everything... but if some of our users have older browsers, does this mean we can't use ES2015?
Nope! You can! Thanks to an amazing tool called Babel. But before we talk about it, we need to do a little bit of installation. In the Node.js world, the package manager is called npm, and because you already installed Node, you already have it!
But wait! There's a disturbance in the Node.js package manager force. Yes, there
is another... package manager... called Yarn! It's sort of a competitor to npm, but
they work almost identically. For example, in PHP, we have a composer.json
file.
In Node, we will have a package.json
... and you can use either npm
or yarn
to read it.
In other words,... you can use npm
or yarn
: they basically do the same thing.
You could even have some people on your team using npm
, and others using yarn
.
We're going to use Yarn... because it's a bit more sophisticated. But, that means you need to install it! I'm on a Mac, so I already installed it via Brew. Check Yarn's Docs for your install details.
To use Yarn, we need a package.json
file... which we don't have yet! No worries,
to create one, run:
yarn init
It'll ask a bunch of questions - none are too important - and you can always
update your new package.json
file by hand later:
{ | |
"name": "javascript", | |
"version": "1.0.0", | |
"main": "index.js", | |
"repository": "git@github.com:knpuniversity/javascript.git", | |
"author": "Ryan Weaver <ryan@thatsquality.com>", | |
"license": "MIT" | |
} |
Awesome!
Ok, the wonderful tool that will fix all of our browser compatibility problems
with ES2015 is called Babel. Google for it to find babeljs.io
.
In a nut shell, Babel reads new JavaScript code, i.e. ES2015 code, and recompiles it to old JavaScript so that all browsers can understand it. Yea, it literally reads source code and converts it to different source code. It's wild!
Go to Setup. In our case, to see how it works, we're going to use the CLI,
which means we will run Babel from the command line. To install Babel CLI, it wants us
to run npm install --save-dev babel-cli
.
But don't do it! Since we're using Yarn, run:
yarn add babel-cli --dev
That does the same thing, but with more exciting output!
This made a few changes to our project. Most importantly, it added this devDependencies
section to package.json
with babel-cli
inside:
{ | |
... lines 2 - 7 | |
"devDependencies": { | |
"babel-cli": "^6.22.2" | |
} | |
} |
It also created a yarn.lock
file: which works like composer.lock
. And finally,
the command added a new node_modules/
directory, where it downloaded babel-cli
and all of its friends, um, dependencies. That is the vendor/
directory for Node.
Open up your .gitignore
file. At the bottom, let's ignore /node_modules/
:
... lines 1 - 16 | |
/node_modules |
We don't need to commit that directory because - thanks to the package.json
and
yarn.lock
files - anyone else can run yarn install
to download everything they need.
Okay, enough setup! Let's use Babel!
// composer.json
{
"require": {
"php": "^7.2.0",
"symfony/symfony": "3.2.*", // v3.2.14
"twig/twig": "2.10.*", // v2.10.0
"doctrine/orm": "^2.5", // v2.7.1
"doctrine/doctrine-bundle": "^1.6", // 1.10.3
"doctrine/doctrine-cache-bundle": "^1.2", // 1.3.2
"symfony/swiftmailer-bundle": "^2.3", // v2.4.2
"symfony/monolog-bundle": "^2.8", // v2.12.1
"symfony/polyfill-apcu": "^1.0", // v1.3.0
"sensio/distribution-bundle": "^5.0", // v5.0.22
"sensio/framework-extra-bundle": "^3.0.2", // v3.0.19
"incenteev/composer-parameter-handler": "^2.0", // v2.1.2
"friendsofsymfony/user-bundle": "~2.0@dev", // dev-master
"doctrine/doctrine-fixtures-bundle": "~2.3", // v2.4.1
"doctrine/doctrine-migrations-bundle": "^1.2", // v1.2.1
"composer/package-versions-deprecated": "^1.11", // 1.11.99
"friendsofsymfony/jsrouting-bundle": "^1.6" // 1.6.0
},
"require-dev": {
"sensio/generator-bundle": "^3.0", // v3.1.2
"symfony/phpunit-bridge": "^3.0" // v3.2.2
}
}
Some issues encoutered with: ./node_modules/.bin/webpack --watch
This is because the last version of css-loader is not compatible with this tutorial, to follow this tutorial, install this version of css-loader:
"^0.28.4"