How to add separator to Concat options when using Usemin
Asked Answered
B

1

9

I am using Grunt-usemin. But the concatenated JS is not properly separated by ';'. How do I tell usemin to add the separator only for JS files but not CSS files?

Currently, my usemin tasks look like this:

    useminPrepare: {
        options: {
            dest: '<%= config.dist %>'
        },
        html: '<%= config.app %>/index.html'
    },

    // Performs rewrites based on rev and the useminPrepare configuration
    usemin: {
        options: {
            assetsDirs: ['<%= config.dist %>', '<%= config.dist %>/images']
        },
        concat: {
            separator: ';'
        },
        html: ['<%= config.dist %>/{,*/}*.html'],
        css: ['<%= config.dist %>/styles/{,*/}*.css']
    },

Another use case would be wrapping each concatenated module in an IIFE, which requires this configuration, but should only be applied to *.js files:

concat: {
    options: {
        banner: ';(function () {',
        separator: '})(); (function () {',
        footer: '})();'
    }
}
Battista answered 20/5, 2014 at 2:45 Comment(0)
S
4
 concat: {
  options: {
    process: function (src, filepath) {
      if (filepath.split(/\./).pop() === 'js') {
        return src + ';\n';
      }
      return src;
    }
  }
}

Explanation of the process option link

The function splits the filepath into an Array with . (dot) as a separator. The pop() method from Array returns the last item of the array which should be the extension.

Suet answered 18/8, 2015 at 6:24 Comment(1)
Can you explain what's going on here and add a link to the relevant doc?Schauer

© 2022 - 2024 — McMap. All rights reserved.