0

My original problem was getting the watch to only execute minify/uglify against the file that changed and not everything. When it runs against everything I have to wait several seconds.

I finally gave up on watch and reluctantly installed another plugin, "grunt-newer".

I don't know how to tell if this is working but the first task seems to execute.

The problem I need to tackle next is how to get the second task and then third tasks to run. So far I'm only able to get the first task in my watch list to work. I've switched the tasks around and always get the same thing. The first task runs then it's done.

Here is my grunt file. I'm still new to grunt, keep that in mind.

module.exports = function (grunt) {

    grunt.initConfig({
        distFolder: 'core/dist',

        pkg: grunt.file.readJSON('package.json'),
        // Task configuration.
        ngAnnotate: {
            options: {
                singleQuotes: true,
            },
            mashupCore: {
                files: {
                    '': ['core/**/*.js', '!core/lib/**/*', '!core/dist/**/*']
                },
            }
        },
        uglify: {
            options: {
                sourceMap: true,
            },
            dist: {
                files: [
                    {
                        expand: true,
                        src: ['<%= distFolder %>/**/*.js', '!<%= distFolder %>**/*.min.js'],
                        dest: '',
                        ext: '.min.js',
                        extDot: 'last'
                    }
                ]
            },
            apps: {
                files: [
                    {
                        expand: true,
                        src: ['core/apps/**/*.js', '!**/*.min.js', '!core/apps/**/route.config.js'],
                        dest: '',
                        ext: '.min.js',
                        extDot: 'last'
                    }
                ]
            },
            coreroot: {
                files: [
                    {
                        expand: true,
                        src: ['core/*.js', '!**/*.min.js'],
                        dest: '',
                        ext: '.min.js',
                        extDot: 'last'
                    }
                ]
            }
        },
        cssmin: {
            all: {
                files: [
                    {
                        expand: true,
                        src: ['core/**/*.css', '!**/*.min.css'],
                        dest: '',
                        ext: '.min.css',
                        extDot: 'last'
                    }
                ]
            }
        }

        ,
        concat: {
            options: {
                separator: ';',
            },
            routeconfig: {
                src: ['core/config/route.config.js', 'core/apps/**/route.config.js', '!core/lib/**/*', '!core/dist/**/*'],
                dest: '<%= distFolder %>/route.config.js',
            },
            coreservices: {
                src: ['core/common/services/**/*', '!core/lib/**/*', '!core/dist/**/*'],
                dest: '<%= distFolder %>/core.services.js',
            },

        },
        clean: {
            dist: {
                src: ['<%= distFolder %>/**/*.*/', '<%= distFolder %>/**/*.*']
            }
        }
        //, jshint: {
        //    all: {
        //        src: ['Gruntfile.js', 'core/**/*.js', '!core/dist/**/*', '!**/*.min.js', '!core/lib/**/*']
        //        , dest: 'output.js'
        //    }
        //}
        ,
        watch: {
            apps: {
                files: ['core/apps/**/*.js', '!**/*.min.js', '!core/apps/**/route.config.js'],
                tasks: ['uglify:apps'],
                options: {
                    spawn: false,
                },
            },
            dist: {
                files: ['core/common/services/**/*','core/config/route.config.js', 'core/apps/**/route.config.js', '!core/lib/**/*', '!core/dist/**/*'],
                tasks: ['clean:dist','concat:routeconfig', 'concat:coreservices', 'uglify:dist'],
                options: {
                    spawn: false,
                },
            },
        },

    });

    // Load modules, register tasks
    // grunt.loadNpmTasks('ngAnnotate');
    grunt.loadNpmTasks('grunt-ng-annotate');
    grunt.loadNpmTasks('grunt-contrib-uglify');
    grunt.loadNpmTasks('grunt-contrib-concat');
    grunt.loadNpmTasks('grunt-contrib-clean');
    grunt.loadNpmTasks('grunt-contrib-cssmin');
    grunt.loadNpmTasks('grunt-contrib-htmlmin');
    grunt.loadNpmTasks('grunt-contrib-jshint');
    grunt.loadNpmTasks('grunt-contrib-watch');
    grunt.loadNpmTasks('grunt-newer');


    // this annotate tasks only needs run periodically.  It processes all files even if they have already been properly annotated.
    // So it's worth removing to save build time at the developer machine.
    // grunt.registerTask('default', ['ngAnnotate']);
    // grunt.registerTask('default', ['uglify']);

    // ------------------------------------------------------------------------------------------
    // grunt default
    grunt.registerTask('default', [
        'annotate', 'clean:dist', 'concat:routeconfig', 'concat:coreservices',
        'uglify:dist', 'uglify:apps', 'uglify:coreroot', 'cssmin:all'
    ]);
    // 1. Annotates all but 'lib' directory.
    // 2. Cleans out the "dist" directory
    // 3. Concatinates together all files named route.config.js into a single file route.config.min.js
    // 4. Uglify creates maps
    // ------------------------------------------------------------------------------------------

    grunt.registerTask('annotate', ['ngAnnotate']);
    grunt.registerTask('clean_dist', ['clean:dist']);
    //grunt.registerTask('watchcode', ['watch:dist', 'watch:apps']);
    grunt.registerTask('watchcode', ['newer:watch:apps', 'newer:watch:dist']);


    //grunt.event.on('watch:apps', function (action, filepath, target) {
    //    //change the source and destination in the uglify task at run time so that it affects the changed file only
    //    var destFilePath = filepath.replace(/(.+)\.js$/, '$1.min.js');
    //    grunt.config('uglify.apps.src', filepath);
    //    //grunt.config('uglify.apps.dest', destFilePath);

    //});
};
Kevin Brown-Silva
  • 40,873
  • 40
  • 203
  • 237
user3448990
  • 323
  • 6
  • 15

1 Answers1

0

I think I'm satisfied with the "newer" plugin.

I was never able to get the watch to run multiple tasks from a registered task and maybe that makes sense though it would be a nice feature to pick and choose which tasks to watch.

I'm simply running "watch" and it's good enough.

user3448990
  • 323
  • 6
  • 15