带有 Grunt 的指南针插件

Compass plugin with Grunt

这是一个关于如何在 Grunt 中使用 Compass 的问题。 数据填充步骤出错。我在控制台上注意到,当我只修改一个 .scss 文件时,compass 会多次填充同一个文件(四次、五次或最多十次)。 在您看来,这取决于什么?这是我的 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']);

};

你这里有几个问题...首先,你已经在 compass 任务中 watch 处理你的 sass 文件,没有必要监控他们有一个明确的 watch 任务。此外,一旦 sass 文件更改,您的 concurrent 任务将再次设置为 运行 watch

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

  1. 启动 watch,它等待 sass 或编译的 css 文件更改
  2. 当 sass 文件更改时,watch 执行 concurrent,后者又执行 compass,然后 [=13= 的另一个实例].
  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

或者如果您想 运行 先罗盘,然后观察变化:

~/my-project$ grunt compass watch