Last active
September 13, 2018 03:30
-
-
Save Joel-hanson/31a76cd8f542fa8681358e452d6b9f5d to your computer and use it in GitHub Desktop.
A gulp file for my dist build
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var htmlmin = require('gulp-htmlmin'); | |
var cleanCSS = require('gulp-clean-css'); | |
var minify = require('gulp-minify'); | |
const gulp = require('gulp'), | |
del = require('del'), | |
runSequence = require('run-sequence'), | |
src = `${__dirname}/.`, | |
dist = `${__dirname}/public`; | |
workbox = require('workbox-build'); | |
imagemin = require('gulp-imagemin'); | |
var minify = require('gulp-minifier'); | |
gulp.task('clean', () => { | |
return del(dist); | |
}); | |
gulp.task('build', () => { | |
gulp.src(`${src}/**/*`) | |
.pipe(gulp.dest(dist)); | |
}); | |
gulp.task('generate-service-worker', () => { | |
return workbox.generateSW({ | |
globDirectory: dist, | |
globPatterns: ['**\/*.{html,js}'], | |
swDest: `${dist}/sw.js`, | |
clientsClaim: true, | |
skipWaiting: true, | |
runtimeCaching: [ | |
{ | |
urlPattern: new RegExp('http://139.59.3.54:8000/'), | |
handler: 'staleWhileRevalidate' | |
} | |
], | |
}).then(() => { | |
}).catch((error) => { | |
}); | |
}); | |
gulp.task('minify_img', () => | |
gulp.src('images/*\*') | |
.pipe(imagemin([],{ | |
verbose: true | |
})) | |
.pipe(imagemin({ | |
interlaced: true, | |
progressive: true, | |
optimizationLevel: 10, | |
svgoPlugins: [{removeViewBox: true}] | |
})) | |
.pipe(gulp.dest('public/images')) | |
); | |
gulp.task('minify_all', function() { | |
return gulp.src('./css/*').pipe(minify({ | |
minify: true, | |
collapseWhitespace: true, | |
conservativeCollapse: true, | |
minifyJS: true, | |
minifyCSS: true, | |
getKeptComment: function (content, filePath) { | |
var m = content.match(/\/\*![\s\S]*?\*\//img); | |
return m && m.join('\n') + '\n' || ''; | |
} | |
})).pipe(gulp.dest('public/')); | |
}); | |
gulp.task('default', () => { | |
runSequence(['build','generate-service-worker','minify_html','minify-css','minify_js']); | |
}); | |
gulp.task('minify_html', function() { | |
return gulp.src('*.html') | |
.pipe(htmlmin({collapseWhitespace: true})) | |
.pipe(gulp.dest('public')); | |
}); | |
gulp.task('minify-css', () => { | |
return gulp.src('css/*.css') | |
.pipe(cleanCSS({compatibility: 'ie8'},{debug: true}, (details) => { | |
})) | |
.pipe(gulp.dest('public/css/')); | |
}); | |
gulp.task('minify_js', function() { | |
gulp.src('js/*.js') | |
.pipe(minify({ | |
ext:{ | |
src:'-debug.js', | |
min:'.js' | |
}, | |
exclude: ['tasks'], | |
ignoreFiles: ['.combo.js', '-min.js'] | |
})) | |
.pipe(gulp.dest('public/js')) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment