grunt uglify task failing

后端 未结 2 554
猫巷女王i
猫巷女王i 2021-01-18 07:01

When running grunt, I get the following error:

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

相关标签:
2条回答
  • 2021-01-18 07:42

    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.
    
    0 讨论(0)
  • 2021-01-18 07:44

    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.

    0 讨论(0)
提交回复
热议问题