53

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?

Ben
  • 47,286
  • 44
  • 159
  • 208
Jamie Hutber
  • 22,870
  • 34
  • 131
  • 236

1 Answers1

121

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.

Martin Hansen
  • 4,774
  • 3
  • 28
  • 47
  • thanks very much, I had thought that as it happens. But still, good to know :) – Jamie Hutber Aug 27 '13 at 07:03
  • 1
    just wanted to highlight the importance of **end** bit once more – tarikakyol Apr 19 '16 at 08:37
  • End here is very important. Agreed @trkaky – Ozil Jun 03 '16 at 07:34
  • Hi there, this does not seem to work with a src file mask `path/to/files/**/*js` . Therefore `src: ['web/js/**/*.js', '!web/js/bin/file1.js', '!web/js/bin/file2.js']` does not ignore file1.js and file2.js . Have you ever encoutered this problem? is it because i am trying to ignore files in a subfolder? – FedericoCapaldo Aug 03 '17 at 04:25
  • 1
    @FedericoCapaldo To exclude multiple files when using a src file mask. I simply put the excluded files into an array at the end of the src. src: ['web/js/**/*.js', ['!web/js/bin/file1.js', '!web/js/bin/file2.js']] That worked just fine for me – Rmalmoe May 07 '18 at 23:31