【问题标题】:Compass plugin with GruntGrunt 指南针插件
【发布时间】:2015-03-27 14:22:09
【问题描述】:

这是一个关于如何在 Grunt 中使用 Compass 的问题。 数据填充步骤出现问题。我在控制台上注意到,当我只修改一个 .scss 文件时,罗盘会多次填充同一个文件(四次、五次或最多十次)。 在您看来,这取决于什么?这是我的 gruntfile.js。 感谢您的热心帮助。

module.exports = function (grunt) {

var _homeJs = ['contents/js/jquery.Cinema.js', 'contents/js/jquery.SkinOverlay.js'];
var _homeJsExclude = []

_homeJs.forEach(function (entry) {
    _homeJsExclude.push('!' + entry)
});


grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),

    concat: {
        dist: {
            src: ['contents/js/*.js', _homeJsExclude],
            dest: 'contents/dist/js/global.js'
        },
        lib: {
            src: 'contents/plugins/**/*.js',
            dest: 'contents/dist/js/libs.js'
        },
        globalhome: {
            files: {
                'contents/dist/js/global.home.js': _homeJs
            }
        }
    },
    uglify: {
        dist: {
            src: ['<%= concat.dist.dest %>'],
            dest: 'contents/dist/js/global.min.js'
        },
        globalhome_min: {
            src: 'contents/dist/js/global.home.js',
            dest: 'contents/dist/js/global.home.min.js'
        },
        lib: {
            src: ['<%= concat.lib.dest %>'],
            dest: 'contents/dist/js/libs.min.js'
        }
    },
    compass: {
        dist: {
            options: {
                sassDir: 'contents/sass/',
                cssDir: 'contents/dist/css',
                watch: true,
                outputStyle: 'compressed',
                linecomments: false
            }
        }
    },
    cssmin: {
        target: {
            files: [
                {
                    './contents/dist/css/ie-css/ie8/ie8.min.css': ['./contents/css/ie-css/ie8/ie8.css']
                },
                {
                    './contents/dist/css/main.min.css': ['./contents/dist/css/main.css']
                },
                {
                    './contents/dist/css/responsive.min.css': ['./contents/dist/css/responsive.css']
                }
            ]
        }
    },
    concurrent: {
        target: {
            tasks: ['compass', 'watch'],
            options: {
                logConcurrentOutput: true
            }
        }
    },
    watch: {
        js: {
            files: ['contents/js/*.js', 'contents/plugins/**/*.js'],
            tasks: ['concat', 'uglify:dist', 'uglify:globalhome_min'],
            options: {
                reload: true
            }
        },
        css: {
            files: ['contents/sass/**/*.scss', 'contents/dist/css/'],
            tasks: ['concurrent:target'], 
            options: {
                reload: true
            }
        }
    },
});

grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-compass');
grunt.loadNpmTasks('grunt-concurrent');
grunt.loadNpmTasks('grunt-contrib-cssmin');

grunt.registerTask('default', ['concat', 'uglify', 'cssmin']);

};

【问题讨论】:

    标签: sass gruntjs compass-sass grunt-contrib-compass


    【解决方案1】:

    这里有几个问题...首先,您已经在 compass 任务中 watch 处理您的 sass 文件,无需使用明确的 watch 任务来监控它们。此外,您的 concurrent 任务设置为在 sass 文件更改后再次运行 watch

    这是您现在设置的逻辑流程:

    1. 启动watch,等待sass或编译后的css文件发生变化
    2. 当 sass 文件发生更改时,watch 执行 concurrent,后者又执行 compass,然后watch 的另一个实例
    3. compass 任务编译您的 css,这会导致 watch 任务检测更改并运行concurrent 任务再次
    4. 这种情况继续下去并毁掉一切。

    所以,不用你现在拥有的,只需使用这个(简化的)配置:

    grunt.initConfig({
        // ...
    
        compass: {
            dist: {
                options: {
                    sassDir: 'contents/sass/',
                    cssDir: 'contents/dist/css',
    
                    // *** remove this option
                    // watch: true,
    
                    outputStyle: 'compressed',
                    linecomments: false
                }
            }
        },
        // ...
    
        // *** remove this whole task, in the current config you do not need it
        /*
        concurrent: {
            target: {
                tasks: ['compass', 'watch'],
                options: {
                    logConcurrentOutput: true
                }
            }
        },
        */
        watch: {
            // ...
    
            css: {
                // *** only watch the source files, not the output
                files: ['contents/sass/**/*.scss'],
                // *** run the `compass` task directly
                tasks: ['compass'], 
                options: {
                    reload: true
                }
            }
        }
    });
    

    你用以下方式开始了整个事情:

    ~/my-project$ grunt watch
    

    或者如果你想先运行 compass,然后观察变化:

    ~/my-project$ grunt compass watch
    

    【讨论】:

    • 我注意到一个小东西,编译文件css知道grunt有点慢,你能告诉我为什么吗?谢谢。
    • 我不确定你到底在问什么,但如果是关于指南针变慢,是的。我对此没有任何解决办法。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-17
    • 2014-05-27
    • 1970-01-01
    • 2013-03-13
    • 1970-01-01
    相关资源
    最近更新 更多