tschaub / grunt-newer

Configure Grunt tasks to run with newer files only.
https://npmjs.org/package/grunt-newer
MIT License
945 stars 61 forks source link

Broken reference to grunt.task.current.data #73

Open iabw opened 9 years ago

iabw commented 9 years ago

Hey, this seems like an issue that someone else would have reported by now if it were "real", but searching got me nothing.

I have a concat task, that adds a banner with a list of all concatted files. This works when used independantly via grunt concat:concatModule. When fired from a newer:concat via watch when saving one of the files in concat:concatModule's src list, it does not properly print the list of files. The files are concatted correctly and the banner and footer are added - it's only the file list being printed via grunt.task.current.data which doesn't work.

Digging into it, it seems like the src and dest are in grunt.task.current.data.files[0]. I thought, "okay, that makes sense, the newer task has its own data, so the originals are nested in here - I can if around that". But when attempting to write the file list from that location, it seems like the newer task either doesn't properly write to the output, or maybe the banner is processed a second time and the task no longer has references to either grunt.task.current.data or grunt.task.current.data[0].files.

I'm pretty sure the process option on the concat task and the module wrapper strings added to the task are NOT the reason for this error, but I've included them for completeness.

Expected banner output:

/*! 
  packagename - v0.0.0
  filepath/dest.js
  Included Files:
    file1.js,
    file2.js,
    file3.js,
    file4.js,
    file5.js
 */

Generated banner output:

/*! 
  packagename - v0.0.0

 */

Here is the simplified Gruntfile

var concatBanner = '/*! \n  <%= pkg.name %> - v<%= pkg.version %>\n' +
    '<% if (grunt.task.current.data && grunt.task.current.data.src){ %>' +
    '  <%= grunt.task.current.data.dest %>\n' +
    '  Included Files:\n    ' +
    '<%= grunt.task.current.data.src.join("\\n    ") %>' +
    '<% } %>\n */',

    moduleStart = "\n(function(){\n",

    moduleEnd = "\n}());\n";

grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    watch: {
        js: {
            files: [
                '/**/*.js'
            ],
            tasks: [
                'newer:concat'
            ]
        }
    },
    concat: {
        options: {
            separator: ';',
            success: true,
            banner: concatBanner,
            process: function(src, filepath) {
                // Add the filename above each concatenated section
                return '\n\n// FILE: ' + filepath + '\n\n' + src;
            }
        },
        concatModule: {
            nonull: true,
            src: [
                'file1.js',
                'file2.js',
                'file3.js',
                'file4.js',
                'file5.js'
            ],
            options: {
                banner: concatBanner + moduleStart,
                footer: moduleEnd
            }
        },
        otherConcatWithoutModuleWrapper1: {},
        otherConcatWithoutModuleWrapper2: {}
    }
});

grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-newer');