Philip Kamenarsky
Philip Kamenarsky

Reputation: 2777

Grunt.js dependency management

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.

Upvotes: 4

Views: 667

Answers (2)

viktorbezdek
viktorbezdek

Reputation: 29

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 }, }, }

Upvotes: 2

Atilla Ozgur
Atilla Ozgur

Reputation: 14701

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'
                        ]);

...

Upvotes: 5

Related Questions