Instructions for setting up Babel to transpile ES6 to a supported format. Part of the activity in the final class of the Girl Develop It Intro to ES6 course.
- A text editor. We'll be using Sublime Text for this tutorial -- it may be slightly easier to follow along if you have Sublime, too!
- Node.js version 6 or above. Be sure Node.js version or higher is installed by opening your command line tool (for example, Terminal, X11, Git Bash, or cmd) and typing
node -v
. If version 6 or above appears, you're good to go! - Google Chrome
- In your command line, create a folder called
gdi-es6
by typingmkdir gdi-es6
- Change directories to this folder by typing
cd gdi-es6
- Run
npm init
and press Enter until complete - Open your text editor (we'll be using Sublime Text) and mount the
gdi-es6
folder you just created, then create two folders withingdi-es6
,src
andbuild
. All our ES6 will be written in thesrc
folder, where it will then be transpiled to ourbuild
folder.
gdi-es6
│
└───build
│
└───src
- Install the babel command line interface:
npm install --save-dev babel-cli
- Install the babel ES6 transpiler:
npm install --save-dev babel-preset-es2015
- To the
gdi-es6
folder, add a.babelrc
file containing the following:
{
"presets": ["es2015", "stage-2"]
}
- We're now ready to transpile! Let's add some test files.
- Within
src
, create a file calledtest.js
containing the following:
const test = msg => {
console.log(msg)
};
export default test;
- Within
src
, create a file calledindex.js
containing the following:
import test from './test';
test('Babel works!');
-
Try running
node src/index.js
. You should get an error becausesrc/index.js
isn't transpiled. -
Go back to your command line and type
babel src/index.js --watch --out-file build/bundle.js
. (Didn't work? Try the below steps or check the Troubleshooting section)- Windows: If
babel src/index.js --watch --out-file build/bundle.js
doesn't work, run.\node_modules\.bin\babel src/index.js --watch --out-file build/bundle.js
- Mac: If
babel src/index.js --watch --out-file build/bundle.js
doesn't work, run./node_modules/.bin/babel src/index.js --watch --out-file build/bundle.js
instead
- Windows: If
-
Check your
build
folder to make sure everything is transpiling properly. When you make a change to any file insrc
, it should automatically transpile for you! -
In the command line, try running
node build/bundle.js
. Note how you won't get an error with the transpiled file! -
Now that we have a bundled file, we'd like to run that every time we type in
npm start
(the default command for almost every Node project). To do so, open yourpackage.json
file. Within the"scripts": {}
object, add a property"start": "node build/bundle.js"
. When we typenpm start
in the command line, node will run our build file. -
Go to the command line and run
npm start
. It should outputBabel works!
(or whatever text you put in yoursrc/index.js
file).
Congrats! Now we can move on to setting up WebPack.
We've now got Babel up and running, but typically we'd like to use a build tool such as WebPack, Gulp, or Grunt, since we can use a build tool for a number of other things: transpilation, minification, optimization, processing CSS, running a server, etc. Let's use WebPack.
- In the command line, navigate to your
gdi-es6
directory if you aren't there already (cd gdi-es6
). - Install WebPack by running
npm install --save-dev webpack
- Let's make sure WebPack is working properly. In the command line, type
webpack ./src/index.js ./build/bundle.js
to transpile yoursrc JS files to
build/bundle.js`. (Didn't work? Try the below steps or check the Troubleshooting section) - Instead of typing in our entry file (
./src/index.js
) and output file (./build/bundle.js
) each time, let's have WebPack automate that. Create a file calledwebpack.config.js
in yourgdi-es6
directory and paste the following:
module.exports = {
entry: './src/index.js',
output: {
path: __dirname + '/build',
filename: 'bundle.js'
}
};
- Now, make a minor change to the text in
src/index.js
, save, and runwebpack
() from the command line. It should build to your bundle.js file. But our result isn't transpiled properly; it's only being bundled into one file. - Let's set up transpilation, by installing some WebPack Babel loaders and their dependencies. Run
npm install --save-dev babel-core babel-loader
in the command line. - Create an
index.html
file within yourgdi-es6
folder containing the following:
<!doctype html>
<html>
<head>
<title>GDI ES6 Activity</title>
</head>
<body>
<script type="text/javascript" src="build/bundle.js"></script>
</body>
</html>
- Open your index.html file in Google Chrome. Check the console to see if your message appears.
- In your command line, run
webpack --watch
. The--watch
flag will automatically transpile your files. Try changing the message insrc/index.js
, saving, and then refreshing the openindex.html
page in your browser to see the change. - One problem with
webpack --watch
is that you have to refresh to see changes. Let's have our changes automatically reload instead using WebPack dev server. Install it by runningnpm install --save-dev webpack-dev-server
. - Run it with
webpack-dev-server
. (Having trouble? Try.\node_modules\.bin\webpack-dev-server
on Windows or./node_modules/.bin/webpack-dev-server
on a Mac) - Go to the localhost URL it gives you and add a
/webpack-dev-server
at the end (e.g.http://localhost:8080/webpack-dev-server
). Click on theindex
file, which is your index.html file.- Note: Adding
/webpack-dev-server
is important! If you go to the root localhost URL, that won't automatically refresh.
- Note: Adding
- Try making some changes to your
src/index.js
file's message. It should automatically update in the browser!
ESLint helps you and your team abide by a common coding standard, from tabs vs. spaces to using semicolons. You can easily change the rules as your codebase evolves. Although it's installed as a command line tool, you can add it as a tooltip to most IDEs and text editors. Let's set it up!
- First we need to install ESLint globally (
-g
) on our machine. In your command line, runnpm install -g eslint
. - From the command line, within your
gdi-es6
folder, initiate ESLint witheslint --init
. It will ask you a series of questions about your code style. Let's say yes to ECMAScript 6 features, ES6 modules, browser use, and CommonJS, and pick JSON as our preferred file type. The rest is up to you. - A
eslintrc.json
file will be created. Open it to see the style rules. - You can now run
eslint *
to see a list of suggestions for all of the code in yourgdi-es6
project.
Helpful, right? But wouldn't it be better to see it in our text editor or IDE? Check out some installation instructions for common editors below.
- Be sure Package Control is installed first, under
Tools
>Install Package Control...
. (If you don't see that option, you're good to go to step 2.) - Go to
Tools
>Command Palette
and typeinstall
to selectPackage Control: Install Package
from the dropdown - Search for
SublimeLinter
and hit Enter to install - Search for
SubLinter-contrib-eslint
and hit Enter to install - Restart Sublime Text and open your
src/index.js
file. Delete one of the semicolons and save. You should now see a red error message around the missing semicolon!
- If you get the
You have mistakenly installed the babel package, which is a no-op in Babel 6
error message...- run
npm uninstall -g babel
and try to run the command again
- run
- If you get a
moduleNameHere is not recognized as an internal or external command
error, first make sure you ran the correctnpm install
command for that module (see instructions). If that doesn't work, you can point to the correct executable by...- Windows: Replace the command with
.\node_modules\.bin\moduleNameHere
. For example, if I wanted to run ababel
command, I would type.\node_modules\.bin\babel
- Mac: Replace the command with
./node_modules/.bin/moduleNameHere
. For example, if I wanted to run ababel
command, I would type./node_modules/.bin/babel
- Windows: Replace the command with