N
N
Nikita Petrov2018-05-16 17:29:08
Layout
Nikita Petrov, 2018-05-16 17:29:08

Gulp doesn't track changes in .html and .css files?

Good afternoon. Can you please tell me where I could be wrong?
I want to follow the changes in .css and .html and do hot reload when saving

var gulp = require('gulp');
var browserSync = require('browser-sync').create();
var reload      = browserSync.reload;
var paths = {
    css: ['./style/*.css'],
    html: ['./*.html']
};

gulp.task('serve', function () {

    // Serve files from the root of this project
    browserSync.init({
        server: {
            baseDir: "./"
        }
    });

    gulp.watch(paths.css, paths.html).on("change", reload);
});

With this option, everything actually works, but it only monitors the .html file.
'use strict';

const gulp = require('gulp');
var browserSync = require('browser-sync').create();
var reload      = browserSync.reload;



gulp.task('serve', function () {

    // Serve files from the root of this project
    browserSync.init({
        server: {
            baseDir: "./"
        }
    });

    gulp.watch("*.html").on("change", reload);
});

There are no errors in the first (required) option when running gulp serve, but nothing in the console itself either.
The structure is simple
└── node_modules
└── style/.css
└── index.html

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
alvvi, 2018-05-16
@alvvi

gulp.watch(paths.css, paths.html)
You passed two different arguments, but you need one.
Try

gulp.watch(['./path/to/styles/*/**.*.css ', './path/to/html/*/**.*html'])

D
Dmitry, 2018-05-17
@oxmad

On good you should have 2 folders src and dist (for example). In src we have the source codes for html, sass, js, etc., and in dist we have a ready-made project (build).
In your case, create a src folder in the project folder and transfer the html and css files there (Also check the file paths). As needed, just add the migration and compilation to the dist folder.
PS Name the html file index.html, if it is different, then add the parameter "index: Name_of_your_file.html"

'use strict';

const gulp            = require('gulp'),
var browserSync  = require('browser-sync'),
var reload            = browserSync.reload;

// BrowserSync
gulp.task('serve', function () {
    browserSync({
        server: {
            baseDir: "src"
            //index: "paths.html" если название нестандартное
        },
        notify: false
    });
});

// Watch
gulp.task('watch', function() {
  gulp.watch('./src/css/**/*.css', reload),
  gulp.watch('./src/*.html', reload);
});

We start with the gulp watch command.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question