Simon Egersand
2018-02-11 db7315ac35b5257824ba2cdbf46b150450f961ed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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('webpack-stream')
    ;
 
const pack = require( './package.json' );
 
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: {
            library: 'Datetime',
            libraryTarget: 'umd',
            filename: 'react-datetime.js'
        }
    };
};
 
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)
    ;