2016-01-26 05:28:36 +01:00
var gulp = require ( 'gulp' ) ,
babel = require ( 'gulp-babel' ) ,
diff = require ( 'gulp-diff' ) ,
notify = require ( 'gulp-notify' ) ,
uglify = require ( 'gulp-uglify' ) ;
gulpUtil = require ( 'gulp-util' ) ,
2016-02-11 20:33:04 +01:00
gulpif = require ( 'gulp-if' ) ,
2016-01-26 05:28:36 +01:00
browserify = require ( 'browserify' ) ,
babelify = require ( 'babelify' ) ,
watchify = require ( 'watchify' ) ,
source = require ( 'vinyl-source-stream' ) ,
buffer = require ( 'vinyl-buffer' ) ,
path = require ( 'path' ) ,
glob = require ( 'glob' ) ,
eventStream = require ( 'event-stream' ) ,
semver = require ( 'semver' ) ,
2016-02-11 20:33:04 +01:00
packageJson = require ( './package.json' ) ,
sourcemaps = require ( 'gulp-sourcemaps' ) ;
2016-01-26 05:28:36 +01:00
var PATHS = {
MODULES : './node_modules' ,
CMS _JAVASCRIPT _SRC : './javascript/src' ,
CMS _JAVASCRIPT _DIST : './javascript/dist'
} ;
var browserifyOptions = {
cache : { } ,
packageCache : { } ,
poll : true ,
plugin : [ watchify ]
} ;
2016-02-11 20:33:04 +01:00
var isDev = typeof process . env . npm _config _development !== 'undefined' ;
process . env . NODE _ENV = isDev ? 'development' : 'production' ;
2016-01-26 05:28:36 +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 ,
2016-02-11 20:33:04 +01:00
plugins : [ 'transform-es2015-modules-umd' ] ,
comments : false
2016-01-26 05:28:36 +01:00
} ) )
. on ( 'error' , notify . onError ( {
message : 'Error: <%= error.message %>' ,
} ) )
. pipe ( gulp . dest ( dest ) ) ;
} ) ) ;
}
// 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-02-11 20:33:04 +01:00
if ( isDev ) {
2016-01-26 05:28:36 +01:00
browserifyOptions . debug = true ;
}
2016-02-11 20:33:04 +01:00
var babelifyOptions = {
2016-04-04 21:57:49 +02:00
presets : [ 'es2015' , 'es2015-ie' , 'react' ] ,
plugins : [ 'transform-object-assign' ] ,
2016-02-11 20:33:04 +01:00
ignore : /(node_modules|thirdparty)/ ,
comments : false
} ;
2016-01-26 05:28:36 +01:00
2016-02-11 20:33:04 +01:00
gulp . task ( 'build' , [ 'umd-cms' , 'umd-watch' , 'bundle-legacy' ] ) ;
2016-01-26 05:28:36 +01:00
2016-02-11 20:33:04 +01:00
gulp . task ( 'bundle-legacy' , function bundleLeftAndMain ( ) {
var bundleFileName = 'bundle-legacy.js' ;
2016-01-26 05:28:36 +01:00
2016-02-11 20:33:04 +01:00
return browserify ( Object . assign ( { } , browserifyOptions , { entries : PATHS . CMS _JAVASCRIPT _SRC + '/bundles/legacy.js' } ) )
. on ( 'update' , bundleLeftAndMain )
. on ( 'log' , function ( msg ) { gulpUtil . log ( 'Finished' , 'bundled ' + bundleFileName + ' ' + msg ) } )
. transform ( 'babelify' , babelifyOptions )
. external ( 'jQuery' )
. external ( 'i18n' )
. external ( 'router' )
. bundle ( )
. on ( 'error' , notify . onError ( { message : bundleFileName + ': <%= error.message %>' } ) )
. pipe ( source ( bundleFileName ) )
. pipe ( buffer ( ) )
. pipe ( sourcemaps . init ( { loadMaps : true } ) )
. pipe ( gulpif ( ! isDev , uglify ( ) ) )
. pipe ( sourcemaps . write ( './' ) )
. pipe ( gulp . dest ( PATHS . CMS _JAVASCRIPT _DIST ) ) ;
2016-01-26 05:28:36 +01:00
} ) ;
gulp . task ( 'umd-cms' , function ( ) {
return transformToUmd ( glob . sync ( PATHS . CMS _JAVASCRIPT _SRC + '/*.js' ) , PATHS . CMS _JAVASCRIPT _DIST ) ;
} ) ;
gulp . task ( 'umd-watch' , function ( ) {
gulp . watch ( PATHS . CMS _JAVASCRIPT _SRC + '/*.js' , [ 'umd-cms' ] ) ;
} ) ;