Last active
August 29, 2015 14:10
-
-
Save phated/1cb4046f8e85a68e92d4 to your computer and use it in GitHub Desktop.
gulp4 gulpfile.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var path = require('path'); | |
| var gulp = require('gulp-next'); | |
| var gulpif = require('gulp-if'); | |
| var gutil = require('gulp-util'); | |
| var sourcemaps = require('gulp-sourcemaps'); | |
| var es = require('event-stream'); | |
| var webpack = require('webpack'); | |
| var sweetjs = require('gulp-sweetjs'); | |
| var regenerator = require('gulp-regenerator'); | |
| var rename = require('gulp-rename'); | |
| var cache = require('gulp-cached'); | |
| var header = require('gulp-header'); | |
| var nodemon = require('nodemon'); | |
| var webpackConfig = require('./webpack.config'); | |
| var webpackInst = webpack(webpackConfig); | |
| gulp.task("webpack", function(cb) { | |
| webpackInst.run(function(err, stats) { | |
| if(err) throw new gutil.PluginError("webpack", err); | |
| //gutil.log("[webpack]", stats.toString()); | |
| cb(); | |
| }); | |
| }); | |
| function jsheader(text) { | |
| return es.through(function(file) { | |
| var contents = file.contents.toString('utf8'); | |
| var strictRe = /^(['"]use strict['"];)/; | |
| if(contents.match(strictRe)) { | |
| contents = contents.replace( | |
| strictRe, | |
| '$1\n' + text | |
| ); | |
| } | |
| else { | |
| contents = text + '\n' + contents; | |
| } | |
| file.contents = new Buffer(contents); | |
| this.emit('data', file); | |
| }); | |
| } | |
| gulp.task("regenerate", function(done) { | |
| return gulp.src('node_modules/js-csp/src/**/*.js') | |
| .pipe(gulpif(/csp\/csp.js/, rename('index.js'))) | |
| .pipe(gulp.dest('static/js/shared/csp')); | |
| }); | |
| function makeNodeStream(src, withoutSourcemaps) { | |
| var stream = src.pipe(cache('src')) | |
| .pipe(sourcemaps.init()) | |
| .pipe(sweetjs({ | |
| readableNames: true, | |
| modules: ['es6-macros'] | |
| })) | |
| .pipe(regenerator()) | |
| .pipe(jsheader('var wrapGenerator = require("regenerator/runtime/dev").wrapGenerator;')) | |
| .pipe(jsheader('require("source-map-support");')); | |
| if(!withoutSourcemaps) { | |
| stream = stream.pipe(sourcemaps.write('.')); | |
| } | |
| return stream; | |
| } | |
| gulp.task("src", function(cb) { | |
| es.merge( | |
| makeNodeStream(gulp.src('src/**/*.js')) | |
| .pipe(gulp.dest('build')), | |
| makeNodeStream(gulp.src('static/js/shared/**/*.js')) | |
| .pipe(gulp.dest('build/shared')), | |
| gulp.src(['src/**/*', '!src/**/*.js']).pipe(gulp.dest('build')) | |
| ).on('end', function() { | |
| nodemon.restart(); | |
| cb(); | |
| }); | |
| }); | |
| gulp.task("bin", function() { | |
| makeNodeStream(gulp.src('bin/**/*.js'), true) | |
| .pipe(header('#!/usr/bin/env node\n')) | |
| .pipe(rename(function(path) { | |
| if(path.extname == '.js') { | |
| path.extname = ''; | |
| } | |
| })) | |
| .pipe(gulp.dest('bin')); | |
| }); | |
| gulp.task("watch", function(done) { | |
| gulp.watch(["static/js/**/*.js", | |
| "static/css*/*.less", | |
| "!static/js/bundle.js"], ["webpack"]); | |
| gulp.watch(["src/**/*.js", | |
| "static/js/shared/**/*.js"], ["src"]); | |
| done(); | |
| }); | |
| gulp.task("run", gulp.series("watch", function() { | |
| nodemon({ | |
| execMap: { | |
| js: 'node --harmony' | |
| }, | |
| script: 'build/main.js', | |
| ext: 'noop' | |
| }).on('restart', function() { | |
| console.log('restarted!'); | |
| }); | |
| })); | |
| gulp.task('default', gulp.series("webpack", "regenerate", "src")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment