2015-01-16 06:23:40 +08:00
'use strict' ;
var gulp = require ( 'gulp' )
2015-01-16 06:44:17 +08:00
, browserify = require ( 'browserify' )
2015-10-03 20:16:23 +08:00
, buffer = require ( 'vinyl-buffer' )
2015-01-26 02:52:18 +08:00
, connect = require ( 'gulp-connect' )
2015-10-03 20:16:23 +08:00
, del = require ( 'del' )
2015-01-26 03:19:37 +08:00
, eslint = require ( 'gulp-eslint' )
2015-04-02 01:10:25 +08:00
, insert = require ( 'gulp-insert' )
2015-10-03 20:16:23 +08:00
, path = require ( 'path' )
2015-01-16 06:23:40 +08:00
, rename = require ( 'gulp-rename' )
, sass = require ( 'gulp-sass' )
2015-10-03 20:16:23 +08:00
, source = require ( 'vinyl-source-stream' )
, stream = require ( 'event-stream' )
2015-02-13 03:12:00 +08:00
, uglify = require ( 'gulp-uglify' )
2015-12-10 18:46:35 +08:00
, autoprefixer = require ( 'gulp-autoprefixer' )
2015-02-13 03:12:00 +08:00
, zip = require ( 'gulp-zip' ) ;
2015-01-16 06:23:40 +08:00
2015-04-02 01:10:25 +08:00
var version = '/* perfect-scrollbar v' + require ( './package' ) . version + ' */\n' ;
2015-01-16 06:23:40 +08:00
gulp . task ( 'lint' , function ( ) {
return gulp . src ( [ './src/**/*.js' , './gulpfile.js' ] )
2015-01-26 03:19:37 +08:00
. pipe ( eslint ( ) )
. pipe ( eslint . format ( ) )
. pipe ( eslint . failOnError ( ) ) ;
2015-01-16 06:23:40 +08:00
} ) ;
gulp . task ( 'clean:js' , function ( ) {
2015-10-03 20:16:23 +08:00
return del ( [ './dist/js/*.js' ] ) ;
2015-01-16 06:23:40 +08:00
} ) ;
2015-01-16 06:44:17 +08:00
gulp . task ( 'clean:js:min' , function ( ) {
2015-10-03 20:16:23 +08:00
return del ( [ './dist/js/min/*.js' ] ) ;
2015-01-16 06:44:17 +08:00
} ) ;
2015-10-03 20:16:23 +08:00
var jsEntries = [
'./src/js/adaptor/global.js' ,
'./src/js/adaptor/jquery.js'
] ;
2015-01-16 06:44:17 +08:00
2015-12-10 18:46:35 +08:00
var autoPrefixerConfig = {
browsers : [ '> 0%' ] , // '> 0%' forces autoprefixer to use all the possible prefixes. See https://github.com/ai/browserslist#queries for more details. IMO 'last 3 versions' would be good enough.
cascade : false
} ;
2015-01-16 06:44:17 +08:00
gulp . task ( 'js' , [ 'clean:js' ] , function ( ) {
2015-10-03 20:16:23 +08:00
var tasks = jsEntries . map ( function ( src ) {
return browserify ( [ src ] ) . bundle ( )
. pipe ( source ( path . basename ( src ) ) )
. pipe ( buffer ( ) )
. pipe ( insert . prepend ( version ) )
. pipe ( rename ( function ( path ) {
if ( path . basename === 'global' ) {
path . basename = 'perfect-scrollbar' ;
} else {
path . basename = 'perfect-scrollbar.' + path . basename ;
}
} ) )
. pipe ( gulp . dest ( './dist/js' ) )
. pipe ( connect . reload ( ) ) ;
} ) ;
return stream . merge . apply ( null , tasks ) ;
2015-01-16 06:44:17 +08:00
} ) ;
gulp . task ( 'js:min' , [ 'clean:js:min' ] , function ( ) {
2015-10-03 20:16:23 +08:00
var tasks = jsEntries . map ( function ( src ) {
return browserify ( [ src ] ) . bundle ( )
. pipe ( source ( path . basename ( src ) ) )
. pipe ( buffer ( ) )
. pipe ( uglify ( ) )
. pipe ( insert . prepend ( version ) )
. pipe ( rename ( function ( path ) {
if ( path . basename === 'global' ) {
2015-10-03 20:48:02 +08:00
path . basename = 'perfect-scrollbar.min' ;
2015-10-03 20:16:23 +08:00
} else {
2015-10-03 20:48:02 +08:00
path . basename = 'perfect-scrollbar.' + path . basename + '.min' ;
2015-10-03 20:16:23 +08:00
}
} ) )
. pipe ( gulp . dest ( './dist/js/min' ) )
. pipe ( connect . reload ( ) ) ;
} ) ;
return stream . merge . apply ( null , tasks ) ;
2015-01-16 06:23:40 +08:00
} ) ;
gulp . task ( 'clean:css' , function ( ) {
2015-10-03 20:16:23 +08:00
return del ( [ './dist/css/perfect-scrollbar.css' ] ) ;
2015-01-16 06:23:40 +08:00
} ) ;
gulp . task ( 'clean:css:min' , function ( ) {
2015-10-03 20:16:23 +08:00
return del ( [ './dist/css/perfect-scrollbar.min.css' ] ) ;
2015-01-16 06:23:40 +08:00
} ) ;
2015-10-03 20:16:23 +08:00
gulp . task ( 'css' , [ 'clean:css' ] , function ( ) {
2015-01-16 06:44:17 +08:00
return gulp . src ( './src/css/main.scss' )
. pipe ( sass ( ) )
2015-12-10 18:46:35 +08:00
. pipe ( autoprefixer ( autoPrefixerConfig ) )
2015-04-02 01:10:25 +08:00
. pipe ( insert . prepend ( version ) )
2015-01-16 06:44:17 +08:00
. pipe ( rename ( 'perfect-scrollbar.css' ) )
2015-02-13 03:56:27 +08:00
. pipe ( gulp . dest ( './dist/css' ) )
2015-01-26 02:52:18 +08:00
. pipe ( connect . reload ( ) ) ;
2015-01-16 06:44:17 +08:00
} ) ;
2015-10-03 20:16:23 +08:00
gulp . task ( 'css:min' , [ 'clean:css:min' ] , function ( ) {
2015-01-16 06:44:17 +08:00
return gulp . src ( './src/css/main.scss' )
2015-01-16 06:23:40 +08:00
. pipe ( sass ( { outputStyle : 'compressed' } ) )
2015-12-10 18:46:35 +08:00
. pipe ( autoprefixer ( autoPrefixerConfig ) )
2015-04-02 01:10:25 +08:00
. pipe ( insert . prepend ( version ) )
2015-01-16 06:23:40 +08:00
. pipe ( rename ( 'perfect-scrollbar.min.css' ) )
2015-02-13 03:56:27 +08:00
. pipe ( gulp . dest ( './dist/css' ) ) ;
2015-01-16 06:23:40 +08:00
} ) ;
2015-10-03 20:16:23 +08:00
gulp . task ( 'build' , [ 'js' , 'js:min' , 'css' , 'css:min' ] ) ;
2015-01-26 02:52:18 +08:00
gulp . task ( 'connect' , [ 'build' ] , function ( ) {
connect . server ( {
root : _ _dirname ,
livereload : true
} ) ;
} ) ;
gulp . task ( 'watch' , function ( ) {
gulp . watch ( [ 'src/js/**/*' ] , [ 'js' ] ) ;
2015-10-03 20:16:23 +08:00
gulp . watch ( [ 'src/css/**/*' ] , [ 'css' ] ) ;
2015-01-26 02:52:18 +08:00
} ) ;
gulp . task ( 'serve' , [ 'connect' , 'watch' ] ) ;
2015-02-13 03:12:00 +08:00
gulp . task ( 'compress' , function ( ) {
2015-02-13 03:56:27 +08:00
return gulp . src ( './dist/**' )
2015-02-13 03:12:00 +08:00
. pipe ( zip ( 'perfect-scrollbar.zip' ) )
2015-02-13 03:56:27 +08:00
. pipe ( gulp . dest ( './dist' ) ) ;
2015-02-13 03:12:00 +08:00
} ) ;
2015-01-16 06:23:40 +08:00
gulp . task ( 'default' , [ 'lint' , 'build' ] ) ;