简体   繁体   中英

Grunt.js & uglify is appending uglified code to file instead of rewriting it

I'm working on some automation tasks and I noticed that grunt.js and uglify module are not rewriting the minified file. They're appending a new version of code everytime I start grunt tasks.

module.exports = function(grunt) {

  grunt.initConfig({
    uglify  : {
      build : {
        src     : ['**/*.js', '!*.min.js'],
        cwd     : 'js/app/modules/',
        dest    : 'js/app/modules/',
        expand  : true,
        ext     : '.main.min.js',
      },
    }
  });

  grunt.loadNpmTasks('grunt-contrib-uglify');
  grunt.registerTask('default', ['uglify']);
};

What can I do to avoid it? I just want the newest version of code in the file.

I had the same problem with the following configuration for all files in subfolders to js/ (eg js/lib/*.js) :

 build: {
             expand: true,
             cwd: 'js/',
             src: ['**/*.js','!*.min.js'],
             dest: 'js/',
             ext: '.min.js',
        }

You have to restrict more files, because if a file matches the src-option the content will be appended and not replaced - because it is "locked" i guess:

    src: ['**/*.js','!**/*.min.js']

That should fix your problem.

Thanks SpazzMarticus! I user grunt-newer to run uglify with newer files only. You can try this:

grunt.initConfig({
        uglify: {
            options: {
            },
            build: {
                files: [{
                    expand: true,
                    cwd: 'public/js',
                    src: ['**/*.js','!**/*.min.js'],
                    dest: 'public/js',
                    ext: '.min.js'
                }]
            }
        },
        watch: {
            options: {
                livereload: true,
                nospawn: true
            },
            scripts:{
                files: ['public/js/**/*.js'],
                tasks: ['newer:uglify']
            }
        }
    });

grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-newer');
grunt.registerTask('yt', ['watch']);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM