Grunt livereload and *html.twig files

637 views Asked by At

How to make Grunt livereload work with Symfony (files using *.html.twig extension) ?

livereload is working with sass however I have to refresh the page manually when I change a *.twig file.

I am using the livereload Chrome extension.

This is my Gruntfile

module.exports = function(grunt) {

  // Time how long tasks take. Can help when optimizing build times
  require('time-grunt')(grunt);

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

    watch: {
      sass: {
        files: 'src/ProjectBundle/Resources/public/scss/{,*/}*.{scss,sass}',
        tasks: ['sass:dev']
      },
      css: {
        files: [
          'src/ProjectBundle/Resources/public/*.sass',
          'src/ProjectBundle/Resources/public/*.scss'
        ]
      },
      js: {
        files: [
          'src/ProjectBundle/Resources/public/*.js',
          'Gruntfile.js'
        ]
      },
      options: {
        livereload: true
      }
    },


    sass: {
      dev: {
            options: {
                style: 'expanded',
                compass: false
            },
            files: {
                'src/ProjectBundle/Resources/public/css/main.css':'src/ProjectBundle/Resources/public/scss/main.scss'
            }
        }
    },

    gruntfile: {
        files: ['Gruntfile.js']
    },


    browserSync: {
      files: {
        src : [
          '**/*.twig',
          '**/*.html',
          '**/*.scss',
          '**/*.css',
          '**/img/*',
          '**/*.js'
        ],
      },
      options: {
        watchTask: true
      }
    }

  });

  // Load the Grunt plugins.
  grunt.loadNpmTasks('grunt-contrib-watch');
  grunt.loadNpmTasks('grunt-sass');
  grunt.loadNpmTasks('grunt-browser-sync');

  // Register the default tasks.
  grunt.registerTask('default', [
    'browserSync',
    'watch',
    'sass:dev'
  ]);

};
1

There are 1 answers

0
WilliamStam On

just add a new section to the watcher part

watch: {
        js: {
            files: ['app/**/*.js'],
            tasks: ['concat:js_quick'],
            options: {
                spawn: false,
                livereload: true
            }
        },
        css: {
            files: ['app/**/*.less','app/style.css'],
            tasks: ['watcher_css'],
            options: {
                spawn: false,
                livereload: true
            }
        },
        twig: { 
            files: ['app/**/*.twig'],
            options: {
                spawn: false,
                livereload: true
            }
        }
    }

some tips:

just be careful with **/*.js cause it will also "watch" the node_modules folder which slows it down.

use jit-grunt - makes things speedier aswell

my complete grunt file

var jsfile = [ ... ] // list of js files to concat


module.exports = function (grunt) {
require('time-grunt')(grunt);
require('jit-grunt')(grunt);

grunt.initConfig({

    concat: {
        js: {
            options: {
                separator: ';',
                stripBanners: true,
                sourceMap :true,
                sourceMapName : 'app/javascript.js.map'
            },
            src: jsfile,
            dest: 'app/javascript.js',
            nonull: true
        },
        js_quick: {
            options: {
                separator: ';',
                stripBanners: true
            },
            src: jsfile,
            dest: 'app/javascript.js',
            nonull: true
        }
    },
    clean: {
        map: ["app/javascript.js.map"],
    },

    uglify: {
        js: {

            files: {
                'app/javascript.js': ['app/javascript.js']
            }
        }
    },
    less: {
        style: {
            files: {
                "app/style.css": "app/_less/style.less",
            }
        }
    },
    cssmin: {
        options: {
            report: "min",
            keepSpecialComments: 0,
            shorthandCompacting: true
        },
        style: {
            files: {
                'app/style.css': 'app/style.css',
            }
        }
    },
    watch: {
        js: {
            files: ['app/**/*.js'],
            tasks: ['concat:js_quick'],
            options: {
                spawn: false,
                livereload: true
            }
        },
        css: {
            files: ['app/**/*.less','app/style.css'],
            tasks: ['watcher_css'],
            options: {
                spawn: false,
                livereload: true
            }
        },
        twig: {
            files: ['app/**/*.twig'],
            options: {
                spawn: false,
                livereload: true
            }
        }
    }

});






grunt.registerTask('jsmin', ['uglify:js']);
grunt.registerTask('js', ['concat:js_quick','clean:map']);
grunt.registerTask('jsmap', ['concat:js']);
grunt.registerTask('watcher_css', ['newer:less:style']);
grunt.registerTask('css', ['less:style']);
grunt.registerTask('build', ['concat:js','less:style', 'uglify:js','cssmin:style','clean:map']);
grunt.registerTask('default', ['watch']);

};

seems to work ok for me