From f75765a1fdf7ee3e91ee71053ef60c3de1126d46 Mon Sep 17 00:00:00 2001
From: Simon Egersand <s.egersand@gmail.com>
Date: Mon, 06 Feb 2017 22:16:34 +0100
Subject: [PATCH] Generate Sourcemap and clean up Gulpfile

---
 gulpfile.js |   96 +++++++++++++++++++++++++++---------------------
 1 files changed, 54 insertions(+), 42 deletions(-)

diff --git a/gulpfile.js b/gulpfile.js
index fa33182..7d195b2 100644
--- a/gulpfile.js
+++ b/gulpfile.js
@@ -1,59 +1,71 @@
-var gulp = require('gulp'),
+const babel = require('gulp-babel'),
+	gulp = require('gulp'),
 	insert = require('gulp-insert'),
+	plumber = require('gulp-plumber'),
+	rename = require('gulp-rename'),
+	sourcemaps = require('gulp-sourcemaps'),
+	through = require('through2'),
 	uglify = require('gulp-uglify'),
-	webpack = require('gulp-webpack')
-;
+	webpack = require('webpack-stream')
+	;
 
-var packageName = 'react-datetime';
-var pack = require( './package.json' );
+const pack = require( './package.json' );
 
-var getWPConfig = function( filename ) {
+gulp.task( 'sub', () => {
+	// Reason behind having sub as separate task:
+	// https://github.com/shama/webpack-stream/issues/114
+	return gulp.src( './Datetime.js' )
+		.pipe( webpack( getWebpackConfig() ) )
+		.pipe( gulp.dest( 'tmp/' ) );
+});
+
+gulp.task( 'build', ['sub'], () => {
+	return gulp.src( ['tmp/react-datetime.js'] )
+		.pipe( sourcemaps.init( { loadMaps: true } ) )
+			.pipe( through.obj( function( file, enc, cb ) {
+				// Dont pipe through any source map files as
+				// it will be handled by gulp-sourcemaps
+				const isSourceMap = /\.map$/.test( file.path );
+				if ( !isSourceMap ) this.push( file );
+				cb();
+			}))
+			.pipe( plumber() )
+			// .pipe( babel( { presets: [ 'es2015'] } ) )
+			.pipe( insert.prepend( setHeader ) )
+			.pipe( gulp.dest( 'dist/' ) ) // Save .js
+			.pipe( uglify() )
+			.pipe( insert.prepend( setHeader ) )
+			.pipe( rename( { extname: '.min.js' } ) )
+		.pipe( sourcemaps.write( '.' ) )
+		.pipe( gulp.dest( 'dist/' ) ); // Save .min.js
+	// TODO: Remove tmp folder
+});
+
+gulp.task( 'default', ['build'] );
+
+/*
+ * Utility functions
+ */
+
+const getWebpackConfig = () => {
 	return {
+		devtool: '#cheap-module-source-map',
 		externals: {
 			react: 'React',
 			'react-dom': 'ReactDOM',
 			moment: 'moment'
 		},
 		output: {
-			libraryTarget: 'umd',
 			library: 'Datetime',
-			filename: filename + '.js'
+			libraryTarget: 'umd',
+			filename: 'react-datetime.js'
 		}
 	};
 };
 
-var cr = ( '/*\n%%name%% v%%version%%\n%%homepage%%\n%%license%%: https://github.com/arqex/' + packageName + '/raw/master/LICENSE\n*/\n' )
-	.replace( '%%name%%', pack.name)
-	.replace( '%%version%%', pack.version)
-	.replace( '%%license%%', pack.license)
-	.replace( '%%homepage%%', pack.homepage)
-;
-
-var handleError = function( err ) {
-	console.error( 'Error:', err );
-};
-
-function wp( config, minify ) {
-	var stream =  gulp.src( './Datetime.js' )
-		.pipe( webpack( config ) )
+const setHeader = ( '/*\n%%name%% v%%version%%\n%%homepage%%\n%%license%%: https://github.com/YouCanBookMe/react-datetime/raw/master/LICENSE\n*/\n' )
+		.replace( '%%name%%', pack.name)
+		.replace( '%%version%%', pack.version)
+		.replace( '%%license%%', pack.license)
+		.replace( '%%homepage%%', pack.homepage)
 	;
-
-	if( minify ) {
-		stream = stream.pipe( uglify() ).on( 'error', handleError );
-	}
-
-	return stream.pipe( insert.prepend( cr ) )
-		.pipe( gulp.dest( 'dist/' ) )
-	;
-}
-
-gulp.task( 'build', function( callback ) {
-	var config = getWPConfig( 'react-datetime' );
-	config.devtool = '#eval';
-	wp( config );
-
-	config = getWPConfig( 'react-datetime.min' );
-	return wp( config, true );
-});
-
-gulp.task( 'default', ['build'] );

--
Gitblit v1.9.3