How do I create a top level grunt file and import it into another grunt file located in subfolders?

I want to create a generic top level Gruntfile.js configured to view fewer files. When fewer files change, I want to compile it to css and then load the css file in the browser. I have the functionality, but I have to duplicate most of the code in Gruntfile.js for every project. So I wondered if there was a way to create a generic Gruntfile.js and include / import it across different projects.

module.exports = function (grunt) {
'use strict';

grunt.initConfig({
  pkg: grunt.file.readJSON('package.json'),

  less: {
    options: {
      compress: true
    },
    compile: {
      extend: true,
      src: 'less/**/project.less',
      dest: 'css/project.css',
      ext: '.css'
    }
  },

  watch : {
    less : {
      files : ['less/**/*.less'],
      tasks : ['less']
    },
    css : {
      files : ['css/*.css'],
      options : {
        livereload: true,
        spawn : false
      }
    }
  }
  });

  grunt.loadNpmTasks('grunt-contrib-watch');
  grunt.loadNpmTasks('grunt-contrib-less');
};

      

+3


source to share


1 answer


I solved the problem using the grunt plugin - 'gruntfile'. It allows you to import top-level Gruntfile.js for inclusion in other Gruntfiles. I am dynamically setting parent Gruntfile parameters from other grunt files.



More information on the plugin and use cases can be found at: https://github.com/shama/gruntfile

+3


source







All Articles