We're now going to use ES6 syntax, which is a great improvement over the "old" ES5 syntax. All browsers and JS environments understand ES5 well, but not ES6. So we're going to use a tool called Babel to transform ES6 files into ES5 files. To run Babel, we are going to use Gulp, a task runner. It is similar to the tasks located under scripts
in package.json
, but writing your task in a JS file is simpler and clearer than a JSON file, so we'll install Gulp, and the Babel plugin for Gulp too:
- Run
yarn add --dev gulp
- Run
yarn add --dev gulp-babel
- Run
yarn add --dev babel-preset-latest
- In
package.json
, add ababel
field for the babel configuration. Make it use the latest Babel preset like this:
"babel": {
"presets": [
"latest"
]
},
Note: A .babelrc
file at the root of your project could also be used instead of the babel
field of package.json
. Your root folder will get more and more bloated over time, so keep the Babel config in package.json
until it grows too large.
- Move your
index.js
into a newsrc
folder. This is where you will write your ES6 code. Alib
folder is where the compiled ES5 code will go. Gulp and Babel will take care of creating it. Remove the previouscolor
-related code inindex.js
, and replace it with a simple:
const str = 'ES6';
console.log(`Hello ${str}`);
We're using a template string here, which is an ES6 feature that lets us inject variables directly inside the string without concatenation using ${}
. Note that template strings are created using backquotes.
- Create a
gulpfile.js
containing:
const gulp = require('gulp');
const babel = require('gulp-babel');
const del = require('del');
const exec = require('child_process').exec;
const paths = {
allSrcJs: 'src/**/*.js',
libDir: 'lib',
};
gulp.task('clean', () => {
return del(paths.libDir);
});
gulp.task('build', ['clean'], () => {
return gulp.src(paths.allSrcJs)
.pipe(babel())
.pipe(gulp.dest(paths.libDir));
});
gulp.task('main', ['build'], (callback) => {
exec(`node ${paths.libDir}`, (error, stdout) => {
console.log(stdout);
return callback(error);
});
});
gulp.task('watch', () => {
gulp.watch(paths.allSrcJs, ['main']);
});
gulp.task('default', ['watch', 'main']);
Let's take a moment to understand all this.
The API of Gulp itself is pretty straightforward. It defines gulp.task
s, that can reference gulp.src
files, applies a chain of treatments to them with .pipe()
(like babel()
in our case) and outputs the new files to gulp.dest
. It can also gulp.watch
for changes on your filesystem. Gulp tasks can run prerequisite tasks before them, by passing an array (like ['build']
) as a second parameter to gulp.task
. Refer to the documentation for a more thorough presentation.
First we define a paths
object to store all our different file paths and keep things DRY.
Then we define 5 tasks: build
, clean
, main
, watch
, and default
.
build
is where Babel is called to transform all of our source files located undersrc
and write the transformed ones tolib
.clean
is a task that simply deletes our entire auto-generatedlib
folder before everybuild
. This is typically useful to get rid of old compiled files after renaming or deleting some insrc
, or to make sure thelib
folder is in sync with thesrc
folder if your build fails and you don't notice. We use thedel
package to delete files in a way that integrates well with Gulp's stream (this is the recommended way to delete files with Gulp). Runyarn add --dev del
to install that package.main
is the equivalent of runningnode .
in the previous chapter, except this time, we want to run it onlib/index.js
. Sinceindex.js
is the default file Node looks for, we can simply writenode lib
(we use thelibDir
variable to keep things DRY). Therequire('child_process').exec
andexec
part in the task is a native Node function that executes a shell command. We forwardstdout
toconsole.log()
and return a potential error usinggulp.task
's callback function. Don't worry if this part is not super clear to you, remember that this task is basically just runningnode lib
.watch
runs themain
task when filesystem changes happen in the specified files.default
is a special task that will be run if you simply callgulp
from the CLI. In our case we want it to run bothwatch
andmain
(for the first execution).
Note: You might be wondering how come we're using some ES6 code in this Gulp file, since it doesn't get transpiled into ES5 by Babel. This is because we're using a version of Node that supports ES6 features out of the box (make sure you are running Node > 6.5.0 by running node -v
).
Alright! Let's see if this works.
-
In
package.json
, change yourstart
script to:"start": "gulp"
. -
Run
yarn start
. It should print "Hello ES6" and start watching for changes. Try writing bad code insrc/index.js
to see Gulp automatically showing you the error when you save. -
Add
/lib/
to your.gitignore
Next section: 4 - Using the ES6 syntax with a class
Back to the previous section or the table of contents.