【问题标题】:gulp sass watch task @import issuegulp sass 监视任务@import 问题
【发布时间】:2023-04-03 09:18:01
【问题描述】:

我想用 sass 尝试 gulp 并遇到问题。 我有以下 sass 目录:

main.scss //all custom styling
mixins.scss //custom mixins
variables.scss //custom variables
style.scss //file that imports all the above with bootstrap and fontawesome

当我运行 gulp 时,一切都编译并且没有错误,我得到了正确的 sytle.min.css,其中包含所有样式。 但是随后我更改了其中一个监视文件(main.scss || mixins.scss || variables.scss)我收到以下错误之一:“未定义的变量”,“没有名为...的mixin”。

此外,如果我更改并保存 main.scss,我不会收到任何错误,但自定义 scss 文件都不会包含在 css 中,只有带有 fontawesome 的引导程序会被编译。

我的设置有什么问题?

gulpfile.js

var gulp = require('gulp'),
    sass = require('gulp-sass'),
    notify = require("gulp-notify"),
    concat = require('gulp-concat'),
    minifycss = require('gulp-minify-css'),
    uglify = require('gulp-uglify'),
    rename = require('gulp-rename')
    bower = require('gulp-bower')
    merge = require('merge-stream')
    watch = require('gulp-watch');

var config = {
    destPath: './dist',
    sassPath: './src/sass',
    jsPath: './src/js',
    bowerDir: './src/components'
}

gulp.task('bower', function() {
    return bower()
    .pipe(gulp.dest(config.bowerDir));
});

gulp.task('icons', function() {
    var fontawesome = gulp.src(config.bowerDir + '/font-awesome/fonts/**.*')
    .pipe(gulp.dest('./src/fonts'));

    var bootstrap = gulp.src(config.bowerDir + '/bootstrap-sass/assets/fonts/bootstrap/**.*')
    .pipe(gulp.dest('./src/fonts/bootstrap'));

    return merge(fontawesome, bootstrap);
});


gulp.task('sass', function() {
    console.log(config.sassPath);
    var stream = gulp.src([config.sassPath + '/style.scss'])
        .pipe(sass().on('error', sass.logError))
        // .pipe(concat('style.css'))
        .pipe(minifycss())
        .pipe(rename('style.min.css'))
        .pipe(gulp.dest(config.destPath));
    return stream;
});

gulp.task('js', function() {
    var stream = gulp.src([config.bowerDir + '/jquery/dist/jquery.js', config.bowerDir + '/bootstrap-sass/assets/javascripts/bootstrap.js', config.jsPath + '/*.js'])
        .pipe(concat('script.js'))
        .pipe(uglify())
        .pipe(rename('script.min.js'))
        .pipe(gulp.dest(config.destPath)); 
    return stream;
});

gulp.task('watch', function(){
    watch([config.sassPath + '/*.scss'], function(event, cb) {
        gulp.start('sass');
    });
    watch([config.jsPath + '/*.js'], function(event, cb) {
        gulp.start('js');
    });
});

gulp.task('default', ['bower', 'icons', 'js','sass', 'watch']);

style.scss

@import "./variables.scss";
@import "./mixins.scss";
@import "../components/bootstrap-sass/assets/stylesheets/bootstrap.scss";
@import "../components/font-awesome/scss/font-awesome.scss";
@import "./main.scss";

【问题讨论】:

    标签: gulp gulp-watch gulp-sass


    【解决方案1】:

    好的,所以我通过在调用 sass 任务之前将 timeout 添加到我的 watch 任务来修复它:

    watch([config.sassPath + '/*.scss'], function(event, cb) {
        setTimeout(function(){
            gulp.start('sass');
        }, 1000);
    });
    

    这是 IDE (sublime 2) 的保存延迟或服务器 ping 问题。

    【讨论】:

    • 10x 伙计,我尝试了数千种方法来完成这项工作,却浪费了数小时的头痛!我尝试使用 sassGlob() 和 includePaths 设置 sass,但看起来超时是解决此问题的方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多