Search code examples
javascriptgruntjsuglifyjsgrunt-contrib-uglify

Grunt-uglify recursively


I have a set of subfolders with some *.dev.js files. I need grunt to uglify those and create the uglified files next to the source files, for each .dev.js file in the directory tree. Here is how I try to do it:

uglify: {
  options: {
    banner: '/*! <%= pkg.name %> <%= grunt.template.today("dd-mm-yyyy") %> */\n'
  },
  dist: {
    files: {
      src: '/folder/**/*.dev.js',
      ext: '.js',
      flatten: true,
      expand: true
    }
  }
}

But this gives an error:

Running "uglify:dist" (uglify) task File src created: 13.49 kB → 5.62 kB

Destination ext not written because src files were empty. Warning: Object true has no method 'indexOf' Use --force to continue.


Solution

  • I saw your question because I had the same issue, the answer for every one else that see this (like me) is:

    • You are using "src: '/folder/**/*.dev.js'"
    • The error is in the first slash (/folder), grunt/nodejs is looking in the folder "folder" in the root of your drive instead in your app root (for example c:\folder or OSX\folder).
    • The solution is to remove the first slash or use a dot before the slash ".\folder", the complete solution should be:

      uglify: {
        options: {
          banner: '/*! <%= pkg.name %> <%= grunt.template.today("dd-mm-yyyy") %> */\n'
        },
        dist: {
          files: [{
            src: './folder/**/*.dev.js',
            ext: '.js',
            flatten: true,
            expand: true
          }]
        }
      }
      

    Also, don't ask me why I just learn about grunt 1 hour ago, you need to but the files value inside a javascript array [{ }] (not just the curly braces), isolating the "files" object looks like this:

        files: [{
          src: './folder/**/*.dev.js',
          ext: '.js',
          flatten: true,
          expand: true
        }]