Skip to content

Instantly share code, notes, and snippets.

@soggie
Created March 5, 2016 07:33
Show Gist options
  • Save soggie/edb56547c4bf81f5d4fc to your computer and use it in GitHub Desktop.
Save soggie/edb56547c4bf81f5d4fc to your computer and use it in GitHub Desktop.
import gulp from 'gulp';
import del from 'del';
import babel from 'gulp-babel';
import mocha from 'gulp-mocha';
import help from 'gulp-task-listing';
import eslint from 'gulp-eslint';
import sourcemaps from 'gulp-sourcemaps';
const config = {
paths: {
js: {
src: 'src/**/*.js', // javascript source files
dist: 'dist/' // destination for compiled source
},
test: {
src: 'test/**/*.js', // test case source files
dist: 'test-dist/', // destination for compiled source
run: 'test-dist/*.js' // destination to run tests
}
}
};
const options = {
presets: ['es2015'],
plugins: ['transform-runtime', 'syntax-async-functions', 'transform-async-to-generator'] // include ES7 async
};
gulp.task('help', help);
// lint the source files and then build them. Doest not include test cases
gulp.task('build', ['lint', 'build-src']);
// build source files
gulp.task('build-src', () =>
gulp.src(config.paths.js.src)
.pipe(sourcemaps.init()) // can sometimes be buggy
.pipe(babel(options))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(config.paths.js.dist))
);
// build test cases
gulp.task('build-test', () =>
gulp.src(config.paths.test.src)
.pipe(sourcemaps.init())
.pipe(babel(options))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(config.paths.test.dist))
);
// watches over all source files (source and test)
gulp.task('watch', () => {
gulp.watch(config.paths.js.src, ['build-src', 'test']);
// do not require since we are using ava and it allows for pre-required files
// we are using babal-register before it
// gulp.watch(config.paths.test.src, ['build-test', 'test']);
});
// Run tests with mocha
gulp.task('test', () =>
gulp.src([config.paths.test.run])
.pipe(mocha({ reporter: 'spec' }))
.on('error', (err) => console.log(err.stack))
);
// Runs linter
gulp.task('lint', () =>
gulp.src([
'gulpfile.js',
config.paths.test.src,
config.paths.js.src
])
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError())
);
// Clean up destination directories for compiled source
gulp.task('clean', () => del([config.paths.js.dist + '**', config.paths.test.dist + '**']));
// Default Task
gulp.task('default', ['lint', 'build', 'test']);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment