2016-01-06 04:06:47 +01:00
var gulp = require ( 'gulp' ) ,
2016-01-11 02:25:30 +01:00
babel = require ( 'gulp-babel' ) ,
2016-01-06 04:06:47 +01:00
diff = require ( 'gulp-diff' ) ,
2016-01-11 02:25:30 +01:00
notify = require ( 'gulp-notify' ) ,
uglify = require ( 'gulp-uglify' ) ;
gulpUtil = require ( 'gulp-util' ) ,
browserify = require ( 'browserify' ) ,
babelify = require ( 'babelify' ) ,
watchify = require ( 'watchify' ) ,
source = require ( 'vinyl-source-stream' ) ,
buffer = require ( 'vinyl-buffer' ) ,
2016-01-06 04:06:47 +01:00
path = require ( 'path' ) ,
2016-01-11 02:25:30 +01:00
glob = require ( 'glob' ) ,
eventStream = require ( 'event-stream' ) ,
2016-01-06 04:06:47 +01:00
semver = require ( 'semver' ) ,
packageJson = require ( './package.json' ) ;
2016-01-11 02:25:30 +01:00
var PATHS = {
MODULES : './node_modules' ,
ADMIN _THIRDPARTY : './admin/thirdparty' ,
ADMIN _JAVASCRIPT _SRC : './admin/javascript/src' ,
ADMIN _JAVASCRIPT _DIST : './admin/javascript/dist' ,
FRAMEWORK _THIRDPARTY : './thirdparty' ,
FRAMEWORK _JAVASCRIPT _SRC : './javascript/src' ,
FRAMEWORK _JAVASCRIPT _DIST : './javascript/dist'
} ;
var browserifyOptions = {
cache : { } ,
packageCache : { } ,
poll : true ,
plugin : [ watchify ]
2016-01-06 04:06:47 +01:00
} ;
var blueimpFileUploadConfig = {
2016-01-11 02:25:30 +01:00
src : PATHS . MODULES + '/blueimp-file-upload' ,
dest : PATHS . FRAMEWORK _THIRDPARTY + '/jquery-fileupload' ,
2016-01-06 04:06:47 +01:00
files : [
2016-01-11 02:25:30 +01:00
'/cors/jquery.postmessage-transport.js' ,
'/cors/jquery.xdr-transport.js' ,
'/jquery.fileupload-ui.js' ,
'/jquery.fileupload.js' ,
'/jquery.iframe-transport.js'
2016-01-06 04:06:47 +01:00
]
} ;
var blueimpLoadImageConfig = {
2016-01-11 02:25:30 +01:00
src : PATHS . MODULES + '/blueimp-load-image' ,
dest : PATHS . FRAMEWORK _THIRDPARTY + '/javascript-loadimage' ,
files : [ '/load-image.js' ]
2016-01-06 04:06:47 +01:00
} ;
var blueimpTmplConfig = {
2016-01-11 02:25:30 +01:00
src : PATHS . MODULES + '/blueimp-tmpl' ,
dest : PATHS . FRAMEWORK _THIRDPARTY + '/javascript-templates' ,
files : [ '/tmpl.js' ]
2016-01-06 04:06:47 +01:00
} ;
var jquerySizesConfig = {
2016-01-11 02:25:30 +01:00
src : PATHS . MODULES + '/jquery-sizes' ,
dest : PATHS . ADMIN _THIRDPARTY + '/jsizes' ,
files : [ '/lib/jquery.sizes.js' ]
2016-01-06 04:06:47 +01:00
} ;
/ * *
* Copies files from a source directory to a destination directory .
*
* @ param object libConfig
* @ param string libConfig . src - The source directory
* @ param string libConfig . dest - The destination directory
* @ param array libConfig . files - The list of files to copy from the source to the destination directory
* /
function copyFiles ( libConfig ) {
libConfig . files . forEach ( function ( file ) {
var dir = path . parse ( file ) . dir ;
gulp . src ( libConfig . src + file )
. pipe ( gulp . dest ( libConfig . dest + dir ) ) ;
} ) ;
}
/ * *
* Diffs files in a source directory against a destination directory .
*
* @ param object libConfig
* @ param string libConfig . src - The source directory
* @ param string libConfig . dest - The destination directory
* @ param array libConfig . files - The list of files to copy from the source to the destination directory
* /
function diffFiles ( libConfig ) {
libConfig . files . forEach ( function ( file ) {
var dir = path . parse ( file ) . dir ;
gulp . src ( libConfig . src + file )
. pipe ( diff ( libConfig . dest + dir ) )
. pipe ( diff . reporter ( { fail : true , quiet : true } ) )
. on ( 'error' , function ( error ) {
console . error ( new Error ( 'Sanity check failed. \'' + libConfig . dest + file + '\' has been modified.' ) ) ;
process . exit ( 1 ) ;
} ) ;
} ) ;
}
2016-01-11 02:25:30 +01:00
/ * *
* Transforms the passed JavaScript files to UMD modules .
*
* @ param array files - The files to transform .
* @ param string dest - The output directory .
* @ return object
* /
function transformToUmd ( files , dest ) {
return eventStream . merge ( files . map ( function ( file ) {
return gulp . src ( file )
. pipe ( babel ( {
presets : [ 'es2015' ] ,
moduleId : 'ss.' + path . parse ( file ) . name ,
plugins : [ 'transform-es2015-modules-umd' ]
} ) )
. on ( 'error' , notify . onError ( {
message : 'Error: <%= error.message %>' ,
} ) )
. pipe ( gulp . dest ( dest ) ) ;
} ) ) ;
}
2016-01-06 04:06:47 +01:00
// Make sure the version of Node being used is valid.
if ( ! semver . satisfies ( process . versions . node , packageJson . engines . node ) ) {
console . error ( 'Invalid Node.js version. You need to be using ' + packageJson . engines . node + '. If you want to manage multiple Node.js versions try https://github.com/creationix/nvm' ) ;
process . exit ( 1 ) ;
}
2016-01-11 02:25:30 +01:00
if ( process . env . npm _config _development ) {
browserifyOptions . debug = true ;
}
gulp . task ( 'build' , [ 'umd' , 'umd-watch' , 'bundle' ] ) ;
2016-02-09 00:26:39 +01:00
gulp . task ( 'bundle' , [ 'bundle-lib' , 'bundle-leftandmain' , 'bundle-react' ] ) ;
2016-01-11 02:25:30 +01:00
gulp . task ( 'bundle-leftandmain' , function bundleLeftAndMain ( ) {
var stream = browserify ( Object . assign ( { } , browserifyOptions , {
entries : PATHS . ADMIN _JAVASCRIPT _SRC + '/bundles/leftandmain.js'
} ) )
. transform ( babelify . configure ( {
presets : [ 'es2015' ] ,
ignore : /(thirdparty)/
} ) )
. on ( 'log' , function ( msg ) { gulpUtil . log ( 'Finished bundle-leftandmain.js ' + msg ) ; } )
. on ( 'update' , bundleLeftAndMain )
. external ( 'jQuery' )
. external ( 'i18n' )
. bundle ( )
. on ( 'error' , notify . onError ( {
message : 'Error: <%= error.message %>' ,
} ) )
. pipe ( source ( 'bundle-leftandmain.js' ) )
. pipe ( buffer ( ) ) ;
if ( typeof process . env . npm _config _development === 'undefined' ) {
stream . pipe ( uglify ( ) ) ;
}
return stream . pipe ( gulp . dest ( PATHS . ADMIN _JAVASCRIPT _DIST ) ) ;
} ) ;
gulp . task ( 'bundle-lib' , function bundleLib ( ) {
var stream = browserify ( Object . assign ( { } , browserifyOptions , {
entries : PATHS . ADMIN _JAVASCRIPT _SRC + '/bundles/lib.js'
} ) )
. transform ( babelify . configure ( {
presets : [ 'es2015' ] ,
ignore : /(thirdparty)/
} ) )
. on ( 'log' , function ( msg ) { gulpUtil . log ( 'Finished bundle-lib.js ' + msg ) ; } )
. on ( 'update' , bundleLib )
. require ( PATHS . FRAMEWORK _JAVASCRIPT _SRC + '/jQuery.js' , { expose : 'jQuery' } )
. require ( PATHS . FRAMEWORK _JAVASCRIPT _SRC + '/i18n.js' , { expose : 'i18n' } )
. bundle ( )
. on ( 'error' , notify . onError ( {
message : 'Error: <%= error.message %>' ,
} ) )
. pipe ( source ( 'bundle-lib.js' ) )
. pipe ( buffer ( ) ) ;
if ( typeof process . env . npm _config _development === 'undefined' ) {
stream . pipe ( uglify ( ) ) ;
}
2016-02-09 00:26:39 +01:00
return stream . pipe ( gulp . dest ( PATHS . ADMIN _JAVASCRIPT _DIST ) ) ;
} ) ;
gulp . task ( 'bundle-react' , function bundleReact ( ) {
var stream = browserify ( Object . assign ( { } , browserifyOptions ) )
. on ( 'log' , function ( msg ) { gulpUtil . log ( 'Finished bundle-react.js ' + msg ) ; } )
. on ( 'update' , bundleReact )
. require ( 'react-addons-test-utils' , { expose : 'react-addons-test-utils' } )
. require ( 'react' , { expose : 'react' } )
. require ( 'react-dom' , { expose : 'react-dom' } )
. require ( 'redux' , { expose : 'redux' } )
. require ( 'react-redux' , { expose : 'react-redux' } )
. require ( 'redux-thunk' , { expose : 'redux-thunk' } )
. require ( 'isomorphic-fetch' , { expose : 'isomorphic-fetch' } )
. require ( PATHS . ADMIN _JAVASCRIPT _DIST + '/SilverStripeComponent' , { expose : 'silverstripe-component' } )
. bundle ( )
. on ( 'error' , notify . onError ( {
message : 'Error: <%= error.message %>' ,
} ) )
. pipe ( source ( 'bundle-react.js' ) )
. pipe ( buffer ( ) ) ;
if ( typeof process . env . npm _config _development === 'undefined' ) {
stream . pipe ( uglify ( ) ) ;
}
2016-01-11 02:25:30 +01:00
return stream . pipe ( gulp . dest ( PATHS . ADMIN _JAVASCRIPT _DIST ) ) ;
2016-01-06 04:06:47 +01:00
} ) ;
gulp . task ( 'sanity' , function ( ) {
diffFiles ( blueimpFileUploadConfig ) ;
diffFiles ( blueimpLoadImageConfig ) ;
diffFiles ( blueimpTmplConfig ) ;
diffFiles ( jquerySizesConfig ) ;
} ) ;
2016-01-11 02:25:30 +01:00
gulp . task ( 'thirdparty' , function ( ) {
copyFiles ( blueimpFileUploadConfig ) ;
copyFiles ( blueimpLoadImageConfig ) ;
copyFiles ( blueimpTmplConfig ) ;
copyFiles ( jquerySizesConfig ) ;
} ) ;
gulp . task ( 'umd' , [ 'umd-admin' , 'umd-framework' ] ) ;
gulp . task ( 'umd-admin' , function ( ) {
var files = glob . sync ( PATHS . ADMIN _JAVASCRIPT _SRC + '/*.js' , { ignore : PATHS . ADMIN _JAVASCRIPT _SRC + '/LeftAndMain.!(Ping).js' } ) ;
return transformToUmd ( files , PATHS . ADMIN _JAVASCRIPT _DIST ) ;
} ) ;
gulp . task ( 'umd-framework' , function ( ) {
return transformToUmd ( glob . sync ( PATHS . FRAMEWORK _JAVASCRIPT _SRC + '/*.js' ) , PATHS . FRAMEWORK _JAVASCRIPT _DIST ) ;
} ) ;
gulp . task ( 'umd-watch' , function ( ) {
gulp . watch ( PATHS . ADMIN _JAVASCRIPT _SRC + '/*.js' , [ 'umd-admin' ] ) ;
gulp . watch ( PATHS . FRAMEWORK _JAVASCRIPT _SRC + '/*.js' , [ 'umd-framework' ] ) ;
} ) ;