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.

175 lines
7.0 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. },
  19. src: {
  20. files: ['lib/*.js', 'css/**/*.scss', '!lib/dontwatch.js'],
  21. tasks: ['default'],
  22. },
  23. test: {
  24. files: '<%= jshint.test.src %>',
  25. tasks: ['jshint:test', 'qunit'],
  26. },
  27. },
  28. });
  29. ```
  30. ## Using the `watch` event
  31. 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. Here is a simple example using the `watch` event:
  32. ```js
  33. grunt.initConfig({
  34. watch: {
  35. scripts: {
  36. files: ['lib/*.js'],
  37. },
  38. },
  39. });
  40. grunt.event.on('watch', function(action, filepath) {
  41. grunt.log.writeln(filepath + ' has ' + action);
  42. });
  43. ```
  44. **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).**
  45. ### Compiling Files As Needed
  46. 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:
  47. ```js
  48. grunt.initConfig({
  49. watch: {
  50. scripts: {
  51. files: ['lib/*.js'],
  52. tasks: ['jshint'],
  53. options: {
  54. nospawn: true,
  55. },
  56. },
  57. },
  58. jshint: {
  59. all: ['lib/*.js'],
  60. },
  61. });
  62. // on watch events configure jshint:all to only run on changed file
  63. grunt.event.on('watch', function(action, filepath) {
  64. grunt.config(['jshint', 'all'], filepath);
  65. });
  66. ```
  67. If you need to dynamically modify your config, the `nospawn` option must be enabled to keep the watch running under the same context.
  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. Feel free to add this script to your template situation and toggle with some sort of `dev` flag.
  129. ### Using Live Reload with the Browser Extension
  130. 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.
  131. Once installed please use the default live reload port `35729` and the browser extension will automatically reload your page without needing the `<script>` tag.
  132. ### Using Connect Middleware
  133. Since live reloading is used when developing, you may want to disable building for production (and are not using the browser extension). One method is to use Connect middleware to inject the script tag into your page. Try the [connect-livereload](https://github.com/intesso/connect-livereload) middleware for injecting the live reload script into your page.
  134. # FAQs
  135. ## How do I fix the error `EMFILE: Too many opened files.`?
  136. 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.
  137. In some versions of OSX the above solution doesn't work. In that case try `launchctl limit maxfiles 10480 10480 ` and restart your terminal. See [here](http://superuser.com/questions/261023/how-to-change-default-ulimit-values-in-mac-os-x-10-6).
  138. ## Can I use this with Grunt v0.3?
  139. 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.
  140. ## Why is the watch devouring all my memory?
  141. 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.
  142. ## Why spawn as child processes as a default?
  143. 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.
  144. 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.
  145. 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.