grunt uglify task failing

萝らか妹 提交于 2019-12-19 17:47:03

问题


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?


回答1:


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.




回答2:


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.


来源:https://stackoverflow.com/questions/15316964/grunt-uglify-task-failing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!