2
votes

I'm trying to use grunt-usemin in my project to minify JS and CSS files, but it don't work good. So I created simple example like in grunt-usemin documentation, which also does not work.

I have simple structure:

.
├── Gruntfile.js
├── dist
│   └── js
├── index.html
├── js
│   ├── bar.js
│   └── foo.js
└── package.json

In index.html I have following block with reference to 2 JS files:

<!-- build:js js/optimized.js -->
<script src="/js/foo.js"></script>
<script src="/js/bar.js"></script>
<!-- endbuild -->

My Gruntfile.js:

module.exports = function(grunt) {
// Project configuration.
  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    useminPrepare: {
        html: 'index.html',
        options: {
          dest: 'dist'
        }
    },
    usemin: {
        html: ['index.html']
    },
    filerev: {
      options: {
        algorithm: 'md5',
        length: 8
      },
      js: {
        src: 'js/optimized.js',
        dest: 'js/'
      }
    }
  });

  grunt.loadNpmTasks('grunt-contrib-uglify');
  grunt.loadNpmTasks('grunt-contrib-cssmin');
  grunt.loadNpmTasks('grunt-contrib-concat');
  grunt.loadNpmTasks('grunt-filerev');
  grunt.loadNpmTasks('grunt-usemin');

  // Register tasks
  grunt.registerTask('deploy', ['useminPrepare', 'uglify:generated']);
// grunt.registerTask('deploy', ['useminPrepare', 'uglify:generated', 'usemin']);

};

When I run grunt deploy -v I expect, that js/optimized.js will be created. But I get this error:

>> Destination dist/js/optimized.js not written because src files were empty.
>> No files created.

Tasks generated by useminPrepare looks like:

Going through index.html to update the config
Looking for build script HTML comment blocks

Configuration is now:

  concat:
  { generated: 
   { files: 
      [ { dest: '.tmp/concat/js/optimized.js',
          src: [ 'js/foo.js', 'js/bar.js' ] } ] } }

  uglify:
  { generated: 
   { files: 
      [ { dest: 'dist/js/optimized.js',
          src: [ '.tmp/concat/js/optimized.js' ] } ] } }

Can somebody help me, why dist/js/optimized.js is not created?

Thanks!

1
How do you inspect the tasks which were generated by useminPrepare - Gerfried

1 Answers

0
votes

Hmm problem was in grunt.registerTask('deploy', ['useminPrepare', 'uglify:generated']);. I forgot to call concat:generated.

So solution is:

grunt.registerTask('deploy', ['useminPrepare', 'concat:generated', 'uglify:generated']);