0
Hello, I’m a beginner in GULP
and I have the following problem, the code below, which simply minifies the JS
and the CSS
, works perfectly if you rotate the functions in the default
and rotate the GULP
. The problem is that with the Watch
it does not work, it creates the file but blank.
Already answering some questions:
- Yes, it is started correctly, ie the
GULP
detects the change in the file and executes the function. - Yes, in the
default
it minifica correctly. - Yes, if you do not have the minified file, it creates, that is, it enters the function, but for some reason it cannot read the
CSS
/JS
when the function is called byWatch
.
Code below:
var css = [
'./css/estilo.css'
];
var js = [
'./js/app.js'
];
var gulp = require('gulp');
var jsmin = require('gulp-jsmin');
var rename = require('gulp-rename');
var uglify = require("gulp-uglify");
var concat = require("gulp-concat");
var watch = require('gulp-watch');
var cssmin = require("gulp-cssmin");
var stripCssComments = require('gulp-strip-css-comments');
gulp.task('minify-css', function(){
gulp.src(css)
.pipe(stripCssComments({all: true}))
.pipe(cssmin())
.pipe(rename({suffix: '.min'}))
.pipe(gulp.dest('./css/min/'));
});
gulp.task('minify-js', function () {
gulp.src(js)
.pipe(jsmin())
.pipe(rename({suffix: '.min'}))
.pipe(gulp.dest('./js/min/'));
});
gulp.task('default', ['watch']);
gulp.task('watch', function() {
gulp.watch(js, ['minify-js']);
gulp.watch(css, ['minify-css']);
});
I’ve tried to declare the path of CSS
directly in the function instead of calling the variable CSS
. I have also tried to declare the variable as global, but none of this changes the result which is the blank file.
I think Voce did not understand my question, the problem is not the call, when changing the . css, it changes the . min, the problem he alters and leaves him blank. If you don’t have a css file, it creates one, that is, it enters the Minify-css function, only it leaves the file blank. This is only when it is updated by the watch, when it runs the function by default, it works smoothly.
– PoolandTech
@Victorditadi But running by default, it does not keep the watch?
– celsomtrindade
It works by default, but on the watch, whenever I save the.css style, the.min.css style goes blank, for some reason it erases what’s already inside it
– PoolandTech
@Victorditadi One thing I noticed in some examples, they have
return
intasks
js and css, for example. Try to define this:gulp.src(css)
->return gulp.src(css)
and in the JSreturn gulp.src(js)
(in the declaration of their tasks, not on watch).– celsomtrindade