2

I have been spending over an hour on this one, looked through several questions, but the answer is nowhere to be found.

I am running into a problem when using watch on my javascript files. All of the other files are watched and the tasks are executed properly.

If I run the default from the start, concat and uglify are working perfectly fine, but if I make any changes to a js file, watch will just plainly ignore it.

This is my file:

module.exports = function(grunt) {

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

        concat: {
            index: {
                src: ['js/shared/*.js', 'js/index/*.js'],
                dest: 'js/concat/index.js',
            },
            page: {
                src: ['js/shared/*.js', 'js/page/*.js'],
                dest: 'js/concat/page.js',
            },
        },

        uglify: {
            dist: {
                files:{
                    'js/build/index.min.js': 'js/concat/index.js',
                    'js/build/page.min.js': 'js/concat/page.js',
                }
            }
        },

        sass: {
            options: {
                outputStyle: 'compressed',
            },
            dist: {
                files: {
                    'css/main-unprefixed.css': 'sass/main.scss'
                }
            }
        },

        autoprefixer: {
            global: {
                src: 'css/main-unprefixed.css',
                dest: 'css/main.css'
            }
        },

        jade: {
            compile: {
                options: {
                    pretty: true
                },
                files: [{
                    expand: true,
                    cwd: '',
                    src: [ '_layouts/jade/*.jade' ],
                    dest: '_layouts',
                    flatten: true,
                    ext: '.html'
                }]
            }
        },

        shell: {
            jekyllServe: {
                command: "jekyll serve --no-watch"
            },
            jekyllBuild: {
                command: "jekyll build"
            }
        },

        open : {
            build: {
                path: 'http://localhost:4000',
                app: 'Firefox'
            }
        },

        watch: {
            options: {
                livereload: true
            },
            site: {
                files: ["*.html", "**/*.html", "*.md", "**/*.md", "**/*.yml", "*.yml", "!_site/*.*", "!_site/**/*.*"],
                tasks: ["shell:jekyllBuild"]
            },
            scripts: {
                files: ["js/*.js", "js/**/*.js"],
                tasks: ["concat", "uglify", "shell:jekyllBuild"]
            },
            jade: {
                files: ["_layouts/jade/*.jade"],
                tasks: ["jade"]
            },
            css: {
                files: ["sass/*.scss", "sass/**/*.scss", "sass/**/**/*.scss"],
                tasks: ["sass", "autoprefixer", "shell:jekyllBuild"]
            }
        }
    });


    require('load-grunt-tasks')(grunt);

    // Default task(s).
    grunt.registerTask("serve", ["shell:jekyllServe"]);
    grunt.registerTask("default", ["open", "concat", "uglify", "sass", "autoprefixer", "jade", "shell:jekyllBuild", "watch"]);
};

Any help is highly appreciated.

KreaTief
  • 416
  • 1
  • 4
  • 13
  • In the watch task, try using this pattern: `files: ['js/{,*/}*.js']` instead of `files: ["js/*.js", "js/**/*.js"]` ? – Tony Barnes Mar 27 '15 at 13:19
  • @TonyBarnes Tried that, the watch task is still not registering any change. – KreaTief Mar 27 '15 at 13:20
  • Is it just the scripts that aren't being watched - are the HTML and CSS files working correctly? Obviously this is hard to debug without the codebase. I would maybe try separating the `watch` task from the `default` task and debug from there. – Tony Barnes Mar 27 '15 at 13:23
  • Just the scripts, the rest is watched correctly. – KreaTief Mar 27 '15 at 13:25
  • 1
    Try removing the 'site' method from the watch task, so the scripts are run first. – Tony Barnes Mar 27 '15 at 13:26
  • if I remove site, it will run, but as soon as the site is in there, it will not work anymore – KreaTief Mar 27 '15 at 13:28
  • Excellent! I think this is the culprit: `!_site/*.*` - try removing those (in the site method). – Tony Barnes Mar 27 '15 at 13:30
  • that's it. Any other possibility how I can exclude the folder from the watch? Because _site is what is built, I don't really want grunt to watch it. – KreaTief Mar 27 '15 at 13:33
  • Hmm i'm not sure, doesn't seem to be anything coming from google about grunt tasks supporting underscores. Could you not rename the directory to `dist` or `site`? – Tony Barnes Mar 27 '15 at 13:37

1 Answers1

2

The issue lays within your site method, in the watch task.

If you remove these, it should work:

"!_site/*.*", "!_site/**/*.*"

Grunt doesn't seem to like the underscores inline with the !.

I'm not sure how to workaround this whilst keeping the underscore, but you could always rename the directory to be site or dist, instead of _site.

Tony Barnes
  • 2,625
  • 1
  • 18
  • 29