Last active
December 7, 2016 21:45
-
-
Save stephentuso/8f25563fd473cbb07c79f82d320d41b2 to your computer and use it in GitHub Desktop.
gulpfile I use for polymer
This file contains 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
/** | |
Structure used: | |
Source files are in /app | |
There are php files in /app/private_php and /app/public_php | |
Static files (elements, bower_components) are in /app/static | |
Builds into /dist-dev and /dist-prod | |
*/ | |
var gulp = require('gulp'); | |
var $ = require('gulp-load-plugins')(); | |
var path = require('path'); | |
var bowerFiles = require('main-bower-files'); | |
var merge = require('merge-stream'); | |
var del = require('del'); | |
var runSequence = require('run-sequence'); | |
var pump = require('pump'); | |
var paths = { | |
scripts: 'app/static/**/*.js', | |
styles: ['./app/**/*.css', './app/**/*.scss'], | |
index: './app/private_php/index.php', | |
html: './app/static/**/*.html', | |
distDev: './dist-dev', | |
distProd: './dist-prod' | |
}; | |
var AUTOPREFIXER_BROWSERS = [ | |
'ie >= 10', | |
'ie_mob >= 10', | |
'ff >= 30', | |
'chrome >= 34', | |
'safari >= 7', | |
'opera >= 23', | |
'ios >= 7', | |
'android >= 4.0', | |
'bb >= 10' | |
]; | |
var distDev = function(subpath) { | |
return !subpath ? paths.distDev : path.join(paths.distDev, subpath); | |
}; | |
var distProd = function(subpath) { | |
return !subpath ? paths.distProd : path.join(paths.distProd, subpath); | |
}; | |
var styleTask = function(stylesPath, srcs) { | |
var src = gulp.src(srcs.map(function(src) { | |
return path.join('app', stylesPath, src); | |
})); | |
var sass = src.pipe($.changed(stylesPath, {extension: '.scss'})) | |
.pipe($.sass()); | |
var css = src.pipe($.changed(stylesPath, {extension: '.css'})); | |
return merge(sass, css) | |
.pipe($.autoprefixer(AUTOPREFIXER_BROWSERS)) | |
.pipe(gulp.dest(distDev(stylesPath))) | |
.pipe($.size({title: stylesPath})) | |
.pipe(gulp.dest('.tmp/' + stylesPath)) | |
.pipe($.cleanCss()) | |
.pipe(gulp.dest(distProd(stylesPath))) | |
.pipe($.size({title: stylesPath + " css min"})); | |
}; | |
var imageOptimizeTask = function(src) { | |
return gulp.src(src) | |
.pipe($.imagemin({ | |
progressive: true, | |
interlaced: true | |
})) | |
.pipe(gulp.dest(distDev('static/images'))) | |
.pipe(gulp.dest(distProd('static/images'))) | |
.pipe($.size({title: 'images'})); | |
}; | |
// Compile and automatically prefix stylesheets | |
gulp.task('styles', function() { | |
return styleTask('/static/styles', ['**/*css']); | |
}); | |
gulp.task('images', function() { | |
return imageOptimizeTask('app/static/images/**/*'); | |
}); | |
gulp.task('copyDev', function() { | |
return gulp.src([ | |
'app/**', | |
'!app/static/styles/**/*css', | |
'!**/.DS_Store' | |
], { | |
dot: true | |
}) | |
.pipe(gulp.dest(distDev())) | |
.pipe($.size({ | |
title: 'copyDev' | |
})); | |
}); | |
gulp.task('copyProd', function() { | |
var app = gulp.src([ | |
'app/**', | |
'!app/static/styles/**/*css', | |
'!app/test/**', | |
'!app/static/elements/**', | |
'!app/static/bower_components/**', | |
'!**/.DS_Store' | |
], { | |
dot: true | |
}).pipe(gulp.dest(distProd())); | |
// Copy over only the bower_components we need | |
// These are things which cannot be vulcanized | |
var bower = gulp.src([ | |
'app/static/bower_components/{webcomponentsjs,promise-polyfill,moment}/**/*' | |
]).pipe(gulp.dest(distProd('/static/bower_components'))); | |
return merge(app, bower) | |
.pipe($.size({ | |
title: 'copyProd' | |
})); | |
}); | |
//Vulcanize for production | |
gulp.task('vulcanize', function() { | |
return gulp.src('app/static/elements/*elements.html') | |
.pipe($.vulcanize({ | |
stripComments: true, | |
inlineCss: true, | |
inlineScripts: true | |
})) | |
.pipe(gulp.dest(distProd('static/elements'))) | |
.pipe($.size({title: 'vulcanize'})); | |
}); | |
//Minify html in production dir | |
gulp.task('minifyHtml', function() { | |
return gulp.src([distProd('/**/*.html'), | |
distProd('/**_php/*.php')]) | |
.pipe($.size({title: 'HTML'})) | |
.pipe($.htmlmin({ | |
collapseWhitespace: true, | |
minifyCSS: true, | |
minifyJS: false, | |
preserveLineBreaks: true, | |
ignoreCustomFragments: [ /<%[\s\S]*?%>/g, /<\?[\s\S]*?(\?>|$)/g ], | |
removeComments: true | |
})) | |
.pipe(gulp.dest(distProd())) | |
.pipe($.size({title: 'HTML min'})) | |
}); | |
gulp.task('minifyJs', function(cb) { | |
runSequence('prodJsSize', '_minifyJs', 'prodJsSize', cb); | |
}); | |
//Didn't work to put size() in the pump, had to split it up | |
gulp.task('_minifyJs', function(cb) { | |
pump([ | |
gulp.src(distProd('/**/scripts/**/*.js')), | |
$.uglify({ compress: false }), | |
gulp.dest(distProd()) | |
], cb); | |
}); | |
gulp.task('prodJsSize', function() { | |
return gulp.src(distProd('/**/scripts/**/*.js')) | |
.pipe($.size({title: 'js'})); | |
}); | |
// Clean output directory | |
gulp.task('clean', function() { | |
return del(['.tmp', distDev(), distProd()]); | |
}); | |
// Build production files, the default task | |
gulp.task('default', ['clean'], function(cb) { | |
runSequence( | |
['copyDev', 'copyProd', 'styles'], | |
'images', | |
'vulcanize', | |
'minifyHtml', | |
'minifyJs', | |
cb); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment