Simon Egersand
2018-02-11 c6bf905a9c74a8c12fd4e3be1802385e22bf3108
commit | author | age
f75765 1 const babel = require('gulp-babel'),
SE 2     gulp = require('gulp'),
d76f7b 3     insert = require('gulp-insert'),
f75765 4     plumber = require('gulp-plumber'),
SE 5     rename = require('gulp-rename'),
6     sourcemaps = require('gulp-sourcemaps'),
7     through = require('through2'),
6f922e 8     uglify = require('gulp-uglify'),
f75765 9     webpack = require('webpack-stream')
SE 10     ;
d76f7b 11
f75765 12 const pack = require( './package.json' );
d76f7b 13
f75765 14 gulp.task( 'sub', () => {
SE 15     // Reason behind having sub as separate task:
16     // https://github.com/shama/webpack-stream/issues/114
a87306 17     return gulp.src( './DateTime.js' )
f75765 18         .pipe( webpack( getWebpackConfig() ) )
SE 19         .pipe( gulp.dest( 'tmp/' ) );
20 });
21
22 gulp.task( 'build', ['sub'], () => {
23     return gulp.src( ['tmp/react-datetime.js'] )
24         .pipe( sourcemaps.init( { loadMaps: true } ) )
25             .pipe( through.obj( function( file, enc, cb ) {
26                 // Dont pipe through any source map files as
27                 // it will be handled by gulp-sourcemaps
28                 const isSourceMap = /\.map$/.test( file.path );
29                 if ( !isSourceMap ) this.push( file );
30                 cb();
31             }))
32             .pipe( plumber() )
33             // .pipe( babel( { presets: [ 'es2015'] } ) )
34             .pipe( insert.prepend( setHeader ) )
35             .pipe( gulp.dest( 'dist/' ) ) // Save .js
36             .pipe( uglify() )
37             .pipe( insert.prepend( setHeader ) )
38             .pipe( rename( { extname: '.min.js' } ) )
39         .pipe( sourcemaps.write( '.' ) )
40         .pipe( gulp.dest( 'dist/' ) ); // Save .min.js
41     // TODO: Remove tmp folder
42 });
43
44 gulp.task( 'default', ['build'] );
45
46 /*
47  * Utility functions
48  */
49
50 const getWebpackConfig = () => {
d76f7b 51     return {
f75765 52         devtool: '#cheap-module-source-map',
d76f7b 53         externals: {
c3ac10 54             react: 'React',
PW 55             'react-dom': 'ReactDOM',
56             moment: 'moment'
d76f7b 57         },
M 58         output: {
59             library: 'Datetime',
f75765 60             libraryTarget: 'umd',
SE 61             filename: 'react-datetime.js'
d76f7b 62         }
M 63     };
64 };
65
f75765 66 const setHeader = ( '/*\n%%name%% v%%version%%\n%%homepage%%\n%%license%%: https://github.com/YouCanBookMe/react-datetime/raw/master/LICENSE\n*/\n' )
SE 67         .replace( '%%name%%', pack.name)
68         .replace( '%%version%%', pack.version)
69         .replace( '%%license%%', pack.license)
70         .replace( '%%homepage%%', pack.homepage)
d76f7b 71     ;