2014-04-24 59 views
0

這是我第一次建立一個Gruntfile.js和一切工作,但現場重新加載,我不斷收到一個任務...錯誤時,我加livereload:true,到手錶選項,我錯過了什麼東西,或者我做錯了什麼?不能得到livereload工作在gruntfile.js

我看了這裏https://github.com/gruntjs/grunt-contrib-watch#optionslivereload並遵循指南,其中說明只是將livereload添加到選項並將其設置爲true。

module.exports = function(grunt) { 
// configure the tasks 
grunt.initConfig({ 

//Build task 
copy: { 
    build: { 
    cwd: 'source', 
    src: [ '**', '!**/*.less', '!**/*.coffee', '!**/*.jade' ], 
    dest: 'build', 
    expand: true 
    }, 
}, 

// Clean task 
clean: { 
    build: { 
    src: [ 'build' ] 
    }, 
    stylesheets: { 
     src: [ 'build/**/*.css', '!build/application.css' ] 
    }, 
    scripts: { 
    src: [ 'build/**/*.js', '!build/application.js' ] 
    }, 
}, 

// Less task 
less: { 
    build: { 
    options: { 
     linenos: true, 
     compress: false 
    }, 
    files: [{ 
     expand: true, 
     cwd: 'source', 
     src: [ '**/*.less' ], 
     dest: 'build', 
     ext: '.css' 
    }] 
    } 
}, 

// Autoprefixer task 
autoprefixer: { 
    build: { 
    expand: true, 
    cwd: 'build', 
    src: [ '**/*.css' ], 
    dest: 'build' 
    } 
}, 

// CSS Minify task 
cssmin: { 
    build: { 
    files: { 
     'build/application.css': [ 'build/**/*.css' ] 
    } 
    } 
}, 

// Coffee task 
coffee: { 
    build: { 
    expand: true, 
    cwd: 'source', 
    src: [ '**/*.coffee' ], 
    dest: 'build', 
    ext: '.js' 
    } 
}, 


// Uglify 
uglify: { 
    build: { 
    options: { 
     mangle: false 
    }, 
    files: { 
     'build/application.js': [ 'build/**/*.js' ] 
    } 
    } 
}, 

// Html task 
jade: { 
    compile: { 
    options: { 
     data: {} 
    }, 
    files: [{ 
     expand: true, 
     cwd: 'source', 
     src: [ '**/*.jade' ], 
     dest: 'build', 
     ext: '.html' 
    }] 
    } 
}, 


// Watch task 
watch: { 
    stylesheets: { 
    files: 'source/**/*.less', 
    tasks: [ 'stylesheets' ] 
    }, 
    scripts: { 
    files: 'source/**/*.coffee', 
    tasks: [ 'scripts' ] 
    }, 
    jade: { 
    files: 'source/**/*.jade', 
    tasks: [ 'jade' ] 
    }, 
    copy: { 
    files: [ 'source/**', '!source/**/*.less', '!source/**/*.coffee', '!source/**/*.jade' ], 
    tasks: [ 'copy' ] 
    } 
}, 

// Connect to server task 
connect: { 
    server: { 
    options: { 
     port: 4000, 
     base: 'build', 
     hostname: '*' 
     livereload: true, 
    } 
    } 
} 

}); 


// load the tasks 
grunt.loadNpmTasks('grunt-contrib-copy'); 
grunt.loadNpmTasks('grunt-contrib-clean'); 
grunt.loadNpmTasks('grunt-contrib-less'); 
grunt.loadNpmTasks('grunt-autoprefixer'); 
grunt.loadNpmTasks('grunt-contrib-cssmin'); 
grunt.loadNpmTasks('grunt-contrib-coffee'); 
grunt.loadNpmTasks('grunt-contrib-uglify'); 
grunt.loadNpmTasks('grunt-contrib-jade'); 
grunt.loadNpmTasks('grunt-contrib-watch'); 
grunt.loadNpmTasks('grunt-contrib-connect'); 

// builds and cleans the task 
grunt.registerTask(
    'build', 
    'Compiles all of the assets and copies the files to the build directory.', 
    [ 'clean:build', 'copy', 'stylesheets', 'scripts', 'jade' ] 
); 

// Adds stylesheet to the task 
grunt.registerTask(
    'stylesheets', 
    'Compiles the stylesheets.', 
    [ 'less', 'autoprefixer', 'cssmin', 'clean:stylesheets' ] 
); 
    // Adds javascript to the task 
    grunt.registerTask(
    'scripts', 
    'Compiles the JavaScript files.', 
    [ 'coffee', 'uglify', 'clean:scripts' ] 
); 

    //watches and connects to the server 
    grunt.registerTask(
    'default', 
    'Watches the project for changes, automatically builds them and runs a server.', 
    [ 'build', 'connect', 'watch'] 
); 
}; 

回答

1

您需要將該選項添加到監視任務的配置中。目前,您在連接任務中擁有設置,而不是手錶。嘗試是這樣的:

watch: { 
    options: { 
    livereload:true, 
    }, 
    stylesheets: { 
    files: 'source/**/*.less', 
    tasks: [ 'stylesheets' ] 
    }, 
    scripts: { 
    files: 'source/**/*.coffee', 
    tasks: [ 'scripts' ] 
    }, 
    jade: { 
    files: 'source/**/*.jade', 
    tasks: [ 'jade' ] 
    }, 
    copy: { 
    files: [ 'source/**', '!source/**/*.less', '!source/**/*.coffee', '!source/**/*.jade' ], 
    tasks: [ 'copy' ] 
    } 
}, 

您還需要從連接任務

+0

奏效刪除,但現在我得到一個致命的錯誤:端口35729已在使用由另一個進程? – Tyrone

+0

您可以使用livereload設置指定一個新端口。看到這裏的示例設置:https://github.com/gruntjs/grunt-contrib-watch#optionslivereload – Guy