Gulp watch 未检测到文件更改

Gulp watch not detecting file changes

我的 Gulp 任务正在正确启动,但未检测到文件更改。我正在从 starter-bootstrap 开发一个 WordPress 主题。 运行 'npm install' 和安装的所有依赖项。现在当我 运行 'npm run watch', gulp 说:

> gulp watch

[18:01:24] Using gulpfile /mnt/c/Users/victor/Local Sites/camping-fin-de-siglo/app/public/wp-content/themes/fin-de-siglo/gulpfile.js
[18:01:24] Starting 'watch'...

仅此而已,没有检测到文件更改。源路径正确。这是我的 gulpfile.js

const gulp = require( 'gulp' ),
    fancylog = require( 'fancy-log' ),
    browserSync = require( 'browser-sync' ),
    server = browserSync.create(),
    dev_url = 'http://localhost/starter-bootstrap';


/**
 * Define all source paths
 */

var paths = {
    styles: {
        src: './assets/*.scss',
        dest: './assets/css'
    },
    scripts: {
        src: './assets/*.js',
        dest: './assets/js'
    }
};


/**
 * Webpack compilation: http://webpack.js.org, https://github.com/shama/webpack-stream#usage-with-gulp-watch
 * 
 * build_js()
 */

function build_js() {
    const compiler = require( 'webpack' ),
        webpackStream = require( 'webpack-stream' );
    
    return gulp.src( paths.scripts.src )
        .pipe(
            webpackStream({
                config: require( './webpack.config.js' )
                },
                compiler
            )
        )
        .pipe(
            gulp.dest( paths.scripts.dest )
        )
        /*.pipe(
            server.stream() // Browser Reload
        )*/;
}


/**
 * SASS-CSS compilation: https://www.npmjs.com/package/gulp-sass
 * 
 * build_css()
 */

function build_css() {
    const sass = require( 'gulp-sass' ),
        postcss = require( 'gulp-postcss' ),
        sourcemaps = require( 'gulp-sourcemaps' ),
        autoprefixer = require( 'autoprefixer' ),
        cssnano = require( 'cssnano' );
    
    const plugins = [
        autoprefixer(),
        cssnano(),
    ];
    
    return gulp.src( paths.styles.src )
        .pipe(
            sourcemaps.init()
        )
        .pipe(
            sass()
                .on( 'error', sass.logError )
        )
        .pipe(
            postcss(plugins)
        )
        .pipe(
            sourcemaps.write( './' )
        )
        .pipe(
            gulp.dest( paths.styles.dest )
        )
        /*.pipe(
            server.stream() // Browser Reload
        )*/;
}

/**
 * Watch task: Webpack + SASS
 * 
 * $ gulp watch
 */

gulp.task('watch',
    function () {
        // Modify "dev_url" constant and uncomment "server.init()" to use browser sync
        /*server.init({
            proxy: dev_url,
        } );*/

        gulp.watch( paths.scripts.src, build_js );
        gulp.watch( [ paths.styles.src, './assets/scss/*.scss' ], build_css );
    }
);

有什么想法吗?

首先,您可能需要迁移到 Gulp V4。

如需这方面的帮助,您可以阅读 article or watch this video

如果您不想执行这些操作,可以按照以下步骤操作,只需复制此代码即可:

将 watch 任务替换为:

function watchFunc(cb) {

    // Modify "dev_url" constant and uncomment "server.init()" to use browser sync
    /*server.init({
       proxy: dev_url,
    } );*/
    
    // Create the watchers
    let jsWatcher = gulp.watch( [ paths.scripts.src ] );
    let scssWatcher = gulp.watch( [ paths.styles.src, './assets/scss/*.scss' ]);

    // Run the tasks when it detects a file change
    jsWatcher.on('change', build_js)
    scssWatcher.on('change', build_css )
    
    // Ends so it dose not get stuck on "[18:01:24] Starting 'watch'..." (Don't worry it still regesters watches)
    cb();
}

通过将此添加到文件末尾来修复任务


exports.watch = watchFunc

有关 gulp.js documentation or the chokidar documantation 的更多信息(包 Gulp 用于观看...)

编辑 1: 忘记在最后添加回调(cb()),这样任务就不会卡住