You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

167 lines
6.2 KiB

  1. # Examples
  2. ```js
  3. // Simple config to run jshint any time a file is added, changed or deleted
  4. grunt.initConfig({
  5. watch: {
  6. files: '**/*',
  7. tasks: ['jshint']
  8. }
  9. });
  10. ```
  11. ```js
  12. // Advanced config. Run specific tasks when specific files are added, changed or deleted.
  13. grunt.initConfig({
  14. watch: {
  15. gruntfile: {
  16. files: 'Gruntfile.js',
  17. tasks: ['jshint:gruntfile'],
  18. options: {
  19. nocase: true
  20. }
  21. },
  22. src: {
  23. files: ['lib/*.js', 'css/**/*.scss', '!lib/dontwatch.js'],
  24. tasks: ['default']
  25. },
  26. test: {
  27. files: '<%= jshint.test.src %>',
  28. tasks: ['jshint:test', 'qunit']
  29. }
  30. }
  31. });
  32. ```
  33. ## Using the `watch` event
  34. This task will emit a `watch` event when watched files are modified. This is useful if you would like a simple notification when files are edited or if you're using this task in tandem with another task, for example a live reload task. Here is a simple example using the `watch` event:
  35. ```js
  36. grunt.initConfig({
  37. watch: {
  38. scripts: {
  39. files: ['lib/*.js'],
  40. tasks: [''],
  41. },
  42. },
  43. });
  44. grunt.event.on('watch', function(action, filepath) {
  45. grunt.log.writeln(filepath + ' has ' + action);
  46. });
  47. ```
  48. **The `watch` event is not intended for replacing the standard Grunt API for configuring and running tasks. If you're trying to run tasks from within the `watch` event you're more than likely doing it wrong. Please read [configuring tasks](http://gruntjs.com/configuring-tasks).**
  49. ### Compiling Files As Needed
  50. A very common request is to only compile files as needed. Here is an example that will only lint changed files with the `jshint` task:
  51. ```js
  52. grunt.initConfig({
  53. watch: {
  54. scripts: {
  55. files: ['lib/*.js'],
  56. tasks: ['jshint'],
  57. },
  58. },
  59. jshint: {
  60. all: ['lib/*.js'],
  61. },
  62. });
  63. // on watch events configure jshint:all to only run on changed file
  64. grunt.event.on('watch', function(action, filepath) {
  65. grunt.config(['jshint', 'all'], filepath);
  66. });
  67. ```
  68. If you save multiple files simultaneously you may opt for a more robust method:
  69. ```js
  70. var changedFiles = Object.create(null);
  71. var onChange = grunt.util._.debounce(function() {
  72. grunt.config(['jshint', 'all'], Object.keys(changedFiles));
  73. changedFiles = Object.create(null);
  74. }, 200);
  75. grunt.event.on('watch', function(action, filepath) {
  76. changedFiles[filepath] = action;
  77. onChange();
  78. });
  79. ```
  80. ## Live Reloading
  81. Live reloading is built into the watch task. Set the option `livereload` to `true` to enable on the default port `35729` or set to a custom port: `livereload: 1337`.
  82. The simplest way to add live reloading to all your watch targets is by setting `livereload` to `true` at the task level. This will run a single live reload server and trigger the live reload for all your watch targets:
  83. ```js
  84. grunt.initConfig({
  85. watch: {
  86. options: {
  87. livereload: true,
  88. },
  89. css: {
  90. files: ['public/scss/*.scss'],
  91. tasks: ['compass'],
  92. },
  93. },
  94. });
  95. ```
  96. You can also configure live reload for individual watch targets or run multiple live reload servers. Just be sure if you're starting multiple servers they operate on different ports:
  97. ```js
  98. grunt.initConfig({
  99. watch: {
  100. css: {
  101. files: ['public/scss/*.scss'],
  102. tasks: ['compass'],
  103. options: {
  104. // Start a live reload server on the default port 35729
  105. livereload: true,
  106. },
  107. },
  108. another: {
  109. files: ['lib/*.js'],
  110. tasks: ['anothertask'],
  111. options: {
  112. // Start another live reload server on port 1337
  113. livereload: 1337,
  114. },
  115. },
  116. dont: {
  117. files: ['other/stuff/*'],
  118. tasks: ['dostuff'],
  119. },
  120. },
  121. });
  122. ```
  123. ### Enabling Live Reload in Your HTML
  124. Once you've started a live reload server you'll be able to access the live reload script. To enable live reload on your page, add a script tag before your closing `</body>` tag pointing to the `livereload.js` script:
  125. ```html
  126. <script src="http://localhost:35729/livereload.js"></script>
  127. ```
  128. ### Using Live Reload with the Browser Extension
  129. Instead of adding a script tag to your page, you can live reload your page by installing a browser extension. Please visit [how do I install and use the browser extensions](http://feedback.livereload.com/knowledgebase/articles/86242-how-do-i-install-and-use-the-browser-extensions-) for help installing an extension for your browser.
  130. Once installed please use the default live reload port `35729` and the browser extension will automatically reload your page without needing the `<script>` tag.
  131. # FAQs
  132. ## How do I fix the error `EMFILE: Too many opened files.`?
  133. This is because of your system's max opened file limit. For OSX the default is very low (256). Temporarily increase your limit with `ulimit -n 10480`, the number being the new max limit.
  134. ## Can I use this with Grunt v0.3?
  135. Yes. Although `grunt-contrib-watch` is a replacement watch task for Grunt v0.4, version `grunt-contrib-watch@0.1.x` is compatible with Grunt v0.3. `grunt-contrib-watch >= 0.2.x` is **only* compatible and recommended to use with Grunt v0.4.
  136. ## Why is the watch devouring all my memory?
  137. Likely because of an enthusiastic pattern trying to watch thousands of files. Such as `'**/*.js'` but forgetting to exclude the `node_modules` folder with `'!node_modules/**/*.js'`. Try grouping your files within a subfolder or be more explicit with your file matching pattern.
  138. ## Why spawn as child processes as a default?
  139. The goal of this watch task is as files are changed, run tasks as if they were triggered by the user themself. Each time a user runs `grunt` a process is spawned and tasks are ran in succession. In an effort to keep the experience consistent and continualy produce expected results, this watch task spawns tasks as child processes by default.
  140. Sandboxing task runs also allows this watch task to run more stable over long periods of time. As well as more efficiently with more complex tasks and file structures.
  141. Spawning does cause a performance hit (usually 500ms for most environments). It also cripples tasks that rely on the watch task to share the context with each subsequent run (i.e., reload tasks). If you would like a faster watch task or need to share the context please set the `nospawn` option to `true`. Just be aware that with this option enabled, the watch task is more prone to failure.