I make this gulp tasks: sass, serve, watch, but I'm trying to run gulp sass when the gulp watch into the gulp serve is activate but it's not work.
I need when I do a change into the sass file, this compile and show in realtime in the browser.
Any help ?
var gulp = require('gulp'),
browserSync = require('browser-sync'),
postcss = require('gulp-postcss'),
autoprefixer = require('autoprefixer'),
sass = require('gulp-sass');
gulp.task('serve', function() {
browserSync.init({
notify: false,
port: 3005,
server: {
baseDir: ["app"],
routes: {
'/bower_components': 'bower_components'
}
}
})
gulp.watch(['app/**/*.*']).on('change', browserSync.reload);
});
gulp.task('sass', function() {
var processors = [
autoprefixer({ browsers: ['last 2 versions']})
];
return gulp.src('./app/sass/*.scss')
.pipe(sass().on('error',sass.logError))
.pipe(postcss(processors))
.pipe(gulp.dest('./app/css'));
});
gulp.task('watch', ['sass'], function() {
gulp.watch('./app/sass/*.scss', ['sass']);
});
gulp.task('default', ['sass']);
Adding .pipe(browserSync.reload({stream: true})) to your sass task pipe should make browsersync reload only the compiled css.
Here's an example of your gulp task
gulp.task('sass', function() {
var processors = [
autoprefixer({ browsers: ['last 2 versions']})
];
return gulp.src('./app/sass/*.scss')
.pipe(sass().on('error',sass.logError))
.pipe(postcss(processors))
.pipe(gulp.dest('./app/css'))
.pipe(browserSync.reload({stream: true}));
});
Related
I am using gulp-nunjucks-render for HTML templating.
The problem is, it is taking more than 3-4 minutes to compile. Even though I have only seven nunjucks templates.
'use strict';
var gulp = require('gulp'),
autoprefixer = require('gulp-autoprefixer'),
sass = require('gulp-sass'),
minifyCSS = require('gulp-minify-css'),
uglify = require('gulp-uglify'),
concat = require('gulp-concat'),
connect = require('gulp-connect'),
sourcemaps = require('gulp-sourcemaps'),
nunjucksRender = require('gulp-nunjucks-render'),
browserSync = require('browser-sync');
gulp.task('html', function(){
gulp.src('build/*.html')
.pipe(browserSync.reload({stream: true}))
});
gulp.task('nunjucksRender', function(){
gulp.src('sources/html/**/*.+(html|nunjucks)')
.pipe(nunjucksRender({
path: ['sources/html/']
}))
.pipe(gulp.dest('build/'))
.pipe(browserSync.reload({stream: true}))
});
gulp.task('css', function(){
gulp.src('sources/scss/main.scss')
.pipe(sourcemaps.init())
.pipe(sass({outputStyle: 'expanded'})
.on('error', sass.logError)
)
.pipe(autoprefixer({
browsers: ['last 50 versions']
}))
// .pipe(minifyCSS())
.pipe(sourcemaps.write(''))
.pipe(gulp.dest('build/css'))
.pipe(browserSync.reload({stream: true}))
});
gulp.task('js', function() {
gulp.src('sources/**/*.js',)
// concat pulls all our files together before minifying them
.pipe( concat('main.min.js') )
// .pipe(uglify())
.pipe(gulp.dest('build/js'))
.pipe(browserSync.reload({stream: true}))
});
gulp.task('watch', function(){
gulp.watch('build/*.html', ['html', 'nunjucksRender']);
gulp.watch('sources/**/*.nunjucks', ['nunjucksRender']);
gulp.watch('sources/scss/**/*.scss', ['css']);
gulp.watch('sources/js/**/*.js', ['js']);
});
gulp.task('browserSync', function(){
browserSync({
server: {
baseDir: 'build'
}
});
});
gulp.task('default', ['css', 'html']);
gulp.task('start', ['browserSync', 'watch']);
Here is how it looks like when compilation is finished
Is there any gulp plugin to reduce compile time or compile only those files which are changed?
here is the github repo in case you need to see full file structure.
Ah.. I was watching .html and .nunjucks files for changes.. so when .nunjucks file compiles and creates .html file then again .html related task runs.
Other answers were not helpful. I'm using Gulp 4 on Windows 10.
I get the following error when running 'gulp serve'
The following tasks did not complete: serve, sass
[18:48:51] Did you forget to signal async completion?
My gulpfile.js
var gulp = require('gulp');
var sass = require('gulp-sass');
var browserSync = require('browser-sync');
var reload = browserSync.reload;
gulp.task('sass', function() {
return sass('./public_html/lib/scss/main.scss')
.pipe(gulp.dest('./public_html/css'))
.pipe(reload({ stream:true }));
});
gulp.task('serve', gulp.series('sass', function() {
browserSync({
server: {
baseDir: 'public_html'
}
});
gulp.watch('lib/scss/*.scss', gulp.series('sass'));
}));
Try this instead:
gulp.task('sass', function() {
return gulp.src('./public_html/lib/sass/main.scss')
.pipe(sass()).on("error", sass.logError))
.pipe(gulp.dest('./public_html/css'))
.pipe(reload({ stream:true }));
});
Note you need gulp.src, then a call to sass.
I've been using Gulp and trying to build to some process and one is BrowserSync. Thing is I want it to live reload on PHP files but it seems to only want to render html files. How can I get around this? My gulp file is here:
// gulpfile.js
var gulp = require('gulp');
var sass = require('gulp-sass');
var cleanCSS = require('gulp-clean-css');
var uglify = require('gulp-uglify');
var flatten = require('gulp-flatten');
var browserSync = require('browser-sync').create();
// Live reload (HTML Only)
gulp.task('browser-sync', function() {
browserSync.init({
server: {
baseDir: "app/build/"
}
});
gulp.watch("app/scss/*.scss", ['minify-css']);
});
// Sass Compile
gulp.task('sass', function(){
return gulp.src('app/src/scss/**/*.scss')
.pipe(sass()) // Converts Sass to CSS with gulp-sass
.pipe(gulp.dest('app/build/css/'))
.pipe(browserSync.stream())
});
// Minify CSS after Scss Compile
gulp.task('minify-css', ['sass'], function() {
return gulp.src('app/build/css/*.css')
.pipe(gulp.dest('app/build/css/'))
});
// Compress JS
gulp.task('minify-js', ['flatten'], function() {
return gulp.src('app/src/js/*.js')
.pipe(uglify())
.pipe(gulp.dest('app/build/js/'))
});
gulp.task('flatten', function() {
gulp.src('bower_components/**/')
.pipe(flatten({ includeParents: [1, 1]}))
.pipe(gulp.dest('app/build/js/'))
});
gulp.task('watch', ['browser-sync'], function (){
gulp.watch('app/src/scss/*.scss', ['minify-css']);
gulp.watch('app/src/js/*.js', ['minify-js', browserSync.reload])
})
gulp.task('default', ['watch']);
Try adding these lines:
// Reloads the page
gulp.task('php', browserSync.reload);
gulp.task('watch', ['browser-sync'], function (){
gulp.watch('app/src/scss/*.scss', ['minify-css']);
gulp.watch('app/src/js/*.js', ['minify-js', browserSync.reload]);
// Watches for .php file changes
gulp.watch("./*.php", ['php']);
});
gulp.task('default', ['watch']);
install AppServ
Put your project to C:\AppServ\www directory
Change
gulp.task('browser-sync', function() {
browserSync.init({
server: {
baseDir: "app/build/"
}
});
to
gulp.task('browser-sync', function() {
browserSync({
proxy: "http://localhost/ PUT HERE NAME OF YOUR PROJECT
FOLDER/app/build/%NAME OF YOUR PHP FILE%.php"
});
For anyone trying to minify-html and gulp-sass and browser-sync below is my fixed gulpfile.js
I hope it helps someone as gulp can be difficult for new folks like myself.
'use strict';
var gulp = require('gulp');
var minifyHTML = require('gulp-minify-html');
var sass = require('gulp-sass');
var browsersync = require('browser-sync');
// BrowserSync proxy
gulp.task('browser-sync', function() {
browsersync({
proxy: "localhost:8888"
});
});
// BrowserSync reload all Browsers
gulp.task('browsersync-reload', function () {
browsersync.reload();
});
gulp.task('minify-html', function() {
return gulp.src('src/*.html')
.pipe(minifyHTML({ empty: true }))
.pipe(gulp.dest('dist'))
.pipe(browsersync.reload({ stream:true }));
});
gulp.task('sass', function () {
gulp.src('./src/sass/**/*.scss')
.pipe(sass().on('error', sass.logError))
.pipe(gulp.dest('./dist/css'))
.pipe(browsersync.reload({ stream:true }));
});
// watch task
gulp.task('watch', ['browser-sync'], function () {
gulp.watch('./src/**/*.html', ['minify-html']);
gulp.watch('./src/sass/**/*.scss', ['sass']);
});
gulp.task('default', [ 'minify-html', 'sass', 'watch']);
Here's a quick stab I took at refactoring your gulpfile. All I've done is removed some duplicated code and split up the tasks in a more logical way. The main thing I'd suggest is that you make sure each task has a single purpose, and that you use the gulp dependency system to combine them as needed.
For future reference I'd ask the Code Review StackExchange as they are more likely to help with refactoring and code reviews.
Sorry for any mistakes, as I don't have an environment to test.
// include gulp
var gulp = require('gulp');
// include plug-ins
var minifyHTML = require('gulp-minify-html');
var minifyCSS = require('gulp-minify-css');
var changed = require('gulp-changed');
var concat = require('gulp-concat');
var browserSync = require('browser-sync').create();
var paths = {
html: { src: './src/*.html', dest: './' },
css: { src: 'src/css/*.css', dest: 'css' },
};
// minify new or changed HTML pages
gulp.task('html:build', function() {
gulp.src(paths.html.src)
.pipe(changed(htmlDst))
.pipe(minifyHTML())
.pipe(gulp.dest(paths.html.dest));
});
// CSS concat, auto-prefix and minify
gulp.task('css:build', function() {
gulp.src(paths.css.src)
.pipe(concat('styles.css'))
.pipe(minifyCSS())
.pipe(gulp.dest(paths.html.dest));
});
gulp.task('html:watch', function() {
gulp.watch(paths.html.src, ['html:build']);
});
gulp.task('css:watch', function() {
gulp.watch(paths.css.src, ['css:build']);
});
gulp.task('serve', function() {
browserSync.init({
server: "./"
});
gulp.watch(["css/*.css", "*.html"]).on('change', browserSync.reload);
});
// default gulp tasks
gulp.task('build', ['html:build', 'css:build']);
gulp.task('watch', ['html:watch', 'css:watch']);
gulp.task('default', ['build', 'watch', 'serve']);
I've just started to learn gulp and I'm trying to use gulp-connect https://www.npmjs.com/package/gulp-connect.
This is my files structure
And it's my gulpfile.js file
var gulp = require('gulp'),
concatCss = require('gulp-concat-css'),
minifyCss = require('gulp-minify-css'),
rename = require('gulp-rename'),
notify = require('gulp-notify'),
autoprefixer = require('gulp-autoprefixer'),
livereload = require('gulp-livereload'),
connect = require('gulp-connect');
// server connect
gulp.task('connect', function() {
connect.server({
root: [__dirname],
livereload: true
});
});
// css
gulp.task('css', function () {
return gulp.src('css/*.css')
/*.pipe(notify('Done!'))*/
.pipe(autoprefixer({
browsers: ['> 1%', 'IE 7']
}))
.pipe(concatCss("style.css"))
.pipe(minifyCss())
.pipe(rename('style.min.css'))
.pipe(gulp.dest('dist/'))
.pipe(connect.reload());
});
//html
gulp.task('html', function(){
gulp.src('index.html')
.pipe(connect.reload());
});
// watch
gulp.task('watch', function () {
gulp.watch('css/*.css', ['css']);
gulp.watch('index.html', ['html']);
});
// default
gulp.task('default', ['connect', 'html', 'css', 'watch']);
I don't undestand why all works correctly when I use command gulp in node.js console, but if I try to use command gulp watch I get an error in browser console
http://localhost:8080/ net::ERR_CONNECTION_REFUSED
What did I do wrong?
You're not running 'connect' task when running 'watch'.
Reformat your tasks like this
gulp.task('watch:assets', function () {
gulp.watch('css/*.css', ['css']);
gulp.watch('index.html', ['html']);
});
gulp.task('default', ['connect', 'html', 'css', 'watch:assets']);
gulp.task('watch', ['connect', 'watch:assets']);
Go back to terminal and run
gulp watch