【问题标题】:GulpJS Task automatically minify when file changesGulpJS 任务在文件更改时自动缩小
【发布时间】:2015-02-22 14:34:46
【问题描述】:

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

我有一些任务用于缩小我的 less 文件和 js 文件,称为“样式”、“服务”和“控件”。

为了缩小这个文件,我只执行这个任务,一切都很好:

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

改为手动运行此任务,我想在开发模式下自动执行。我该怎么做?

【问题讨论】:

    标签: javascript gulp minify


    【解决方案1】:

    你需要做的是用 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 将运行,您将能够使用自动缩小进行开发。

    希望对你有帮助

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-06
    • 1970-01-01
    • 1970-01-01
    • 2011-05-01
    • 1970-01-01
    相关资源
    最近更新 更多