Reputation: 24768
I changed from Gulp 3 to Gulp 4, but I can't get it to work.
When I run gulp
and save a js or scss file it creates a new file successfully. If I save a again nothing happends. My guess is that I don't understand how gulp.series
works. It only run ones.
var gulp = require('gulp');
var concat = require('gulp-concat');
var sass = require('gulp-sass');
var rename = require('gulp-rename');
var gutil = require('gulp-util');
var minifyJS = require('gulp-minify');
gulp.task('css', function() {
gulp.src([
'assets/css/src/tinyFileTree.scss'
])
.pipe(sass().on('error', sass.logError))
.pipe(gulp.dest('assets/css/dist'))
.pipe(rename('tinyFileTree.min.scss'))
.pipe(sass({outputStyle: 'compressed'}).on('error', sass.logError))
.pipe(gulp.dest('assets/css/dist'))
});
// JS
gulp.task('js', function() {
gulp.src([
'assets/js/src/tinyFileTree.js'
])
.pipe(minifyJS({
ext:{
min:'.min.js'
},
}))
.on('error', function (err) { gutil.log(gutil.colors.red('[Error]'), err.toString()); })
.pipe(gulp.dest('assets/js/dist'))
});
// Default
gulp.task('default', function() {
gulp.watch('assets/css/src/tinyFileTree.scss', gulp.series('css'));
gulp.watch('assets/js/src/tinyFileTree.js', gulp.series('js'));
});
Upvotes: 3
Views: 1885
Reputation: 9969
If you don't want to return an gulp instance. You can just follow the signature used on the gulp watch docs. The function expects a callback function with one argument and you need to execute that argument at the end of your task. It took me a couple hours to figure out this issue but here it is.
notice the cb();
gulp.task('css', function(cb) {
gulp.src([
'assets/css/src/tinyFileTree.scss'
])
.pipe(sass().on('error', sass.logError))
.pipe(gulp.dest('assets/css/dist'))
.pipe(rename('tinyFileTree.min.scss'))
.pipe(sass({outputStyle: 'compressed'}).on('error', sass.logError))
.pipe(gulp.dest('assets/css/dist'));
cb();
});
Upvotes: 1
Reputation: 24768
I made a complete rewrite that can be found here: https://github.com/jenstornell/gulpfile-style-script.
You can also see it here. Hope it helps someone.
let gulp = require("gulp");
let sass = require("gulp-sass");
let autoprefixer = require("gulp-autoprefixer");
let concat = require("gulp-concat");
let uglify = require('gulp-uglify-es').default;
let csso = require('gulp-csso');
let rename = require('gulp-rename');
var css = {
src: 'assets/css/src/**/*.scss',
dest: 'assets/css/dist',
filename: 'style.scss'
};
var js = {
src: 'assets/js/src/**/*.js',
dest: 'assets/js/dist',
filename: 'script.js'
};
function style() {
return (
gulp
.src(css.src)
.pipe(concat(css.filename))
.pipe(sass())
.on("error", sass.logError)
.pipe(autoprefixer())
.pipe(gulp.dest(css.dest))
.pipe(csso())
.pipe(rename({extname: '.min.css'}))
.pipe(gulp.dest(css.dest))
);
}
function script() {
return (
gulp
.src(js.src)
.pipe(concat(js.filename))
.pipe(gulp.dest(js.dest))
.pipe(uglify())
.pipe(rename({extname: '.min.js'}))
.pipe(gulp.dest(js.dest))
);
}
function watch(){
gulp.watch(css.src, style);
gulp.watch(js.src, script);
}
exports.css = style;
exports.js = script;
exports.default = watch;
Upvotes: 0