4

Is there a way to specify file dependencies in Grunt? I.e. if I have:

global/page.jade
project/index.jade -- includes global/page.jade
project/about.jade -- includes global/page.jade
project/test.jade

and I change global/page.jade then I would like to get project/index|about.jade recompiled. I checked out the plugins but couldn't find anything that would provide that functionality.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Philip Kamenarsky
  • 2,757
  • 2
  • 24
  • 30

2 Answers2

5

grunt watch use in your gruntfile.js in following way

...

  watch: {
    scripts: {
      files: 'src/**/*',
      tasks: ['buildDevelopment'],
      options: {
        interrupt: true,
      },
    }

grunt.registerTask('buildDevelopment', ['clean'
                        ,'jade'
                        ,'copy:development'
                        ,'bowercopy:development'
                        ]);

...
Atilla Ozgur
  • 14,339
  • 3
  • 49
  • 69
2

I'd suggest enhancing Atillas solution. Rebuilding all templates on every change isn't optimal and will piss you off on large projects.

Here's what should help:


Use newer plugin to process only changed files

Install: npm install grunt-newer --save-dev
Docs: https://www.npmjs.org/package/grunt-newer#optionsoverride
Usage:

  • prefix tasks with newer:
  • add grunt.loadNpmTasks('grunt-newer'); to gruntfile
  • use override option to check includes (the magic you're looking for)

newer: { options: { override: function (detail, include) { if (detail.task === 'jade') { checkForModifiedImports(detail.path, detail.time, include); } else { include(false); } } } }



Use watch to detect file changes

Install: npm install grunt-contrib-watch --save-dev Docs: https://www.npmjs.org/package/grunt-contrib-watch
Usage:

  • add grunt.loadNpmTasks('grunt-contrib-watch'); to gruntfile
  • task can look like something like this

watch: { scripts: { files: '**/*.jade', tasks: ['newer:jade'], options: { interrupt: true }, }, }

  • I've got `Fatal error: checkForModifiedImports is not defined` on this. Used [`gruntfile.coffee`](https://gist.github.com/Grawl/0eef67b20a40ec97ace1) – Даниил Пронин May 16 '14 at 01:27
  • looks like `checkForModifiedImports()` is a user defined function - there is no definition in the newer, jade, or less projects (the newer readme cites less). I'd love to see an example! – ptim May 16 '14 at 03:40
  • Okay I asked about this problem at `newer` repo Issues https://github.com/tschaub/grunt-newer/issues/44 – Даниил Пронин May 17 '14 at 16:34
  • You have to write your own function for that. I will post example soon. – viktorbezdek May 18 '14 at 21:00
  • I just find a working solution into someone's [gruntfile.coffee#105](https://github.com/nwalton3/grunt-starter/blob/6992093e3513d67b910fbba81b30132fd0f6792e/Gruntfile.coffee#L105) on Github. it's simple as 1,2,3: `if detail.task is 'jade` then `include true`. and it works. – Даниил Пронин Jun 05 '14 at 20:36