Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grunt uglify task failing

When running grunt, I get the following error:

Warning: Unable to write "client/dist/js/build.js" file (Error code: undefined). Use --force to continue.

The config of uglify in my Gruntfile.js :

uglify: {
      build: {
        src: ['client/src/js/*.js'],
        dest:['client/dist/js/build.js']
      }
    }

I'm using grunt-contrib-uglify.

Any ideas why this is happening?

like image 510
Soroush Hakami Avatar asked Mar 09 '13 23:03

Soroush Hakami


2 Answers

Assuming your Grunt Version is 0.4.0, AFAIK you are not using the most recent Syntax (See https://github.com/gruntjs/grunt-contrib-uglify#usage-examples).

Try

uglify: {
    build: {
        files: {
            'client/dist/js/build.js': ['client/src/js/*.js']
        }
    }
}

I am also not sure if the wildcards are handled properly.

like image 52
raffomania Avatar answered Oct 22 '22 10:10

raffomania


I know this is marked as solved, but I still prefer this answer from a similar question because you can easily use the files again for an other command without writing them twice.

In short, answer says

//Does not work
src: ['client/src/js/*.js'],
dest: ['client/dist/js/build.js']
//Works
src: ['client/src/js/*.js'],
dest: 'client/dist/js/build.js'

Tested working example without writing files twice:

'use strict';
module.exports = function(grunt) {
  grunt.initConfig({
    uglify: {
      build: {
        src: ['client/src/js/*.js'],
        dest: 'client/dist/js/build.js'
      }
    },
    watch: {
      js: {
        files: '<%= uglify.build.src %>',
        tasks: ['uglify']
      }
    }
  });
  grunt.loadNpmTasks('grunt-contrib-uglify');
  grunt.loadNpmTasks('grunt-contrib-watch');
  grunt.registerTask('default', [
    'uglify',
    ]);
  grunt.registerTask('dev', [
    'watch'
    ]);
};

Notice that '<%= uglify.build.src %>' is very handy ;)

Execution

$ grunt watch
Running "watch" task
Waiting...OK
>> File "client/src/js/hello.js" changed.
Running "uglify:build" (uglify) task
File "client/dist/js/build.js" created.
Uncompressed size: 15 bytes.
Compressed size: 32 bytes gzipped (15 bytes minified).

Done, without errors.
like image 30
GabLeRoux Avatar answered Oct 22 '22 09:10

GabLeRoux