【问题标题】:Gulp watches/changes only main file instead of all filesGulp 只监视/更改主文件而不是所有文件
【发布时间】:2018-04-13 18:26:11
【问题描述】:

我对 gulp watch 任务(我想)或编译文件有问题。我希望 gulp 监视样式文件夹中的所有 css 文件,但它只监视主要 index.html 文件的更改,并且我看不到我在 css 文件中所做的浏览器中的更改。

文件夹结构:https://ibb.co/kU6c7S

gulpfile.js:

require("./gulp/tasks/styles");
require("./gulp/tasks/watch");

styles.js:

var gulp = require('gulp');
postcss = require('gulp-postcss');
autoprefixer = require('autoprefixer');
cssvars = require('postcss-simple-vars');
nested = require('postcss-nested');
cssImport = require('postcss-import');
mixins = require('postcss-mixins');

gulp.task('styles', function(){
    return gulp.src('./app/assets/styles/styles.css').pipe(postcss([cssImport, mixins, cssvars, nested, autoprefixer])).on("error", function(errorInfo){
            console.log(errorInfo.toString());
            this.emit("end");
        });
        pipe(gulp.dest('./app/temp/styles'));
        });

watch.js:

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

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

    browserSync.init({
        notify: false,
        server: {
            baseDir: "app"
        }
    });

     watch('./app/index.html', function() {
    browserSync.reload();
  });

watch('./app/assets/styles/**/*.css', function(){
        gulp.start('cssInject');
    });
});

gulp.task('cssInject', ['styles'], function() {
    return gulp.src('./app/temp/styles/styles.css')
    pipe(browserSync.stream());
});

_mainPhoto.css:

.main {
position: relative;


&__text {
    position: absolute;
    top: 50%;
    left: 0;
    transform: translateY(-80%);
    width: 100%;
    text-align: center;
}

&__logo {
    font-weight: normal;
    color: #3c443d;
    font-size: 1.1rem;
    margin: 0;

    @mixin atSmall {
        font-size: 2.2rem;
    }

    @mixin atMedium {
        font-size: 3.5rem;
    }

    @mixin atLarge {
        font-size: 5.8rem;
    }

}

&__subtitle {
    font-weight: 300;
    color: #3c443d;
    font-size: 1.1rem;

    @mixin atSmall {
        font-size: 1.5rem;
    }
}

&__subsubtitle {
    color: #d4d8d6;
    text-shadow: 2px 2px 0 rgba(0, 0, 0, .2);
    max-width: 30rem;
    font-size: 1.1rem;
    margin-left: auto;
    margin-right: auto;
}

}

Bash 没有显示任何错误:

$ gulp watch
[20:22:52] Using gulpfile ~\Desktop\Web developer course - mastering modern 
workflow\Projects\Project no.1\travel-site\gulpfile.js
[20:22:52] Starting 'watch'...
[20:22:52] Finished 'watch' after 86 ms
[Browsersync] Access URLs:
 -------------------------------------
       Local: http://localhost:3000
    External: http://192.168.1.14:3000
 -------------------------------------
          UI: http://localhost:3001
 UI External: http://192.168.1.14:3001
 -------------------------------------
[Browsersync] Serving files from: app

感谢任何提示! 安

【问题讨论】:

    标签: javascript css gulp inject


    【解决方案1】:

    我认为您实际上不需要require gulp-watch。您应该能够执行以下操作:

    代替

    watch('./app/assets/styles/**/*.css', function(){
            gulp.start('cssInject');
        });
    });
    

    gulp.watch(['./app/assets/styles/**/*.css'], ['cssInject']);
    

    也就是说,当 /styles 目录中的 css 文件发生变化时,运行 cssInject 任务

    【讨论】:

    • 这对我不起作用。我认为 gulp 无法编译除 index.html 之外的文件中的任何其他更改。似乎 gulp 是基于以前保存的数据(在我添加 mixins 并将 gulpfile js. 拆分为 style.js 和 watch.js 之前,这意味着我可以删除 css 和 js 文件中的代码行并且没有任何变化。Bash 现在显示SyntaxError: Unexpected end of input (by additional " ; " in my code),但即使我删除并保存它也会显示相同的内容。Gulp 只是忽略文件中的更改。
    • 听起来这里可能还有另一个问题。只是出于好奇,如果您将所有任务都列在 gulpfile.js 中,并且不包括要求,会发生什么情况。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-10
    • 2018-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多