3

I have the following gruntfile.js. Using grunt-contrib-concat and grunt-contrib-uglify together.

This is an extention of a developement asked @Dynamic Mapping and Concat with Grunt Uglify

I use banner in the uglify task to reference a file created in the concat task.

module.exports = function(grunt){

    // Configure task(s)
    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),

        // setup concats
        concat: {
            options: {
                separator: ';\n',
            },
            dev: {
                files: [{
                    src: [
                        'sports.js',
                        'salon.js',
                        'offRoad.js',
                    ],
                    dest: 'src/js/concat/car-dev.js'
                }],
            },
        },

        // setup uglify task
        // this will produce a car js file (which includes car-dev.js) for every car js file located within scr/js/cars/
        uglify: {
            // dev - use in development mode, global overrides section
            dev: {
                options: {
                    // include concat dev.js
                    banner: grunt.file.read('src/js/concat/car-dev.js'),
                    beautify: true,
                    mangle: false,
                    compress: false,
                    preserveComments: 'all'
                },
                files: [{
                    expand: true,
                    cwd: 'src/js/cars/',
                    src: '*.js',
                    dest: 'javascript/dev',
                    ext: '.dev.js',
                    extDot: 'first'
                }],
            },
        },
    });

    // Load the plugins
    grunt.loadNpmTasks('grunt-contrib-concat');
    grunt.loadNpmTasks('grunt-contrib-uglify');
    grunt.loadNpmTasks('grunt-contrib-watch');
    grunt.loadNpmTasks('grunt-sass');

    // Register task(s)
    grunt.registerTask('default',
        'concat:dev'
        'uglify:dev'
    ]);
};

My issue is that if src/js/concat/car-dev.js doesn't exist, I get the following error:

Error: Unable to read "src/js/concat/car-dev.js" file <Error code: ENOENT).

If I create an empty file @src/js/concat/car-dev.js the grunt command works fine.

However this is not always possible. I want uglify to reference the car-dev.js file even if it doesn't exist. What I'm looking for is someway to reference the grunt->concat->dev->files->dest file.

I've tried adding the following

banner: grunt.file.read(grunt.concat.dev.files.dest),

and

banner: grunt.concat.dev.files.dest

but neither work. Is this possible? This is not a task dependency issue - the error gets thrown before any tasks are run because banner: src/js/concat/car-dev.js does not exist.

Any help - much appreciated.

Community
  • 1
  • 1
fidev
  • 1,222
  • 2
  • 21
  • 51

0 Answers0