6

运行 grunt 时,出现以下错误:

警告:无法写入“client/dist/js/build.js”文件(错误代码:未定义)。使用 --force 继续。

我的 uglify 配置Gruntfile.js

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

我正在使用grunt-contrib-uglify.

任何想法为什么会发生这种情况?

4

2 回答 2

15

假设你的 Grunt 版本是 0.4.0,AFAIK 你没有使用最新的语法(参见https://github.com/gruntjs/grunt-contrib-uglify#usage-examples)。

尝试

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

我也不确定通配符是否处理得当。

于 2013-03-12T10:11:30.790 回答
15

我知道这被标记为已解决,但我仍然更喜欢类似问题的答案,因为您可以轻松地将文件再次用于其他命令,而无需两次编写它们。

简而言之,答案说

//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'

没有两次写入文件的测试工作示例:

'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'
    ]);
};

请注意,这'<%= uglify.build.src %>'非常方便;)

执行

$ 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.
于 2013-07-13T04:29:22.350 回答