How to ignore files grunt uglify

Javascriptnode.jsGruntjsUglifyjs

Javascript Problem Overview


Background

I've just started using grunt as of about 30mins ago. So bear with me.

But I have a rather simple script going that will look at my js and then compress it all into one file for me.

Code

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

    // load all grunt tasks
    require('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks);

    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        uglify: {
            options: {
                beautify: true,
                report: 'gzip'
            },
            build: {
                src: ['docroot/js/*.js', 'docroot/components/pages/*.js', 'docroot/components/plugins/*.js'],
                dest: 'docroot/js/main.min.js'
            }
        },
        watch: {
            options: {
                dateFormat: function(time) {
                    grunt.log.writeln('The watch finished in ' + time + 'ms at' + (new Date()).toString());
                    grunt.log.writeln('Waiting for more changes...');
                }
            },
            js: {
                files: '<%= uglify.build.src %>',
                tasks: ['uglify']
            }
        }
    });

    grunt.registerTask('default', 'watch');

}

Question

My main.min.js is getting included in the compile each time. Meaning my min.js is getting 2x, 4x, 8x, 16x etc etc. Is best way around this is to add an exception and ignore main.min.js?

Javascript Solutions


Solution 1 - Javascript

To the end of the src array, add

'!docroot/js/main.min.js'

This will exclude it. The ! turns it into an exclude.

http://gruntjs.com/api/grunt.file#grunt.file.expand

> Paths matching patterns that begin with ! will be excluded from the returned array. Patterns are processed in order, so inclusion and exclusion order is significant.

This is not specific to grunt uglify, but any task that uses grunt convention for specifying files will work this way.

As a general advice though I would suggest putting built files somewhere else than your source files. Like in a root dist folder.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJamie HutberView Question on Stackoverflow
Solution 1 - JavascriptMartin HansenView Answer on Stackoverflow