Grunt文件无法完成

时间:2014-02-04 21:38:18

标签: javascript node.js gruntjs

我最近发现了grunt,因为它是在我开始研究的一个开源项目中使用的。在我努力想看它是如何工作之前没有使用过咕噜声,或者在我的情况下没有。

grunt文件由项目提供,我认为它适用于其他所有人。我已经安装了grunt,必要的grunt模块已经安装在“Node_modules”目录中。

当运行grunt文件时,第一个进程执行多个连接,这似乎工作正常。创建连接文件。

所有其他步骤似乎都没有执行。不会创建要创建的文件。执行grunt时,控制台上没有显示错误消息。

我很难过 - 任何人都有任何线索可能是什么问题。

grunt文件是:

    /*global module:false*/
module.exports = function(grunt) {
  // Project configuration...
  grunt.initConfig({
    manifest: grunt.file.readJSON('chrome/manifest.json'),

    concat: {
      dist: {
        src: ['chrome/js/requester/**/*.js'],
        dest: 'chrome/js/requester.js'
      },
      requester_html: {
        src: [
        'chrome/html/requester/header.html',
        'chrome/html/requester/sidebar.html',
        'chrome/html/requester/main.html',
        'chrome/html/requester/loggers/*.html',
        'chrome/html/requester/modals/*.html',
        'chrome/html/requester/footer.html'
        ],
        dest: 'chrome/requester.html'
      },
      requester_tester: {
        src: [
        'chrome/html/requester/header.html',
        'chrome/html/requester/sidebar.html',
        'chrome/html/requester/main.html',
        'chrome/html/requester/modals/*.html',
        'chrome/html/requester/loggers/*.html',
        'chrome/html/requester/footer.html',
        'chrome/html/requester/tester.html'
        ],
        dest: 'chrome/tester.html'
      }
    },

    mindirect: {
      dist: {
        src: ['chrome/js/requester.js'],
        dest: 'chrome/js/requester.min.js'
      }
    },

    watch: {
      requester_templates: {
        files: ['chrome/html/requester/templates/*'],
        tasks: ['handlebars'],
        options: {
          livereload: true
        }
      },

      requester_js: {
        files: ['chrome/js/requester/**/*.js'],
        tasks: ['concat:dist'],
        options: {
          livereload: true
        }
      },

      requester_html: {
        files: ['chrome/html/requester/*', 'chrome/html/requester/modals/*', 'chrome/html/requester/loggers/*'],
        tasks: ['concat:requester_html', 'concat:requester_tester'],
        options: {
          livereload: true
        }
      },

      requester_css: {
        files: ['chrome/css/**/*.scss'],
        tasks: ['sass'],
        options: {
          livereload: true
        }
      }
    },

    jshint: {
      options: {
        curly: true,
        eqeqeq: true,
        immed: true,
        latedef: true,
        newcap: true,
        noarg: true,
        sub: true,
        undef: true,
        boss: true,
        eqnull: true,
        browser: true
      },
      globals: {
        jQuery: true
      }
    },

    handlebars: {
      compile: {
        options: {
          partialsUseNamespace: true,
          namespace: 'Handlebars.templates',
          processPartialName: function(filePath) {
            var pieces = filePath.split("/");
            var name = pieces[pieces.length - 1].split(".")[0];
            return name;
          },
          processName: function(filePath) {
            var pieces = filePath.split("/");
            var name = pieces[pieces.length - 1].split(".")[0];
            return name;
          }
        },
        files: {
          "chrome/html/requester/templates.js": "chrome/html/requester/templates/*"
        }
      }
    },

    sass: {
      dist: {
        files: {
          'chrome/css/requester/styles.css': 'chrome/css/requester/styles.scss'
        }
      }
    },

    compress: {
      main: {
          options: {
            archive: 'releases/v<%= manifest.version %>.zip'
          },
          files: [
            {src: ['chrome/**', '!chrome/tests/**', '!chrome/manifest_key.json', '!chrome/tester.html'], dest: '/'}, // includes files in path and its subdirs
          ]
        }
    }
  });

  // These plugins provide necessary tasks.
  grunt.loadNpmTasks('grunt-contrib-handlebars');
  grunt.loadNpmTasks('grunt-contrib-concat');
  grunt.loadNpmTasks('grunt-contrib-jshint');
  grunt.loadNpmTasks('grunt-mindirect');
  grunt.loadNpmTasks('grunt-contrib-watch');
  grunt.loadNpmTasks('grunt-contrib-sass');
  grunt.loadNpmTasks('grunt-contrib-compress');

  // Default task.
  grunt.registerTask('default', ['jshint', 'concat']);
  grunt.registerTask('package', ['concat', 'handlebars', 'sass', 'compress']);

};

控制台的输出如下:

Running "jshint:globals" (jshint) task
>> 0 files lint free.

Running "concat:dist" (concat) task
File "chrome/js/requester.js" created.

Running "concat:requester_html" (concat) task
File "chrome/requester.html" created.

Running "concat:requester_tester" (concat) task
File "chrome/tester.html" created.

Done, without errors.

3 个答案:

答案 0 :(得分:1)

鉴于任务定义如下:

grunt.registerTask('default', ['jshint', 'concat']);
grunt.registerTask('package', ['concat', 'handlebars', 'sass', 'compress']);

如果您在没有任务名称的情况下运行grunt,那么您显示的输出就是您所期望的。它运行jshintconcat任务。

如果您想运行与package相关联的任务,则必须运行grunt package才能运行这些任务。

答案 1 :(得分:0)

看起来我不理解&#34;任务&#34;在咕噜声中。

而不是执行&#34; grunt&#34;运行&#34;默认&#34;任务,我必须执行&#34; grunt package&#34;它运行我感兴趣的任务。

答案 2 :(得分:0)

正如路易斯所说,你必须指定正确的任务才能运行。

但您也可以创建或修改您拥有的任务,以使其更简单。在您的示例中,您可以在默认任务中包含package。由于concat任务已在package内执行,您可以在default任务中替换它:

grunt.registerTask('default', ['jshint', 'package']);
grunt.registerTask('package', ['concat', 'handlebars', 'sass', 'compress']);

并且,为了构建您的网站,只需输入

即可
grunt

将执行jshint和package任务