【问题标题】:Different grunt tasks for watch and sasswatch 和 sass 的不同任务
【发布时间】:2016-08-08 10:31:22
【问题描述】:

我有一个 grunt 项目,我正在同时使用 sass 和 jam。我想在开发时为 sass 制定一项任务,在哪里扩展样式以进行故障排除,并在我“完成”项目然后压缩样式时执行一项任务。我是 grunt 新手,不知道该怎么做。

我的 gruntfile

module.exports = function(grunt) {
    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),

        jade: {
            compile: {
                options: {
                    pretty: true,
                    nospawn: false
                },

                files: {
                    'index.html' : 'src/index.jade'
                }
            }
        },

        sass: {
            dist: {
                options: {
                    style: 'expanded',
                    nospawn: false
                },

                files: {
                    'build/css/app.css' : 'src/sass/app.sass'
                }
            }
        },

        watch: {
            jade: {
                files: 'src/**/*.jade',
                tasks: ['jade']
            },

            css: {
                files: 'src/sass/**/*.sass',
                tasks: ['sass']
            },

            options: {
                livereload: true,
                nospawn: false
            }
        },

        connect: {
            server: {
                options: {
                    port: 9000,
                    base: '.',
                    hostname: '0.0.0.0',
                    protocol: 'http',
                    livereload: true,
                    open: true
                }
            }
        }
    });

    grunt.loadNpmTasks('grunt-contrib-jade');
    grunt.loadNpmTasks('grunt-contrib-sass');
    grunt.loadNpmTasks('grunt-contrib-watch');
    grunt.loadNpmTasks('grunt-contrib-connect');

    grunt.registerTask('default', ['connect', 'watch']);
};

【问题讨论】:

    标签: javascript css sass gruntjs pug


    【解决方案1】:

    要获得压缩 css 而不是扩展,您首先需要创建另一个 sass-task(例如在 sass:{}) 中,将其命名为 finish: 并更改压缩设置。

    它应该看起来像这样:

    finish: {
       options: {
                style: 'compressed',
                nospawn: false
               },
    
       files: {
                'build/css/app.css' : 'src/sass/app.sass'
               }
    }
    

    然后在grunt.registerTask('default', ['connect', 'watch']);之后 您可以添加另一个任务,即完成应该像: grunt.registerTask('finish', ['sass:finish']);

    要运行它,您需要在命令行中输入grunt finish

    【讨论】:

    • 不是这样的。我想做一些类似 grunt production 的事情,其中​​ sass 将被扩展,而 grunt finish sass 将被压缩。
    • 所以我应该这样写:sass_fin: { dist: { options: { style: compressed....... } } } ?
    • 再次更新了我的答案:/
    • 谢谢,现在我可以更频繁地使用它并加快我的工作流程。
    • 很高兴我能帮上忙!干杯
    猜你喜欢
    • 2016-06-04
    • 1970-01-01
    • 1970-01-01
    • 2017-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-03
    相关资源
    最近更新 更多