GulpJS任务在文件更改时自动缩小

GulpJS Task automatically minify when file changes

本文关键字:缩小 任务 文件 GulpJS      更新时间:2024-02-28

当js或更少的文件发生更改时,我需要自动缩小所有文件。

我有一些任务用于最小化我的less文件和js文件,称为"样式"、"服务"answers"控制器"。

对于minimy这个文件,我只是执行这个任务,一切都很好:

gulp.task('minify', ['styles', 'services','controllers']);

相反,我想在开发模式下手动运行这个任务。我该怎么做?

您需要做的是使用nodemon 运行您的项目

nodemon = require('gulp-nodemon');

Nodemon运行您的代码,并在代码更改时自动重新启动。

因此,对于自动缩小,请使用此Gulp任务:

gulp.task('watch', function() {
    gulp.watch(pathLess, ['styles']);
    gulp.watch(pathJs.services, ['services']);
    gulp.watch(pathJs.controllers, ['controllers']);
});

然后设置nodemon以运行您的项目

gulp.task('nodemon', function () {
    nodemon({ script: 'server.js'})
        .on('start', ['watch'], function () {
            console.log('start!');
        })
       .on('change', ['watch'], function () {
            console.log('change!');
        })
        .on('restart', function () {
            console.log('restarted!');
       });
})

现在,如果您在提示符中键入:gulp nodemon,您的server.js将运行,并且您将能够使用自动缩小进行开发。

我希望它能帮助