grunt-contrib-watch实时重载不起作用

3

我一整天都在尝试让livereload工作,但是我被卡住了。我的grunt.js文件如下,我确保将我的脚本添加到了HTML文件的末尾。有什么建议吗?

module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-compass');
grunt.initConfig({
    uglify: {
        my_target: {
            files: {
                '_/js/script.js' : ['_/components/js/script.js']
            }//files
        } //mytarget
    }, //uglify
    watch: {
        options : { livereload: true },
        scripts: {
        files: ['_/components/js/*.js'],
        tasks: ['uglify']
    }, //scripts
    html: {
        files: ['*.html']
    }//html
    }//watch
}) // initconfig
grunt.registerTask('default', 'watch');
} //exports
4个回答

3

2
我为您录制了一个gif动画小视频。在那里,我展示了如何通过npm安装依赖模块,并启用livereload构建项目。
该屏幕录像位于http://imgur.com/3CpVI2j上。
我的工具版本是NPM 1.4.21NodeJS v0.10.30
这是我的Gruntfile.js文件,带有注释。
module.exports = function (grunt) {

  grunt.initConfig({

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

    // Delete contents before the full build
    clean: {
      dist: ['dist', 'dist/js'],
      html: ['dist/**/*.html']
    },

    /* Uglify javascript located in app/js/*.js
       And copy it to dist/js/main.js

       So all JS will be in one file.
       P.S. Remember that our js task in uglify is called "js" */
    uglify: {
      options: {
        report: 'min'
      },
      js: { // task to combine separate files into main.js
        files: {
          'dist/js/main.js': [
            'app/js/**/*.js'
          ]
        }
      }
    },

    copy: { // Just copy HTML files from app folder
      html: {
        files: [{
          expand: true,
          cwd: 'app/',
          src: ['**/*.html'],
          dest: 'dist/',
          filter: 'isFile'
        }]
      },
    },

    connect: { // Create server that will serve
               // requests to our compiled app
      dist: {
        options: {
          port: 3000,
          base: 'dist'
        }
      }
    },

    watch: { // Most instersting.
             // Will be watching for changes in JS and HTML.
      options: {
        livereload: true
      },
      js: { // Here you need to specify the same task name
            // as in uglify task we thought above
        files: ['app/js/**/*.js'], // Which files to uglify and copy
        tasks: ['newer:uglify'] // Do it
      },
      copy: { // Remove old html and copy new to dist folder
        files: ['app/**/*.html'],
        tasks: ['clean:html','newer:copy:html']
      }
    },

  });

  // Load all grunt modules with one command
  require('load-grunt-tasks')(grunt);

  grunt.registerTask('build', [
    'clean:dist', // Remove all
    'uglify', // Uglify all
    'copy' // Copy all HTML
  ]);

  grunt.registerTask('default', [
    'build', // Build the project
    'connect:dist', // Start server
    'watch' // Watch for changes
  ]);

};

这是没有注释的package.json(所有内容都已经相当清楚)。

{
  "engines": {
    "node": ">= 0.10.0"
  },
  "devDependencies": {
    "grunt": "~0.4.5",
    "grunt-contrib-clean": "0.5.0",
    "grunt-contrib-connect": "~0.8.0",
    "grunt-contrib-copy": "~0.5.0",
    "grunt-contrib-uglify": "~0.5.0",
    "grunt-contrib-watch": "~0.6.1",
    "grunt-newer": "~0.7.0",
    "load-grunt-tasks": "~0.6.0"
  },
  "scripts": {
    "preinstall": "npm cache clear"
  }
}

1

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接