A while ago, I decided to give Sublime Text a try when learning ECMAScript 6. I mostly use Visual Studio, and with Sublime It feels like I'm on a diet! As a side effect I am also learning a little bit of Node, because many packages are based on tools installed with the Node package manager (npm).
ECMAScript 6 (ES6)
In order to get full ES6 syntax, I have installed the babel-sublime package. Enable the package by open or create a file with a .js extension, select View -> Syntax -> Open all with current extension as ... -> Babel -> JavaScript (Babel).
JSHint
I think JSHint does the job well also with checking for errors in ES6 code. I have only found one issue - grouping exports in a module - but I can live with writing separate multiple exports until it is fixed. I think ESLint also looks very promising, but for now I will keep using JSHint.
SublimeLinter-jshint
Linting ES6?
Selecting Babel as the default syntax for .js files will actually disable the JavaScript linting. The SublimeLinter settings file need syntax mappings for Babel (the selected row in the image below). Add the row and the linting will be enabled again.
Share the Rules
Instead of adding comments to every single .js file (like the esnext option), I use settings files (named .jshintrc). Are you on a Windows machine and can't create file names starting with a dot? Save the file with the name jshintrc, and rename it from the command prompt using the "ren" command:
ren jshintrc .jshintrc
Transpile to old school JavaScript with Babel
Using Babel from the command line is really simple and it runs fast. This command will create JavaScript RequireJS style modules (AMD):
babel my/es6/path --modules amd --out-dir destination/path
But running a command after each code change is an interruption, a manual step that should be automated. I use Gulp to run Babel commands and watcher to track changes in files.
Keep the flow with Gulp
With Gulp running in the background, each file change will trigger lint rules validation, ES6-to-ES5 transpilation and unit tests. An example:
var gulp = require('gulp');
var newer = require('gulp-newer');
var babel = require('gulp-babel');
var qunit = require('node-qunit-phantomjs');
var jshint = require('gulp-jshint');
gulp.task('lint', function () {
return gulp.src(source)
.pipe(jshint())
.pipe(jshint.reporter('default')) // linting passed
.pipe(jshint.reporter('fail')); // linting failed
});
gulp.task('transpile', function () {
return gulp.src(source)
.pipe(newer(destination)) // is the file changed?
.pipe(babel({modules: 'amd'})) // run babel and pass options
.pipe(gulp.dest(destination));
});
gulp.task('qunit', function () {
qunit('path/to/qunit/testrunner.html', { 'verbose': false });
});
Test Driven ES6 Development with QUnit
This is a separate blog post.
Update: Here it is the post: ES6 & TDD - can I test it?
No comments:
Post a Comment