generator-markup-source
Version:
This is generator using yeoman for markup source
102 lines (96 loc) • 2.92 kB
JavaScript
const gulp = require('gulp');
const sass = require('gulp-sass');
const browserSync = require('browser-sync').create();
const useref = require('gulp-useref');
const uglify = require('gulp-uglify');
const gulpIf = require('gulp-if');
const imagemin = require('gulp-imagemin');
const cache = require('gulp-cache');
const del = require('del');
const runSequence = require('run-sequence');
const autoprefixer = require('autoprefixer');
const cssnano = require('cssnano');
const changed = require('gulp-changed');
const postcss = require('gulp-postcss');
const uncss = require('postcss-uncss');
const modernizr = require('gulp-modernizr');
const assemble = require('assemble');
const app = assemble();
const extname = require('gulp-extname');
// compile sass and minify css
gulp.task('css', ['sass'], function () {
var plugins = [
autoprefixer({browsers: ['last 1 version']}),
cssnano()
];
return gulp.src('../common/css/*.css')
.pipe(postcss(plugins))
.pipe(gulp.dest('../common/css'));
});
gulp.task('sass', () => {
return gulp.src('_scss/style.scss')
.pipe(changed('../common/css', {hasChanged : changed.compareContents}))
.pipe(sass())
.pipe(gulp.dest('../common/css/'))
.pipe(browserSync.reload({
stream : true
}))
});
gulp.task('browserSync', function() {
browserSync.init({
server: {
baseDir: '../',
},
startPath : '/mock'
})
});
gulp.task('load', function(cb) {
app.partials('_templates/_partials/*.hbs');
app.layouts('_templates/_layouts/*.hbs');
app.pages('_templates/_pages/*.hbs');
app.data('_templates/_data/*.{json,yml}');
app.option('layout', 'default');
cb();
});
gulp.task('assemble', ['load'], function() {
return app.toStream('pages')
.pipe(app.renderFile())
.pipe(extname())
.pipe(app.dest('../mock'));
});
gulp.task('useref', ['assemble'], function(){
return gulp.src('../mock/**/*.html')
.pipe(useref())
.pipe(gulpIf('*.js', uglify()))
.pipe(gulp.dest('../mock'))
});
gulp.task('images', function(){
return gulp.src('../common/images/**/*.+(png|jpg|gif|svg)')
.pipe(cache(imagemin({
interlaced: true
})))
.pipe(gulp.dest('../common/images'))
});
gulp.task('clean:dist', function() {
return del.sync('../mock/');
});
gulp.task('modernizr', function() {
return gulp.src('../common/js/*.js')
.pipe(modernizr('../common/js/modernizr-custom.js'));
});
gulp.task('watch', function (){
gulp.watch('_scss/**/*.scss', ['sass']);
gulp.watch('_templates/**/*.+(hbs|json)', ['assemble']);
gulp.watch('../mock/**/*.html').on('change', browserSync.reload);
gulp.watch('../common/css/*.css').on('change', browserSync.reload);
gulp.watch('../common/js/**/*.js').on('change', browserSync.reload);
});
gulp.task('dev', function (callback) {
runSequence('watch',
['sass', 'assemble', 'browserSync'],
callback
)
});
gulp.task('prod', function(){
return runSequence ('useref', ['css']);
});