There are probably a couple of little style issues here, but mostly this works. However, it doesn't seem to be watching changes to styles/*.scss files or new SCSS in that folder. Same thing with Javascript: scripts/*.js isn't updating on watch.
Could also stand to combine the SCSS and CSS without destroying the sourcemap, but that's not really important. Right now, just getting live updates to work would be nice.
const gulp = require('gulp')
const plumber = require('gulp-plumber')
const rename = require('gulp-rename')
const autoprefixer = require('gulp-autoprefixer')
const babel = require('gulp-babel')
const concat = require('gulp-concat')
const sass = require('gulp-sass')
const browserSync = require('browser-sync')
const clean = require('gulp-clean')
const sourcemaps = require('gulp-sourcemaps');
const uglify = require('gulp-uglify');
const minifycss = require('gulp-minify-css');
const gulpfn = require('gulp-fn')
const fs = require('fs');
const util = require('gulp-util');
let config = { production: !!util.env.production }
gulp.task('clean', function () {
return gulp.src('build', {read: false})
.pipe(clean())
.pipe(gulpfn(function (file) {
if (!fs.existsSync('build')){
fs.mkdirSync('build');
}
}));
});
gulp.task('browser-sync', function() {
browserSync({
server: {
baseDir: "./build"
}
});
});
gulp.task('bs-reload', function () {
browserSync.reload();
});
gulp.task('styles', function(){
gulp.src(['source/styles/**/*.scss'])
.pipe(plumber({
errorHandler: function (error) {
console.log(error.message);
this.emit('end');
}}))
.pipe(sourcemaps.init())
.pipe(sass())
.pipe(autoprefixer('last 2 versions'))
.pipe(gulp.dest('build/styles/'))
.pipe(rename({suffix: '.min'}))
.pipe(config.production ? minifyCSS() : util.noop())
.pipe(sourcemaps.write())
.pipe(gulp.dest('build/styles/'))
.pipe(browserSync.reload({stream:true}))
});
gulp.task('scripts', function(){
return gulp.src('source/scripts/**/*.js')
.pipe(plumber({
errorHandler: function (error) {
console.log(error.message);
this.emit('end');
}}))
.pipe(concat('main.js'))
.pipe(babel({presets: ['es2015', 'es2016']}))
.pipe(gulp.dest('build/scripts/'))
.pipe(rename({suffix: '.min'}))
.pipe(config.production ? uglify() : util.noop())
.pipe(gulp.dest('build/scripts/'))
.pipe(browserSync.reload({stream:true}))
});
gulp.task('external-scss', function(){
gulp.src(['external/font-awesome-4.7.0/scss/*.scss'])
.pipe(plumber({
errorHandler: function (error) {
console.log(error.message);
this.emit('end');
}}))
.pipe(sourcemaps.init())
.pipe(sass())
.pipe(autoprefixer('last 2 versions'))
.pipe(gulp.dest('build/external/font-awesome-4.7.0/css/'))
.pipe(rename({suffix: '.min'}))
.pipe(config.production ? minifyCSS() : util.noop())
.pipe(sourcemaps.write())
.pipe(gulp.dest('build/external/font-awesome-4.7.0/css/'))
.pipe(browserSync.reload({stream:true}))
});
const STATIC_STYLES = ['source/styles/**/*.css']
const STATIC_HYPERTEXT = ['source/**/*!template.html', 'source/**/*.html']
const STATIC_TEMPLATES = ['source/templates/*.template.html']
gulp.task('static-sources', function(){
gulp.src(STATIC_STYLES)
.pipe(gulp.dest('build/styles/'))
return gulp.src(STATIC_HYPERTEXT)
.pipe(gulp.dest('build/'))
});
gulp.task('default', ['browser-sync', 'styles', 'scripts', 'external-scss', 'static-sources'], function(){
gulp.src(['external/font-awesome-4.7.0/fonts/fontawesome-webfont.*']).pipe(gulp.dest('build/external/font-awesome-4.7.0/fonts'));
gulp.src( 'external/font-awesome-4.7.0/scss/*.scss', ['external-scss']);
gulp.src(['external/fccfavicons-master/*']).pipe(gulp.dest('build/external/fccfavicons-master'));
gulp.src(['node_modules/jquery/dist/*']).pipe(gulp.dest('build/external/jquery/dist'));
gulp.src(['node_modules/lodash/**/*']).pipe(gulp.dest('build/external/lodash'));
gulp.src(['node_modules/normalize.css/*.css']).pipe(gulp.dest('build/external/normalize.css'));
gulp.watch('source/styles/**/*.scss', ['styles']);
gulp.watch('source/scripts/**/*.js', ['scripts']);
let static_sources = []
static_sources.push(...STATIC_HYPERTEXT)
static_sources.push(...STATIC_STYLES)
gulp.watch(static_sources, ['static-sources']);
gulp.watch('*.html', ['bs-reload']);
});
You need to add return statements in your other tasks, besides scripts, to signal to gulp that they have all finished.
Related
I'm working on an old project which is using Gulp.
I had to update gulp v3 to v4 and this is how it looks like:
var gulp = require('gulp')
var sass = require('gulp-sass')
var sourcemaps = require('gulp-sourcemaps')
var browserSync = require('browser-sync').create()
var uglify = require('gulp-uglify')
var concat = require('gulp-concat')
var notify = require('gulp-notify')
var series = require('stream-series') // Helps to concat js files in desired order
// BrowserSync task
gulp.task('browserSync', function () {
browserSync.init({
server: {
baseDir: './'
}
})
})
// Style task
gulp.task('sass', function () {
return gulp.src('./src/sass/**/*.scss')
.pipe(sourcemaps.init())
.pipe(sass({
errLogToConsole: true,
outputStyle: 'compressed'
}))
.pipe(concat('style.min.css'))
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('./css'))
.pipe(browserSync.stream())
.pipe(notify({ message: 'Styles task complete' }))
})
// Script task
gulp.task('script', function () {
return series(
gulp.src('./src/js/vendor/**/*.js'),
gulp.src('./src/js/modules/**/*.js')
)
.pipe(concat('bundle.js'))
.pipe(uglify())
.pipe(gulp.dest('./js'))
.pipe(browserSync.stream())
.pipe(notify({ message: 'Script task complete' }))
})
// watch task
gulp.task('watch', gulp.series('sass', 'script', 'browserSync'), function () {
gulp.watch('./src/sass/**/*.scss', ['sass'])
gulp.watch('./src/js/**/*.js', ['script'])
gulp.watch('./index.html', browserSync.reload)
})
If I run gulp watch it compiles all sass and JS files and shows the page in browser correctly.
But after that it doesn't watch anymore and if I change any sass or JS files, those changes are not applied.
What is wrong with my gulp script?
I managed to fix it myself.
const gulp = require('gulp')
const sass = require('gulp-sass')
const sourcemaps = require('gulp-sourcemaps')
const browserSync = require('browser-sync').create()
const uglify = require('gulp-uglify')
const concat = require('gulp-concat')
const notify = require('gulp-notify')
const series = require('stream-series') // Helps to concat js files in desired order
// HTML task
function html () {
return gulp.src('./*.html')
.pipe(gulp.dest('./build'))
.pipe(browserSync.stream())
.pipe(notify({ message: 'HTML task complete' }))
}
// Style task
function style () {
return gulp.src('./src/sass/**/*.scss')
.pipe(sourcemaps.init())
.pipe(sass({
errLogToConsole: true,
outputStyle: 'compressed'
}))
.pipe(concat('style.min.css'))
// .pipe(cssmin())
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('./build/css'))
.pipe(browserSync.stream())
.pipe(notify({ message: 'Styles task complete' }))
}
// Script task
function script () {
// return gulp.src('./src/js/**/*.js')
return series( // concats js files in desired order
gulp.src('./src/js/vendor/**/*.js'),
gulp.src('./src/js/modules/**/*.js')
)
.pipe(concat('bundle.js'))
.pipe(uglify())
.pipe(gulp.dest('./build/js'))
.pipe(browserSync.stream())
.pipe(notify({ message: 'Script task complete' }))
}
function watch () {
html()
style()
script()
browserSync.init({
server: {
baseDir: './build'
}
})
gulp.watch('./*.html', html).on('change', browserSync.reload)
gulp.watch('./src/sass/**/*.scss', style).on('change', browserSync.reload)
gulp.watch('./src/js/**/*.js', script).on('change', browserSync.reload)
}
exports.html = html
exports.style = style
exports.script = script
exports.watch = watch
please let me know if you have any suggestion to improve my solution.
I'm studying GULP, and I want the page to update automatically when I modify the CSS.
const gulp = require("gulp");
const jshint = require("gulp-jshint");
const changed = require("gulp-changed");
const watch = require("gulp-watch");
const rename = require("gulp-rename");
const minifyCSS = require("gulp-uglifycss");
const sass = require("gulp-sass");
const cssImport = require("gulp-cssimport");
gulp.task("changed", function() {
return gulp
.src("src/css/*.css")
.pipe(changed("public/assets/css/"))
.pipe(cssImport())
.pipe(sass())
.pipe(minifyCSS())
.pipe(rename({ extname: ".min.css" }))
.pipe(gulp.dest("public/assets/css/"));
});
gulp.task("jshint", function() {
gulp
.src("src/css/*.css")
.pipe(jshint())
.pipe(jshint.reporter("default"));
});
gulp.task("watch", function() {
watch("src/css/*.css", ["changed"]);
});
gulp.task("default", ["jshint", "watch"]);
I'm trying to use "gulp-watch", but it's giving the error "Task function must be specified" in this code above.
The problem was that I was trying to watch the old version of GULP, not version 4.0.0.
So I changed my code to work, and it looked like this:
const gulp = require("gulp");
const rename = require("gulp-rename");
const minifyJS = require("gulp-uglify");
const minifyCSS = require("gulp-uglifycss");
const sass = require("gulp-sass");
const babel = require("gulp-babel");
const cssImport = require("gulp-cssimport");
gulp.task(
"base",
gulp.series(function() {
return gulp.src("src/templates/*.html").pipe(gulp.dest("public/"));
})
);
gulp.task(
"javascript",
gulp.series(function() {
return gulp
.src("src/js/*.js")
.pipe(babel({ presets: ["#babel/env"] }))
.pipe(minifyJS())
.pipe(rename({ extname: ".min.js" }))
.pipe(gulp.dest("public/assets/js/"));
})
);
gulp.task(
"css",
gulp.series(function() {
return gulp
.src("src/css/*.css")
.pipe(cssImport())
.pipe(sass())
.pipe(minifyCSS())
.pipe(rename({ extname: ".min.css" }))
.pipe(gulp.dest("public/assets/css/"));
})
);
gulp.task(
"watch",
gulp.series(function() {
gulp.watch("src/templates/*.html", gulp.parallel(["base"]));
gulp.watch("src/js/*.js", gulp.parallel(["javascript"]));
gulp.watch("src/css/*.css", gulp.parallel(["css"]));
})
);
gulp.task(
"default",
gulp.series(gulp.parallel("base", "javascript", "css", "watch"))
);
I have total followed the instructions of https://browsersync.io/docs/gulp
my gulpfile.js code:
let gulp = require('gulp');
let sass = require('gulp-sass');
let browserSync = require('browser-sync').create();
let reload = browserSync.reload;
gulp.task('server', ['css'], function() {
browserSync.init({
server: {
baseDir: './dist'
}
});
gulp.watch('src/sass/*.scss', ['css']);
gulp.watch("dist/*.html").on('change', reload);
gulp.watch("dist/sass/*.css").on('change', reload);
});
// 编译Sass
gulp.task('css', function() { // 任务名
return gulp.src('src/sass/a.scss') // 目标 sass 文件
.pipe(sass()) // sass -> css
.pipe(gulp.dest('dist/sass/')) // 目标目录
// .pipe(reload({stream: true}))
.pipe(browserSync.stream());
});
gulp.task('default', ['server']);
when I update the sass file, it seems that the css file will be updated immediately, but the broswer cannot reload.
and the command line show:
It seem that the cli cannot connect to the broswer?
===
The problem is solved,my html file does not have a body tag,see https://github.com/BrowserSync/browser-sync/issues/392#issuecomment-245380582
I post you my GULP FILE ... as you can see i use the
gulp-webserver
it only need to put reload:true to work as you aspect:
"use strict";
var gulp = require("gulp"),
concat = require("gulp-concat"),
cssmin = require("gulp-cssmin"),
htmlmin = require("gulp-htmlmin"),
uglify = require("gulp-uglify"),
merge = require("merge-stream"),
del = require("del"),
bundleconfig = require("./bundleconfig.json"); // make sure bundleconfig.json doesn't contain any comments
var livereload = require('gulp-livereload');
var changed = require('gulp-changed');
var stripDebug = require('gulp-strip-debug');
var minifyHTML = require('gulp-minify-html');
var autoprefix = require('gulp-autoprefixer');
var minifyCSS = require('gulp-minify-css');
var rename = require('gulp-rename');
var jshint = require('gulp-jshint');
var shell = require('gulp-shell');
var sass = require('gulp-sass');
var preprocess = require('gulp-preprocess');
var ngAnnotate = require('gulp-ng-annotate');
var templateCache = require('gulp-angular-templatecache');
var webserver = require('gulp-webserver'); //<-- YOU HAVE TO INSTALL IT MAYBE WITH NPM (npm install --save -dev gulp-webserver)
// THESE IS THE TASK FOR WEB SERVER AND RELOAD
gulp.task('webserver', function () {
gulp.src('')
.pipe(webserver({
host: 'localhost', // <-- IF YOU PUT YOUR IP .. YOU CAN WATCH IT FROM OTHER devices
port: 80,
livereload: true,
directoryListing: true,
open: true,
fallback: 'index-dev.html'
}));
});
// Task to process Index page with different include ile based on enviroment
gulp.task('html-dev', function () {
return gulp.src('Index.html')
.pipe(preprocess({ context: { NODE_ENV: 'dev', DEBUG: true } })) //To set environment variables in-line
.pipe(rename({ suffix: '-dev' }))
.pipe(gulp.dest(''));
});
// Task to process Index page with different include ile based on enviroment
gulp.task('html-prod', function () {
return gulp.src('Index.html')
.pipe(preprocess({ context: { NODE_ENV: 'prod', DEBUG: false } })) //To set environment variables in-line
.pipe(rename({ suffix: '-prod' }))
.pipe(gulp.dest(''));
});
// CSS concat, auto-prefix, minify, then rename output file
gulp.task('minify-css', function () {
var cssPath = { cssSrc: ['./app/view/assets/content/css/styles-default.css', './app/view/assets/content/css/styles-theme-1.css', './app/view/assets/content/css/styles-theme-2.css', '!*.min.css', '!/**/*.min.css'], cssDest: './app_build/content_build/css' };
return gulp.src(cssPath.cssSrc)
// .pipe(shell(['attrib C:/_goocity/Goocity/Goocity.Web/app_build/content_build/css/*.css -r']))
// .pipe(concat('styles.css'))
.pipe(autoprefix('last 2 versions'))
.pipe(minifyCSS())
.pipe(rename({ suffix: '.min' }))
.pipe(gulp.dest(cssPath.cssDest));
});
// Lint Task
gulp.task('lint', function () {
var jsPath = { jsSrc: ['./app/app.js', './app/controller/*.js', './app/view/utils/directives/*.js', './app/model/services/*.js'] };
return gulp.src(jsPath.jsSrc)
.pipe(jshint())
.pipe(jshint.reporter('default'));
});
//SASS task
gulp.task('sass', function () {
var sassPath = {
cssSrc: ['./app/view/assets/content/css/*.scss'], cssDest: './app/view/assets/content/css'
};
gulp.src(sassPath.cssSrc)
.pipe(shell(['attrib C:/_goocity/Goocity/Goocity.Web/app/view/assets/content/css/*.css -r']))
.pipe(sass().on('error', sass.logError))
.pipe(gulp.dest(sassPath.cssDest))
.pipe(livereload());
});
gulp.task('cache', function () {
return gulp.src('./app/view/template/**/*.html')
.pipe(templateCache('templates.js', { module: 'Goocity', root: '/app/view/template' }))
.pipe(gulp.dest('./app/view/template'))
.pipe(livereload());
});
gulp.task('cache-directives', function () {
return gulp.src('./app/view/utils/directives/template/**/*.html')
.pipe(templateCache('templates.js', { module: 'Goocity', root: '/app/view/utils/directives/template' }))
.pipe(gulp.dest('./app/view/utils/directives/template'))
.pipe(livereload());
});
gulp.task("min:js", function () {
var tasks = getBundles(".js").map(function (bundle) {
return gulp.src(bundle.inputFiles, { base: "." })
.pipe(ngAnnotate({
remove: true,
add: true,
single_quotes: false
}))
.pipe(concat(bundle.outputFileName))
.pipe(uglify())
.pipe(gulp.dest("."));
});
return merge(tasks);
});
//gulp.task("min:css", function () {
// var tasks = getBundles(".css").map(function (bundle) {
// return gulp.src(bundle.inputFiles, { base: "." })
// .pipe(concat(bundle.outputFileName))
// .pipe(cssmin())
// .pipe(gulp.dest("."));
// });
// return merge(tasks);
//});
gulp.task("min:html", function () {
var tasks = getBundles(".html").map(function (bundle) {
return gulp.src(bundle.inputFiles, { base: "." })
.pipe(concat(bundle.outputFileName))
.pipe(htmlmin({ collapseWhitespace: true, minifyCSS: true, minifyJS: true }))
.pipe(gulp.dest("."));
//.pipe(livereload());
});
return merge(tasks);
});
gulp.task("clean", function () {
var files = bundleconfig.map(function (bundle) {
return bundle.outputFileName;
});
return del(files);
});
gulp.task("min", ["clean", "min:js", "minify-css", 'html-dev', "cache", "cache-directives"]);
gulp.task("default", ["watch", "webserver"]);
gulp.task("watch", function () {
livereload.listen();
// watch for JS error
gulp.watch(['./app/app.js', './app/controllers/*.js', './app/directives/*.js', './app/services/*.js'], ['lint']);
// min js
getBundles(".js").forEach(function (bundle) {
gulp.watch(bundle.inputFiles, ["min:js"]);
});
//watch for SASS changes
gulp.watch('./app/view/assets/content/css/scss/*.scss', ['sass']);
gulp.watch('./app/view/assets/content/css/scss/settings/*.scss', ['sass']);
gulp.watch('./app/view/assets/lib/bower/lumx/dist/scss/base/*.scss', ['sass']);
gulp.watch('./app/view/assets/lib/bower/lumx/dist/scss/modules/*.scss', ['sass']);
gulp.watch('./app/view/assets/content/css/*.scss', ['sass']);
gulp.watch('./app/view/assets/content/css/Hover-master/scss/*.scss', ['sass']);
//gulp.watch('./app/view/assets/content/css/*.css', ['minify-css']);
//watch for PREPROCCESS x PROD
gulp.watch('Index.html', ['html-prod']);
//watch for PREPROCCESS x DEV
gulp.watch('Index.html', ['html-dev']);
//watch for TEMPLATE CACHE
gulp.watch('./app/view/template/**/*.html', ['cache']);
gulp.watch('./app/view/utils/directives/template/**/*.html', ['cache-directives']);
});
function getBundles(extension) {
return bundleconfig.filter(function (bundle) {
return new RegExp(extension).test(bundle.outputFileName);
});
}
The problem is solved,my html file does not have a body tag,see https://github.com/BrowserSync/browser-sync/issues/392#issuecomment-245380582
My gulp setup with browsersync seems to have a problem with detecting changes for SCSS files. It works perfectly fine for SLIM with reload and detection. There's no error with the gulp. The only problem is the browsersync fail to detect changes in SCSS file and reload or inject changes.
var gulp = require('gulp'),
sass = require('gulp-ruby-sass'),
slim = require('gulp-slim'),
autoprefix = require('gulp-autoprefixer'),
notify = require('gulp-notify'),
bower = require('gulp-bower'),
image = require('gulp-image'),
concat = require('gulp-concat'),
uglify = require('gulp-uglify'),
rename = require('gulp-rename'),
browserSync = require('browser-sync').create();
var config = {
sassPath: './source/sass',
slimPath: './source/slim',
bowerDir: './bower_components',
jsPath: './source/js'
}
gulp.task('bower', function() {
return bower()
.pipe(gulp.dest(config.bowerDir))
});
gulp.task('js', function() {
return gulp.src('./source/js/**/*.js')
.pipe(concat('script.js'))
.pipe(gulp.dest('./dist/js'));
});
gulp.task('icons', function() {
return gulp.src(config.bowerDir + '/font-awesome/fonts/**.*')
.pipe(gulp.dest('./dist/fonts'));
});
gulp.task('image', function () {
gulp.src('./source/images/*')
.pipe(image())
.pipe(gulp.dest('./dist/images'));
} );
gulp.task('css', function() {
return sass('./source/scss/**/*.scss', {
style: 'compressed',
loadPath: [
'./source/scss',
config.bowerDir + '/bootstrap/scss',
config.bowerDir + '/font-awesome/scss',
]
}).on("error", notify.onError(function (error) {
return "Error: " + error.message;
}))
.pipe(gulp.dest('./dist/css'))
.pipe(browserSync.stream());
});
gulp.task('html', function() {
gulp.src('./source/slim/**/*.slim')
.pipe(slim({
pretty: true
}))
.pipe(gulp.dest('./dist/'));
});
gulp.task('serve', ['css', 'html', 'js'], function() {
browserSync.init({
injectChanges: true,
server: "./dist"
});
gulp.watch(config.sassPath + '/**/*.scss', ['css']);
gulp.watch(config.slimPath + '/**/*.slim', ['html']).on('change', browserSync.reload);;
});
gulp.task('default', ['bower','js', 'icons', 'css', 'html', 'image', 'serve']);
gulp.task('build',['bower', 'js', 'icons', 'css', 'html', 'image'] );
This might not be the answer you are looking for but its the work around that I've been using since I ran into a similar issue using browserSync.stream() not firing off consistently.
'use strict';
var gulp = require('gulp'),
browserSync = require('browser-sync').create();
gulp.task('serve',function(){
browserSync.init({
server: {
baseDir: './dev/'
},
files: ['./dev/**/*.html',
'./dev/css/**/*.css',
'./dev/js/**/*.js',
'./dev/img/**/*.*'],
notify: false
});
});
As you can see, this uses browserSyncs own watch task as opposed to piping it through the entire build process, which can have its own advantages by making the serve task easily abstracted and pulled into another project.
Hope this helps.
I got this Gulp file where I'm running various tasks.
I would like to delete the contents of my build directory before running any other tasks but can't work out how to do this.
I've tried lots of various ways I found when searching for a solution to this but can't get it to work.
Here is my Gulp file:
// List required plugins
var gulp = require('gulp'),
del = require('del'),
uglify = require('gulp-uglify'),
postcss = require('gulp-postcss'),
lost = require('lost'),
nano = require('gulp-cssnano'),
sourcemaps = require('gulp-sourcemaps'),
autoprefixer = require('autoprefixer'),
mqpacker = require('css-mqpacker'),
mixins = require('postcss-mixins'), // Must go before other plugins in the PostCSS task
nested = require('postcss-nested'),
vars = require('postcss-simple-vars'),
partials = require('postcss-import'),
plumber = require('gulp-plumber'),
gutil = require('gulp-util'),
imagemin = require('gulp-imagemin'),
pngquant = require('imagemin-pngquant'),
browserSync = require('browser-sync').create();
// Config - Paths and other config
var src = {
cssDir: 'src/css/**/*.css',
jsDir: 'src/js/**/*.js',
imgDir: 'src/img/*',
svgDir: 'src/img/svg/**/*.svg',
htmlDir: 'src/**/*.html',
srcDir: 'src/',
},
build = {
buildDir: 'build/',
cssDir: 'build/assets/css/',
htmlDir: 'build/**/*.html',
jsDir: 'build/assets/js/',
imgDir: 'build/assets/img/',
},
options = {
autoprefix: { browsers: ['last 2 versions'] },
imagemin: { optimizationLevel: 7, progressive: true, interlaced: true, svgoPlugins: [ {removeViewBox: false}, {cleanupIDs: false} ], use: [pngquant()] },
port: '80',
};
// Error handling function
// Thanks to https://cameronspear.com/blog/how-to-handle-gulp-watch-errors-with-plumber/
var onError = function(err) {
gutil.beep();
console.log(err);
this.emit('end');
}
// Clean task
gulp.task('clean:build', function () {
return del([
build.buildDir,
]);
});
// Copy HTML to Build
gulp.task('html', function () {
return gulp.src(src.htmlDir)
.pipe(plumber({
errorHandler: onError
}))
.pipe(gulp.dest(build.buildDir))
.pipe(browserSync.stream());
});
// BrowserSync Server + watching files for changes
gulp.task('server', function() {
browserSync.init({
server: {
baseDir: build.buildDir
},
port: options.port,
});
});
// PostCSS Task
gulp.task('css', function () {
var processors = [
partials(),
lost(),
autoprefixer({ browsers: ['last 2 version'] }),
mixins(),
nested(),
vars(),
mqpacker(), // Make sure mqpacker is last!
];
return gulp.src(src.cssDir)
.pipe(plumber({
errorHandler: onError
}))
.pipe(sourcemaps.init())
.pipe(postcss(processors))
//.pipe(nano()) - Disable minification during development
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(build.cssDir))
.pipe(browserSync.stream());
});
// JS Task
gulp.task('compressJS', function() {
return gulp.src(src.jsDir)
.pipe(plumber({
errorHandler: onError
}))
.pipe(uglify())
.pipe(gulp.dest(build.jsDir))
.pipe(browserSync.stream());
});
// Image Task
gulp.task('imagemin', function() {
return gulp.src(src.imgDir)
.pipe(plumber({
errorHandler: onError
}))
.pipe(imagemin(
options.imagemin
))
.pipe(gulp.dest(build.imgDir))
.pipe(browserSync.stream());
});
gulp.task('watch', function () {
gulp.watch(src.cssDir, ['css']);
gulp.watch(src.jsDir, ['compressJS']);
gulp.watch(src.htmlDir, ['html']);
gulp.watch(src.imgDir, ['imagemin']);
});
gulp.task('default', ['html', 'imagemin', 'compressJS', 'server', 'watch']);
How should I do to run the task clean:build before any other tasks but only run it once. Or is there a better way of cleaning out files before running a build?
Thanks!!
Have your tasks depend on your clean task:
gulp.task('html', ['clean:build'], function() { ... })
this is how i do it.
var del = require('del') ;
gulp.task('delete-build-folder', function(cb) {
return del([config.buildDir], cb);
});
gulp.task('default', ['delete-build-folder'], function() {
console.log('building.....');
gulp.start('copy-html', 'copy-html-views');
});