【问题标题】:Grunt not running any tasks except for watchGrunt 除了 watch 没有运行任何任务
【发布时间】:2015-12-29 06:08:55
【问题描述】:

我正在尝试使用 Bootstrap 构建一个网站,使用 Grunt 来运行任务。我一直在试图弄清楚为什么我的 gruntfile 除了“watch”之外似乎没有运行任何任务,但我似乎找不到答案。也没有出现错误。在终端中,time-grunt 将显示执行时间(尽管没有执行任何任务),然后它会显示“Waiting....”,这意味着 watch 正在运行。有人可以阐明我在 gruntfile 中做错了什么吗?任何帮助将不胜感激:)

module.exports = function(grunt) {

    //time
    require('time-grunt')(grunt);

    grunt.initConfig({

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

        sass: {
            dist: {
                options: {
                    sourceMap: true,
                    style: 'compressed',
                },

                files: {
                    'assets/css/style.css' : 'assets/scss/style.scss',
                }
            }
        },

        copy: {
            scripts: {
                cwd: 'bower_components/jquery/dist/',
                expand: true,
                src: [
                    'jquery.min.js',
                    ],
                dest: 'assets/js/',
                filter: 'isFile',
            }
        },

        concat: {
            options: {
                separator: ';',
            },
            js: {
                src: [
                    'bower_components/bootstrap-sass/assets/javascripts/bootstrap.js',
                    'bower_components/bootstrap-sass/assets/javascripts/bootstrap/button.js',
                    'bower_components/bootstrap-sass/assets/javascripts/bootstrap/collapse.js',
                    'bower_components/bootstrap-sass/assets/javascripts/bootstrap/dropdown.js',
                    'bower_components/bootstrap-sass/assets/javascripts/bootstrap/modal.js',
                    'bower_components/bootstrap-sass/assets/javascripts/bootstrap/tab.js',
                    'bower_components/bootstrap-sass/assets/javascripts/bootstrap/transition.js',
                ],
                dest: 'assets/js/bootstrap.js',
            },
            sass: {
                src: [
                    'bower_components/bootstrap-sass/assets/stylesheets/_bootstrap.scss',
                    'bower_components/bootstrap-sass/assets/stylesheets/bootstrap/**/*.scss'
                ],
                dest: 'assets/scss/style.scss'
            }
        },

        uglify: {
            options: {
                mangle: false,
            },

            my_target: {
                files: {
                    'assets/css/style.min.css' : ['assets/css/style.css'],
                    'assets/js/bootstrap.min.js' : ['assets/js/bootstrap.js']
                }
            }
        },

        watch: {
            gruntfile: {
                files: 'Gruntfile.js',
                tasks: ['jshint:gruntfile'],
                options: {
                    livereload: true
                }
            },
            src: {
                files: ['assets/js/*.js', 'assets/scss/*.scss'],
                tasks: ['default'],
                options: {
                    livereload: true
                }
            }
        },

        jshint: {
            all: ['Gruntfile.js', 'assets/**/*.js']
        },


    });

    grunt.loadNpmTasks('grunt-contrib-sass');
    grunt.loadNpmTasks('grunt-contrib-copy');
    grunt.loadNpmTasks('grunt-contrib-watch');
    grunt.loadNpmTasks('grunt-contrib-concat');
    grunt.loadNpmTasks('grunt-contrib-uglify');
    grunt.loadNpmTasks('grunt-contrib-jshint');

    grunt.registerTask('build', ['sass', 'copy', 'concat', 'uglify']);
    grunt.registerTask('default', ['watch', 'jshint']);
};

【问题讨论】:

  • 你试过“grunt buld”吗?这将调用构建任务
  • 除了 Harikrishnan 的评论之外,grunt 将运行 default 任务,该任务执行 watch 和 jshint。我相信您应该将watch 移动到数组的末尾,否则它会阻止其他任务执行。
  • 好的,非常感谢你们。我在终端中输入了“grunt build”,并将“watch”放在数组的末尾,现在它可以工作了!

标签: node.js twitter-bootstrap gruntjs npm bower


【解决方案1】:

您的任务名称和顺序有点混淆,这里有 3 点可以完全修复它 - 并为 StackOverflow 提供问题的答案:

  • watch 应该始终是最后一个运行的任务,因为它不会结束。所以:

    grunt.registerTask('default', ['jshint', 'watch']);
    
  • 如果你想运行一个任务(比如build只需调用 grunt,你必须把它放在你的默认任务中,所以:

    grunt.registerTask('default', ['jshint', 'build', 'watch']);
    
  • 您的监视任务本身不能启动监视任务,因此watch.src 不应调用default(默认包括监视):

    watch: {
        gruntfile: {
            files: 'Gruntfile.js',
            tasks: ['jshint:gruntfile'],
            options: {
                livereload: true
            }
        },
        src: {
            files: ['assets/js/*.js', 'assets/scss/*.scss'],
            tasks: ['jshint', 'build'],
            options: {
                livereload: true
            }
        }
    },
    

【讨论】:

  • 感谢您的提示。阐明了我对如何运行默认任务和构建任务的理解。
猜你喜欢
  • 1970-01-01
  • 2015-06-04
  • 1970-01-01
  • 2013-05-25
  • 2016-01-28
  • 1970-01-01
  • 1970-01-01
  • 2013-02-23
  • 1970-01-01
相关资源
最近更新 更多