Skip to content

Instantly share code, notes, and snippets.

@ZZR-china
Created December 27, 2016 12:44
Show Gist options
  • Save ZZR-china/1d556c0c63dc8c4c9a1a48a99ae96682 to your computer and use it in GitHub Desktop.
Save ZZR-china/1d556c0c63dc8c4c9a1a48a99ae96682 to your computer and use it in GitHub Desktop.
gulp task js
import gulp from 'gulp';
import gulpLoadPlugins from 'gulp-load-plugins';
import path from 'path';
import del from 'del';
import runSequence from 'run-sequence';
const plugins = gulpLoadPlugins();
const paths = {
js: ['./**/*.js', '!dist/**', '!node_modules/**'],
nonJs: ['./package.json', './.gitignore'],
tests: './server/tests/*.js'
};
// Clean up dist and coverage directory
gulp.task('clean', () =>
del(['dist/**', '!dist'])
);
// Copy non-js files to dist
gulp.task('copy', () =>
gulp.src(paths.nonJs)
.pipe(plugins.newer('dist'))
.pipe(gulp.dest('dist'))
);
// Compile ES6 to ES5 and copy to dist
gulp.task('babel', () =>
gulp.src([...paths.js, '!gulpfile.babel.js'], { base: '.' })
.pipe(plugins.newer('dist'))
.pipe(plugins.sourcemaps.init())
.pipe(plugins.babel({
plugins: ['transform-runtime']
}))
.pipe(plugins.sourcemaps.write('.', {
includeContent: false,
sourceRoot(file) {
return path.relative(file.path, __dirname);
}
}))
.pipe(gulp.dest('dist'))
);
// Start server with restart on file changes
gulp.task('nodemon', ['copy', 'babel'], () =>
plugins.nodemon({
script: path.join('dist', 'server.js'),
env: {'NODE_ENV':'development'},
ext: 'js',
ignore: ['node_modules/**/*.js', 'dist/**/*.js'],
tasks: ['copy', 'babel']
})
);
// gulp serve for development
gulp.task('serve', ['clean'], () => runSequence('nodemon'));
//gulp build for product
gulp.task('build', ['clean'], () => {
runSequence(
['copy', 'babel']
);
});
// default task: clean dist, compile js files and copy non-js files.
gulp.task('default', ['serve']);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment