gulp3.9.1版本
var gulp = require ( 'gulp' ) ;
var cssnano = require ( 'gulp-cssnano' ) ;
var rename = require ( 'gulp-rename' ) ;
var uglify = require ( 'gulp-uglify' ) ;
var tinypng_nokey = require ( 'gulp-tinypng-nokey' ) ;
var cache = require ( 'gulp-cache' ) ;
var bs = require ( 'browser-sync' ) . create ( ) ;
var sass = require ( 'gulp-sass' ) ;
var util = require ( 'gulp-util' ) ;
var sourcemaps = require ( 'gulp-sourcemaps' ) ;
var path = {
'html' : './templates/**/' ,
'css' : './src/css/' ,
'js' : './src/js/' ,
'image' : './src/images/' ,
'css_dist' : './dist/css/' ,
'js_dist' : './dist/js/' ,
'image_dist' : './dist/images/'
} ;
gulp. task ( 'html' , function ( ) {
gulp. src ( path. html+ '*.html' )
. pipe ( bs. stream ( ) ) ;
} ) ;
gulp. task ( 'css' , function ( ) {
gulp. src ( path. css+ '*.scss' )
. pipe ( sass ( ) . on ( 'error' , sass. logError) )
. pipe ( cssnano ( ) )
. pipe ( rename ( { 'suffix' : '.min' } ) )
. pipe ( gulp. dest ( path. css_dist) )
. pipe ( bs. stream ( ) ) ;
} ) ;
gulp. task ( 'js' , function ( ) {
gulp. src ( path. js+ '*.js' )
. pipe ( sourcemaps. init ( ) )
. pipe ( uglify ( { 'toplevel' : true } ) . on ( 'error' , util. log) )
. pipe ( rename ( { 'suffix' : '.min' } ) )
. pipe ( sourcemaps. write ( ) )
. pipe ( gulp. dest ( path. js_dist) )
. pipe ( bs. stream ( ) ) ;
} ) ;
gulp. task ( 'image' , function ( ) {
gulp. src ( path. image+ '*.*' )
. pipe ( cache ( tinypng_nokey ( ) ) )
. pipe ( gulp. dest ( path. image_dist) )
. pipe ( bs. stream ( ) ) ;
} ) ;
gulp. task ( 'watch' , function ( ) {
gulp. watch ( path. html+ '*.html' , [ 'html' ] ) ;
gulp. watch ( path. css+ '*.scss' , [ 'css' ] ) ;
gulp. watch ( path. js+ '*.js' , [ 'js' ] ) ;
gulp. watch ( path. image+ '*.*' , [ 'image' ] ) ;
} ) ;
gulp. task ( 'bs' , function ( ) {
bs. init ( {
'server' : {
'baseDir' : './' ,
} ,
} ) ;
} ) ;
gulp. task ( 'default' , [ 'bs' , 'watch' ] ) ;
gulp4.x版本
var gulp = require ( "gulp" ) ;
var cssnano = require ( "gulp-cssnano" ) ;
var rename = require ( "gulp-rename" ) ;
var uglify = require ( "gulp-uglify" ) ;
var concat = require ( "gulp-concat" ) ;
var imagemin = require ( 'gulp-imagemin' ) ;
var cache = require ( 'gulp-cache' ) ;
var bs = require ( 'browser-sync' ) . create ( ) ;
var sass = require ( 'gulp-sass' ) ;
var util = require ( 'gulp-util' ) ;
var sourcemaps = require ( 'gulp-sourcemaps' ) ;
var path = {
'html' : './templates/**/' ,
'css' : './src/css/**/' ,
'js' : './src/js/' ,
'images' : './src/images' ,
'css_dist' : './dist/css/' ,
'js_dist' : './dist/js/' ,
'images_dist' : './dist/images/'
} ;
gulp. task ( "html" , function ( done) {
gulp. src ( path. html + '*.html' )
. pipe ( bs. stream ( ) ) ;
done ( ) ;
} ) ;
gulp. task ( "css" , function ( done) {
gulp. src ( path. css + '*.scss' )
. pipe ( sass ( ) . on ( 'error' , sass. logError) )
. pipe ( cssnano ( ) )
. pipe ( rename ( { "suffix" : ".min" } ) )
. pipe ( gulp. dest ( path. css_dist) )
. pipe ( bs. stream ( ) ) ;
done ( ) ;
} ) ;
gulp. task ( "js" , function ( done) {
gulp. src ( path. js + '*.js' )
. pipe ( sourcemaps. init ( ) )
. pipe ( uglify ( ) . on ( 'error' , util. log) )
. pipe ( rename ( { "suffix" : ".min" } ) )
. pipe ( sourcemaps. write ( ) )
. pipe ( gulp. dest ( path. js_dist) )
. pipe ( bs. stream ( ) ) ;
done ( ) ;
} ) ;
gulp. task ( "images" , function ( done) {
gulp. src ( path. images + "*.*" )
. pipe ( cache ( imagemin ( ) ) )
. pipe ( gulp. dest ( path. images_dist) )
. pipe ( bs. stream ( ) ) ;
done ( ) ;
} ) ;
gulp. task ( "watch" , function ( ) {
gulp. watch ( path. css+ "*.scss" , gulp. series ( "css" ) ) ;
gulp. watch ( path. js+ "*.js" , gulp. series ( "js" ) ) ;
gulp. watch ( path. images+ "*.*" , gulp. series ( "images" ) ) ;
gulp. watch ( path. html+ '*.html' , gulp. series ( "html" ) ) ;
} ) ;
gulp. task ( "bs" , function ( ) {
bs. init ( {
"server" : {
"baseDir" : "./"
}
} )
} ) ;
gulp. task ( 'default' , gulp. series ( "watch" ) ) ;
package.json文件
{
"name" : "front" ,
"version" : "1.0.0" ,
"description" : "" ,
"main" : "gulpfile.js" ,
"scripts" : {
"test" : "echo \"Error: no test specified\" && exit 1"
} ,
"author" : "" ,
"license" : "ISC" ,
"devDependencies" : {
"browser-sync" : "^2.26.7" ,
"gulp" : "^3.9.1" ,
"gulp-cache" : "^1.1.3" ,
"gulp-concat" : "^2.6.1" ,
"gulp-cssnano" : "^2.1.3" ,
"gulp-rename" : "^1.4.0" ,
"gulp-sass" : "^4.0.2" ,
"gulp-sourcemaps" : "^2.6.5" ,
"gulp-tinypng-nokey" : "^1.1.0" ,
"gulp-uglify" : "^3.0.2" ,
"gulp-util" : "^3.0.8"
}
}