Skip to content

Instantly share code, notes, and snippets.

@jerrylau91
Created October 13, 2015 01:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jerrylau91/06af7eddb25380ebeb21 to your computer and use it in GitHub Desktop.
Save jerrylau91/06af7eddb25380ebeb21 to your computer and use it in GitHub Desktop.
A personal gulp configuration file
// require modules
var fs = require('fs');
var path = require('path');
var merge = require('merge-stream');
var gulp = require('gulp');
var sass = require('gulp-sass');
var autoprefixer = require('gulp-autoprefixer');
var sourcemaps = require('gulp-sourcemaps');
var concat = require('gulp-concat'); // 合并js
var uglify = require("gulp-uglify");
var rename = require('gulp-rename');
var notify = require('gulp-notify');
// define variables
var sassPath = './resources/sass/**/*.scss',
cssPath = './resources/css/',
jsPath = 'resources/js/*.js',
jsOut = 'assets/js/';
var sassOptions = {
errLogToConsole: true,
outputStyle: 'compressed'
};
function getFolders(dir) {
return fs.readdirSync(dir)
.filter(function(file){
return fs.statSync(path.join(dir,file)).isDirectory();
});
}
// Sass
gulp.task('sass', function(){
gulp.src(sassPath)
.pipe(sourcemaps.init())
.pipe(sass(sassOptions).on('error', sass.logError))
.pipe(autoprefixer('last 2 version'))
.pipe(rename('style.css'))
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest(cssPath));
});
// JS
gulp.task('js', function(){
var folders = getFolders(jsPath);
var tasks = folders.map(function(folder){
return gulp.src(path.join(jsPath, folder, '/**/*.js'))
.pipe(concat(folder + '.js'))
.pipe(gulp.dest(jsOut))
.pipe(uglify())
.pipe(rename(folder + '.min.js'))
.pipe(gulp.dest(jsOut));
});
var root = gulp.src(path.join(jsPath, '/*.js'))
.pipe(concat('main.js'))
.pipe(gulp.dest(jsOut))
.pipe(uglify())
.pipe(rename({suffix: '.min'}))
.pipe(gulp.dest(jsOut));
return merge(tasks,root);
// gulp.src(jsPath)
// .pipe(uglify())
// .pipe(rename({suffix:'.min'}))
// .pipe(gulp.dest(jsOut));
});
// Watch Task
gulp.task('default',function(){
gulp.watch(sassPath, ['sass']);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment