2020-08-26 17:57:10 +02:00
const gulp = require ( 'gulp' ) ;
const cssnano = require ( 'cssnano' )
const gulpLoadPlugins = require ( 'gulp-load-plugins' )
const uglify = require ( 'gulp-uglify-es' ) . default ;
const del = require ( 'del' ) ;
2020-08-26 20:32:34 +02:00
const browserify = require ( 'browserify' ) ;
const babelify = require ( 'babelify' ) ;
const source = require ( 'vinyl-source-stream' ) ;
const buffer = require ( 'vinyl-buffer' ) ;
2020-08-26 20:52:15 +02:00
const fs = require ( 'fs' ) ;
2020-08-26 17:57:10 +02:00
const $ = gulpLoadPlugins ( )
$ . sass . compiler = require ( 'sass' ) ;
/ * = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Base consts
=== === === === === === === === === === === === === === === === === === === === === === === === === = * /
// Project sources
const src = {
root : 'src' ,
2020-08-26 18:09:10 +02:00
manifests : 'src/manifests' ,
2020-08-26 17:57:10 +02:00
styles : 'src/stylesheets' ,
scripts : 'src/javascript' ,
images : 'src/images' ,
}
// Build path
const tmp = {
root : '.tmp' ,
2020-08-26 18:09:10 +02:00
manifests : '.tmp/manifests' ,
2020-08-26 17:57:10 +02:00
styles : '.tmp/stylesheets' ,
scripts : '.tmp/javascript' ,
images : '.tmp/images' ,
vendor : '.tmp/libs' ,
}
// Dist path
const dist = {
root : 'dist' ,
2020-08-26 18:09:10 +02:00
manifests : 'dist/manifests' ,
2020-08-26 17:57:10 +02:00
styles : 'dist/stylesheets' ,
scripts : 'dist/javascript' ,
images : 'dist/images' ,
vendor : 'dist/libs' ,
}
// Build mode
const isProd = process . env . NODE _ENV === 'production' ;
const isDev = ! isProd ;
/ * = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Build tasks
=== === === === === === === === === === === === === === === === === === === === === === === === === = * /
gulp . task ( 'styles' , ( ) => {
return gulp . src ( ` ${ src . styles } /*.scss ` )
. pipe ( $ . plumber ( ) )
// sourcemap initialization
. pipe ( $ . if ( isDev , $ . sourcemaps . init ( ) ) )
// sass compilation
. pipe ( $ . sass . sync ( {
outputStyle : 'expanded' ,
precision : 7 ,
includePaths : [ '.' ] ,
} ) . on ( 'error' , $ . sass . logError ) )
// autoprefix
. pipe ( $ . autoprefixer ( ) )
// out stream size
. pipe ( $ . size ( {
showFiles : true
} ) )
. pipe ( gulp . dest ( tmp . styles ) )
. pipe ( $ . rename ( { suffix : '.min' } ) )
// minimize and optimize
. pipe ( $ . if ( '*.css' , $ . postcss ( [
cssnano ( {
safe : true ,
autoprefixer : false ,
zindex : false ,
reduceIdents : {
keyframes : true ,
} ,
} )
] ) ) )
// out stream size
. pipe ( $ . size ( {
showFiles : true
} ) )
// write sourcemaps
2020-08-26 20:32:34 +02:00
. pipe ( $ . if ( isDev , $ . sourcemaps . write ( ) ) )
2020-08-26 17:57:10 +02:00
. pipe ( gulp . dest ( tmp . styles ) )
} )
gulp . task ( 'scripts' , ( ) => {
2020-08-26 20:32:34 +02:00
let b = browserify ( {
entries : ` ${ src . scripts } /index.js ` ,
debug : true
} ) ;
return b . transform ( 'babelify' ) . bundle ( )
2020-08-26 17:57:10 +02:00
. pipe ( $ . plumber ( ) )
2020-08-26 20:32:34 +02:00
. pipe ( source ( 'app.js' ) )
. pipe ( buffer ( ) )
. pipe ( $ . if ( isDev , $ . sourcemaps . init ( { loadMaps : true } ) ) )
//.pipe(uglify({ compress: { drop_console: isProd, drop_debugger: isProd } }))
2020-08-26 17:57:10 +02:00
. pipe ( $ . rename ( { suffix : '.min' } ) )
. pipe ( $ . size ( {
showFiles : true ,
} ) )
2020-08-26 20:32:34 +02:00
. pipe ( $ . if ( isDev , $ . sourcemaps . write ( ) ) )
2020-08-26 17:57:10 +02:00
. pipe ( gulp . dest ( ` ${ tmp . scripts } ` ) )
} )
gulp . task ( 'images' , ( ) => {
return gulp . src ( ` ${ src . images } /**/* ` )
. pipe ( $ . plumber ( ) )
. pipe ( $ . imagemin ( [
$ . imagemin . gifsicle ( { interlaced : true } ) ,
$ . imagemin . mozjpeg ( { progressive : true } ) ,
$ . imagemin . optipng ( ) ,
$ . imagemin . svgo ( { plugins : [ { cleanupIDs : false } ] } )
] ) )
2020-08-26 18:09:10 +02:00
. pipe ( $ . size ( {
showFiles : true ,
} ) )
2020-08-26 17:57:10 +02:00
. pipe ( gulp . dest ( tmp . images ) )
} )
2020-08-26 18:09:10 +02:00
gulp . task ( 'manifests' , ( ) => {
2020-08-26 20:52:15 +02:00
const templateFile = ` ${ src . manifests } /manifest.template.json ` ;
let template = JSON . parse ( fs . readFileSync ( templateFile ) )
return gulp . src ( ` ${ src . manifests } /**/!(*.template).json ` )
2020-08-26 18:09:10 +02:00
. pipe ( $ . plumber ( ) )
2020-08-26 20:52:15 +02:00
. pipe ( $ . replace ( '$name' , template . name ) )
. pipe ( $ . replace ( '$shortName' , template . short _name ) )
. pipe ( $ . replace ( '$version' , template . version ) )
. pipe ( $ . replace ( '$semanticVersion' , template . version _name ) )
. pipe ( $ . replace ( '$description' , template . description ) )
. pipe ( $ . replace ( '$author' , template . author ) )
. pipe ( $ . replace ( '$developer' , template . developer ) )
. pipe ( $ . replace ( '$homepageURL' , template . homepage _url ) )
2020-08-26 18:09:10 +02:00
. pipe ( $ . size ( {
showFiles : true ,
} ) )
. pipe ( gulp . dest ( tmp . manifests ) )
} )
2020-08-26 17:57:10 +02:00
/ * = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Clean
=== === === === === === === === === === === === === === === === === === === === === === === === === = * /
gulp . task ( 'clean:build' , del . bind ( null , [ tmp . root ] ) )
gulp . task ( 'clean:dist' , del . bind ( null , [ dist . root ] , { force : true } ) ) // Das Force brauchen wir, da das Assets Verzeichnis außerhalb des Working Directories ist.
gulp . task ( 'clean' , gulp . series ( 'clean:build' , 'clean:dist' ) )
/ * = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
BUILD CLEAN ALL
=== === === === === === === === === === === === === === === === === === === === === === === === === = * /
2020-08-26 18:09:10 +02:00
gulp . task ( 'build' , gulp . series ( 'clean:build' , 'manifests' , 'images' , 'scripts' , 'styles' ) ) ;
2020-08-26 17:57:10 +02:00
/ * = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
DIST CLEAN ALL
=== === === === === === === === === === === === === === === === === === === === === === === === === = * /
gulp . task ( 'dist:copy' , ( done ) => {
// copy vendor
gulp . src ( ` ${ tmp . vendor } /**/* ` )
. pipe ( gulp . dest ( dist . vendor ) )
// copy images
gulp . src ( ` ${ tmp . images } /**/* ` )
. pipe ( gulp . dest ( dist . images ) )
// copy scripts
gulp . src ( ` ${ tmp . scripts } /**/*.{min.js,min.js.gz} ` )
. pipe ( gulp . dest ( dist . scripts ) )
// copy styles
gulp . src ( ` ${ tmp . styles } /*.{min.css,min.css.gz} ` )
. pipe ( gulp . dest ( dist . styles ) )
done ( ) ;
} )
gulp . task ( 'dist' , gulp . series ( 'clean' , 'build' , 'dist:copy' ) ) ;