Skip to content

Instantly share code, notes, and snippets.

@antwon
Last active December 5, 2015 06:32
Show Gist options
  • Save antwon/496cff2ce0d5897d7c7d to your computer and use it in GitHub Desktop.
Save antwon/496cff2ce0d5897d7c7d to your computer and use it in GitHub Desktop.
(Gulp 3) Basic front-end starter kit. Compiles sass with sourcemaps, combines js, prefix-free styles, live reloads browser
// ----- Gulp ----- //
// Compiles sass with sourcemaps, combines js, auto prefixes styles, live reloads browser
// $ cd this-project
// $ npm install
// $ gulp
var gulp = require('gulp');
var autoprefixer = require('gulp-autoprefixer'), // No more -vendor-prefixes- :)
autoprefixerOptions = {
browsers: ['last 2 versions', '> 5%', 'Firefox ESR']
};
var concat = require('gulp-concat'); // Combine js files into one
var livereload = require('gulp-livereload'); // Inject stylesjs into browser without refreshing
var rename = require('gulp-rename'); // Rename the js file after its combined
var sass = require('gulp-sass'),
sassOptions = {
errLogToConsole: true,
outputStyle: 'expanded'
};
var sourcemaps = require('gulp-sourcemaps'); // Sourcemaps to show where lines of sass are from compiled css
var uglify = require('gulp-uglify'); // Uglify the js and save a bunch on bytez
var webserver = require('gulp-webserver');
var opn = require('opn');
// Paths & Directories
var paths = {
js: ['./static/public/js/build/libs/*.js', './static/public/js/build/libs/*/*.js', './static/public/js/build/scripts/*.js'],
jsBuild: './static/public/js/', // this is where the minified and concat'd project js build file will go
styles: ['./static/public/css/*.scss', './static/public/css/partials/*.scss'], // watch these directories
stylesBuild: './static/public/css/' // this is where the minified, compiled css will go
};
// Server host and port
var server = {
host: 'localhost',
port: '8001'
}
// ----- Scripts ----- //
// Concat, Rename, Uglify
gulp.task('build-js', function() {
gulp.src(paths.js)
.pipe(concat('all.js'))
.pipe(gulp.dest(paths.jsBuild))
.pipe(rename('all.min.js'))
.pipe(uglify())
.on('error', logError)
.pipe(gulp.dest(paths.jsBuild))
.pipe(livereload({ start: true }))
});
// ----- Styles ----- //
// Compile css from sass
gulp.task('build-styles', function () {
gulp.src(paths.styles)
.pipe(sourcemaps.init())
// Run Sass on those files
.pipe(sass(sassOptions))
// We need those sass line numbers
.pipe(sourcemaps.write())
.on('error', logError)
// Autoprefixer :)
.pipe(autoprefixer(autoprefixerOptions))
// Write the resulting CSS in the output folder
.pipe(gulp.dest(paths.stylesBuild))
.pipe(livereload({ start: true }))
});
// ----- Watch Files ----- //
// Watch Files For Changes (Scripts and Styles)
gulp.task('watch', function() {
livereload.listen()
gulp.watch(paths.js, ['build-js'])
gulp.watch(paths.styles, ['build-styles'])
.on('change', function(event) {
console.log('File ' + event.path + ' was ' + event.type + ', running tasks...')
})
});
// ----- Server stuff ----- //
// Feel free to remove if using express or something else
gulp.task('webserver', function() {
gulp.src( '.' )
.pipe(webserver({
host: server.host,
port: server.port,
livereload: true,
directoryListing: false
}));
});
gulp.task('openbrowser', function() {
opn( 'http://' + server.host + ':' + server.port );
});
// ----- Default Task ----- //
// Run from command line with: gulp
gulp.task('default', ['build-js', 'webserver', 'build-styles', 'watch', 'openbrowser']);
// ----- Errors ----- //
var logError = function(error) {
console.log(error.toString());
this.emit('end');
};
{
"name": "starter-kit",
"version": "0.1.0",
"description": "Basic front-end starter kit, compiles sass, live reload browser, auto-prefixer for vendor prefixes, concats and minify js",
"main": "gulpfile.js",
"scripts": {
"test": "console.log('yo son')"
},
"author": "antwon",
"license": "ISC",
"devDependencies": {
"gulp": "^3.9.0",
"gulp-autoprefixer": "^3.1.0",
"gulp-concat": "^2.6.0",
"gulp-livereload": "^3.8.1",
"gulp-rename": "^1.2.2",
"gulp-sass": "^2.1.0",
"gulp-sourcemaps": "^1.6.0",
"gulp-uglify": "^1.5.1",
"gulp-webserver": "^0.9.1",
"opn": "^3.0.3"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment