2016-03-23 22:34:14 +01:00
var packageJson = require ( './package.json' ) ,
2016-02-03 02:32:45 +01:00
autoprefixer = require ( 'autoprefixer' ) ,
2016-03-23 22:34:14 +01:00
babelify = require ( 'babelify' ) ,
browserify = require ( 'browserify' ) ,
eventStream = require ( 'event-stream' ) ,
glob = require ( 'glob' ) ,
gulp = require ( 'gulp' ) ,
babel = require ( 'gulp-babel' ) ,
diff = require ( 'gulp-diff' ) ,
gulpif = require ( 'gulp-if' ) ,
notify = require ( 'gulp-notify' ) ,
postcss = require ( 'gulp-postcss' ) ,
sass = require ( 'gulp-sass' ) ,
sourcemaps = require ( 'gulp-sourcemaps' ) ,
uglify = require ( 'gulp-uglify' ) ,
gulpUtil = require ( 'gulp-util' ) ,
path = require ( 'path' ) ,
source = require ( 'vinyl-source-stream' ) ,
buffer = require ( 'vinyl-buffer' ) ,
semver = require ( 'semver' ) ,
sprity = require ( 'sprity' ) ,
watchify = require ( 'watchify' ) ;
2016-03-21 01:51:33 +01:00
2016-02-10 23:13:32 +01:00
var isDev = typeof process . env . npm _config _development !== 'undefined' ;
2016-01-06 04:06:47 +01:00
2016-01-11 02:25:30 +01:00
var PATHS = {
MODULES : './node_modules' ,
2016-02-10 23:13:32 +01:00
ADMIN : './admin' ,
ADMIN _IMAGES : './admin/images' ,
2016-02-03 02:32:45 +01:00
ADMIN _SCSS : './admin/scss' ,
2016-01-11 02:25:30 +01:00
ADMIN _THIRDPARTY : './admin/thirdparty' ,
ADMIN _JAVASCRIPT _SRC : './admin/javascript/src' ,
ADMIN _JAVASCRIPT _DIST : './admin/javascript/dist' ,
2016-02-03 02:32:45 +01:00
FRAMEWORK : '.' ,
2016-01-11 02:25:30 +01:00
FRAMEWORK _THIRDPARTY : './thirdparty' ,
2016-02-10 23:13:32 +01:00
FRAMEWORK _DEV _INSTALL : './dev/install' ,
2016-01-11 02:25:30 +01:00
FRAMEWORK _JAVASCRIPT _SRC : './javascript/src' ,
FRAMEWORK _JAVASCRIPT _DIST : './javascript/dist'
} ;
2016-02-10 23:13:32 +01:00
// Folders which contain both scss and css folders to be compiled
var rootCompileFolders = [ PATHS . FRAMEWORK , PATHS . ADMIN , PATHS . FRAMEWORK _DEV _INSTALL ]
2016-03-26 06:41:24 +01:00
var browserifyOptions = {
debug : true ,
paths : [ PATHS . ADMIN _JAVASCRIPT _SRC , PATHS . FRAMEWORK _JAVASCRIPT _SRC ]
} ;
2016-03-23 22:34:14 +01:00
var babelifyOptions = {
presets : [ 'es2015' , 'react' ] ,
ignore : /(node_modules|thirdparty)/ ,
comments : false
} ;
2016-01-06 04:06:47 +01:00
2016-02-03 02:32:45 +01:00
// Used for autoprefixing css properties (same as Bootstrap Aplha.2 defaults)
var supportedBrowsers = [
'Chrome >= 35' ,
'Firefox >= 31' ,
'Edge >= 12' ,
'Explorer >= 9' ,
'iOS >= 8' ,
'Safari >= 8' ,
'Android 2.3' ,
'Android >= 4' ,
'Opera >= 12'
] ;
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
} ;
2016-04-03 22:15:27 +02:00
var tinymceConfig = {
src : PATHS . MODULES + '/tinymce' ,
dest : PATHS . FRAMEWORK _THIRDPARTY + '/tinymce' ,
files : [
'/tinymce.min.js' , // Exclude unminified file to keep repository size down
'/jquery.tinymce.min.js' ,
'/themes/**' ,
'/skins/**' ,
'/plugins/**'
]
} ;
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 ,
2016-02-22 23:12:24 +01:00
plugins : [ 'transform-es2015-modules-umd' ] ,
comments : false
2016-01-11 02:25:30 +01:00
} ) )
. 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-02-10 23:13:32 +01:00
if ( isDev ) {
2016-03-23 22:34:14 +01:00
browserifyOptions . cache = { } ;
browserifyOptions . packageCache = { } ;
browserifyOptions . plugin = [ watchify ] ;
2016-01-11 02:25:30 +01:00
}
2016-03-23 22:34:14 +01:00
gulp . task ( 'build' , [ 'umd' , 'bundle' ] ) ;
2016-01-11 02:25:30 +01:00
2016-03-23 22:34:14 +01:00
gulp . task ( 'bundle' , [ 'bundle-lib' , 'bundle-legacy' , 'bundle-framework' ] ) ;
2016-01-11 02:25:30 +01:00
gulp . task ( 'bundle-lib' , function bundleLib ( ) {
2016-03-16 01:30:39 +01:00
var bundleFileName = 'bundle-lib.js' ;
return browserify ( Object . assign ( { } , browserifyOptions , { entries : PATHS . ADMIN _JAVASCRIPT _SRC + '/bundles/lib.js' } ) )
2016-03-23 22:34:14 +01:00
. on ( 'update' , bundleLib )
. on ( 'log' , function ( msg ) { gulpUtil . log ( 'Finished' , 'bundled ' + bundleFileName + ' ' + msg ) } )
. transform ( 'babelify' , babelifyOptions )
2016-03-30 05:38:34 +02:00
. require ( PATHS . ADMIN _JAVASCRIPT _SRC + '/components/form/index' , { expose : 'components/form/index' } )
. require ( PATHS . ADMIN _JAVASCRIPT _SRC + '/components/form-action/index' , { expose : 'components/form-action' } )
. require ( PATHS . ADMIN _JAVASCRIPT _SRC + '/components/form-builder/index' , { expose : 'components/form-builder/index' } )
. require ( PATHS . ADMIN _JAVASCRIPT _SRC + '/components/grid-field/index' , { expose : 'components/grid-field/index' } )
. require ( PATHS . ADMIN _JAVASCRIPT _SRC + '/components/grid-field/cell' , { expose : 'components/grid-field/cell/index' } )
. require ( PATHS . ADMIN _JAVASCRIPT _SRC + '/components/grid-field/header' , { expose : 'components/grid-field/header' } )
. require ( PATHS . ADMIN _JAVASCRIPT _SRC + '/components/grid-field/header-cell' , { expose : 'components/grid-field/header-cell' } )
. require ( PATHS . ADMIN _JAVASCRIPT _SRC + '/components/grid-field/row' , { expose : 'components/grid-field/row' } )
. require ( PATHS . ADMIN _JAVASCRIPT _SRC + '/components/grid-field/table' , { expose : 'components/grid-field/table' } )
. require ( PATHS . ADMIN _JAVASCRIPT _SRC + '/components/hidden-field/index' , { expose : 'components/hidden-field/index' } )
. require ( PATHS . ADMIN _JAVASCRIPT _SRC + '/components/text-field/index' , { expose : 'components/text-field/index' } )
. require ( PATHS . ADMIN _JAVASCRIPT _SRC + '/components/north-header/index' , { expose : 'components/north-header/index' } )
. require ( PATHS . ADMIN _JAVASCRIPT _SRC + '/components/north-header-breadcrumbs/index' , { expose : 'components/north-header-breadcrumbs/index' } )
. require ( 'deep-freeze' , { expose : 'deep-freeze' } )
. require ( PATHS . FRAMEWORK _JAVASCRIPT _SRC + '/i18n.js' , { expose : 'i18n' } )
. require ( PATHS . FRAMEWORK _JAVASCRIPT _SRC + '/jQuery.js' , { expose : 'jQuery' } )
. require ( 'react' , { expose : 'react' } )
. require ( 'react-addons-css-transition-group' , { expose : 'react-addons-css-transition-group' } )
. require ( 'react-addons-test-utils' , { expose : 'react-addons-test-utils' } )
. require ( 'react-dom' , { expose : 'react-dom' } )
. require ( 'react-redux' , { expose : 'react-redux' } )
. require ( PATHS . ADMIN _JAVASCRIPT _SRC + '/reducer-register.js' , { expose : 'reducer-register' } )
. require ( 'redux' , { expose : 'redux' } )
. require ( 'redux-thunk' , { expose : 'redux-thunk' } )
. require ( PATHS . FRAMEWORK _JAVASCRIPT _SRC + '/router.js' , { expose : 'router' } )
. require ( PATHS . ADMIN _JAVASCRIPT _SRC + '/silverstripe-component' , { expose : 'silverstripe-component' } )
2016-01-11 02:25:30 +01:00
. bundle ( )
2016-03-16 01:30:39 +01:00
. on ( 'update' , bundleLib )
. on ( 'error' , notify . onError ( { message : bundleFileName + ': <%= error.message %>' } ) )
. pipe ( source ( bundleFileName ) )
2016-03-07 04:13:55 +01:00
. pipe ( buffer ( ) )
2016-03-23 22:34:14 +01:00
. pipe ( sourcemaps . init ( { loadMaps : true } ) )
2016-03-07 04:13:55 +01:00
. pipe ( gulpif ( ! isDev , uglify ( ) ) )
2016-03-23 22:34:14 +01:00
. pipe ( sourcemaps . write ( './' ) )
2016-03-07 04:13:55 +01:00
. pipe ( gulp . dest ( PATHS . ADMIN _JAVASCRIPT _DIST ) ) ;
2016-02-09 00:26:39 +01:00
} ) ;
2016-03-16 01:30:39 +01:00
2016-03-23 22:34:14 +01:00
gulp . task ( 'bundle-legacy' , function bundleLeftAndMain ( ) {
var bundleFileName = 'bundle-legacy.js' ;
2016-03-16 01:30:39 +01:00
2016-03-23 22:34:14 +01:00
return browserify ( Object . assign ( { } , browserifyOptions , { entries : PATHS . ADMIN _JAVASCRIPT _SRC + '/bundles/legacy.js' } ) )
. on ( 'update' , bundleLeftAndMain )
. on ( 'log' , function ( msg ) { gulpUtil . log ( 'Finished' , 'bundled ' + bundleFileName + ' ' + msg ) } )
. transform ( 'babelify' , babelifyOptions )
2016-03-22 04:27:44 +01:00
. external ( 'jQuery' )
2016-03-23 22:34:14 +01:00
. external ( 'i18n' )
. external ( 'router' )
2016-03-16 01:30:39 +01:00
. bundle ( )
2016-03-23 22:34:14 +01:00
. on ( 'update' , bundleLeftAndMain )
2016-03-16 01:30:39 +01:00
. on ( 'error' , notify . onError ( { message : bundleFileName + ': <%= error.message %>' } ) )
. pipe ( source ( bundleFileName ) )
. pipe ( buffer ( ) )
2016-03-23 22:34:14 +01:00
. pipe ( sourcemaps . init ( { loadMaps : true } ) )
2016-03-16 01:30:39 +01:00
. pipe ( gulpif ( ! isDev , uglify ( ) ) )
2016-03-23 22:34:14 +01:00
. pipe ( sourcemaps . write ( './' ) )
2016-03-16 01:30:39 +01:00
. pipe ( gulp . dest ( PATHS . ADMIN _JAVASCRIPT _DIST ) ) ;
} ) ;
2016-03-23 22:34:14 +01:00
gulp . task ( 'bundle-framework' , function bundleBoot ( ) {
var bundleFileName = 'bundle-framework.js' ;
2016-03-16 01:30:39 +01:00
2016-03-23 22:34:14 +01:00
return browserify ( Object . assign ( { } , browserifyOptions , { entries : PATHS . ADMIN _JAVASCRIPT _SRC + '/boot/index.js' } ) )
. on ( 'update' , bundleBoot )
. on ( 'log' , function ( msg ) { gulpUtil . log ( 'Finished' , 'bundled ' + bundleFileName + ' ' + msg ) } )
. transform ( 'babelify' , babelifyOptions )
2016-03-30 05:38:34 +02:00
. external ( 'components/action-button/index' )
. external ( 'components/north-header/index' )
. external ( 'components/form-builder/index' )
2016-03-16 01:30:39 +01:00
. external ( 'deep-freeze' )
2016-03-30 05:38:34 +02:00
. external ( 'components/grid-field/index' )
2016-03-16 01:30:39 +01:00
. external ( 'i18n' )
. external ( 'jQuery' )
. external ( 'page.js' )
. external ( 'react-addons-test-utils' )
. external ( 'react-dom' )
. external ( 'react-redux' )
2016-03-23 22:34:14 +01:00
. external ( 'react' )
2016-03-22 00:25:23 +01:00
. external ( 'reducer-register' )
2016-03-16 01:30:39 +01:00
. external ( 'redux-thunk' )
2016-03-23 22:34:14 +01:00
. external ( 'redux' )
2016-03-16 01:30:39 +01:00
. external ( 'silverstripe-component' )
. bundle ( )
2016-03-23 22:34:14 +01:00
. on ( 'update' , bundleBoot )
2016-03-16 01:30:39 +01:00
. on ( 'error' , notify . onError ( { message : bundleFileName + ': <%= error.message %>' } ) )
. pipe ( source ( bundleFileName ) )
2016-03-07 04:13:55 +01:00
. pipe ( buffer ( ) )
2016-03-23 22:34:14 +01:00
. pipe ( sourcemaps . init ( { loadMaps : true } ) )
2016-03-07 04:13:55 +01:00
. pipe ( gulpif ( ! isDev , uglify ( ) ) )
2016-03-23 22:34:14 +01:00
. pipe ( sourcemaps . write ( './' ) )
2016-03-07 04:13:55 +01:00
. 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-04-03 22:15:27 +02:00
diffFiles ( tinymceConfig ) ;
2016-01-06 04:06:47 +01:00
} ) ;
2016-01-11 02:25:30 +01:00
gulp . task ( 'thirdparty' , function ( ) {
copyFiles ( blueimpFileUploadConfig ) ;
copyFiles ( blueimpLoadImageConfig ) ;
copyFiles ( blueimpTmplConfig ) ;
copyFiles ( jquerySizesConfig ) ;
2016-02-12 04:00:15 +01:00
copyFiles ( tinymceConfig ) ;
2016-01-11 02:25:30 +01:00
} ) ;
2016-03-07 04:13:55 +01:00
gulp . task ( 'umd' , [ 'umd-admin' , 'umd-framework' ] , function ( ) {
if ( isDev ) {
gulp . watch ( PATHS . ADMIN _JAVASCRIPT _SRC + '/*.js' , [ 'umd-admin' ] ) ;
gulp . watch ( PATHS . FRAMEWORK _JAVASCRIPT _SRC + '/*.js' , [ 'umd-framework' ] ) ;
}
} ) ;
2016-01-11 02:25:30 +01:00
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 ) ;
} ) ;
2016-02-10 23:13:32 +01:00
/ *
* Takes individual images and compiles them together into sprites
* /
gulp . task ( 'sprites' , function ( ) {
return sprity . src ( {
src : PATHS . ADMIN _IMAGES + '/sprites/src/**/*.{png,jpg}' ,
cssPath : '../images/sprites/dist' ,
style : './_spritey.scss' ,
processor : 'sass' ,
split : true ,
margin : 0
} )
. pipe ( gulpif ( '*.png' , gulp . dest ( PATHS . ADMIN _IMAGES + '/sprites/dist' ) , gulp . dest ( PATHS . ADMIN _SCSS ) ) )
} ) ;
2016-02-03 02:32:45 +01:00
gulp . task ( 'css' , [ 'compile:css' ] , function ( ) {
if ( isDev ) {
rootCompileFolders . forEach ( function ( folder ) {
gulp . watch ( folder + '/scss/**/*.scss' , [ 'compile:css' ] ) ;
} ) ;
2016-04-03 22:15:27 +02:00
2016-03-28 02:10:00 +02:00
// Watch the .scss files in react components
gulp . watch ( './admin/javascript/src/**/*.scss' , [ 'compile:css' ] ) ;
2016-02-03 02:32:45 +01:00
}
} )
2016-02-10 23:13:32 +01:00
/ *
* Compiles scss into css
* Watches for changes if -- development flag is given
* /
2016-02-03 02:32:45 +01:00
gulp . task ( 'compile:css' , function ( ) {
2016-02-10 23:13:32 +01:00
var outputStyle = isDev ? 'expanded' : 'compressed' ;
var tasks = rootCompileFolders . map ( function ( folder ) {
return gulp . src ( folder + '/scss/**/*.scss' )
. pipe ( sourcemaps . init ( ) )
. pipe ( sass ( { outputStyle : outputStyle } )
. on ( 'error' , notify . onError ( {
message : 'Error: <%= error.message %>'
} ) )
)
2016-02-03 02:32:45 +01:00
. pipe ( postcss ( [ autoprefixer ( { browsers : supportedBrowsers } ) ] ) )
2016-02-10 23:13:32 +01:00
. pipe ( sourcemaps . write ( ) )
. pipe ( gulp . dest ( folder + '/css' ) )
} ) ;
return tasks ;
} ) ;